![](/img/trans.png)
[英]Visual C++ Project type for Windows Service that calls the WinAPI directly
[英]Issue with windows service (C++,WinAPI)
我的Windows服务有问题,我的应用程序注册了Windows服务,但是当我尝试运行该服务时,出现以下错误:“错误1053:服务未及时响应启动或控制请求”。 以下代码负责注册服务(我从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);
您能帮忙解决这个问题吗?
如果给定的代码是您尝试的唯一方法,那么您将缺少Windows服务的一些重要要求。 请看一下文档
您至少需要一个服务主函数 (与主方法不同!)和一个控制处理程序函数,因为如果未注册任何控制处理程序功能 ,您将无法处理“启动”命令(在服务主目录中完成)
为了正常工作,您需要:
SERVICE_TABLE_ENTRY
启动服务控制调度程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.