Reputation: 7630
I have created a Http Server(C++ Win 32 console application ) and I wanted to run it is service. This server has main thread and Listener thread which will listen to the incoming traffic.main thread will block forever.
I have created a NSIS Installer which uses SimpleSC plugin to install and the run the server
SimpleSC::InstallService "HttpServer" "HttpServer" "16" "2" "$INSTDIR\Server.exe" "" "" ""
SimpleSC::StartService "HttpServer" "" 30
I am able to install service but its not starting and return 1053 error.Is this because of main thread block ? Please help me on this.
Upvotes: 1
Views: 1465
Reputation: 11465
The problem must be in your service code. In the service control handler that you declared in the call to RegisterServiceCtrlHandler()
you need to handle several request types and return according feedback to the system service manager. It let the system kow that your service is working correctly and what is its current state.
If you do not answer to all request types (esp. SERVICE_CONTROL_INTERROGATE
), or if you do not answer in a limited time, the system will deduce that your service has failed / is stalled.
This is an example of control handler that I use in a code of mine:
//Feedback() is a custom function to put log into the system events and / or OutputDebugString()
//mSrvStatus is a global SERVICE_STATUS
void WINAPI SrvControlHandler(DWORD Opcode) {
DWORD state;
switch (Opcode) {
case SERVICE_CONTROL_PAUSE:
Feedback(FEED_EVENTS|FEED_ODS, "Pausing %s", SRVNAME);
bActive = false;
state = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
//refresh our settings from registry before continuing
GetRegistrySettings();
Feedback(FEED_EVENTS|FEED_ODS, "Continuing %s with refresh=%d", SRVNAME, dwRefresh);
bActive = true;
state = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
Feedback(FEED_EVENTS|FEED_ODS, "Stopping %s", SRVNAME);
ReportSrvStatus(SERVICE_STOP_PENDING, NO_ERROR, 0); //ok, we begin to stop
//The final ReportSrvStatus(SERVICE_STOPPED, NO_ERROR, 0);
//is sent from the function that started the service
//that is waiting forever on the hSrvStopEvt event
bActive = false; //we tell the thread to stop fetching
SetEvent(hSrvStopEvt); //and we signal the final event
return;
break;
case SERVICE_CONTROL_INTERROGATE:
state = mSrvStatus.dwCurrentState;
Feedback(FEED_ODS, "%s interrogated by SCM, returned %d", SRVNAME, state);
break;
default:
Feedback(FEED_ODS, "other control resquest ?? - %d", Opcode);
state = mSrvStatus.dwCurrentState;
}
ReportSrvStatus(state, NO_ERROR, 0);
}
/* Sets the current service status and reports it to the SCM.
Parameters:
dwCurrentState - The current state (see SERVICE_STATUS)
dwWin32ExitCode - The system error code
dwWaitHint - Estimated time for pending operation, in milliseconds
*/
void ReportSrvStatus( DWORD dwCurrentState,
DWORD dwWin32ExitCode,
DWORD dwWaitHint) {
static DWORD dwCheckPoint = 1;
mSrvStatus.dwCurrentState = dwCurrentState;
mSrvStatus.dwWin32ExitCode = dwWin32ExitCode;
mSrvStatus.dwWaitHint = dwWaitHint;
if (dwCurrentState == SERVICE_START_PENDING)
mSrvStatus.dwControlsAccepted = 0;
else mSrvStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_PAUSE_CONTINUE|SERVICE_ACCEPT_SHUTDOWN;
if ( (dwCurrentState == SERVICE_RUNNING) ||
(dwCurrentState == SERVICE_STOPPED) )
mSrvStatus.dwCheckPoint = 0;
else mSrvStatus.dwCheckPoint = dwCheckPoint++;
SetServiceStatus( hSrvStatus, &mSrvStatus );
}
Upvotes: 1