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
原创粉丝点击