博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Changing a Service's Configuration
阅读量:5809 次
发布时间:2019-06-18

本文共 6127 字,大约阅读时间需要 20 分钟。

Changing a Service's Configuration

 

A  uses the  and  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  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  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, see.

C++
 
//// 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

转载地址:http://eqcbx.baihongyu.com/

你可能感兴趣的文章
Linux学习笔记之三
查看>>
Floyd最短路算法
查看>>
Class.forName(String name)方法,到底会触发那个类加载器进行类加载行为?
查看>>
CentOS 6.6 FTP install
查看>>
C#------判断btye[]是否为空
查看>>
图解Ajax工作原理
查看>>
oracle导入导出小记
查看>>
聊一聊log4j2配置文件log4j2.xml
查看>>
NeHe OpenGL教程 第七课:光照和键盘
查看>>
修改上一篇文章的node.js代码,支持默认页及支持中文
查看>>
Php实现版本比较接口
查看>>
删除设备和驱动器中软件图标
查看>>
第四章 TCP粘包/拆包问题的解决之道---4.1---
查看>>
html语言
查看>>
从源码看集合ArrayList
查看>>
spring-boot支持websocket
查看>>
菜鸟笔记(一) - Java常见的乱码问题
查看>>
我理想中的前端工作流
查看>>
记一次Git异常操作:将多个repository合并到同一repository的同一分支
查看>>
CodeIgniter 3.0 新手捣鼓源码(一) base_url()
查看>>