zoukankan      html  css  js  c++  java
  • Changing a Service's Configuration

    Changing a Service's Configuration

     

    service configuration program uses the ChangeServiceConfig and ChangeServiceConfig2 functions to change the configuration parameters of an installed service. The program opens a handle to the service object, modifies its configuration, and then closes the service object handle.

    In the following example, the DoDisableSvc function uses ChangeServiceConfig to change the service start type to "Disabled", the DoEnableSvc function usesChangeServiceConfig to change the service start type to "Enabled", and the DoUpdateSvcDesc function uses ChangeServiceConfig2 to set the service description to "This is a test description". The szSvcName variable is a global variable that contains the name of the service. For the complete example that sets this variable, seeSvcConfig.cpp.

     
    //
    // Purpose: 
    //   Disables the service.
    //
    // Parameters:
    //   None
    // 
    // Return value:
    //   None
    //
    VOID __stdcall DoDisableSvc()
    {
        SC_HANDLE schSCManager;
        SC_HANDLE schService;
    
        // 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_CHANGE_CONFIG);  // need change config access 
     
        if (schService == NULL)
        { 
            printf("OpenService failed (%d)
    ", GetLastError()); 
            CloseServiceHandle(schSCManager);
            return;
        }    
    
        // Change the service start type.
    
        if (! ChangeServiceConfig( 
            schService,        // handle of service 
            SERVICE_NO_CHANGE, // service type: no change 
            SERVICE_DISABLED,  // service start type 
            SERVICE_NO_CHANGE, // error control: no change 
            NULL,              // binary path: no change 
            NULL,              // load order group: no change 
            NULL,              // tag ID: no change 
            NULL,              // dependencies: no change 
            NULL,              // account name: no change 
            NULL,              // password: no change 
            NULL) )            // display name: no change
        {
            printf("ChangeServiceConfig failed (%d)
    ", GetLastError()); 
        }
        else printf("Service disabled successfully.
    "); 
    
        CloseServiceHandle(schService); 
        CloseServiceHandle(schSCManager);
    }
    
    //
    // Purpose: 
    //   Enables the service.
    //
    // Parameters:
    //   None
    // 
    // Return value:
    //   None
    //
    VOID __stdcall DoEnableSvc()
    {
        SC_HANDLE schSCManager;
        SC_HANDLE schService;
    
        // 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_CHANGE_CONFIG);  // need change config access 
     
        if (schService == NULL)
        { 
            printf("OpenService failed (%d)
    ", GetLastError()); 
            CloseServiceHandle(schSCManager);
            return;
        }    
    
        // Change the service start type.
    
        if (! ChangeServiceConfig( 
            schService,            // handle of service 
            SERVICE_NO_CHANGE,     // service type: no change 
            SERVICE_DEMAND_START,  // service start type 
            SERVICE_NO_CHANGE,     // error control: no change 
            NULL,                  // binary path: no change 
            NULL,                  // load order group: no change 
            NULL,                  // tag ID: no change 
            NULL,                  // dependencies: no change 
            NULL,                  // account name: no change 
            NULL,                  // password: no change 
            NULL) )                // display name: no change
        {
            printf("ChangeServiceConfig failed (%d)
    ", GetLastError()); 
        }
        else printf("Service enabled successfully.
    "); 
    
        CloseServiceHandle(schService); 
        CloseServiceHandle(schSCManager);
    }
    
    //
    // Purpose: 
    //   Updates the service description to "This is a test description".
    //
    // Parameters:
    //   None
    // 
    // Return value:
    //   None
    //
    VOID __stdcall DoUpdateSvcDesc()
    {
        SC_HANDLE schSCManager;
        SC_HANDLE schService;
        SERVICE_DESCRIPTION sd;
        LPTSTR szDesc = TEXT("This is a test description");
    
        // 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_CHANGE_CONFIG);  // need change config access 
     
        if (schService == NULL)
        { 
            printf("OpenService failed (%d)
    ", GetLastError()); 
            CloseServiceHandle(schSCManager);
            return;
        }    
    
        // Change the service description.
    
        sd.lpDescription = szDesc;
    
        if( !ChangeServiceConfig2(
            schService,                 // handle to service
            SERVICE_CONFIG_DESCRIPTION, // change: description
            &sd) )                      // new description
        {
            printf("ChangeServiceConfig2 failed
    ");
        }
        else printf("Service description updated successfully.
    ");
    
        CloseServiceHandle(schService); 
        CloseServiceHandle(schSCManager);
    }
    
    
    

    Related topics

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms681988(v=vs.85).aspx

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms682006(v=vs.85).aspx

  • 相关阅读:
    使用union all 遇到的问题(俩条sql语句行数的和 不等于union all 后的 行数的和 !);遗留问题 怎么找到 相差的呐俩条数据 ?
    78W的数据使用forall 进行批量转移;
    oracle 不能是用变量来作为列名和表名 ,但使用动态sql可以;
    oracle 查询优化及sql改写
    (十三)Jmeter之Bean Shell 的使用(二)
    (十二)Jmeter之Bean Shell的使用(一)
    (十一)Jmeter另一种调试工具 HTTP Mirror Server
    Image Processing and Computer Vision_Review:A survey of recent advances in visual feature detection(Author's Accepted Manuscript)——2014.08
    Image Processing and Computer Vision_Review:Local Invariant Feature Detectors: A Survey——2007.11
    Image Processing and Computer Vision_Review:A survey of recent advances in visual feature detection—2014.08
  • 原文地址:https://www.cnblogs.com/micro-chen/p/5924429.html
Copyright © 2011-2022 走看看