博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ 修改 Windows Service【转】
阅读量:5095 次
发布时间:2019-06-13

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

//// 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);}

 

转载于:https://www.cnblogs.com/Leo-Forest/archive/2013/05/03/3056640.html

你可能感兴趣的文章
拉格朗日乘子法 那些年学过的高数
查看>>
vs code 的便捷使用
查看>>
Spring MVC @ResponseBody返回中文字符串乱码问题
查看>>
用户空间与内核空间,进程上下文与中断上下文[总结]
查看>>
JS 中的跨域请求
查看>>
JAVA开发环境搭建
查看>>
vim插件ctags的安装和使用
查看>>
mysql基础语句
查看>>
Oracle中的rownum不能使用大于>的问题
查看>>
[Data Structure & Algorithm] 有向无环图的拓扑排序及关键路径
查看>>
git 常用命令
查看>>
cassandra vs mongo (1)存储引擎
查看>>
Visual Studio基于CMake配置opencv1.0.0、opencv2.2
查看>>
Vue音乐项目笔记(三)
查看>>
遍历Map对象
查看>>
计算剪贴板里仿制的代码行数
查看>>
MySQL索引背后的数据结构及算法原理
查看>>
#Leetcode# 209. Minimum Size Subarray Sum
查看>>
C#语言-04.OOP基础
查看>>
1)session总结
查看>>