forked from tdhoward/sample-wxwidgets-windows-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.cpp
435 lines (362 loc) · 12.8 KB
/
service.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
service.cpp
Contains all the stuff for
installing, starting, managing, stopping and uninstalling the service
Credits:
Ideas and example code were taken from:
https://forums.wxwidgets.org/viewtopic.php?f=20&t=4375
http://www.codeproject.com/Articles/499465/Simple-Windows-Service-in-Cplusplus
*/
#include <wx/stdpaths.h>
#include <wx/wx.h>
#include <wx/evtloop.h>
#include <wx/event.h>
#include "service.h"
#include "logs.h"
SERVICE_STATUS g_ServiceStatus = {0};
SERVICE_STATUS_HANDLE g_StatusHandle;
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD opcode);
SC_HANDLE m_globalSCM;
SERVICE_TABLE_ENTRY ServiceTable[] =
{
{ SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION) ServiceMain },
{ NULL, NULL }
};
IMPLEMENT_APP_NO_MAIN(MainApp)
HINSTANCE g_hInstance;
HINSTANCE g_hPrevInstance;
wxCmdLineArgType g_lpCmdLine;
int g_nCmdShow;
svcTimer *svc_timer; // optional, if you want the service to do something every x seconds
extern "C" int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
wxCmdLineArgType lpCmdLine,
int nCmdShow)
{
init_logger(LOG_DEBUG); // init the logger, asking it to record to the LOG_DEBUG level
log(LOG_DEBUG,"WinMain: Start");
if(strstr(lpCmdLine, "--svc")==NULL) // didn't find it
{
log(LOG_DEBUG,"WinMain: Didn't find --svc in argv");
return wxEntry(hInstance, hPrevInstance, lpCmdLine, nCmdShow); // calls OnInit()
}
else // if we found it, start the service
{
log(LOG_DEBUG,"WinMain: Found --svc in argv");
g_hInstance = hInstance;
g_hPrevInstance = hPrevInstance;
g_lpCmdLine = lpCmdLine;
g_nCmdShow = nCmdShow;
if(!StartServiceCtrlDispatcher (ServiceTable))
{
log(LOG_ERROR,"WinMain: StartServiceCtrlDispatcher returned error!");
return GetLastError();
}
}
log(LOG_DEBUG,"WinMain: End");
close_logger();
return 0;
}
// you can override wxAppConsole::OnEventLoopEnter() in order to do initialization
// of functions that require the event loop to be running
// OnInit should return true if wxWidgets is supposed to keep on processing,
// or false to exit. However, if there is no parent frame created in OnInit,
// wxWidgets will still automatically exit, unless you call SetExitOnFrameDelete(false).
bool MainApp::OnInit()
{
log(LOG_DEBUG,"OnInit: Start");
wxString usage = "--install\n--uninstall";
log(LOG_DEBUG,"OnInit: argc is %d", argc);
if(argc < 2)
{
wxMessageDialog(NULL, usage, SERVICE_NAME, wxOK).ShowModal();
log(LOG_DEBUG,"OnInit: End");
close_logger();
return false;
}
wxString command = argv[1];
if(command=="--svc") // INIT THE REAL SERVICE HERE
{
log(LOG_DEBUG,"OnInit: Starting service loop");
// Set wxApp::SetExitOnFrameDelete to false to keep processing
SetExitOnFrameDelete(false);
// START TIMERS (optional)
// start a timer to perform some work every x seconds
svc_timer = new svcTimer();
if(!svc_timer->Start(2000,wxTIMER_CONTINUOUS)) // every 2 seconds
{
log(LOG_DEBUG,"OnInit: Error starting svc_timer!");
log(LOG_DEBUG,"OnInit: End");
close_logger();
return false;
}
// BIND EVENT HANDLERS
Bind( wxEVT_THREAD, &MainApp::EvtHndlr, this, EvtID);
// if successful, let the SCM know that we are running
// this should be after the service has been initialized
log(LOG_DEBUG,"OnInit: Service loop started");
g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwCheckPoint = 0;
if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)
{
log(LOG_ERROR,"OnInit: SetServiceStatus returned an error setting SERVICE_RUNNING.");
log(LOG_DEBUG,"OnInit: End");
close_logger();
return false;
}
return true; // this allows wxWidgets mainloop and event handlers to run
}
else if(command=="--install")
{
log(LOG_DEBUG,"OnInit: Trying to install...");
if(isInstalled())
{
log(LOG_INFO,"OnInit: Already installed");
wxMessageDialog(NULL, "Service is already installed", SERVICE_NAME, wxOK).ShowModal();
}
else
{
install();
}
}
else if(command=="--uninstall")
{
log(LOG_DEBUG,"OnInit: Trying to uninstall...");
if(!isInstalled())
{
log(LOG_INFO,"OnInit: Already uninstalled");
wxMessageDialog(NULL, "Service is not installed.", SERVICE_NAME, wxOK).ShowModal();
}
else
{
uninstall();
}
}
else
{
log(LOG_INFO,"OnInit: Unknown option");
wxMessageDialog(NULL, usage, SERVICE_NAME, wxOK).ShowModal();
}
log(LOG_DEBUG,"OnInit: End");
close_logger();
return false;
}
/*
wxApp::OnExit is called when the application exits but before wxWidgets
cleans up its internal structures. You should delete all wxWidgets objects
that you created by the time OnExit finishes.
*/
int MainApp::OnExit()
{
log(LOG_DEBUG,"OnExit: Start");
// stop the service timer
svc_timer->Stop();
delete svc_timer;
// notify the SCM that we have stopped.
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwCheckPoint = 0;
if (SetServiceStatus (g_StatusHandle, &g_ServiceStatus) == FALSE)
log(LOG_ERROR,"OnExit: SetServiceStatus returned error!");
log(LOG_DEBUG,"OnExit: End");
return 0;
}
void MainApp::EvtHndlr( wxThreadEvent &evt )
{
log(LOG_DEBUG,"MainApp::EvtHndlr: Start");
switch(evt.GetInt())
{
case E_EXIT:
// Attempt to gracefully close everything
log(LOG_DEBUG,"MainApp::EvtHndlr: Event called for exit");
log(LOG_DEBUG,"MainApp::EvtHndlr: End");
ExitMainLoop(); // tell wxWidgets we're done
return;
default:
evt.Skip();
log(LOG_DEBUG,"MainApp::EvtHndlr: Unknown event code %d", evt.GetInt());
log(LOG_DEBUG,"MainApp::EvtHndlr: End");
return;
}
}
bool MainApp::isInstalled(void)
{
log(LOG_DEBUG,"isInstalled: Start");
bool found;
openSCM();
SC_HANDLE svc = OpenService(
m_globalSCM,
SERVICE_NAME, //TEXT("SampleService"),
SERVICE_ALL_ACCESS);
if(svc)
{
log(LOG_DEBUG,"isInstalled: found = true");
found=true;
CloseServiceHandle(svc);
}
else
{
log(LOG_DEBUG,"isInstalled: found = false");
found=false;
}
closeSCM();
log(LOG_DEBUG,"isInstalled: End");
return found;
}
bool MainApp::openSCM(void)
{
log(LOG_DEBUG,"openSCM: Start");
m_globalSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (m_globalSCM == NULL)
{
log(LOG_ERROR,"openSCM: Error %d", GetLastError());
log(LOG_DEBUG,"openSCM: End");
return false;
}
else
{
log(LOG_DEBUG,"openSCM: End");
return true;
}
}
void MainApp::closeSCM(void)
{
CloseServiceHandle(m_globalSCM);
}
void MainApp::install(void)
{
log(LOG_DEBUG,"install: Start");
wxString exePath = wxStandardPaths::Get().GetExecutablePath();
openSCM();
SC_HANDLE schService = CreateService(
m_globalSCM,
SERVICE_NAME, // lpServiceName
SERVICE_NAME, // lpDisplayName
SERVICE_ALL_ACCESS, // dwDesiredAccess
SERVICE_WIN32_OWN_PROCESS, // dwServiceType -- add | SERVICE_INTERACTIVE_PROCESS if using gui
SERVICE_AUTO_START, // dwStartType -- Could also be SERVICE_DEMAND_START to start the service manually
// since SERVICE_AUTO_START starts the service upon startup
SERVICE_ERROR_NORMAL, // dwErrorControl
wxString::Format("\"%s\" --svc", exePath), // lpBinaryPathName
NULL, // lpLoadOrderGroup
NULL, // lpdwTagId
NULL, // lpDependencies -- Pointer to a double null-terminated array of null-separated names of services that the system must start before this service.
NULL, // lpServiceStartName
NULL); // lpPassword
if(schService == NULL)
log(LOG_ERROR,"install: createSvc error %d", GetLastError());
else
log(LOG_DEBUG,"install: Service installed.");
closeSCM();
log(LOG_DEBUG,"install: End");
}
void MainApp::uninstall(void)
{
log(LOG_DEBUG,"uninstall: Start");
openSCM();
SC_HANDLE svc = OpenService(
m_globalSCM,
SERVICE_NAME,
SERVICE_ALL_ACCESS);
SERVICE_STATUS status;
ControlService(svc, SERVICE_CONTROL_STOP, &status); // instruct the service to stop
if(!DeleteService(svc))
log(LOG_ERROR,"uninstall: DeleteService error %d", GetLastError());
else
log(LOG_DEBUG,"uninstall: Service uninstalled.");
CloseServiceHandle(svc);
closeSCM();
log(LOG_DEBUG,"uninstall: End");
}
// starts the service. called by StartServiceCtrlDispatcher()
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
log(LOG_DEBUG,"ServiceMain: Start");
g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, &ServiceCtrlHandler);
if(!g_StatusHandle)
{
log(LOG_ERROR,"ServiceMain: ServiceCtrlHandler error!");
log(LOG_ERROR,"ServiceMain: End");
return;
}
g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
g_ServiceStatus.dwWin32ExitCode = NO_ERROR; // (0)
g_ServiceStatus.dwServiceSpecificExitCode = 0;
g_ServiceStatus.dwCheckPoint = 0;
g_ServiceStatus.dwWaitHint = 0;
// to indicate an error on exit,
// g_ServiceStatus.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR; // this should be set
// g_ServiceStatus.dwServiceSpecificExitCode = -1; // so that this value is returned
if(SetServiceStatus(g_StatusHandle, &g_ServiceStatus))
{
log(LOG_DEBUG,"ServiceMain: Calling wxEntry()...");
wxEntry(g_hInstance, g_hPrevInstance, g_lpCmdLine, g_nCmdShow); // This calls OnInit() // should this be wxEntryStart() ?
}
else
log(LOG_ERROR,"ServiceMain: SetServiceStatus failed while starting the service!");
log(LOG_DEBUG,"ServiceMain: End");
return;
}
/*
Receives control requests from the SCM and handles them appropriately
*/
void WINAPI ServiceCtrlHandler (DWORD opcode)
{
log(LOG_DEBUG,"ServiceCtrlHandler: Start");
DWORD status;
wxThreadEvent* evt;
switch(opcode)
{
case SERVICE_CONTROL_SHUTDOWN: // system is shutting down, so we need to stop.
log(LOG_DEBUG,"ServiceCtrlHandler: SERVICE_CONTROL_SHUTDOWN");
case SERVICE_CONTROL_STOP: // service has been politely asked to stop.
if(opcode == SERVICE_CONTROL_STOP)
log(LOG_DEBUG,"ServiceCtrlHandler: SERVICE_CONTROL_STOP");
if (g_ServiceStatus.dwCurrentState != SERVICE_RUNNING)
break;
g_ServiceStatus.dwControlsAccepted = 0;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
g_ServiceStatus.dwCheckPoint = 0; // for lengthy starts, stops (while pending), you can increment dwCheckPoint to show progress
g_ServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (g_StatusHandle, &g_ServiceStatus))
{
status = GetLastError();
log(LOG_ERROR,"ServiceCtrlHandler: SetServiceStatus error! Error %d", status);
}
// send the exit prog request
evt = new wxThreadEvent(wxEVT_THREAD, EvtID);
evt->SetInt(5);
wxTheApp->QueueEvent(evt);
log(LOG_DEBUG,"ServiceCtrlHandler: End");
return;
case SERVICE_CONTROL_INTERROGATE:
log(LOG_DEBUG,"ServiceCtrlHandler: SERVICE_CONTROL_INTERROGATE");
break;
default:
log(LOG_DEBUG,"ServiceCtrlHandler: Unhandled opcode.");
break;
}
if (!SetServiceStatus (g_StatusHandle, &g_ServiceStatus))
{
status = GetLastError();
log(LOG_ERROR,"ServiceCtrlHandler: Error setting service status! Error %d", status);
}
log(LOG_DEBUG,"ServiceCtrlHandler: End");
return;
}
void svcTimer::Notify()
{
log(LOG_DEBUG,"svcTimer::Notify: Start");
// DO SOMETHING PRODUCTIVE HERE
// Note: you can't Beep() in a service on Windows 7!
log(LOG_DEBUG,"svcTimer::Notify: End");
return;
}