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

  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. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值