• Starting a Service


    https://docs.microsoft.com/en-us/windows/win32/services/starting-a-service

    05/31/2018

    To start a service, a service control program opens a handle to an installed database and then specifies the handle in a call to the StartService function. After starting the service, the program uses the members of the SERVICE_STATUS_PROCESS structure returned by the QueryServiceStatusEx function to track the progress of the service.

    The DoStartSvc function in the following example shows how to start a service. The szSvcName variable is a global variable that contains the name of the service to be started. For the complete example that sets this variable, see SvcControl.cpp.

    //
    // Purpose: 
    //   Starts the service if possible.
    //
    // Parameters:
    //   None
    // 
    // Return value:
    //   None
    //
    VOID __stdcall DoStartSvc()
    {
        SERVICE_STATUS_PROCESS ssStatus; 
        DWORD dwOldCheckPoint; 
        DWORD dwStartTickCount;
        DWORD dwWaitTime;
        DWORD dwBytesNeeded;
    
        // Get a handle to the SCM database. 
     
        schSCManager = OpenSCManager( 
            NULL,                    // local computer
            NULL,                    // servicesActive database 
            SC_MANAGER_ALL_ACCESS);  // full access rights 
     
        if (NULL == schSCManager) 
        {
            printf("OpenSCManager failed (%d)
    ", GetLastError());
            return;
        }
    
        // Get a handle to the service.
    
        schService = OpenService( 
            schSCManager,         // SCM database 
            szSvcName,            // name of service 
            SERVICE_ALL_ACCESS);  // full access 
     
        if (schService == NULL)
        { 
            printf("OpenService failed (%d)
    ", GetLastError()); 
            CloseServiceHandle(schSCManager);
            return;
        }    
    
        // Check the status in case the service is not stopped. 
    
        if (!QueryServiceStatusEx( 
                schService,                     // handle to service 
                SC_STATUS_PROCESS_INFO,         // information level
                (LPBYTE) &ssStatus,             // address of structure
                sizeof(SERVICE_STATUS_PROCESS), // size of structure
                &dwBytesNeeded ) )              // size needed if buffer is too small
        {
            printf("QueryServiceStatusEx failed (%d)
    ", GetLastError());
            CloseServiceHandle(schService); 
            CloseServiceHandle(schSCManager);
            return; 
        }
    
        // Check if the service is already running. It would be possible 
        // to stop the service here, but for simplicity this example just returns. 
    
        if(ssStatus.dwCurrentState != SERVICE_STOPPED && ssStatus.dwCurrentState != SERVICE_STOP_PENDING)
        {
            printf("Cannot start the service because it is already running
    ");
            CloseServiceHandle(schService); 
            CloseServiceHandle(schSCManager);
            return; 
        }
    
        // Save the tick count and initial checkpoint.
    
        dwStartTickCount = GetTickCount();
        dwOldCheckPoint = ssStatus.dwCheckPoint;
    
        // Wait for the service to stop before attempting to start it.
    
        while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
        {
            // Do not wait longer than the wait hint. A good interval is 
            // one-tenth of the wait hint but not less than 1 second  
            // and not more than 10 seconds. 
     
            dwWaitTime = ssStatus.dwWaitHint / 10;
    
            if( dwWaitTime < 1000 )
                dwWaitTime = 1000;
            else if ( dwWaitTime > 10000 )
                dwWaitTime = 10000;
    
            Sleep( dwWaitTime );
    
            // Check the status until the service is no longer stop pending. 
     
            if (!QueryServiceStatusEx( 
                    schService,                     // handle to service 
                    SC_STATUS_PROCESS_INFO,         // information level
                    (LPBYTE) &ssStatus,             // address of structure
                    sizeof(SERVICE_STATUS_PROCESS), // size of structure
                    &dwBytesNeeded ) )              // size needed if buffer is too small
            {
                printf("QueryServiceStatusEx failed (%d)
    ", GetLastError());
                CloseServiceHandle(schService); 
                CloseServiceHandle(schSCManager);
                return; 
            }
    
            if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
            {
                // Continue to wait and check.
    
                dwStartTickCount = GetTickCount();
                dwOldCheckPoint = ssStatus.dwCheckPoint;
            }
            else
            {
                if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
                {
                    printf("Timeout waiting for service to stop
    ");
                    CloseServiceHandle(schService); 
                    CloseServiceHandle(schSCManager);
                    return; 
                }
            }
        }
    
        // Attempt to start the service.
    
        if (!StartService(
                schService,  // handle to service 
                0,           // number of arguments 
                NULL) )      // no arguments 
        {
            printf("StartService failed (%d)
    ", GetLastError());
            CloseServiceHandle(schService); 
            CloseServiceHandle(schSCManager);
            return; 
        }
        else printf("Service start pending...
    "); 
    
        // Check the status until the service is no longer start pending. 
     
        if (!QueryServiceStatusEx( 
                schService,                     // handle to service 
                SC_STATUS_PROCESS_INFO,         // info level
                (LPBYTE) &ssStatus,             // address of structure
                sizeof(SERVICE_STATUS_PROCESS), // size of structure
                &dwBytesNeeded ) )              // if buffer too small
        {
            printf("QueryServiceStatusEx failed (%d)
    ", GetLastError());
            CloseServiceHandle(schService); 
            CloseServiceHandle(schSCManager);
            return; 
        }
     
        // Save the tick count and initial checkpoint.
    
        dwStartTickCount = GetTickCount();
        dwOldCheckPoint = ssStatus.dwCheckPoint;
    
        while (ssStatus.dwCurrentState == SERVICE_START_PENDING) 
        { 
            // Do not wait longer than the wait hint. A good interval is 
            // one-tenth the wait hint, but no less than 1 second and no 
            // more than 10 seconds. 
     
            dwWaitTime = ssStatus.dwWaitHint / 10;
    
            if( dwWaitTime < 1000 )
                dwWaitTime = 1000;
            else if ( dwWaitTime > 10000 )
                dwWaitTime = 10000;
    
            Sleep( dwWaitTime );
    
            // Check the status again. 
     
            if (!QueryServiceStatusEx( 
                schService,             // handle to service 
                SC_STATUS_PROCESS_INFO, // info level
                (LPBYTE) &ssStatus,             // address of structure
                sizeof(SERVICE_STATUS_PROCESS), // size of structure
                &dwBytesNeeded ) )              // if buffer too small
            {
                printf("QueryServiceStatusEx failed (%d)
    ", GetLastError());
                break; 
            }
     
            if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
            {
                // Continue to wait and check.
    
                dwStartTickCount = GetTickCount();
                dwOldCheckPoint = ssStatus.dwCheckPoint;
            }
            else
            {
                if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
                {
                    // No progress made within the wait hint.
                    break;
                }
            }
        } 
    
        // Determine whether the service is running.
    
        if (ssStatus.dwCurrentState == SERVICE_RUNNING) 
        {
            printf("Service started successfully.
    "); 
        }
        else 
        { 
            printf("Service not started. 
    ");
            printf("  Current State: %d
    ", ssStatus.dwCurrentState); 
            printf("  Exit Code: %d
    ", ssStatus.dwWin32ExitCode); 
            printf("  Check Point: %d
    ", ssStatus.dwCheckPoint); 
            printf("  Wait Hint: %d
    ", ssStatus.dwWaitHint); 
        } 
    
        CloseServiceHandle(schService); 
        CloseServiceHandle(schSCManager);
    }
  • 相关阅读:
    [django]Manipulator解惑
    [AJAX]Ajax.Net取Application的值问题
    IBatis.Net中为什么Output的paramMap的class设置为int就获取不到值
    [转]Python下载百度新歌100的代码
    [django]Django输出页面方式的补充
    Asp.Net的控件如何与Server交互
    [django]学习Model API的实例
    周杰伦的第七张专辑依然范特西
    立冬了,换个Skin
    InterDev调试asp页面和自定义activex all
  • 原文地址:https://www.cnblogs.com/liujx2019/p/14339492.html
Copyright © 2020-2023  润新知