首页 > 代码库 > Windows服务程序_测试01
Windows服务程序_测试01
1、
#include <stdio.h> #include <Windows.h> #include <tchar.h> #include <process.h> TCHAR szServiceName[] = _T("MyTestService"); DWORD dwThreadID; HANDLE hEvent = INVALID_HANDLE_VALUE; SERVICE_STATUS status; SERVICE_STATUS_HANDLE hServiceStatus; void Init(); BOOL IsServiceInstalled(); BOOL InstallService(); BOOL UninstallService(); BOOL ServiceCtrlStart(); BOOL ServiceCtrlStop(); void WINAPI ServiceMain(); void WINAPI ServiceStrl(DWORD dwOpcode); void LogEvent(LPCTSTR pszFormat, ...); //Usuage // Install service: //TestService.exe /install // Uninstall service: //TestService.exe /uninstall // Start service: //TestService.exe /start // Stop service: //TestService.exe /stop int main( int argc, char* argv[] ) { Init(); dwThreadID = ::GetCurrentThreadId(); SERVICE_TABLE_ENTRY st[] = { { szServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain }, { NULL, NULL } }; LPSTR lpCmdLine = argv[1]; if ( argc == 2 ) { BOOL bRet; if (stricmp(lpCmdLine, "/install") == 0) { bRet = InstallService(); if ( bRet == TRUE ) { printf( "Install service %s success", szServiceName ); }else { printf( "Install service %s failed", szServiceName ); } } else if (stricmp(lpCmdLine, "/uninstall") == 0) { bRet = UninstallService(); if ( bRet == TRUE ) { printf( "Uninstall service %s success", szServiceName ); } else { printf( "Uninstall service %s failed", szServiceName ); } } else if (stricmp(lpCmdLine, "/start") == 0) { bRet = ServiceCtrlStart(); if ( bRet == TRUE ) { printf( "Start service %s success", szServiceName ); }else { printf( "Start service %s failed", szServiceName ); } } else if (stricmp(lpCmdLine, "/stop") == 0) { bRet = ServiceCtrlStop(); if ( bRet == TRUE ) { printf( "Stop service %s success", szServiceName ); } else { printf( "Stop service %s failed", szServiceName ); } } else { if (!::StartServiceCtrlDispatcher(st)) { LogEvent(_T("Register Service Main Function Error!")); } } } else { if (!::StartServiceCtrlDispatcher(st)) { LogEvent(_T("Register Service Main Function Error!")); } } return 0; } //初始化相关结构 void Init() { hServiceStatus = NULL; status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; status.dwCurrentState = SERVICE_STOPPED; status.dwControlsAccepted = SERVICE_ACCEPT_STOP; status.dwWin32ExitCode = 0; status.dwServiceSpecificExitCode = 0; status.dwCheckPoint = 0; status.dwWaitHint = 0; } void SubThread(void *_pParam) { LogEvent(_T("SubThread - 1")); Sleep(1000 * 10); LogEvent(_T("SubThread - 2")); _endthread(); } //服务主线程函数,既用户选择开启服务时调用的函数 void WINAPI ServiceMain() { // Register the control request handler status.dwCurrentState = SERVICE_START_PENDING; status.dwControlsAccepted = SERVICE_ACCEPT_STOP; LogEvent(_T("ServiceMain")); //注册服务控制 hServiceStatus = RegisterServiceCtrlHandler(szServiceName, ServiceStrl); if (hServiceStatus == NULL) { LogEvent(_T("Handler not installed")); return; } SetServiceStatus(hServiceStatus, &status); //如下代码可以为启动服务前的准备工作 hEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL ); if ( hEvent == NULL ) { status.dwCurrentState = SERVICE_STOPPED; SetServiceStatus(hServiceStatus, &status); LogEvent(_T("Failure to start service")); return; } // //更改服务状态为启动 status.dwWin32ExitCode = S_OK; status.dwCheckPoint = 0; status.dwWaitHint = 0; status.dwCurrentState = SERVICE_RUNNING; SetServiceStatus(hServiceStatus, &status); { _beginthread( SubThread, 0, NULL ); } //等待用户选择停止服务, //当然你也可以把你的服务代码用线程来执行, //此时这里只需等待线程结束既可。 while ( WaitForSingleObject( hEvent, 1000 ) != WAIT_OBJECT_0 ) { //模拟服务的运行,应用时将主要任务放于此即可 LogEvent(_T("Service runing...")); } // //停止服务 status.dwCurrentState = SERVICE_STOPPED; SetServiceStatus(hServiceStatus, &status); LogEvent(_T("Service stopped")); } //服务控制函数 void WINAPI ServiceStrl(DWORD dwOpcode) { switch (dwOpcode) { case SERVICE_CONTROL_STOP: status.dwCurrentState = SERVICE_STOP_PENDING; SetServiceStatus(hServiceStatus, &status); //告诉服务线程停止工作 ::SetEvent( hEvent ); LogEvent(_T("want to stopped Service")); break; case SERVICE_CONTROL_PAUSE: break; case SERVICE_CONTROL_CONTINUE: break; case SERVICE_CONTROL_INTERROGATE: break; case SERVICE_CONTROL_SHUTDOWN: break; default: LogEvent(_T("Bad service request")); } } //判断服务是否已安装 BOOL IsServiceInstalled() { BOOL bResult = FALSE; //打开服务控制管理器 SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCM != NULL) { //打开服务 SC_HANDLE hService = ::OpenService(hSCM, szServiceName, SERVICE_QUERY_CONFIG); if (hService != NULL) { bResult = TRUE; ::CloseServiceHandle(hService); } ::CloseServiceHandle(hSCM); } return bResult; } //安装服务 BOOL InstallService() { if (IsServiceInstalled()) { return TRUE; } //打开服务控制管理器 SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCM == NULL) { return FALSE; } // Get the executable file path TCHAR szFilePath[MAX_PATH]; ::GetModuleFileName(NULL, szFilePath, MAX_PATH); //创建服务 SC_HANDLE hService = ::CreateService( hSCM, szServiceName, szServiceName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, //如果为SERVICE_DEMAND_START则表示此服务需手工启动 SERVICE_ERROR_NORMAL, szFilePath, NULL, NULL, _T(""), NULL, NULL); if (hService == NULL) { ::CloseServiceHandle(hSCM); return FALSE; } ::CloseServiceHandle(hService); ::CloseServiceHandle(hSCM); return TRUE; } //删除服务 BOOL UninstallService() { if (!IsServiceInstalled()) { return TRUE; } SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCM == NULL) { return FALSE; } SC_HANDLE hService = ::OpenService(hSCM, szServiceName, SERVICE_STOP | DELETE); if (hService == NULL) { ::CloseServiceHandle(hSCM); return FALSE; } SERVICE_STATUS status; ::ControlService(hService, SERVICE_CONTROL_STOP, &status); //删除服务 BOOL bDelete = ::DeleteService(hService); ::CloseServiceHandle(hService); ::CloseServiceHandle(hSCM); if (bDelete) { return TRUE; } LogEvent(_T("Service could not be deleted")); return FALSE; } //开启服务 BOOL ServiceCtrlStart() { BOOL bRet; SC_HANDLE hSCM; SC_HANDLE hService; hSCM=OpenSCManager(NULL,NULL,SC_MANAGER_CONNECT); if (hSCM!=NULL) { hService=OpenService( hSCM, szServiceName, SERVICE_START); if (hService!=NULL) { //开始Service bRet = StartService(hService,0,NULL); CloseServiceHandle(hService); }else { bRet = FALSE; } CloseServiceHandle(hSCM); } else { bRet = FALSE; } return bRet; } //停止服务 BOOL ServiceCtrlStop() { BOOL bRet; SC_HANDLE hSCM; SC_HANDLE hService; SERVICE_STATUS ServiceStatus; hSCM=OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if (hSCM!=NULL) { hService=OpenService( hSCM, szServiceName, SERVICE_STOP|SERVICE_QUERY_STATUS ); if (hService!=NULL) { QueryServiceStatus( hService, &ServiceStatus ); if (ServiceStatus.dwCurrentState == SERVICE_RUNNING) { bRet = ControlService( hService, SERVICE_CONTROL_STOP, &ServiceStatus ); } else { bRet = FALSE; } CloseServiceHandle( hService ); } else { bRet = FALSE; } CloseServiceHandle( hSCM ); } else { bRet = FALSE; } return bRet; } //记录日记 void LogEvent(LPCTSTR pFormat, ...) { TCHAR chMsg[256]; HANDLE hEventSource; LPTSTR lpszStrings[1]; va_list pArg; va_start(pArg, pFormat); _vstprintf(chMsg, pFormat, pArg); va_end(pArg); lpszStrings[0] = chMsg; hEventSource = RegisterEventSource(NULL, szServiceName); if (hEventSource != NULL) { ReportEvent(hEventSource, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (LPCTSTR*) &lpszStrings[0], NULL); DeregisterEventSource(hEventSource); } }
2、CreateProcess
http://www.jb51.net/article/66511.htm
3、
Windows服务程序_测试01
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。