1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include <windows.h> #include <tchar.h> #include <strsafe.h>
#define MyServiceName TEXT("MyService")
bool g_bPausedFlag = false; HANDLE g_hSvcStopEvent = NULL; SERVICE_STATUS g_MyServiceStatus; SERVICE_STATUS_HANDLE g_hMyServiceStatusHandle;
void __stdcall MyServiceMain(DWORD dwArgc, LPTSTR *lpszArgv); DWORD __stdcall MyServiceHandlerEx(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext); void ReportServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint); DWORD MyServiceInitialization(DWORD dwArgc, LPTSTR *lpszArgv); DWORD MyServiceWorker(DWORD dwArgc, LPTSTR *lpszArgv);
int _tmain(int argc, TCHAR* argv[]) { SERVICE_TABLE_ENTRY dispatchTable[] = { {MyServiceName, (LPSERVICE_MAIN_FUNCTION)MyServiceMain}, {NULL, NULL} };
if (!StartServiceCtrlDispatcher(dispatchTable)) { }
return 0; }
void __stdcall MyServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) { g_hSvcStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); g_hMyServiceStatusHandle = RegisterServiceCtrlHandlerEx(MyServiceName, (LPHANDLER_FUNCTION_EX)MyServiceHandlerEx, NULL); if (g_hMyServiceStatusHandle == NULL) return;
g_MyServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS; g_MyServiceStatus.dwServiceSpecificExitCode = 0; ReportServiceStatus(SERVICE_START_PENDING, 0, 0);
DWORD dwRet = MyServiceInitialization(dwArgc, lpszArgv); if (dwRet != NO_ERROR) ReportServiceStatus(SERVICE_STOPPED, dwRet, 0); ReportServiceStatus(SERVICE_RUNNING, 0, 0);
MyServiceWorker(dwArgc, lpszArgv);
while (true) { WaitForSingleObject(g_hSvcStopEvent, INFINITE); ReportServiceStatus(SERVICE_STOPPED, 0, 0); return; } }
DWORD __stdcall MyServiceHandlerEx(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext) { switch (dwControl) { case SERVICE_CONTROL_SHUTDOWN: case SERVICE_CONTROL_STOP: ReportServiceStatus(SERVICE_STOP_PENDING, 0, 0); SetEvent(g_hSvcStopEvent); break; case SERVICE_CONTROL_PAUSE: ReportServiceStatus(SERVICE_PAUSE_PENDING, 0, 0); g_bPausedFlag = true; break; case SERVICE_CONTROL_CONTINUE: ReportServiceStatus(SERVICE_CONTINUE_PENDING, 0, 0); g_bPausedFlag = false; break; case SERVICE_CONTROL_INTERROGATE: break; default: break; } return 0; }
DWORD MyServiceInitialization(DWORD dwArgc, LPTSTR *lpszArgv) { return NO_ERROR; }
DWORD MyServiceWorker(DWORD dwArgc, LPTSTR *lpszArgv) { return 0; }
void ReportServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint) { static DWORD dwCheckPoint = 1;
g_MyServiceStatus.dwCurrentState = dwCurrentState; g_MyServiceStatus.dwWin32ExitCode = dwWin32ExitCode; g_MyServiceStatus.dwWaitHint = dwWaitHint;
if (dwCurrentState == SERVICE_START_PENDING) g_MyServiceStatus.dwControlsAccepted = 0; else g_MyServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;
if ((dwCurrentState == SERVICE_RUNNING) || (dwCurrentState == SERVICE_STOPPED)) g_MyServiceStatus.dwCheckPoint = 0; else g_MyServiceStatus.dwCheckPoint = dwCheckPoint++;
SetServiceStatus(g_hMyServiceStatusHandle, &g_MyServiceStatus); }
|