-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
226 lines (172 loc) · 6.17 KB
/
main.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <Windows.h>
#include <tchar.h>
#include <Dbt.h>
//#include "Logger/plog/Log.h"
//#include "Logger/plog/Initializers/RollingFileInitializer.h"
SERVICE_STATUS g_ServiceStatus = { 0 };
SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
HANDLE g_ServiceStopEvent = INVALID_HANDLE_VALUE;
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam);
DWORD WINAPI ServiceCtrlHandler(DWORD CtrlCode, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext);
VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv);
LPSTR pUserData;
#define SERVICE_NAME const_cast<LPWSTR>(_T("MyServiceName"))
int _tmain(int argc, TCHAR* argv[])
{
SERVICE_TABLE_ENTRY ServiceTable[] =
{
{SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL, NULL}
};
if (StartServiceCtrlDispatcher(ServiceTable) == FALSE)
{
//PLOG_ERROR << "Start returned error. Exiting...";
return GetLastError();
}
return NO_ERROR;
}
VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv)
{
DWORD Status = E_FAIL;
//g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
g_StatusHandle = RegisterServiceCtrlHandlerEx(SERVICE_NAME, ServiceCtrlHandler, pUserData);
if (g_StatusHandle == NULL)
{
//PLOG_ERROR << "Register returned error. Exiting...";
return;
}
// Tell the service controller we are starting
ZeroMemory(&g_ServiceStatus, sizeof(g_ServiceStatus));
g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwServiceSpecificExitCode = 0;
g_ServiceStatus.dwCheckPoint = 0;
if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
{
//PLOG_ERROR << "SetStatus returned error";
}
/*
* Perform tasks neccesary to start the service here
*/
// Create stop event to wait on later.
g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (g_ServiceStopEvent == NULL)
{
//PLOG_ERROR << "CreateEvent returned error. Exiting...";
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
g_ServiceStatus.dwWin32ExitCode = GetLastError();
g_ServiceStatus.dwCheckPoint = 1;
if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
{
//PLOG_ERROR << "SetStatus returned error. Exiting...";
}
return;
}
// Tell the service controller we are started
g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwCheckPoint = 0;
if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
{
//PLOG_ERROR << "SetStatus returned error, Exiting...";
}
//Register service for recieve Device Event
//https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerdevicenotificationa
HDEVNOTIFY hDeviceNotify;
//GUID InterfaceClassGuid = { 0x25dbce51, 0x6c8f, 0x4a72, 0x8a,0x6d,0xb5,0x4c,0x2b,0x4f,0xc8,0x35 };
GUID InterfaceClassGuid = { 0x71a27cdd, 0x812a, 0x11d0, 0xbe, 0xc7, 0x08, 0x00, 0x2b, 0xe2, 0x09, 0x2f };
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = InterfaceClassGuid;
//PLOG_DEBUG << "Registering service for recieve device events...";
hDeviceNotify = RegisterDeviceNotification(g_StatusHandle, &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
if (hDeviceNotify != NULL) {
//PLOG_DEBUG << "Registeraion was successful.";
}
else
{
//PLOG_DEBUG << "Registeration failed: " << GetLastError();
}
//Disable Stopability!
//g_ServiceStatus.dwControlsAccepted = 0;
//g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
//g_ServiceStatus.dwWin32ExitCode = 0;
//g_ServiceStatus.dwCheckPoint = 0;
//if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
//{
// //PLOG_ERROR << "SetStatus returned error, Exiting...";
//}
// Start the thread that will perform the main task of the service
HANDLE hThread = CreateThread(NULL, 0, ServiceWorkerThread, NULL, 0, NULL);
//PLOG_DEBUG << "Waiting for Worker Thread to complete";
// Wait until our worker thread exits effectively signaling that the service needs to stop
WaitForSingleObject(hThread, INFINITE);
//PLOG_DEBUG << "Worker Thread Stop Event signaled";
/*
* Perform any cleanup tasks
*/
//PLOG_DEBUG << "Performing Cleanup Operations";
CloseHandle(g_ServiceStopEvent);
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwCheckPoint = 3;
if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
{
//PLOG_ERROR << "SetStatus returned error";
}
//PLOG_DEBUG << "ServiceMain: Exit";
return;
}
//for RegisterServiceCtrlHandler
//DWORD WINAPI ServiceCtrlHandler(DWORD CtrlCode)
//for RegisterServiceCtrlHandlerEx
DWORD WINAPI ServiceCtrlHandler(DWORD CtrlCode, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
{
switch (CtrlCode)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
//PLOG_DEBUG << "SERVICE_CONTROL_STOP Request";
if (g_ServiceStatus.dwCurrentState != SERVICE_RUNNING)
break;
/*
* Perform tasks neccesary to stop the service here
*/
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwCheckPoint = 4;
if (SetServiceStatus(g_StatusHandle, &g_ServiceStatus) == FALSE)
{
//PLOG_ERROR << "SetStatus returned error";
}
// This will signal the worker thread to start shutting down
SetEvent(g_ServiceStopEvent);
break;
case SERVICE_CONTROL_DEVICEEVENT:
// Do whatever need
break;
default:
//PLOG_DEBUG << "UnKnown Event Requested";
break;
}
return NO_ERROR;
}
DWORD WINAPI ServiceWorkerThread(LPVOID lpParam)
{
// Periodically check if the service has been requested to stop
while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0)
{
/*
* Perform main service function here
*/
}
return NO_ERROR;
}