-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.cpp
More file actions
69 lines (60 loc) · 1.87 KB
/
Service.cpp
File metadata and controls
69 lines (60 loc) · 1.87 KB
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
#include "main.h"
#include "service.h"
static SERVICE_STATUS ServiceStatus;
static SERVICE_STATUS_HANDLE hStatus;
static LPWSTR serviceName;
int service_register(const LPWSTR serviceName_)
{
serviceName = serviceName_;
int ret;
SERVICE_TABLE_ENTRYW ServiceTable[] = {
{ serviceName, (LPSERVICE_MAIN_FUNCTIONW)service_main},
{NULL, NULL}
};
ret = StartServiceCtrlDispatcherW(ServiceTable);
VLOG(1) << "DEBUG: StartServiceCtrlDispatcher returned with: " << ret;
return ret;
}
void service_main()
{
ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 1;
ServiceStatus.dwWaitHint = 0;
hStatus = RegisterServiceCtrlHandlerW(
serviceName,
(LPHANDLER_FUNCTION)service_controlhandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0)
{
//Registering Control Handler failed"
VLOG(1) << "DEBUG: Registering Control Handler failed with: " << hStatus;
return;
}
SetServiceStatus(hStatus, &ServiceStatus);
// Calling main
VLOG(1) << "DEBUG: starting server from service.";
ServiceStatus.dwWin32ExitCode = wmain();
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
// Control handler function
void service_controlhandler(DWORD request)
{
switch(request)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SafeExit();
default:
break;
}
// Report current status
SetServiceStatus(hStatus, &ServiceStatus);
return;
}