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.

 
//
// 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的更多相关文章

  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. Python自动化之Django的CSRF

    什么CSRF? CSRF, Cross Site Request Forgery, 跨站点伪造请求.举例来讲,某个恶意的网站上有一个指向你的网站的链接,如果 某个用户已经登录到你的网站上了,那么当这个 ...

  2. top -Hp pid 显示所有的线程

    可以显示所有的线程 top -Hp pid [root@jiangyi01.sqa.zmf /home/ahao.mah] #top -Hp 41330 top - 22:49:40 up 27 da ...

  3. Ubuntu上安装QQ2015

    先不卖关子直接上图:Ubuntu 14.04.5 LTS Deb包下载地址: http://www.longene.org/download/WineQQ7.8-20151109-Longene.de ...

  4. 【服务器运维】Windows Server 2008 R2 下配置证书服务器和HTTPS

    前言 2017年1月1日起App Store上的所有App应用将强制开启ATS功能. 苹果的ATS(App Transport Security)对服务器硬性3点要求: ① ATS要求TLS1.2或者 ...

  5. atitit。浏览器缓存机制 and 微信浏览器防止缓存的设计 attilax 总结

    atitit.浏览器缓存机制 and 微信浏览器防止缓存的设计 attilax 总结 1. 缓存的一些机制 1 1.1. http 304 1 1.2. 浏览器刷新的处理机制 1 1.3. Expir ...

  6. 提高你的Java代码质量吧:让我们疑惑的字符串拼接方式的选择

    一.分析  对于一个字符串进行拼接有三种方法:加号.concat方法.及StringBuiler或StringBuffer. 1."+"方法拼接字符串  str += " ...

  7. Lombok介绍及使用方法

    lombok简介 lombok是暑假来到公司实习的时候发现的一个非常好用的小工具,刚见到的时候就感觉非常惊艳,有一种相见恨晚的感觉,用了一段时间之后感觉的确挺不错,所以特此来推荐一下. lombok的 ...

  8. POJ2914

    POJ2914 无向图的最小割 题意:给你一个无向图,然后去掉其中的n条边,使之形成两个连通分量,也即原无向图不连通,求n的最小值. 输入: m(无向图点集),n(无向图边集) a,b,c(a,b两点 ...

  9. css布局之负margin妙用及其他实现

    相信大家在项目的开发中都遇到过这样的需求,一行放X(X>1)个块且相邻块之间的间距相同. 大概就是上面这个样子,下面介绍几种实现的方式. 1.负margin大法 设置好元素的宽度和留白占满父级的 ...

  10. 2015-09-28Javascript(一)