Board index » Visual Studio » Windows shutdown & program running as service

Windows shutdown & program running as service

Visual Studio233
Hello



I have made a program that runs as a service.

My program does not shut down correctly when windows is restarted or shut

down.



It seems as if my service handler is never called (only the service main

function is called at start up). If I stop the service manually from

services.msc (I am using win xp pro) the service handler is called correctly

and the programs shuts down correctly.



My program has a window that is closed when the users logs off the computer

(by overriding CWnd::OnEndSession), but how do I know tell if OnEndSession

has been called because the computer is being shutdown or if it has been

called because the user is logging off??



cheers



Peter


-
 

Re:Windows shutdown & program running as service



"Peter" <pindemad@yahoo.com>wrote in message

Quote
Hello



I have made a program that runs as a service.

My program does not shut down correctly when windows is restarted or shut

down.



It seems as if my service handler is never called (only the service main

function is called at start up). If I stop the service manually from

services.msc (I am using win xp pro) the service handler is called

correctly

and the programs shuts down correctly.



My program has a window that is closed when the users logs off the

computer

(by overriding CWnd::OnEndSession), but how do I know tell if OnEndSession

has been called because the computer is being shutdown or if it has been

called because the user is logging off??



cheers



Peter







I solved part of the problem, in OnEndSession you can use this code to

determine if windows is shutting down or if the user is logging off.

if (GetCurrentMessage()->lParam & ENDSESSION_LOGOFF) // user logoff



else // computer shutdown







However if you shut down the computer from the welcome screen (when no user

has logged on & thus OndEndSession does not work) my program does not shut

down correctly.



When the computer is shut down it seems that no message is sent to either

the service handler function or to call to



WTSWaitSystemEvent(WTS_CURRENT_SERVER_HANDLE,WTS_EVENT_ALL,&EventFlags); in

the service main function.







Happy new year!



Peter





-

Re:Windows shutdown & program running as service

I suppose you call RegisterServiceCtrlHandler(Ex)? Does your handler receive

SERVICE_CONTROL_SHUTDOWN?



"Peter" <pindemad@yahoo.com>wrote in message

Quote


"Peter" <pindemad@yahoo.com>wrote in message

news:rZPAd.78337$Vf.3670938@news000.worldonline.dk...

>Hello

>

>I have made a program that runs as a service.

>My program does not shut down correctly when windows is restarted or shut

>down.

>

>It seems as if my service handler is never called (only the service main

>function is called at start up). If I stop the service manually from

>services.msc (I am using win xp pro) the service handler is called

correctly

>and the programs shuts down correctly.

>

>My program has a window that is closed when the users logs off the

computer

>(by overriding CWnd::OnEndSession), but how do I know tell if

>OnEndSession

>has been called because the computer is being shutdown or if it has been

>called because the user is logging off??

>

>cheers

>

>Peter

>

>

>

I solved part of the problem, in OnEndSession you can use this code to

determine if windows is shutting down or if the user is logging off.

if (GetCurrentMessage()->lParam & ENDSESSION_LOGOFF) // user logoff



else // computer shutdown







However if you shut down the computer from the welcome screen (when no

user

has logged on & thus OndEndSession does not work) my program does not shut

down correctly.



When the computer is shut down it seems that no message is sent to either

the service handler function or to call to



WTSWaitSystemEvent(WTS_CURRENT_SERVER_HANDLE,WTS_EVENT_ALL,&EventFlags);

in

the service main function.







Happy new year!



Peter









-

Re:Windows shutdown & program running as service

Yes I have used RegisterServiceCtrlHandlerEx to register the service

handler.

When I close the service manually (clicking stop service in services.msc) it

does receive

SERVICE_CONTROL_STOP



.

It never receives SERVICE_CONTROL_SHUTDOWN when the computer shuts down.

Peter

"Alexander Grigoriev" <alegr@earthlink.net>wrote in message

Quote
I suppose you call RegisterServiceCtrlHandler(Ex)? Does your handler

receive

SERVICE_CONTROL_SHUTDOWN?



"Peter" <pindemad@yahoo.com>wrote in message

news:ki%Ad.78576$Vf.3674367@news000.worldonline.dk...

>

>"Peter" <pindemad@yahoo.com>wrote in message

>news:rZPAd.78337$Vf.3670938@news000.worldonline.dk...

>>Hello

>>

>>I have made a program that runs as a service.

>>My program does not shut down correctly when windows is restarted or

shut

>>down.

>>

>>It seems as if my service handler is never called (only the service

main

>>function is called at start up). If I stop the service manually from

>>services.msc (I am using win xp pro) the service handler is called

>correctly

>>and the programs shuts down correctly.

>>

>>My program has a window that is closed when the users logs off the

>computer

>>(by overriding CWnd::OnEndSession), but how do I know tell if

>>OnEndSession

>>has been called because the computer is being shutdown or if it has

been

>>called because the user is logging off??

>>

>>cheers

>>

>>Peter

>>

>>

>>

>I solved part of the problem, in OnEndSession you can use this code to

>determine if windows is shutting down or if the user is logging off.

>if (GetCurrentMessage()->lParam & ENDSESSION_LOGOFF) // user logoff

>

>else // computer shutdown

>

>

>

>However if you shut down the computer from the welcome screen (when no

>user

>has logged on & thus OndEndSession does not work) my program does not

shut

>down correctly.

>

>When the computer is shut down it seems that no message is sent to

either

>the service handler function or to call to

>

>WTSWaitSystemEvent(WTS_CURRENT_SERVER_HANDLE,WTS_EVENT_ALL,&EventFlags);

>in

>the service main function.

>

>

>

>Happy new year!

>

>Peter

>

>









-

Re:Windows shutdown & program running as service

This may help:



When the user shuts down the system, all control handlers that have called

SetServiceStatus with the SERVICE_ACCEPT_SHUTDOWN control code receive the

SERVICE_CONTROL_SHUTDOWN control code.



"Peter" <pindemad@yahoo.com>wrote in message

Quote
Yes I have used RegisterServiceCtrlHandlerEx to register the service

handler.

When I close the service manually (clicking stop service in services.msc)

it

does receive

SERVICE_CONTROL_STOP



.

It never receives SERVICE_CONTROL_SHUTDOWN when the computer shuts down.

Peter

"Alexander Grigoriev" <alegr@earthlink.net>wrote in message

news:uUkRN1u7EHA.3368@TK2MSFTNGP10.phx.gbl...

>I suppose you call RegisterServiceCtrlHandler(Ex)? Does your handler

receive

>SERVICE_CONTROL_SHUTDOWN?

>

>"Peter" <pindemad@yahoo.com>wrote in message

>news:ki%Ad.78576$Vf.3674367@news000.worldonline.dk...

>>

>>"Peter" <pindemad@yahoo.com>wrote in message

>>news:rZPAd.78337$Vf.3670938@news000.worldonline.dk...

>>>Hello

>>>

>>>I have made a program that runs as a service.

>>>My program does not shut down correctly when windows is restarted or

shut

>>>down.

>>>

>>>It seems as if my service handler is never called (only the service

main

>>>function is called at start up). If I stop the service manually from

>>>services.msc (I am using win xp pro) the service handler is called

>>correctly

>>>and the programs shuts down correctly.

>>>

>>>My program has a window that is closed when the users logs off the

>>computer

>>>(by overriding CWnd::OnEndSession), but how do I know tell if

>>>OnEndSession

>>>has been called because the computer is being shutdown or if it has

been

>>>called because the user is logging off??

>>>

>>>cheers

>>>

>>>Peter

>>>

>>>

>>>

>>I solved part of the problem, in OnEndSession you can use this code to

>>determine if windows is shutting down or if the user is logging off.

>>if (GetCurrentMessage()->lParam & ENDSESSION_LOGOFF) // user logoff

>>

>>else // computer shutdown

>>

>>

>>

>>However if you shut down the computer from the welcome screen (when no

>>user

>>has logged on & thus OndEndSession does not work) my program does not

shut

>>down correctly.

>>

>>When the computer is shut down it seems that no message is sent to

either

>>the service handler function or to call to

>>

>>WTSWaitSystemEvent(WTS_CURRENT_SERVER_HANDLE,WTS_EVENT_ALL,&EventFlags);

>>in

>>the service main function.

>>

>>

>>

>>Happy new year!

>>

>>Peter

>>

>>

>

>









-

Re:Windows shutdown & program running as service

Thanks a lot!

Now it works

"Alexander Grigoriev" <alegr@earthlink.net>wrote in message

Quote
This may help:



When the user shuts down the system, all control handlers that have called

SetServiceStatus with the SERVICE_ACCEPT_SHUTDOWN control code receive the

SERVICE_CONTROL_SHUTDOWN control code.



"Peter" <pindemad@yahoo.com>wrote in message

news:to7Bd.78593$Vf.3674657@news000.worldonline.dk...

>Yes I have used RegisterServiceCtrlHandlerEx to register the service

>handler.

>When I close the service manually (clicking stop service in

services.msc)

>it

>does receive

>SERVICE_CONTROL_STOP

>

>.

>It never receives SERVICE_CONTROL_SHUTDOWN when the computer shuts down.

>Peter

>"Alexander Grigoriev" <alegr@earthlink.net>wrote in message

>news:uUkRN1u7EHA.3368@TK2MSFTNGP10.phx.gbl...

>>I suppose you call RegisterServiceCtrlHandler(Ex)? Does your handler

>receive

>>SERVICE_CONTROL_SHUTDOWN?

>>

>>"Peter" <pindemad@yahoo.com>wrote in message

>>news:ki%Ad.78576$Vf.3674367@news000.worldonline.dk...

>>>

>>>"Peter" <pindemad@yahoo.com>wrote in message

>>>news:rZPAd.78337$Vf.3670938@news000.worldonline.dk...

>>>>Hello

>>>>

>>>>I have made a program that runs as a service.

>>>>My program does not shut down correctly when windows is restarted or

>shut

>>>>down.

>>>>

>>>>It seems as if my service handler is never called (only the service

>main

>>>>function is called at start up). If I stop the service manually from

>>>>services.msc (I am using win xp pro) the service handler is called

>>>correctly

>>>>and the programs shuts down correctly.

>>>>

>>>>My program has a window that is closed when the users logs off the

>>>computer

>>>>(by overriding CWnd::OnEndSession), but how do I know tell if

>>>>OnEndSession

>>>>has been called because the computer is being shutdown or if it has

>been

>>>>called because the user is logging off??

>>>>

>>>>cheers

>>>>

>>>>Peter

>>>>

>>>>

>>>>

>>>I solved part of the problem, in OnEndSession you can use this code

to

>>>determine if windows is shutting down or if the user is logging off.

>>>if (GetCurrentMessage()->lParam & ENDSESSION_LOGOFF) // user logoff

>>>

>>>else // computer shutdown

>>>

>>>

>>>

>>>However if you shut down the computer from the welcome screen (when

no

>>>user

>>>has logged on & thus OndEndSession does not work) my program does not

>shut

>>>down correctly.

>>>

>>>When the computer is shut down it seems that no message is sent to

>either

>>>the service handler function or to call to

>>>

>>>

WTSWaitSystemEvent(WTS_CURRENT_SERVER_HANDLE,WTS_EVENT_ALL,&EventFlags);

>>>in

>>>the service main function.

>>>

>>>

>>>

>>>Happy new year!

>>>

>>>Peter

>>>

>>>

>>

>>

>

>









-