Reputation: 6323
I have an issue with windows services, my application registering Windows service but when I'm trying to run the service I'm getting the following error: "Error 1053: The service did not respond to the start or control request in a timely fashion". The following code is responsible for registering service (I've got it from MSDN).
SC_HANDLE schSCManager;
SC_HANDLE schService;
path modulePath("some path to executable");
std::string moduleName = narrow(modulePath.native());
if(!GetModuleFileNameA(NULL, &moduleName[0], MAX_PATH))
{
throw std::runtime_error("Cannot register service, error code: " + boost::lexical_cast<std::string>(GetLastError()));
}
// Get a handle to the SCM database.
schSCManager = OpenSCManager(NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if(!schSCManager)
{
throw std::runtime_error("OpenSCManager failed: " + boost::lexical_cast<std::string>(GetLastError()));
}
// Create the service
schService = CreateServiceA(
schSCManager, // SCM database
"name", // name of service
"displayname", // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
narrow(modulePath.native()).c_str(), // path to service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if(!schService)
{
CloseServiceHandle(schSCManager);
throw std::runtime_error("CreateService failed: " + boost::lexical_cast<std::string>(GetLastError()));
}
else
{
//std::cout << "\nService installed successfully\n";
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
Could you please help to fix this ?
Upvotes: 1
Views: 1752
Reputation: 2702
In case the given code is the only thing you tried you're missing some important requirements for a windows service. Please have a look at the documentation
You need at least a service main function (wich is different from the main method!) and a control handler function as you can't handle the "start" command if there's no control handler function registered (wich is done in service main)
In order to work properly you need:
SERVICE_TABLE_ENTRY
Upvotes: 6