-
Notifications
You must be signed in to change notification settings - Fork 115
/
win_service.c
80 lines (66 loc) · 2.38 KB
/
win_service.c
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
#include "win_service.h"
#include <windows.h>
#define SERVICE_NAME "ByeDPI"
static SERVICE_STATUS ServiceStatus;
static SERVICE_STATUS_HANDLE hStatus;
static int svc_argc = 0;
static char **svc_argv = NULL;
int main(int argc, char *argv[]);
void service_ctrl_handler(DWORD request)
{
switch(request)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
default:
break;
}
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
void service_main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
{
// Current working directory for services is %WinDir%\System32, this breaks
// relative paths. Set working directory to the directory of the executable file.
char file_name[_MAX_PATH];
GetModuleFileNameA(NULL, file_name, sizeof(file_name));
char working_dir[_MAX_PATH], _tmp[_MAX_DIR];
_splitpath_s(file_name, working_dir, _MAX_DRIVE, _tmp, _MAX_DIR, NULL, 0, NULL, 0);
strcat_s(working_dir, sizeof(working_dir), _tmp);
SetCurrentDirectoryA(working_dir);
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 = RegisterServiceCtrlHandler(SERVICE_NAME, (LPHANDLER_FUNCTION)service_ctrl_handler);
if (hStatus == (SERVICE_STATUS_HANDLE)0)
{
// Registering Control Handler failed
return;
}
SetServiceStatus(hStatus, &ServiceStatus);
// Calling main with saved argc & argv
ServiceStatus.dwWin32ExitCode = (DWORD)main(svc_argc, svc_argv);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
int register_winsvc(int argc, char *argv[])
{
SERVICE_TABLE_ENTRY ServiceTable[] = {
{SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)service_main},
{NULL, NULL}
};
// Save args passed to the program to use instead of the service args.
if (svc_argv) {
return 0;
}
svc_argc = argc;
svc_argv = argv;
return StartServiceCtrlDispatcher(ServiceTable);
}