Creating a service (SERVICE_ACCEPT_SESSIONCHANGE)

Posted by Ron on Stack Overflow See other posts from Stack Overflow or by Ron
Published on 2010-04-27T16:30:02Z Indexed on 2010/04/27 16:33 UTC
Read the original article Hit count: 361

Filed under:
|
|

Hi there,

I am trying to create a service following the example documented in the link below: http://msdn.microsoft.com/en-us/library/bb540475(v=VS.85).aspx

What I am interested in is to be able to catch user "lock" and "unlock" workstation events.

Using the code on from the example provided, I modified the following:

Line 15:

Original:

VOID WINAPI SvcCtrlHandler( DWORD );

Modified:

DWORD WINAPI SvcCtrlHandler( DWORD, DWORD, LPVOID, LPVOID );

Line 141:

Original:

gSvcStatusHandle = RegisterServiceCtrlHandler( SVCNAME, SvcCtrlHandler);

Modified:

gSvcStatusHandle = RegisterServiceCtrlHandlerEx( SVCNAME, SvcCtrlHandler, NULL);

Line 244:

Original:

SvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;

Modified:

gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SESSIONCHANGE;

Line 266:

Original:

VOID WINAPI SvcCtrlHandler( DWORD dwCtrl ) { // Handle the requested control code.

switch(dwCtrl) {
case SERVICE_CONTROL_STOP: ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);

     // Signal the service to stop.

     SetEvent(ghSvcStopEvent);
     ReportSvcStatus(gSvcStatus.dwCurrentState, NO_ERROR, 0);

     return;

  case SERVICE_CONTROL_INTERROGATE: 
     break; 

  default: 
     break;

}
}`

Modified:

DWORD WINAPI SvcCtrlHandler( DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext ) { DWORD dwErrorCode = NO_ERROR;

switch(dwControl) {
case SERVICE_CONTROL_STOP: ReportSvcStatus(SERVICE_STOP_PENDING, NO_ERROR, 0);

     // Signal the service to stop.

     SetEvent(ghSvcStopEvent);
     ReportSvcStatus(gSvcStatus.dwCurrentState, NO_ERROR, 0);

break;

  case SERVICE_CONTROL_INTERROGATE: 
     break; 

  case SERVICE_CONTROL_SESSIONCHANGE:

ReportSvcStatus(gSvcStatus.dwCurrentState, NO_ERROR, 0); break;

  default:

break; }

return dwErrorCode;
}

With the changes above, my service compiled and install fine. But when I try to start my service on my Windows 2000 machine, it does not start properly (it will be stuck on the "starting" status)

Can anyone please advise? Thank you in advance,

Ron

© Stack Overflow or respective owner

Related posts about Windows

Related posts about win32