-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathservicemgr.cpp
132 lines (117 loc) · 3.61 KB
/
servicemgr.cpp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
///////////////////////////////////////////////////////////////////////////////
//
// (c) 2000 by Volkmar Uhlig, [email protected], www.uhlig-langert.de/volkmar
//
// all code can be reused and modified
//
//
//
#include <windows.h>
#include <stdio.h>
#include "servicemgr.h"
extern void Server();
SERVICE_STATUS ssStatus;
SERVICE_STATUS_HANDLE sshStatusHandle;
HANDLE hServiceEvent = NULL;
BOOL ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwCheckPoint, DWORD dwWaitHint)
{
BOOL fResult;
if (dwCurrentState==SERVICE_START_PENDING)
ssStatus.dwControlsAccepted=0;
else
ssStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP; // | SERVICE_ACCEPT_PAUSE_CONTINUE;
ssStatus.dwCurrentState=dwCurrentState;
ssStatus.dwWin32ExitCode=dwWin32ExitCode;
ssStatus.dwCheckPoint=dwCheckPoint;
ssStatus.dwWaitHint=dwWaitHint;
if (!(fResult = SetServiceStatus(sshStatusHandle, &ssStatus))) {
SetEvent(hServiceEvent);
}
return fResult;
}
void RegisterService(LPCSTR lpszBinaryPathName, LPCSTR lpszDependencies)
{
printf("Create %s Service for executable %s\n", GetServiceName(), lpszBinaryPathName);
SC_HANDLE schService, schSCManager;
schSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager!=NULL) {
schService=CreateService(
schSCManager,
GetServiceName(FALSE),
GetServiceName(TRUE),
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
(char*)lpszBinaryPathName,
NULL, // no load ordering
NULL, // no tag id
lpszDependencies,
NULL, // Local system account
NULL); // no pwd
if (schService==NULL)
printf("CreateService Error: %ld", GetLastError());
else
printf("CreateService SUCCESS\n");
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
void UnregisterService() {
SC_HANDLE schService, schSCManager;
schSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
schService=OpenService(schSCManager,
GetServiceName(FALSE),
SERVICE_ALL_ACCESS);
if (DeleteService(schService))
printf("Service %s successfully unregistered.\n", GetServiceName());
else
printf("Error while unregistering service %s: %ld\n", GetServiceName(), GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
VOID service_ctrl(DWORD dwCtrlCode) {
DWORD dwState = SERVICE_RUNNING;
switch(dwCtrlCode) {
case SERVICE_CONTROL_STOP:
dwState = SERVICE_STOP_PENDING;
ReportStatusToSCMgr(
SERVICE_STOP_PENDING,
NO_ERROR,
1,
3000);
SetEvent(hServiceEvent);
return;
case SERVICE_CONTROL_INTERROGATE:
break;
default:
break;
}
ReportStatusToSCMgr(dwState, NO_ERROR, 0, 0);
}
VOID service_main(DWORD dwArgs, LPTSTR *lpszArgv)
{
DWORD dwWait;
sshStatusHandle=RegisterServiceCtrlHandler(GetServiceName(),
(LPHANDLER_FUNCTION)service_ctrl);
if (!sshStatusHandle) goto cleanup;
ssStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
ssStatus.dwServiceSpecificExitCode=0;
if(!ReportStatusToSCMgr(SERVICE_START_PENDING, //service state
NO_ERROR, // exit code
1, // checkpoint
3000)) // wait hint
goto cleanup;
hServiceEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hServiceEvent == (HANDLE)NULL) goto cleanup;
if (!ReportStatusToSCMgr(SERVICE_START_PENDING, NO_ERROR, 2, 3000)) goto cleanup;
// starting...
if (CreateThread(NULL, 4000, LPTHREAD_START_ROUTINE(Server), NULL, 0, NULL)==NULL) goto cleanup;
if (!ReportStatusToSCMgr(SERVICE_RUNNING, NO_ERROR, 0, 0)) goto cleanup;
dwWait = WaitForSingleObject( hServiceEvent, INFINITE);
cleanup:
CloseHandle(hServiceEvent);
if (sshStatusHandle!=NULL)
ReportStatusToSCMgr(SERVICE_STOPPED, 0, 0, 0);
return;
}