2011-11-21 58 views
1

我有一个问题与Windows服务,我的应用程序注册的Windows服务,但是当我试图跑,我发现了以下错误的服务:“错误1053:服务没有没有及时响应启动或控制请求“。以下代码负责注册服务(我从MSDN获得它)。问题与Windows服务(C++,WinAPI的)

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); 

你能帮我解决这个问题吗?

+0

你需要做的服务的一些调试。我们无法从服务注册码中解决问题! –

+0

我完成了所有可能的调试过程,服务只是不开始。并说已经提到的错误。 –

+0

服务得到执行多远?告诉我们关于迄今为止所做的调试。你有没有试过从你的服务中剥离掉所有的代码,这样它什么都不做? –

回答

6

如果给定的代码是你试过你错过了窗口服务的一些重要要求的唯一的东西。请看看在documentation

至少需要一个服务的主要功能(至极是主要方法不同!)和控制处理函数,你不能处理“开始”命令,如果有没有登记控制处理功能

(至极的服务主要完成)。为了正常工作,你需要:

  1. 普通的主要方法,以确定是否要安装服务或以其他方式启动与此表中包含的服务SERVICE_TABLE_ENTRY
    服务控制调度baiscally进程名和函数指针到它的服务功能主要
  2. 您需要的服务的主要功能至极registeres功能的服务控制处理功能事后启动该服务代码功能
  3. 服务代码功能包含与服务工作的代码,它的服务
  4. 您需要的业务控制功能左撇子的心脏。它是从Windows的服务控制管理器中调用的,只要控制代码发送到服务...这是接收“停止”命令的方法...并且如果此函数不存在或未正确注册你可能最终得到像提到的错误...
+0

非常感谢,这工作!非常感谢:) –

+0

不客气;-) – xmoex