I've coded a WinNT service, but as soon as I call
StartServiceCtrlDispatcher(), this function returns with Error Code 1063
(means something like "Can't connect to Service Control Manager", right?).
My main() part looks like this:
/*********************/
void _cdecl main(void)
/*********************/
{
SERVICE_TABLE_ENTRY DispatchTable[] = {
{"tppe", TPPEServiceStart},
{NULL, NULL }
};
if(!StartServiceCtrlDispatcher(DispatchTable)) {
SvcDebugOut("TPPE StartServiceCtrlDispatcher error= %d\n",
GetLastError());
}
}
I copied that from the MSDN CD. What could be wrong? If I write some Service
Control Programs, they work just fine. I can install/delete the service and
retrieve status information.
TIA, Alex
How do you try to start the service ?
If you just call the EXE file, you're wrong : once registered, a service
must be started by "NET START TPPE" (if your service's name is TPPE).
If that's what you do, be sure the function TPPEServiceStart registers
a service control handler function to handle service control manager
requests (such as STOP).
You can do this by calling :
RegisterServiceCtrlHandler or RegisterServiceCtrlHandlerEx.
Then, make sure you call tell the service control manager the service is
fully started, by calling :
SetServiceStatus.
Be careful : RegisterServiceCtrlHandler must be called before the first
call to SetServiceStatus.
A service main should be like this :
// ---------------------------------------------------------
void WINAPI NTSVCMain( IN DWORD dwArgc, IN char ** ppcArgv )
{
DWORD dwErr;
SERVICE_STATUS_HANDLE hStatus;
hStatus = RegisterServiceCtrlHandler(
NAME_OF_THE_SERVICE,
(LPHANDLER_FUNCTION)NTSVCHandler );
if ( ! hStatus )
{
NTSVCError( "RegisterServiceCtrlHandler", GetLastError() );
return;
}
// TO DO : Change the service state to SERVICE_START_PENDING
// TO DO : Initialize the service (resources etc.)
// TO DO : Change the service state to SERVICE_STARTED
// TO DO : Run the service until stop request
// TO DO : Change the service state to SERVICE_STOP_PENDING
// TO DO : Terminate the service (resources etc.)
// TO DO : Change the service state to SERVICE_STOPPED
}
// ---------------------------------------------------------
A service control handler should be like this :
// ---------------------------------------------------------
void WINAPI NTSVCHandler( IN DWORD dwCtrlCode )
{
switch( dwCtrlCode )
{
case SERVICE_CONTROL_STOP :
case SERVICE_CONTROL_SHUTDOWN :
// TO DO : Change the service state to SERVICE_STOP_PENDING
// TO DO : Signal the service main thread to terminate
break;
case SERVICE_CONTROL_PAUSE :
// TO DO : ONLY IF THE SERVICE HANDLES PAUSE COMMANDS
// TO DO : Change the service state to SERVICE_PAUSE_PENDING
// TO DO : Signal the service main thread to pause
break;
case SERVICE_CONTROL_CONTINUE :
// TO DO : ONLY IF THE SERVICE HANDLES PAUSE COMMANDS
// TO DO : Signal the service main thread to resume
break;
case SERVICE_CONTROL_INTERROGATE :
default:
// TO DO : Report immediatly current status
break;
}
}
// ---------------------------------------------------------