Controlling Shutdown [EDITED]

Moderator: Rathinagiri

Post Reply
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Controlling Shutdown [EDITED]

Post 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.
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
andyglezl
Posts: 1461
Joined: Fri Oct 26, 2012 7:58 pm
Location: Guadalajara Jalisco, MX
Contact:

Re: Controlling Shutdown

Post by andyglezl »

Hola Roberto

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

http://stackoverflow.com/questions/2178 ... ing-winapi
Andrés González López
Desde Guadalajara, Jalisco. México.
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Re: Controlling Shutdown

Post by Roberto Lopez »

andyglezl wrote:Hola Roberto

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

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

Roberto


(Veritas Filia Temporis)
User avatar
Roberto Lopez
HMG Founder
Posts: 4004
Joined: Wed Jul 30, 2008 6:43 pm

Re: Controlling Shutdown [EDITED]

Post 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.
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
Post Reply