Windows service is disabled

I am using VS6 and ATL with CServiceModule to implement a custom windows service. In the event of a fatal error, the service should close. Since CServiceModule is available through the _Module variable in all files, I thought of something like this to force CServiceModule :: Run to stop pumping messages and close itself

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

      

Is this correct or do you have an idea?

0


source to share


3 answers


To stop it yourself, you send a Service Manager command. Try this sample:




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


source


I believe that if you do this, the service manager will think that your service has crashed, and if the user ever configures it to restart automatically, it will.

In .NET, you use a ServiceController to signal the end of a service. I would expect this to be similar to Win32, since most of this stuff in .NET is just wrappers. Unfortunately I don't have any C ++ code to disable, but here's the .NET code. This will hopefully help you Google with the information you need or find docs on MSDN.



This is from some test suite code, so error checking style;) You will need to put this code into a stream to handle the completion message.

  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


source


You probably want to use the ControlService or ControlServiceEx methods to shut down the service. You should be able to get the required handle from the CServiceModule.

0


source







All Articles