Hello, I'm having a problem developing a service for Windows using MinGW-W64 g++: When I start the service with the sc command, I get an error: "The service did not respond to the start or control request in a timely fashion.".
I believe the problem is not with my code because when I build with MinGW-W64 gcc, the service starts and operates as expected. Also, when I build with Visual Studio 2013 Express, the service starts and operates as expected. I've attached testService.cpp, a minimal version of the code that exhibits this issue, buildAndRun, a bash script to build and run the service, and errorOutput.log that contains the output from building with g++, and starting/stopping the service. I would appreciate any help in building the service in g++, because I'll be using g++, and wish to avoid using Visual Studio. Additional info-- The g++ command line is: g++ -I. -std=c++11 -Wall -g -O0 -DWIN32 testService.cpp -o testService The output of "g++ --version" is: g++.exe (i686-posix-dwarf-rev1, Built by MinGW-W64 project) 4.9.1 Copyright (C) 2014 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. My Windows system is Windows 7 Home Premium, Service Pack 1 under VirtualBox (AMD Phenom II X4 955 processor 3.17 GHz, 2 GB RAM, 64-bit OS). I'm running MSYS; the output of "uname --all" is: MINGW32_NT-6.1 STUMPY 1.0.11(0.46/3/2) 2009-07-11 17:46 i686 Msys Looking over the documentation of the command-line parameters for gcc and g++ didn't shed any light on this. While trying to resolve this, I have been able to build a service with g++ that did run, but failed when I modified the service to write to a file using std::ofstream. I'd appreciate any suggestions, workarounds, etc. Thanks, Alban
buildAndRun
Description: Binary data
[SC] CreateService SUCCESS [SC] StartService FAILED 1053: The service did not respond to the start or control request in a timely fashion. [SC] ControlService FAILED 1062: The service has not been started. [SC] DeleteService SUCCESS
#include <windows.h>
#include <stdio.h>
#define SLEEP_TIME 1000
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
extern "C" {
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ControlHandler(DWORD request);
char logFile[2048];
int WriteToLog(const char *str) {
FILE* log;
log = fopen(logFile, "a+");
if (log == NULL)
return -1;
fprintf(log, "%s\n", str);
fclose(log);
return 0;
}
int main() {
SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceName = (char *) "TestService";
ServiceTable[0].lpServiceProc = ServiceMain;
ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = NULL;
StartServiceCtrlDispatcher(ServiceTable);
return 0;
}
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv) {
int error;
/* Save log file name. */
strcpy(logFile, argv[1]);
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted =
SERVICE_ACCEPT_STOP |
SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
hStatus = RegisterServiceCtrlHandler("TestService",
ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0) {
return;
}
error = WriteToLog("Service started.");
if (error) {
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
/* Report status to SCM. */
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus (hStatus, &ServiceStatus);
/* Simulate doing work. */
while (ServiceStatus.dwCurrentState == SERVICE_RUNNING) {
if (WriteToLog("Service is running"))
{
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
Sleep(SLEEP_TIME);
}
return;
}
/* Control handler function. */
void WINAPI ControlHandler(DWORD request) {
switch(request)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
WriteToLog("Service stopped.");
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
default:
break;
}
SetServiceStatus (hStatus, &ServiceStatus);
return;
}
}
------------------------------------------------------------------------------ Comprehensive Server Monitoring with Site24x7. Monitor 10 servers for $9/Month. Get alerted through email, SMS, voice calls or mobile push notifications. Take corrective actions from your mobile device. http://p.sf.net/sfu/Zoho
_______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
