2008-12-05 99 views
0

我使用VS6和ATL与CServiceModule来实现自定义Windows服务。如果发生致命错误,服务应该自行关闭。由于CServiceModule是通过_Module变量可以在所有的文件,我认为是这样造成CServiceModule ::运行停止抽水信息和自行关闭Windows服务关闭

PostThreadMessage(_Module.dwThreadID, WM_QUIT, 0, 0); 

这是正确的,或者你有更好的主意吗?

回答

0

对于自我关机,您将命令发送到服务管理器。试试这个样本:


BOOL StopServiceCmd (const char * szServiceName) 
{ 
    SC_HANDLE schService; 
    SC_HANDLE schSCManager; 
    SERVICE_STATUS ssStatus;  // current status of the service 
    BOOL bRet; 
    int iCont=0; 

    schSCManager = OpenSCManager( 
     NULL, // machine (NULL == local) 
     NULL, // database (NULL == default) 
     SC_MANAGER_ALL_ACCESS // access required 
     ); 
    if (schSCManager) 
    { 
     schService = OpenService(schSCManager, szServiceName, SERVICE_ALL_ACCESS); 

     if (schService) 
     { 
      // try to stop the service 
      if (ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus)) 
      { 
       Sleep(1000); 

       while(QueryServiceStatus(schService, &ssStatus)) 
       { 
        iCont++; 
        if (ssStatus.dwCurrentState == SERVICE_STOP_PENDING) 
        { 
         Sleep(1000); 
         if (iCont > 4) break; 
        } 
        else 
         break; 
       } 

       if (ssStatus.dwCurrentState == SERVICE_STOPPED) 
        bRet = TRUE; 
       else 
        bRet = FALSE; 
      } 

      CloseServiceHandle(schService); 
     } 
     else 
      bRet = FALSE; 

     CloseServiceHandle(schSCManager); 
    } 
    else 
     bRet = FALSE; 

    return bRet; 
} 
0

我相信,如果你这样做,那么服务经理会认为你的服务已经崩溃,如果用户将它设置为自动重启,它会。

在.NET中,您使用ServiceController来指示服务关闭。我期望它在Win32中类似,因为.NET中的大部分东西都只是包装器。对不起,我没有方便关闭服务的C++代码,但这里是.NET代码。这将有希望帮助您Google所需的信息,或者在MSDN中查找文档。

这是来自一些测试套件代码,因此错误检查的样式;)您将需要将此代码放入一个线程中,以便处理关闭消息。

private void stopPLService(bool close) 
    { 
    if (m_serviceController == null) 
    { 
     m_serviceController = new ServiceController("PLService"); 
    } 

    WriteLine("StopPLService"); 

    if (m_serviceController != null) 
    { 
     try 
     { 
      m_serviceController.Stop(); 
     } 
     catch 
     { 
      // Probably just means that it wasn't running or installed, ignore 
     } 

     // Wait up to 30 seconds for the service to stop 
     try 
     { 
      m_serviceController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30)); 
     } 
     catch (System.ServiceProcess.TimeoutException) 
     { 
      Assert.Fail("Timeout waiting for PLService to stop"); 
     } 
     catch 
     { 
      // Not installed, we only care in the start 
     } 
     if (close) 
     { 
      m_serviceController.Close(); 
      m_serviceController = null; 
     } 
    } 
    } 
0

您可能想要使用ControlService或ControlServiceEx方法关闭您的服务。您应该能够从CServiceModule获取所需的句柄。