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 = RegisterServiceCtrlHa开发者_开发技巧ndlerEx( SVCNAME, SvcCtrlHandler, NULL);
Line 244:
Original:
gSvcStatus.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.
I try starting my service on the windows 7 machine, it would start fine (but I will not be able to stop, pause or do anything to the service as all option will be disabled).
I also tried my service on my Windows 2000 machine, it does not even start(it will be stuck on the "starting" status)
My main objective is getting the service to run properly on the Windows 7 environment, getting it backward compatible in Windows 2000 will be a bonus.
Can anyone please advise? Thank you in advance,
Ron
I have a test service here that accepts session change events and runs just fine on Windows 7. It supports all notifications possible and correctly allows stop and pause and deals with the session change events.
Not that helpful, I know, but at least you know that you should be able to get it to work as documented... My service code is considerably more complex than the basic example you have there so I can't see at a glance what mistake you're making.
Since you're not getting seeing stop available as an option I'd check that you are setting the controls accepted correctly, or change just that back to just stop and make sure that you are seeing stop as an option once you start the service...
MSDN says : SERVICE_ACCEPT_SESSIONCHANGE: "Windows 2000: This value is not supported."
Not sure if that is the actual reason why the service gets stuck in the starting phase.
Edit: Alternative ways to get session events on windows 2000:
- Winlogon Notification Packages
- SENS: ISensLogon
精彩评论