Changing a Service's Configuration
Changing a Service's Configuration
A 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)\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
- 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)\n", GetLastError());
- }
- else printf("Service enabled successfully.\n");
- 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)\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 description.
- sd.lpDescription = szDesc;
- if( !ChangeServiceConfig2(
- schService, // handle to service
- SERVICE_CONFIG_DESCRIPTION, // change: description
- &sd) ) // new description
- {
- printf("ChangeServiceConfig2 failed\n");
- }
- else printf("Service description updated successfully.\n");
- 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
Changing a Service's Configuration的更多相关文章
- ChangeServiceConfig2 function
ChangeServiceConfig2 function Changes the optional configuration parameters of a service. Syntax C ...
- 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 ...
- Windows Azure Cloud Service (39) 如何将现有Web应用迁移到Azure PaaS平台
<Windows Azure Platform 系列文章目录> 本文将简单介绍,如何将企业内现有的ASP.NET应用程序迁移到Azure PaaS平台. 因为在迁移过程中,可能需要对现有的 ...
- WCF Windows Service Using TopShelf and ServiceModelEx z
http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are ...
- 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 ...
- 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 ...
- 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 ...
- Ambari自定义Service
一.Ambari基本架构 img016.jpg Ambari Server 会读取 Stack 和 Service 的配置文件.当用 Ambari 创建服务的时候,Ambari Server 传送 ...
- Linux Simple Systemd Service Guide
Simple Systemd Service Guide 主题 Systemd介绍 Systemd基本操作 怎样编写_service_.service文件 怎样部署service Systemd介绍 ...
随机推荐
- int.Parse()与int.TryParse()
int i = -1;bool b = int.TryParse(null, out i);执行完毕后,b等于false,i等于0,而不是等于-1,切记. int i = -1;bool b = ...
- 二、linux文件系统之linux启动
Linux组成 kernel shell 文件系统 application(应用程序) 标准库函数 内核源码位置: /usr/src /boot/vmlinuz*(内核压缩文件,启动要加载) ...
- GDB错误:Cannot find bounds of current function
http://blog.csdn.net/zoomdy/article/details/17249165 mingdu.zheng <at> gmail <dot> com 使 ...
- SKAction类
继承自 NSObject 符合 NSCodingNSCopyingNSObject(NSObject) 框架 /System/Library/Frameworks/SpriteKit.framewo ...
- ios开发应用内实现多语言自由切换
需求描述:应用内部有一按钮,点击切换语言(如中英文切换).说起来这个是好久以前做的一个功能点了,刚开始也是没有头绪,后来解决了发现很简单,把方法分享一下.1.原理.查看NSLocalizedStrin ...
- 将Apache添加为Linux的服务 实现自启动(转)
在Linux下用源代码方式编译安装完Apache后,启动关闭Apache可以通过如下命令实现: /app/apache2.2.14/bin/apachectl start | stop | resta ...
- 跟我一起学PCL打印语言(一)
引言 本人从事打印机开发和打印驱动开发的相关工作,深感资料特别是中文资料的匮乏和不成系统,对新入门的从事该行业的人来说,门槛很高.在这里一方面是将开发中遇到的相关知识点整理出来,另一方面也能够促进自己 ...
- python基础--杂项
字符串格式化: ython的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...
- mongodb的地理空间索引常见的问题
创建地理空间索引注意事项 创建地理空间索引失败,提示错误信息如下 > db.places.ensureIndex({"loc":"2dsphere"}){ ...
- 第一个androidAPP项目总结—ListView的上拉和下拉
1.下拉刷新 需继承implements SwipeRefreshLayout.OnRefreshListener @Overridepublic void onRefresh() { new Wea ...