按照自己的理解,重新实现了一下,代码如下:
#include <string.h> #include <stdio.h> #include <Windows.h> #define SERVICE_NAME TEXT("MyBeepService") SERVICE_STATUS g_status; SERVICE_STATUS_HANDLE g_ServiceStatusHandle; HANDLE g_stopEvent; BOOL InstallService() { SC_HANDLE hSCManager; SC_HANDLE hSCService; hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCManager == NULL) { return FALSE; } TCHAR szModuleFileName[MAX_PATH]; GetModuleFileName(NULL, szModuleFileName, MAX_PATH); hSCService = CreateService(hSCManager, SERVICE_NAME, SERVICE_NAME, SC_MANAGER_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, szModuleFileName, NULL, NULL, NULL, NULL, NULL); if (hSCManager == NULL) { return FALSE; } StartService(hSCService, NULL, NULL); printf("Install Service Success!\n"); CloseServiceHandle(hSCService); CloseServiceHandle(hSCManager); return TRUE; } BOOL UninstallService() { SC_HANDLE hSCManager; SC_HANDLE hSCService; SERVICE_STATUS curStatus; SERVICE_STATUS ctrlstatus; hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCManager == NULL) { return FALSE; } hSCService = OpenService(hSCManager, SERVICE_NAME, SERVICE_ALL_ACCESS); if (hSCService == NULL) { return FALSE; } if (!QueryServiceStatus(hSCService, &curStatus)) { return FALSE; } if (curStatus.dwCurrentState != SERVICE_STOPPED) { if (!ControlService(hSCService, SERVICE_CONTROL_STOP, &ctrlstatus)) { printf("Stop Service failed:%d!\n", GetLastError()); return FALSE; } } if (DeleteService(hSCService)) { printf("Uninstall Service Success!\n"); } else { printf("Uninstall Service failed:%d!\n", GetLastError()); } CloseServiceHandle(hSCService); CloseServiceHandle(hSCManager); printf("Uninstall Service Success!\n"); return TRUE; } VOID WINAPI ServiceHandler(DWORD fdwControl) { switch (fdwControl) { case SERVICE_CONTROL_PAUSE: g_status.dwCurrentState = SERVICE_PAUSED; break; case SERVICE_CONTROL_CONTINUE: g_status.dwCurrentState = SERVICE_RUNNING; break; case SERVICE_CONTROL_STOP: case SERVICE_CONTROL_SHUTDOWN: g_status.dwCurrentState = SERVICE_STOPPED; SetEvent(g_stopEvent); break; default: break; } SetServiceStatus(g_ServiceStatusHandle, &g_status); } VOID WINAPI ServiceMain(DWORD dwArgc, LPTSTR *lpszArgv) { g_status.dwCurrentState = SERVICE_STOPPED; g_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE; g_status.dwWin32ExitCode = 0; g_status.dwCheckPoint = 0; g_status.dwWaitHint = 0; g_status.dwServiceSpecificExitCode = 0; g_status.dwServiceType = SERVICE_WIN32; g_ServiceStatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceHandler); if (g_ServiceStatusHandle == 0) { return; } g_status.dwCurrentState = SERVICE_START_PENDING; SetServiceStatus(g_ServiceStatusHandle, &g_status); g_stopEvent = CreateEvent(NULL, FALSE, FALSE, NULL); g_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; g_status.dwCurrentState = SERVICE_RUNNING; SetServiceStatus(g_ServiceStatusHandle, &g_status); do { Beep(1000, 100); } while (WaitForSingleObject(g_stopEvent, 5000) == WAIT_TIMEOUT); g_status.dwCurrentState = SERVICE_STOP_PENDING; SetServiceStatus(g_ServiceStatusHandle, &g_status); CloseHandle(g_stopEvent); g_status.dwCurrentState = SERVICE_STOPPED; g_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE; SetServiceStatus(g_ServiceStatusHandle, &g_status); } void RunService() { SERVICE_TABLE_ENTRY dispatchTable[] = { { SERVICE_NAME, ServiceMain }, { NULL, NULL }, }; StartServiceCtrlDispatcher(dispatchTable); } int main(int argc, char* argv[]) { if (argc == 1) { RunService(); } else { if (strcmp(argv[1], "install") == 0) { InstallService(); } else if (strcmp(argv[1], "uninstall") == 0) { UninstallService(); } } return 0; }
一开始报1053,是因为ServiceMain没写对,没有CreateEvent、WaitForSingleObject、循环。
要注意的地方有:
1、安装好服务后,是需要手动启动还是会自动启动?
2、关机时会停止服务吗?
3、开机后,服务是否会自动运行?