forked from BOINC/boinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysmon_win.cpp
734 lines (626 loc) · 21.5 KB
/
sysmon_win.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
#include "boinc_win.h"
#include "diagnostics.h"
#include "error_numbers.h"
#include "url.h"
#include "util.h"
#include "win_util.h"
#include "prefs.h"
#include "filesys.h"
#include "network.h"
#include "client_state.h"
#include "log_flags.h"
#include "client_msgs.h"
#include "http_curl.h"
#include "sandbox.h"
#include "main.h"
#include "cs_proxy.h"
#include "net_stats.h"
#include "sysmon_win.h"
static HANDLE g_hWindowsMonitorSystemPowerThread = NULL;
static HWND g_hWndWindowsMonitorSystemPower = NULL;
static HANDLE g_hWindowsMonitorSystemProxyThread = NULL;
// The following 3 functions are called in a separate thread,
// so we can't do anything directly.
// Set flags telling the main thread what to do.
//
// Quit operations
static void quit_client() {
gstate.requested_exit = true;
while (1) {
boinc_sleep(1.0);
if (gstate.cleanup_completed) break;
}
}
// Suspend client operations
static void suspend_client() {
gstate.os_requested_suspend = true;
gstate.os_requested_suspend_time = dtime();
}
// Resume client operations
static void resume_client() {
gstate.os_requested_suspend = false;
}
// Process console messages sent by the system
static BOOL WINAPI console_control_handler( DWORD dwCtrlType ){
BOOL bReturnStatus = FALSE;
BOINCTRACE("***** Console Event Detected *****\n");
switch( dwCtrlType ){
case CTRL_LOGOFF_EVENT:
BOINCTRACE("Event: CTRL-LOGOFF Event\n");
if (!gstate.executing_as_daemon) {
quit_client();
}
bReturnStatus = TRUE;
break;
case CTRL_C_EVENT:
case CTRL_BREAK_EVENT:
BOINCTRACE("Event: CTRL-C or CTRL-BREAK Event\n");
quit_client();
bReturnStatus = TRUE;
break;
case CTRL_CLOSE_EVENT:
case CTRL_SHUTDOWN_EVENT:
BOINCTRACE("Event: CTRL-CLOSE or CTRL-SHUTDOWN Event\n");
quit_client();
break;
}
return bReturnStatus;
}
// Trap events on Windows so we can clean ourselves up.
static LRESULT CALLBACK WindowsMonitorSystemPowerWndProc(
HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam
) {
switch(uMsg) {
// On Windows power events are broadcast via the WM_POWERBROADCAST
// window message. It has the following parameters:
// PBT_APMQUERYSUSPEND
// PBT_APMQUERYSUSPENDFAILED
// PBT_APMSUSPEND
// PBT_APMRESUMECRITICAL
// PBT_APMRESUMESUSPEND
// PBT_APMBATTERYLOW
// PBT_APMPOWERSTATUSCHANGE
// PBT_APMOEMEVENT
// PBT_APMRESUMEAUTOMATIC
case WM_POWERBROADCAST:
switch(wParam) {
// System is preparing to suspend. This is valid on
// Windows versions older than Vista
case PBT_APMQUERYSUSPEND:
return TRUE;
break;
// System is resuming from a failed request to suspend
// activity. This is only valid on Windows versions
// older than Vista
case PBT_APMQUERYSUSPENDFAILED:
resume_client();
break;
// System is critically low on battery power. This is
// only valid on Windows versions older than Vista
case PBT_APMBATTERYLOW:
msg_printf(NULL, MSG_INFO, "Critical battery alarm, Windows is suspending operations");
suspend_client();
break;
// System is suspending
case PBT_APMSUSPEND:
msg_printf(NULL, MSG_INFO, "Windows is suspending operations");
suspend_client();
break;
// System is resuming from a normal power event
case PBT_APMRESUMESUSPEND:
gstate.set_now();
msg_printf(NULL, MSG_INFO, "Windows is resuming operations");
// Check for a proxy
working_proxy_info.need_autodetect_proxy_settings = true;
resume_client();
break;
}
break;
default:
break;
}
return (DefWindowProc(hWnd, uMsg, wParam, lParam));
}
// Create a thread to monitor system events
static DWORD WINAPI WindowsMonitorSystemPowerThread( LPVOID ) {
WNDCLASS wc;
MSG msg;
wc.style = CS_GLOBALCLASS;
wc.lpfnWndProc = (WNDPROC)WindowsMonitorSystemPowerWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = NULL;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = "BOINCWindowsMonitorSystemPower";
if (!RegisterClass(&wc)) {
log_message_error("Failed to register the WindowsMonitorSystem window class.");
return 1;
}
g_hWndWindowsMonitorSystemPower = CreateWindow(
wc.lpszClassName,
"BOINC Monitor System (Power)",
WS_OVERLAPPEDWINDOW & ~WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
NULL,
NULL);
if (!g_hWndWindowsMonitorSystemPower) {
log_message_error("Failed to create the WindowsMonitorSystem window.");
return 0;
}
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
// Detect any proxy configuration settings automatically.
static void windows_detect_autoproxy_settings() {
if (log_flags.proxy_debug) {
msg_printf(NULL, MSG_INFO, "[proxy] automatic proxy check in progress");
}
HMODULE hModWinHttp = LoadLibrary("winhttp.dll");
if (!hModWinHttp) {
return;
}
pfnWinHttpOpen pWinHttpOpen =
(pfnWinHttpOpen)GetProcAddress(hModWinHttp, "WinHttpOpen");
if (!pWinHttpOpen) {
return;
}
pfnWinHttpCloseHandle pWinHttpCloseHandle =
(pfnWinHttpCloseHandle)(GetProcAddress(hModWinHttp, "WinHttpCloseHandle"));
if (!pWinHttpCloseHandle) {
return;
}
pfnWinHttpGetProxyForUrl pWinHttpGetProxyForUrl =
(pfnWinHttpGetProxyForUrl)(GetProcAddress(hModWinHttp, "WinHttpGetProxyForUrl"));
if (!pWinHttpGetProxyForUrl) {
return;
}
HINTERNET hWinHttp = NULL;
WINHTTP_AUTOPROXY_OPTIONS autoproxy_options;
WINHTTP_PROXY_INFO proxy_info;
PARSED_URL purl;
std::wstring network_test_url;
size_t pos;
memset(&autoproxy_options, 0, sizeof(autoproxy_options));
memset(&proxy_info, 0, sizeof(proxy_info));
autoproxy_options.dwFlags =
WINHTTP_AUTOPROXY_AUTO_DETECT;
autoproxy_options.dwAutoDetectFlags =
WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
autoproxy_options.fAutoLogonIfChallenged = TRUE;
network_test_url = A2W(config.network_test_url).c_str();
hWinHttp = pWinHttpOpen(
L"BOINC client",
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS,
NULL
);
if (pWinHttpGetProxyForUrl(hWinHttp, network_test_url.c_str(), &autoproxy_options, &proxy_info)) {
if (log_flags.proxy_debug) {
msg_printf(NULL, MSG_INFO, "[proxy] successfully executed proxy check", hWinHttp);
}
// Apparently there are some conditions where WinHttpGetProxyForUrl can return
// success but where proxy_info.lpszProxy is null. Maybe related to UPNP?
//
// For the time being check to see if proxy_info.lpszProxy is non-null.
//
if (proxy_info.lpszProxy) {
std::string proxy(W2A(std::wstring(proxy_info.lpszProxy)));
std::string new_proxy;
if (log_flags.proxy_debug) {
msg_printf(NULL, MSG_INFO, "[proxy] proxy list '%s'", proxy.c_str());
}
if (!proxy.empty()) {
// Trim string if more than one proxy is defined
// proxy list is defined as:
// ([<scheme>=][<scheme>"://"]<server>[":"<port>])
// Find and erase first delimeter type.
pos = proxy.find(';');
if (pos != -1 ) {
new_proxy = proxy.erase(pos);
proxy = new_proxy;
}
// Find and erase second delimeter type.
pos = proxy.find(' ');
if (pos != -1 ) {
new_proxy = proxy.erase(pos);
proxy = new_proxy;
}
// Parse the remaining url
parse_url(proxy.c_str(), purl);
// Store the results for future use.
if (0 != strcmp(working_proxy_info.autodetect_server_name, purl.host)) {
// Reset clients connection error detection path
net_status.need_physical_connection = false;
working_proxy_info.autodetect_protocol = purl.protocol;
strcpy(working_proxy_info.autodetect_server_name, purl.host);
working_proxy_info.autodetect_port = purl.port;
}
if (log_flags.proxy_debug) {
msg_printf(NULL, MSG_INFO,
"[proxy] automatic proxy detected %s:%d",
purl.host, purl.port
);
}
}
}
// Clean up
if (proxy_info.lpszProxy) GlobalFree(proxy_info.lpszProxy);
if (proxy_info.lpszProxyBypass) GlobalFree(proxy_info.lpszProxyBypass);
} else {
// We can get here if the user is switching from a network that
// requires a proxy to one that does not require a proxy.
working_proxy_info.autodetect_protocol = 0;
strcpy(working_proxy_info.autodetect_server_name, "");
working_proxy_info.autodetect_port = 0;
if (log_flags.proxy_debug) {
msg_printf(NULL, MSG_INFO, "[proxy] no automatic proxy detected");
}
}
if (hWinHttp) pWinHttpCloseHandle(hWinHttp);
FreeLibrary(hModWinHttp);
if (log_flags.proxy_debug) {
msg_printf(NULL, MSG_INFO, "[proxy] automatic proxy check finished");
}
}
static DWORD WINAPI WindowsMonitorSystemProxyThread( LPVOID ) {
// notify the main client thread that detecting proxies is
// supported.
working_proxy_info.autodetect_proxy_supported = true;
while (1) {
if (working_proxy_info.need_autodetect_proxy_settings) {
working_proxy_info.have_autodetect_proxy_settings = false;
windows_detect_autoproxy_settings();
working_proxy_info.need_autodetect_proxy_settings = false;
working_proxy_info.have_autodetect_proxy_settings = true;
}
Sleep(1000);
}
return 0;
}
// Setup the client software to monitor various system events
int initialize_system_monitor(int /*argc*/, char** /*argv*/) {
// Windows: install console controls
if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)console_control_handler, TRUE)){
log_message_error("Failed to register the console control handler.");
return ERR_IO;
}
// Create a window to receive system power events.
g_hWindowsMonitorSystemPowerThread = CreateThread(
NULL,
0,
WindowsMonitorSystemPowerThread,
NULL,
0,
NULL);
if (!g_hWindowsMonitorSystemPowerThread) {
g_hWindowsMonitorSystemPowerThread = NULL;
g_hWndWindowsMonitorSystemPower = NULL;
}
// Create a thread to handle proxy auto-detection.
g_hWindowsMonitorSystemProxyThread = CreateThread(
NULL,
0,
WindowsMonitorSystemProxyThread,
NULL,
0,
NULL);
if (!g_hWindowsMonitorSystemProxyThread) {
g_hWindowsMonitorSystemProxyThread = NULL;
}
return 0;
}
// Cleanup the system event monitor
int cleanup_system_monitor() {
if (g_hWindowsMonitorSystemPowerThread) {
TerminateThread(g_hWindowsMonitorSystemPowerThread, 0);
CloseHandle(g_hWindowsMonitorSystemPowerThread);
g_hWindowsMonitorSystemPowerThread = NULL;
g_hWndWindowsMonitorSystemPower = NULL;
}
if (g_hWindowsMonitorSystemProxyThread) {
TerminateThread(g_hWindowsMonitorSystemProxyThread, 0);
CloseHandle(g_hWindowsMonitorSystemProxyThread);
g_hWindowsMonitorSystemProxyThread = NULL;
}
return 0;
}
// internal variables for managing the service
SERVICE_STATUS ssStatus; // current status of the service
SERVICE_STATUS_HANDLE sshStatusHandle;
DWORD dwErr = 0;
TCHAR szErr[1024];
SERVICE_TABLE_ENTRY service_dispatch_table[] = {
{ TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)BOINCServiceMain },
{ NULL, NULL }
};
// Inform the service control manager that the service is about to
// start.
int initialize_service_dispatcher(int /*argc*/, char** /*argv*/) {
fprintf(stdout, "\nStartServiceCtrlDispatcher being called.\n");
fprintf(stdout, "This may take several seconds. Please wait.\n");
if (!StartServiceCtrlDispatcher(service_dispatch_table)) {
log_message_error("StartServiceCtrlDispatcher failed.");
return ERR_IO;
}
return 0;
}
//
// FUNCTION: BOINCServiceMain
//
// PURPOSE: To perform actual initialization of the service
//
// PARAMETERS:
// dwArgc - number of command line arguments
// lpszArgv - array of command line arguments
//
// RETURN VALUE:
// none
//
// COMMENTS:
// This routine performs the service initialization and then calls
// the user defined main() routine to perform majority
// of the work.
//
void WINAPI BOINCServiceMain(DWORD /*dwArgc*/, LPTSTR * /*lpszArgv*/) {
// SERVICE_STATUS members that don't change in example
//
ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ssStatus.dwControlsAccepted = SERVICE_ACCEPTED_ACTIONS;
ssStatus.dwServiceSpecificExitCode = 0;
// register our service control handler:
//
sshStatusHandle = RegisterServiceCtrlHandler( TEXT(SZSERVICENAME), BOINCServiceCtrl);
if (!sshStatusHandle)
goto cleanup;
if (!ReportStatus(
SERVICE_RUNNING, // service state
ERROR_SUCCESS, // exit code
0)) // wait hint
goto cleanup;
dwErr = boinc_main_loop();
cleanup:
// try to report the stopped status to the service control manager.
//
if (sshStatusHandle) {
(VOID)ReportStatus(
SERVICE_STOPPED,
dwErr,
0
);
}
}
//
// FUNCTION: BOINCServiceCtrl
//
// PURPOSE: This function is called by the SCM whenever
// ControlService() is called on this service.
//
// PARAMETERS:
// dwCtrlCode - type of control requested
//
// RETURN VALUE:
// none
//
// COMMENTS:
//
VOID WINAPI BOINCServiceCtrl(DWORD dwCtrlCode) {
// Handle the requested control code.
//
switch(dwCtrlCode) {
// Stop the service.
//
// SERVICE_STOP_PENDING should be reported before
// setting the Stop Event - hServerStopEvent - in
// ServiceStop(). This avoids a race condition
// which may result in a 1053 - The Service did not respond...
// error.
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
ReportStatus(SERVICE_STOP_PENDING, ERROR_SUCCESS, 30000);
quit_client();
return;
// Pause the service.
//
case SERVICE_CONTROL_PAUSE:
ReportStatus(SERVICE_PAUSE_PENDING, ERROR_SUCCESS, 10000);
suspend_client();
ReportStatus(SERVICE_PAUSED, ERROR_SUCCESS, 10000);
return;
// Continue the service.
//
case SERVICE_CONTROL_CONTINUE:
ReportStatus(SERVICE_CONTINUE_PENDING, ERROR_SUCCESS, 10000);
resume_client();
ReportStatus(SERVICE_RUNNING, ERROR_SUCCESS, 10000);
return;
// Update the service status.
//
case SERVICE_CONTROL_INTERROGATE:
break;
// invalid control code
//
default:
break;
}
ReportStatus(ssStatus.dwCurrentState, ERROR_SUCCESS, 1000);
}
//
// FUNCTION: ReportStatus()
//
// PURPOSE: Sets the current status of the service and
// reports it to the Service Control Manager
//
// PARAMETERS:
// dwCurrentState - the state of the service
// dwWin32ExitCode - error code to report
// dwWaitHint - worst case estimate to next checkpoint
//
// RETURN VALUE:
// TRUE - success
// FALSE - failure
//
// COMMENTS:
//
BOOL ReportStatus(
DWORD dwCurrentState,
DWORD dwWin32ExitCode,
DWORD dwWaitHint
) {
static DWORD dwCheckPoint = 1;
BOOL fResult = TRUE;
if (dwCurrentState == SERVICE_START_PENDING)
ssStatus.dwControlsAccepted = 0;
else
ssStatus.dwControlsAccepted = SERVICE_ACCEPTED_ACTIONS;
ssStatus.dwCurrentState = dwCurrentState;
ssStatus.dwWin32ExitCode = dwWin32ExitCode;
ssStatus.dwWaitHint = dwWaitHint;
if ( ( dwCurrentState == SERVICE_RUNNING ) ||
( dwCurrentState == SERVICE_STOPPED )
) {
ssStatus.dwCheckPoint = 0;
} else {
ssStatus.dwCheckPoint = dwCheckPoint++;
}
// Report the status of the service to the service control manager.
//
fResult = SetServiceStatus( sshStatusHandle, &ssStatus);
if (!fResult) {
LogEventErrorMessage(TEXT("SetServiceStatus"));
}
return fResult;
}
//
// FUNCTION: LogEventErrorMessage(LPTSTR lpszMsg)
//
// PURPOSE: Allows any thread to log an error message
//
// PARAMETERS:
// lpszMsg - text for message
//
// RETURN VALUE:
// none
//
// COMMENTS:
//
VOID LogEventErrorMessage(LPTSTR lpszMsg) {
TCHAR szMsg[1024];
HANDLE hEventSource;
LPTSTR lpszStrings[2];
dwErr = GetLastError();
// Use event logging to log the error.
//
hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME));
_stprintf(szMsg, TEXT("%s error: %d"), TEXT(SZSERVICENAME), dwErr);
lpszStrings[0] = szMsg;
lpszStrings[1] = lpszMsg;
if (hEventSource != NULL) {
ReportEvent(hEventSource, // handle of event source
EVENTLOG_ERROR_TYPE, // event type
0, // event category
1, // event ID
NULL, // current user's SID
2, // strings in lpszStrings
0, // no bytes of raw data
(LPCSTR*)lpszStrings, // array of error strings
NULL // no raw data
);
(VOID) DeregisterEventSource(hEventSource);
}
}
//
// FUNCTION: LogEventWarningMessage(LPTSTR lpszMsg)
//
// PURPOSE: Allows any thread to log an warning message
//
// PARAMETERS:
// lpszMsg - text for message
//
// RETURN VALUE:
// none
//
// COMMENTS:
//
VOID LogEventWarningMessage(LPTSTR lpszMsg) {
HANDLE hEventSource;
LPTSTR lpszStrings[2];
// Use event logging to log the error.
//
hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME));
lpszStrings[0] = lpszMsg;
lpszStrings[1] = '\0';
if (hEventSource != NULL) {
ReportEvent(hEventSource, // handle of event source
EVENTLOG_WARNING_TYPE,// event type
0, // event category
1, // event ID
NULL, // current user's SID
2, // strings in lpszStrings
0, // no bytes of raw data
(LPCSTR*)lpszStrings, // array of error strings
NULL); // no raw data
(VOID) DeregisterEventSource(hEventSource);
}
}
//
// FUNCTION: LogEventInfoMessage(LPTSTR lpszMsg)
//
// PURPOSE: Allows any thread to log an info message
//
// PARAMETERS:
// lpszMsg - text for message
//
// RETURN VALUE:
// none
//
// COMMENTS:
//
VOID LogEventInfoMessage(LPTSTR lpszMsg) {
HANDLE hEventSource;
LPTSTR lpszStrings[2];
// Use event logging to log the error.
//
hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME));
lpszStrings[0] = lpszMsg;
lpszStrings[1] = '\0';
if (hEventSource != NULL) {
ReportEvent(hEventSource, // handle of event source
EVENTLOG_INFORMATION_TYPE,// event type
0, // event category
1, // event ID
NULL, // current user's SID
2, // strings in lpszStrings
0, // no bytes of raw data
(LPCSTR*)lpszStrings, // array of error strings
NULL); // no raw data
(VOID) DeregisterEventSource(hEventSource);
}
}