/* 函数名: 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; } /* 函数名: 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; } /* 函数名: 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); } }