VC编程进行windows NT 服务程序的创建,删除,启动,停止 【NT Service编程】
来源:互联网 发布:淘宝发货骗局 编辑:程序博客网 时间:2024/05/23 12:08
// NTServiceStudy.cpp : 定义控制台应用程序的入口点。原文:http://blog.csdn.net/hurryboylqs/article/details/6024921// 经过测试,原作者的这个几个方法都有效;#include "stdafx.h"#include <Windows.h>/*函数名: RtStatrtService描述:启动一个NT服务,服务名字由参数传入*/BOOL RtStatrtService(LPCTSTR lpMachineName,LPCTSTR lpServiceName){BOOL bResult = FALSE;SC_HANDLE hSCManager = OpenSCManager(lpMachineName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);if (hSCManager != NULL){SC_HANDLE hService = OpenService(hSCManager, lpServiceName, SERVICE_ALL_ACCESS);if (hService != NULL){if(StartService(hService, 0, 0)){bResult = TRUE;}else{bResult = FALSE;OutputDebugString(_T("start service failed!"));}CloseServiceHandle(hService);}CloseServiceHandle(hSCManager);}return bResult;}/*函数名: RtStopService描述:停止一个NT服务,服务名字由参数传入*/BOOL RtStopService(LPCTSTR lpMachineName,LPCTSTR lpServiceName){BOOL bResult = FALSE;SC_HANDLE hSCManager = OpenSCManager(lpMachineName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);if (hSCManager != NULL){SC_HANDLE hService = OpenService(hSCManager, lpServiceName, SERVICE_ALL_ACCESS);if (hService != NULL){SERVICE_STATUS ServiceStatus;ZeroMemory(&ServiceStatus,sizeof(SERVICE_STATUS));ControlService(hService, SERVICE_CONTROL_STOP, &ServiceStatus);CloseServiceHandle(hService);}CloseServiceHandle(hSCManager);}return bResult;}/*函数名: RtCreateService描述:创建一个NT服务,服务名字由参数传入*/BOOL RtCreateService(LPCTSTR lpServiceName,LPCTSTR lpDisplayName){SC_HANDLE schSCManager;SC_HANDLE schService;TCHAR szPath[MAX_PATH];if( !GetModuleFileName( NULL, szPath, MAX_PATH ) ){printf("Cannot install service (%d)/n", GetLastError());return FALSE;}schSCManager = OpenSCManager( NULL, // local computerNULL, // ServicesActive database SC_MANAGER_ALL_ACCESS); // full access rights if (NULL == schSCManager) {printf("OpenSCManager failed (%d)/n", GetLastError());return FALSE;}// Create the service.schService = CreateService( schSCManager, // SCM database lpServiceName, // name of service lpDisplayName, // service name to display SERVICE_ALL_ACCESS, // desired access SERVICE_WIN32_OWN_PROCESS, // service type SERVICE_DEMAND_START, // start type SERVICE_ERROR_NORMAL, // error control type szPath, // path to service's binary NULL, // no load ordering group NULL, // no tag identifier NULL, // no dependencies NULL, // LocalSystem account NULL); // no password if (schService == NULL) {printf("CreateService failed (%d)/n", GetLastError()); CloseServiceHandle(schSCManager);return FALSE;}else printf("Service installed successfully/n"); CloseServiceHandle(schService); CloseServiceHandle(schSCManager);return TRUE;}/*函数名: RtDeleteService描述:删除一个NT服务,服务名字由参数传入*/BOOL RtDeleteService(LPCTSTR lpServiceName){SC_HANDLE schSCManager;SC_HANDLE schService;SERVICE_STATUS ssStatus; BOOL bResult = FALSE;// Get a handle to the SCM database. schSCManager = OpenSCManager( NULL, // local computerNULL, // ServicesActive database SC_MANAGER_ALL_ACCESS); // full access rights if (NULL == schSCManager) {printf("OpenSCManager failed (%d)/n", GetLastError());return bResult;}// Get a handle to the service.schService = OpenService( schSCManager, // SCM database lpServiceName, // name of service DELETE); // need delete access if (schService == NULL){ printf("OpenService failed (%d)/n", GetLastError()); CloseServiceHandle(schSCManager);return bResult;}// Delete the service.if (! DeleteService(schService) ) {printf("DeleteService failed (%d)/n", GetLastError()); }else{bResult = TRUE;printf("Service deleted successfully/n"); }CloseServiceHandle(schService); CloseServiceHandle(schSCManager);return bResult;}/*函数名: RtQueryServiceStatus描述:查询服务的状态*/void RtQueryServiceStatus(LPCTSTR lpMachineName,LPCTSTR lpServiceName){SC_HANDLE hSCManager = OpenSCManager(lpMachineName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);if (hSCManager != NULL){SC_HANDLE hService = OpenService(hSCManager, lpServiceName, SERVICE_ALL_ACCESS);if (hService != NULL){SERVICE_STATUS ServiceStatus;ZeroMemory(&ServiceStatus,sizeof(SERVICE_STATUS));if(QueryServiceStatus(hService, &ServiceStatus)){switch (ServiceStatus.dwCurrentState){case SERVICE_STOPPED:OutputDebugString(_T("Stopped"));break;case SERVICE_START_PENDING:OutputDebugString(_T("Start Pending"));break;case SERVICE_STOP_PENDING:OutputDebugString(_T("Stop Pending"));break;case SERVICE_RUNNING:OutputDebugString(_T("Running"));break;case SERVICE_CONTINUE_PENDING:OutputDebugString(_T("Coninue Pending"));break;case SERVICE_PAUSE_PENDING:OutputDebugString(_T("Pause Pending"));break;case SERVICE_PAUSED:OutputDebugString(_T("Paused"));break;}}CloseServiceHandle(hService);}CloseServiceHandle(hSCManager);}}int _tmain(int argc, _TCHAR* argv[]){// RtQueryServiceStatus(NULL,_T("KeyIso"));// RtStopService(NULL, _T("AxInstSV"));// RtStatrtService(NULL, _T("AxInstSV"));RtCreateService(NULL, _T("AxInstSV"));RtDeleteService(_T("AxInstSV"));return 0;}
0 0
- VC编程进行windows NT 服务程序的创建,删除,启动,停止 【NT Service编程】
- windows NT 服务程序的创建,删除,启动,停止
- windows NT 服务程序的创建,删除,启动,停止
- NT Service 编程 操作 NT 服务
- NT服务启动Windows程序 -- NTService
- WIN32编程--用Windows API实现Windows NT服务的程序框架
- 改变Windows NT服务的启动顺序
- WINDOWS NT服务的创建方法
- 如何创建 windows NT/Windows 2000 服务(Service)
- C# 创建Windows(NT)服务
- 对NT服务型程序的控制代码。如安装服务、启动、停止服务、取服务状态等。
- Windows NT 的OpenGL编程:入门(译)
- NT Service 服务程序调试小问题的解决
- 用DELPHI进行NT、2000、2003服务程序的调试
- NT Service服务详解
- 基于visual c++之windows核心编程代码分析(35)实践NT服务的框架
- 命令行创建,删除,启动,停止Windows服务
- 用C#创建Windows(NT)服务
- 界面刷新
- RecyclerView 加载不用同的itme 布局 ---并且不会复用
- 使用maven打可执行jar包的两种方式
- 中国激光雷达技术崛起 服务机器人用激光雷与进口相差无几
- ReactNative学习实例(十)接管Back键BackAndroid&&ToastAndroid
- VC编程进行windows NT 服务程序的创建,删除,启动,停止 【NT Service编程】
- 刷题总结#9
- Android系统访问串口设备
- 京东前端笔试错题集
- Redis keys 性能问题
- UGUI制作与运用美术字体
- android studio环境下上传新项目到SVN
- Tk/Tkx滚动条的使用
- Valgrind检测内存泄漏