Page 1 of 1

Controlling Shutdown [EDITED]

Posted: Wed Feb 24, 2016 9:23 pm
by Roberto Lopez
Hi All,

I'm trying to create an utility that impede users to shutdown the machine and instead do some maintenance tasks and when completed, programmatically shutdown the PC.

The problem I'm facing is the following:

I'm successfully 'trapping' the session end/shutdown when it is issued from the start menu, but when I push the power button instead, it works fine the first time (the event is trapped by the application) but if you push the power button for a second time, the event is not trapped and the system shutdown with no warning about 30/40 seconds later.

EDIT: I'm testing in another WIn7 machine now (and the event is trapped by the app 30/40 seconds later but no shutdown occurs).

Any ideas?

Tested on 32 bits Windows 7 Pro and Starter. Current HMG version.

This is the code:

Code: Select all

#include "hmg.ch"

#define WM_QUERYENDSESSION 0x0011

Function Main

   DEFINE WINDOW Form_1 ;
      AT 0,0 ;
      WIDTH 500 HEIGHT 300 ;
      TITLE 'On Shutdown Test' ;
      MAIN

   END WINDOW

   CREATE EVENT PROCNAME OnCloseSession() HWND Form_1.HANDLE STOREINDEX nIndex

   EventProcessAllHookMessage ( nIndex, .T. ) 
   
   Form_1.Center

   Form_1.Activate

Return Nil

Function OnCloseSession()

      IF EventMSG() == WM_QUERYENDSESSION

	CallDll32 ( "CancelShutdown" , "User32.dll" )

	MSGINFO("Do My Thing Here!!!")

	return 0

      ENDIF

Return nil
TIA.

Re: Controlling Shutdown

Posted: Wed Feb 24, 2016 9:38 pm
by andyglezl
Hola Roberto

Lei algo parecido aqui... tal vez te sirva.

http://stackoverflow.com/questions/2178 ... ing-winapi

Re: Controlling Shutdown

Posted: Wed Feb 24, 2016 11:43 pm
by Roberto Lopez
andyglezl wrote:Hola Roberto

Lei algo parecido aqui... tal vez te sirva.

http://stackoverflow.com/questions/2178 ... ing-winapi
MUCHAS GRACIAS!

Re: Controlling Shutdown [EDITED]

Posted: Wed Feb 24, 2016 11:46 pm
by Roberto Lopez
One more thing:

'CancelShutdown' is an undocumented Windows API function.

If you omit it, a warning screen appears, allowing the user to force shutdown.