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.

 
  1. //
  2. // Purpose:
  3. // Disables the service.
  4. //
  5. // Parameters:
  6. // None
  7. //
  8. // Return value:
  9. // None
  10. //
  11. VOID __stdcall DoDisableSvc()
  12. {
  13. SC_HANDLE schSCManager;
  14. SC_HANDLE schService;
  15.  
  16. // Get a handle to the SCM database.
  17.  
  18. schSCManager = OpenSCManager(
  19. NULL, // local computer
  20. NULL, // ServicesActive database
  21. SC_MANAGER_ALL_ACCESS); // full access rights
  22.  
  23. if (NULL == schSCManager)
  24. {
  25. printf("OpenSCManager failed (%d)\n", GetLastError());
  26. return;
  27. }
  28.  
  29. // Get a handle to the service.
  30.  
  31. schService = OpenService(
  32. schSCManager, // SCM database
  33. szSvcName, // name of service
  34. SERVICE_CHANGE_CONFIG); // need change config access
  35.  
  36. if (schService == NULL)
  37. {
  38. printf("OpenService failed (%d)\n", GetLastError());
  39. CloseServiceHandle(schSCManager);
  40. return;
  41. }
  42.  
  43. // Change the service start type.
  44.  
  45. if (! ChangeServiceConfig(
  46. schService, // handle of service
  47. SERVICE_NO_CHANGE, // service type: no change
  48. SERVICE_DISABLED, // service start type
  49. SERVICE_NO_CHANGE, // error control: no change
  50. NULL, // binary path: no change
  51. NULL, // load order group: no change
  52. NULL, // tag ID: no change
  53. NULL, // dependencies: no change
  54. NULL, // account name: no change
  55. NULL, // password: no change
  56. NULL) ) // display name: no change
  57. {
  58. printf("ChangeServiceConfig failed (%d)\n", GetLastError());
  59. }
  60. else printf("Service disabled successfully.\n");
  61.  
  62. CloseServiceHandle(schService);
  63. CloseServiceHandle(schSCManager);
  64. }
  65.  
  66. //
  67. // Purpose:
  68. // Enables the service.
  69. //
  70. // Parameters:
  71. // None
  72. //
  73. // Return value:
  74. // None
  75. //
  76. VOID __stdcall DoEnableSvc()
  77. {
  78. SC_HANDLE schSCManager;
  79. SC_HANDLE schService;
  80.  
  81. // Get a handle to the SCM database.
  82.  
  83. schSCManager = OpenSCManager(
  84. NULL, // local computer
  85. NULL, // ServicesActive database
  86. SC_MANAGER_ALL_ACCESS); // full access rights
  87.  
  88. if (NULL == schSCManager)
  89. {
  90. printf("OpenSCManager failed (%d)\n", GetLastError());
  91. return;
  92. }
  93.  
  94. // Get a handle to the service.
  95.  
  96. schService = OpenService(
  97. schSCManager, // SCM database
  98. szSvcName, // name of service
  99. SERVICE_CHANGE_CONFIG); // need change config access
  100.  
  101. if (schService == NULL)
  102. {
  103. printf("OpenService failed (%d)\n", GetLastError());
  104. CloseServiceHandle(schSCManager);
  105. return;
  106. }
  107.  
  108. // Change the service start type.
  109.  
  110. if (! ChangeServiceConfig(
  111. schService, // handle of service
  112. SERVICE_NO_CHANGE, // service type: no change
  113. SERVICE_DEMAND_START, // service start type
  114. SERVICE_NO_CHANGE, // error control: no change
  115. NULL, // binary path: no change
  116. NULL, // load order group: no change
  117. NULL, // tag ID: no change
  118. NULL, // dependencies: no change
  119. NULL, // account name: no change
  120. NULL, // password: no change
  121. NULL) ) // display name: no change
  122. {
  123. printf("ChangeServiceConfig failed (%d)\n", GetLastError());
  124. }
  125. else printf("Service enabled successfully.\n");
  126.  
  127. CloseServiceHandle(schService);
  128. CloseServiceHandle(schSCManager);
  129. }
  130.  
  131. //
  132. // Purpose:
  133. // Updates the service description to "This is a test description".
  134. //
  135. // Parameters:
  136. // None
  137. //
  138. // Return value:
  139. // None
  140. //
  141. VOID __stdcall DoUpdateSvcDesc()
  142. {
  143. SC_HANDLE schSCManager;
  144. SC_HANDLE schService;
  145. SERVICE_DESCRIPTION sd;
  146. LPTSTR szDesc = TEXT("This is a test description");
  147.  
  148. // Get a handle to the SCM database.
  149.  
  150. schSCManager = OpenSCManager(
  151. NULL, // local computer
  152. NULL, // ServicesActive database
  153. SC_MANAGER_ALL_ACCESS); // full access rights
  154.  
  155. if (NULL == schSCManager)
  156. {
  157. printf("OpenSCManager failed (%d)\n", GetLastError());
  158. return;
  159. }
  160.  
  161. // Get a handle to the service.
  162.  
  163. schService = OpenService(
  164. schSCManager, // SCM database
  165. szSvcName, // name of service
  166. SERVICE_CHANGE_CONFIG); // need change config access
  167.  
  168. if (schService == NULL)
  169. {
  170. printf("OpenService failed (%d)\n", GetLastError());
  171. CloseServiceHandle(schSCManager);
  172. return;
  173. }
  174.  
  175. // Change the service description.
  176.  
  177. sd.lpDescription = szDesc;
  178.  
  179. if( !ChangeServiceConfig2(
  180. schService, // handle to service
  181. SERVICE_CONFIG_DESCRIPTION, // change: description
  182. &sd) ) // new description
  183. {
  184. printf("ChangeServiceConfig2 failed\n");
  185. }
  186. else printf("Service description updated successfully.\n");
  187.  
  188. CloseServiceHandle(schService);
  189. CloseServiceHandle(schSCManager);
  190. }

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

Changing a Service's Configuration的更多相关文章

  1. ChangeServiceConfig2 function

    ChangeServiceConfig2 function   Changes the optional configuration parameters of a service. Syntax C ...

  2. Service Discovery in WCF 4.0 – Part 2 z

    Service Discovery in WCF 4.0 – Part 2 In the previous post I discussed about the basic usage of WCF ...

  3. Windows Azure Cloud Service (39) 如何将现有Web应用迁移到Azure PaaS平台

    <Windows Azure Platform 系列文章目录> 本文将简单介绍,如何将企业内现有的ASP.NET应用程序迁移到Azure PaaS平台. 因为在迁移过程中,可能需要对现有的 ...

  4. WCF Windows Service Using TopShelf and ServiceModelEx z

    http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are ...

  5. Learning WCF Chapter1 Hosting a Service in IIS

    How messages reach a service endpoint is a matter of protocols and hosting. IIS can host services ov ...

  6. Learning WCF Chapter1 Generating a Service and Client Proxy

    In the previous lab,you created a service and client from scratch without leveraging the tools avail ...

  7. Learning WCF Chapter1 Exposing Multiple Service Endpoints

    So far in this chapter,I have shown you different ways to create services,how to expose a service en ...

  8. Ambari自定义Service

    一.Ambari基本架构   img016.jpg Ambari Server 会读取 Stack 和 Service 的配置文件.当用 Ambari 创建服务的时候,Ambari Server 传送 ...

  9. Linux Simple Systemd Service Guide

    Simple Systemd Service Guide 主题 Systemd介绍 Systemd基本操作 怎样编写_service_.service文件 怎样部署service Systemd介绍 ...

随机推荐

  1. int.Parse()与int.TryParse()

      int i = -1;bool b = int.TryParse(null, out i);执行完毕后,b等于false,i等于0,而不是等于-1,切记. int i = -1;bool b = ...

  2. 二、linux文件系统之linux启动

    Linux组成 kernel  shell  文件系统  application(应用程序) 标准库函数 内核源码位置: /usr/src   /boot/vmlinuz*(内核压缩文件,启动要加载) ...

  3. GDB错误:Cannot find bounds of current function

    http://blog.csdn.net/zoomdy/article/details/17249165 mingdu.zheng <at> gmail <dot> com 使 ...

  4. SKAction类

    继承自 NSObject 符合 NSCodingNSCopyingNSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framewo ...

  5. ios开发应用内实现多语言自由切换

    需求描述:应用内部有一按钮,点击切换语言(如中英文切换).说起来这个是好久以前做的一个功能点了,刚开始也是没有头绪,后来解决了发现很简单,把方法分享一下.1.原理.查看NSLocalizedStrin ...

  6. 将Apache添加为Linux的服务 实现自启动(转)

    在Linux下用源代码方式编译安装完Apache后,启动关闭Apache可以通过如下命令实现: /app/apache2.2.14/bin/apachectl start | stop | resta ...

  7. 跟我一起学PCL打印语言(一)

    引言 本人从事打印机开发和打印驱动开发的相关工作,深感资料特别是中文资料的匮乏和不成系统,对新入门的从事该行业的人来说,门槛很高.在这里一方面是将开发中遇到的相关知识点整理出来,另一方面也能够促进自己 ...

  8. python基础--杂项

    字符串格式化: ython的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...

  9. mongodb的地理空间索引常见的问题

    创建地理空间索引注意事项 创建地理空间索引失败,提示错误信息如下 > db.places.ensureIndex({"loc":"2dsphere"}){ ...

  10. 第一个androidAPP项目总结—ListView的上拉和下拉

    1.下拉刷新 需继承implements SwipeRefreshLayout.OnRefreshListener @Overridepublic void onRefresh() { new Wea ...