0

我将 VS6 和 ATL 与 CServiceModule 一起使用来实现自定义 Windows 服务。如果出现致命错误,服务应自行关闭。由于 CServiceModule 可通过所有文件中的 _Module 变量获得,我想到了这样的事情来导致 CServiceModule::Run 停止发送消息并自行关闭

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

这是正确的还是你有更好的主意?

4

3 回答 3

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;
        }
     }
  }
于 2008-12-05T14:58:51.937 回答
0

您可能希望使用 ControlService 或ControlServiceEx方法来关闭您的服务。您应该能够从 CServiceModule 获得所需的句柄。

于 2008-12-07T16:47:54.197 回答
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;
} 
于 2009-01-21T00:35:22.043 回答