简体   繁体   English

Windows服务问题(C ++,WinAPI)

[英]Issue with windows service (C++,WinAPI)

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". 我的Windows服务有问题,我的应用程序注册了Windows服务,但是当我尝试运行该服务时,出现以下错误:“错误1053:服务未及时响应启动或控制请求”。 The following code is responsible for registering service (I've got it from MSDN). 以下代码负责注册服务(我从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 ? 您能帮忙解决这个问题吗?

In case the given code is the only thing you tried you're missing some important requirements for a windows service. 如果给定的代码是您尝试的唯一方法,那么您将缺少Windows服务的一些重要要求。 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: 为了正常工作,您需要:

  1. the ordinary main method , to determine if you want to install the service or to otherwise start the service control dispatcher with the services SERVICE_TABLE_ENTRY 普通的main方法 ,以确定您是要安装服务还是要通过服务SERVICE_TABLE_ENTRY启动服务控制调度程序
    this table contains baiscally the process name and the function pointer to it's service main function 该表以百分数形式包含进程名称和指向其服务主函数的函数指针
  2. you need the service main function wich registeres the functions service control handler function and afterwards starts the service code function 您需要服务主要功能,其中需要注册功能服务控制处理程序功能,然后启动服务代码功能
  3. the service code function contains the code related to the services job, it's the heart of the service 服务代码功能包含与服务工作相关的代码,这是服务的核心
  4. you need the service control hander function . 您需要服务控制处理程序功能 it is called from windows' service control manager, whenever a control code is sent to the service... this is the method that receives the "stop" command... and if this function doesn't exists or isn't registered properly you might end up with an error like the one mentioned... 每当将控制代码发送到服务时,都会从Windows的服务控制管理器中调用该方法...这是接收“停止”命令的方法...并且如果此功能不存在或未正确注册您可能会遇到类似上述错误的错误...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM