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. escape encodeURI encodeURIComponent区别

    escape() 函数可对字符串进行编码,这样就可以在所有的计算机上读取该字符串.使用unescape来解码. 有效的URI(统一资源标示符)是不能包含某些字符的,如空格,所以需要进行编码,编码方法有 ...

  2. Git学习资料整理

    Git作为一个优秀的版本控制系统,是我们开发人员必须要学会使用的一个工具,接触git一年多以来,也看了不少相关资料,今天把我所看过的一些学习资源整理一下. Git入门当然首推廖雪峰廖老师的教程:Git ...

  3. ubuntu下libjson-c库的使用问题备忘

    首先安装libjson的c库 #apt-get install libjson0-dev libjson0 安装好后查看/usr/include/json下是否有头文件,有就对了! gcc -o ...

  4. [RxJS] Combining streams in RxJS

    Source: Link We will looking some opreators for combining stream in RxJS: merge combineLatest withLa ...

  5. rman catalog (rman 恢复目录)

    受控制文件大小的限制,一般rman需要用rman catalog来管理及存放备份信息: 这里介绍一下创建rman catalog的步骤: C:\Documents andSettings\Admini ...

  6. 多线程、Service与IntentService的比较

    资料摘自网络(侵删)     Service Thread IntentService AsyncTask When to use ? Task with no UI, but shouldn't b ...

  7. linux 网络状态工具ss命令

    ss命令用于显示socket状态. 他可以显示PACKET sockets, TCP sockets, UDP sockets, DCCP sockets, RAW sockets, Unix dom ...

  8. sql 常用语法汇总

    Sql常用语法 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控 ...

  9. The requested page cannot be accessed because the related configuration data for the page is invalid

    当在VS2013下开发web site时,调试时都是在IIS Express中进行的,没有问题.当部署到IIS中,出现:The requested page cannot be accessed be ...

  10. VIM打开文件与保存文件

    打开文件 VIM /etc/inittab 默认的安装没有设置ctrl_W为退出.也可以设置::map <C-W> :close<CR> 或:map <C-W> : ...