c++写修改Windows服务类型的代码

来源:互联网 发布:喀秋莎录屏软件录微课 编辑:程序博客网 时间:2024/06/11 20:31
[cpp] view plaincopyprint?
  1. VOID DoSvcStartType() 
  2. SC_HANDLE schSCManager; 
  3. SC_HANDLE schService; 
  4.  
  5. // Get a handle to the SCM database.  
  6.  
  7. schSCManager = OpenSCManager(  
  8.   NULL,                    // local computer 
  9.   NULL,                    // ServicesActive database 
  10.   SC_MANAGER_ALL_ACCESS);  // full access rights 
  11.  
  12. if (NULL == schSCManager)  
  13.   printf("OpenSCManager failed (%d)/n", GetLastError()); 
  14.   return
  15.  
  16. // Get a handle to the service. 
  17.  
  18. schService = OpenService(  
  19.   schSCManager,            // SCM database  
  20.   szSvcName,               // name of service 
  21.   SERVICE_CHANGE_CONFIG);  // need change config access 
  22.  
  23. if (schService == NULL) 
  24. {  
  25.   printf("OpenService failed (%d)/n", GetLastError());  
  26.   CloseServiceHandle(schSCManager); 
  27.   return
  28. }     
  29.  
  30. // Change the service start type. 
  31. SC_LOCK sclLock;  
  32. sclLock = LockServiceDatabase(schService);  
  33.  
  34.   
  35. if (! ChangeServiceConfig(  
  36.   schService,            // handle of service 
  37.   SERVICE_NO_CHANGE,     // service type: no change 
  38.   //SERVICE_CHANGE_CONFIG, 
  39.   SERVICE_DEMAND_START,  // service start type  
  40.   SERVICE_NO_CHANGE,     // error control: no change 
  41.   NULL,                  // binary path: no change 
  42.   NULL,                  // load order group: no change 
  43.   NULL,                  // tag ID: no change  
  44.   NULL,                  // dependencies: no change 
  45.   NULL,                  // account name: no change 
  46.   L"ABC",                 // password: no change  
  47.   NULL) )                // display name: no change 
  48.   //SERVICE_CHANGE_CONFIG 
  49.   printf("ChangeServiceConfig failed (%d)/n", GetLastError());  
  50. else printf("Service enabled successfully./n");  
  51.  
  52. //将启动类型更改为禁用 
  53. if (! ChangeServiceConfig(  
  54.   schService,            // handle of service 
  55.   SERVICE_NO_CHANGE,     // service type: no change 
  56.   SERVICE_DISABLED, 
  57.   //SERVICE_DEMAND_START, // service start type  
  58.   SERVICE_NO_CHANGE,     // error control: no change 
  59.   NULL,                  // binary path: no change 
  60.   NULL,                  // load order group: no change 
  61.   NULL,                  // tag ID: no change  
  62.   NULL,                  // dependencies: no change 
  63.   NULL,                  // account name: no change 
  64.   NULL,                  // password: no change 
  65.   NULL) )                // display name: no change 
  66.   //SERVICE_CHANGE_CONFIG 
  67.   printf("2:ChangeServiceConfig failed (%d)/n", GetLastError());  
  68. else printf("2:Service enabled successfully./n");  
  69. UnlockServiceDatabase(sclLock);  
  70.  
  71. CloseServiceHandle(schService);  
  72. CloseServiceHandle(schSCManager); 
  73. }