VC编程进行windows NT 服务程序的创建,删除,启动,停止 【NT Service编程】
2017-04-07 17:54
603 查看
// 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 computer NULL, // 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 computer NULL, // 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; }
相关文章推荐
- windows NT 服务程序的创建,删除,启动,停止
- windows NT 服务程序的创建,删除,启动,停止
- 对NT服务型程序的控制代码。如安装服务、启动、停止服务、取服务状态等。
- 对NT服务型程序的控制代码。如安装服务、启动、停止服务、取服务状态等。
- oracle11g 启动停止服务,修改字符集,导入导出,创建删除表空间,卸载oracle等
- 对系统服务进行检查&增加&删除&启动&停止&重启
- 创建服务,启动服务,停止服务,删除服务的批处理的编写
- 服务程序的注册、启动、停止、删除
- C#Windows Service服务程序的安装/卸载、启动/停止 桌面客户端管理程序设计
- VC 创建NT服务程序
- 启动,停止,安装,删除功能的NT服务
- C#制作Windows service服务系列一:制作一个可安装、可启动、可停止、可卸载的Windows service
- 通过vc 中Windows application 工程创建Windows service程序
- 用VC创建程序启动画面
- VC 服务程序编程
- Windows 服务程序创建与停止
- 如何在windows NT 下安装jboss为服务?(How to install jboss as Windows NT OS Service?)
- Win2000/XP安装软件时提示“无法访问windows安装程序,服务中windows Installer状态为停止,不能启动”
- 本地计算机上的ArcSde Service 服务启动后又停止了,如果没有什么可做的
- 如何删除 MOSS 共享服务提供程序 (SSP, Shared Service Provider)