修改系统服务的启动类型并启动服务
来源:互联网 发布:淘宝草根vr工厂 编辑:程序博客网 时间:2024/05/16 06:08
有时可能需要手动修改系统服务或者我们自己写的服务的启动类型,并手动将服务启动起来,这些手动操作均可以通过代码实现。要实现这样的操作,需要用到下列函数:OpenSCManager、OpenService、ChangeServiceConfig、StartService、CloseServiceHandle,函数的具体说明和用法查看MSDN即可。对于下图,注意调用API函数时,要使用服务名称,不能使用显示名称。
下面给出将服务设置为自动启动,并将服务启动起来的相关代码:
BOOL ChangeServiceStartTypeAndStart( CString strSvrName ) { SC_HANDLE schSCManager; SC_HANDLE schService; // Get a handle to the SCM database. CString strErrInfo; schSCManager = OpenSCManager( NULL, // local computer NULL, // ServicesActive database SC_MANAGER_ALL_ACCESS); // full access rights if (NULL == schSCManager) { strErrInfo.Format(_T("OpenSCManager failed (%d)\n"), GetLastError()); AfxMessageBox(strErrInfo); return FALSE; } // Get a handle to the service. schService = OpenService( schSCManager, // SCM database (LPCTSTR)strSvrName, // name of service SERVICE_CHANGE_CONFIG|SERVICE_START); // need change config access if (schService == NULL) { strErrInfo.Format(_T("OpenService failed (%d)\n"), GetLastError()); AfxMessageBox(strErrInfo); CloseServiceHandle(schSCManager); return FALSE; } // Change the service start type.(set to auto start type) BOOL bRet = ChangeServiceConfig( schService, // handle of service SERVICE_NO_CHANGE, // service type: no change SERVICE_AUTO_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); if ( !bRet ) // display name: no change { strErrInfo.Format(_T("ChangeServiceConfig failed (%d)\n"), GetLastError()); AfxMessageBox(strErrInfo); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return FALSE; } else { strErrInfo.Format(_T("Service disabled successfully.\n")); } // Start the service -- 启动该服务 bRet = StartService( schService, // handle to service 0, // number of arguments NULL); // Attempt to start the service. if ( !bRet ) // no arguments { strErrInfo.Format(_T("StartService failed (%d)\n"), GetLastError()); AfxMessageBox(strErrInfo); } else { printf("Service start pending...\n"); } // Close service handles CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return bRet; }
0 0
- 修改系统服务的启动类型并启动服务
- 关于tomcat注册成一个服务,并修改启动类型
- 修改tomcat启动端口,并让tomcat作为系统服务后台启动
- C++通过WMI启动/停止服务,修改服务类型 (
- Linux攻略 如何修改系统的服务启动和停止
- LINUX下如何修改系统的服务启动和停止
- 批处理设置服务的启动类型与启动/停止服务
- 动态的启动系统服务
- Delphi启动/停止Windows服务,启动类型修改为"自动"
- Delphi启动/停止Windows服务,启动类型修改为"自动"
- Delphi启动/停止Windows服务,启动类型修改为"自动"
- Windows服务用C++代码实现的一些操作-1:修改服务启动类型
- 系统定时启动服务
- bat启动系统服务
- Activity启动系统服务
- 开机启动服务的修改与查看
- 遍历windows服务并启动指定服务
- 定时检测并启动服务
- 利用html5画出五角星画出星空
- ProjectEuler - 11
- PostGresQL 的方案Schema
- nginx 新手攻略 Ubuntu Linux下安装配置教程
- COM组件接口的实质及ArcGIS Object Model Diagram的理解
- 修改系统服务的启动类型并启动服务
- iOS多线程GCD(转)
- 第12周项目3 汉诺塔
- 似懂非懂双方的非官方然而他
- 【Spark亚太研究院系列丛书】Spark实战高手之路-第一章 构建Spark集群(第五步)(9)
- VC6.0与Visio冲突问题解决方案
- 应该学会HTML和CSS的9大理由
- 事件冒泡的一些应用_利用事件冒泡处理多个事件[1]
- 《android编程权威指南》学习笔记之第二章 Android与MVC设计模式