当前位置:编程学习 > VC++ >>

VC关于windows服务状态更改

更改服务状态


[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)\n", 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)\n", 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)\n", GetLastError());  
    } 
    else printf("Service disabled successfully.\n");  
 
    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)\n", 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)\n", GetLastError());  
        CloseServiceHandle(schSCManager); 
        return; 
    }     
 
    // Change the service start type.  
 
    if (! ChangeServiceConfig(  
        schService,            // handle of service   
        SERVICE_NO_CHANGE,     // service type: no change&

补充:软件开发 , Vc ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,