winevt_c 0.8.0 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.clang-format +4 -4
- data/.github/workflows/linux.yml +26 -0
- data/Gemfile +6 -6
- data/LICENSE.txt +202 -202
- data/README.md +97 -42
- data/Rakefile +37 -37
- data/appveyor.yml +32 -26
- data/example/bookmark.rb +9 -9
- data/example/enumerate_channels.rb +13 -13
- data/example/eventlog.rb +13 -9
- data/example/locale.rb +13 -0
- data/example/rate_limit.rb +14 -14
- data/example/tailing.rb +21 -15
- data/ext/winevt/extconf.rb +24 -24
- data/ext/winevt/winevt.c +30 -26
- data/ext/winevt/winevt_bookmark.c +149 -149
- data/ext/winevt/winevt_c.h +133 -109
- data/ext/winevt/winevt_channel.c +327 -327
- data/ext/winevt/winevt_locale.c +92 -68
- data/ext/winevt/winevt_locale_info.c +68 -0
- data/ext/winevt/winevt_query.c +649 -551
- data/ext/winevt/winevt_session.c +425 -0
- data/ext/winevt/winevt_subscribe.c +756 -652
- data/ext/winevt/winevt_utils.cpp +727 -696
- data/lib/winevt.rb +14 -13
- data/lib/winevt/bookmark.rb +6 -6
- data/lib/winevt/query.rb +6 -6
- data/lib/winevt/session.rb +15 -0
- data/lib/winevt/subscribe.rb +18 -15
- data/lib/winevt/version.rb +3 -3
- data/winevt_c.gemspec +34 -34
- metadata +12 -9
- data/.travis.yml +0 -15
data/ext/winevt/winevt_utils.cpp
CHANGED
@@ -1,696 +1,727 @@
|
|
1
|
-
#include <winevt_c.h>
|
2
|
-
|
3
|
-
#include <sddl.h>
|
4
|
-
#include <stdlib.h>
|
5
|
-
#include <string>
|
6
|
-
#include <vector>
|
7
|
-
|
8
|
-
VALUE
|
9
|
-
wstr_to_rb_str(UINT cp, const WCHAR* wstr, int clen)
|
10
|
-
{
|
11
|
-
VALUE vstr;
|
12
|
-
CHAR* ptr;
|
13
|
-
int len = WideCharToMultiByte(cp, 0, wstr, clen, nullptr, 0, nullptr, nullptr);
|
14
|
-
ptr = ALLOCV_N(CHAR, vstr, len);
|
15
|
-
WideCharToMultiByte(cp, 0, wstr, clen, ptr, len, nullptr, nullptr);
|
16
|
-
VALUE str = rb_utf8_str_new_cstr(ptr);
|
17
|
-
ALLOCV_END(vstr);
|
18
|
-
|
19
|
-
return str;
|
20
|
-
}
|
21
|
-
|
22
|
-
void
|
23
|
-
raise_system_error(VALUE error, DWORD errorCode)
|
24
|
-
{
|
25
|
-
WCHAR msgBuf[256] = { 0 };
|
26
|
-
VALUE errorMessage;
|
27
|
-
|
28
|
-
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
29
|
-
NULL,
|
30
|
-
errorCode,
|
31
|
-
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
32
|
-
msgBuf,
|
33
|
-
_countof(msgBuf),
|
34
|
-
NULL);
|
35
|
-
errorMessage = wstr_to_rb_str(CP_UTF8, msgBuf, -1);
|
36
|
-
|
37
|
-
#pragma GCC diagnostic push
|
38
|
-
#pragma GCC diagnostic ignored "-Wformat="
|
39
|
-
#pragma GCC diagnostic ignored "-Wformat-extra-args"
|
40
|
-
rb_raise(error, "ErrorCode: %lu\nError: %" PRIsVALUE "\n", errorCode, errorMessage);
|
41
|
-
#pragma GCC diagnostic pop
|
42
|
-
}
|
43
|
-
|
44
|
-
VALUE
|
45
|
-
render_to_rb_str(EVT_HANDLE handle, DWORD flags)
|
46
|
-
{
|
47
|
-
VALUE vbuffer;
|
48
|
-
WCHAR* buffer;
|
49
|
-
ULONG bufferSize = 0;
|
50
|
-
ULONG bufferSizeUsed = 0;
|
51
|
-
ULONG count;
|
52
|
-
BOOL succeeded;
|
53
|
-
VALUE result;
|
54
|
-
|
55
|
-
if (flags != EvtRenderEventXml && flags != EvtRenderBookmark) {
|
56
|
-
return Qnil;
|
57
|
-
}
|
58
|
-
|
59
|
-
// Get the size of the buffer
|
60
|
-
EvtRender(nullptr, handle, flags, 0, NULL, &bufferSize, &count);
|
61
|
-
|
62
|
-
// bufferSize is in bytes, not characters
|
63
|
-
buffer = (WCHAR*)ALLOCV(vbuffer, bufferSize);
|
64
|
-
|
65
|
-
succeeded =
|
66
|
-
EvtRender(nullptr, handle, flags, bufferSize, buffer, &bufferSizeUsed, &count);
|
67
|
-
if (!succeeded) {
|
68
|
-
DWORD status = GetLastError();
|
69
|
-
ALLOCV_END(vbuffer);
|
70
|
-
raise_system_error(rb_eWinevtQueryError, status);
|
71
|
-
}
|
72
|
-
|
73
|
-
result = wstr_to_rb_str(CP_UTF8, buffer, -1);
|
74
|
-
ALLOCV_END(vbuffer);
|
75
|
-
|
76
|
-
return result;
|
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
|
-
rb_ary_push(userValues,
|
127
|
-
break;
|
128
|
-
case
|
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
|
-
rb_ary_push(userValues,
|
157
|
-
break;
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
case
|
167
|
-
rbObj = pRenderedValues[i].
|
168
|
-
rb_ary_push(userValues, rbObj);
|
169
|
-
break;
|
170
|
-
case
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
rb_ary_push(userValues,
|
183
|
-
break;
|
184
|
-
|
185
|
-
|
186
|
-
CHAR
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
case
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
st.
|
224
|
-
st.
|
225
|
-
st.
|
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
|
-
if (
|
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
|
-
|
1
|
+
#include <winevt_c.h>
|
2
|
+
|
3
|
+
#include <sddl.h>
|
4
|
+
#include <stdlib.h>
|
5
|
+
#include <string>
|
6
|
+
#include <vector>
|
7
|
+
|
8
|
+
VALUE
|
9
|
+
wstr_to_rb_str(UINT cp, const WCHAR* wstr, int clen)
|
10
|
+
{
|
11
|
+
VALUE vstr;
|
12
|
+
CHAR* ptr;
|
13
|
+
int len = WideCharToMultiByte(cp, 0, wstr, clen, nullptr, 0, nullptr, nullptr);
|
14
|
+
ptr = ALLOCV_N(CHAR, vstr, len);
|
15
|
+
WideCharToMultiByte(cp, 0, wstr, clen, ptr, len, nullptr, nullptr);
|
16
|
+
VALUE str = rb_utf8_str_new_cstr(ptr);
|
17
|
+
ALLOCV_END(vstr);
|
18
|
+
|
19
|
+
return str;
|
20
|
+
}
|
21
|
+
|
22
|
+
void
|
23
|
+
raise_system_error(VALUE error, DWORD errorCode)
|
24
|
+
{
|
25
|
+
WCHAR msgBuf[256] = { 0 };
|
26
|
+
VALUE errorMessage;
|
27
|
+
|
28
|
+
FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
29
|
+
NULL,
|
30
|
+
errorCode,
|
31
|
+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
32
|
+
msgBuf,
|
33
|
+
_countof(msgBuf),
|
34
|
+
NULL);
|
35
|
+
errorMessage = wstr_to_rb_str(CP_UTF8, msgBuf, -1);
|
36
|
+
|
37
|
+
#pragma GCC diagnostic push
|
38
|
+
#pragma GCC diagnostic ignored "-Wformat="
|
39
|
+
#pragma GCC diagnostic ignored "-Wformat-extra-args"
|
40
|
+
rb_raise(error, "ErrorCode: %lu\nError: %" PRIsVALUE "\n", errorCode, errorMessage);
|
41
|
+
#pragma GCC diagnostic pop
|
42
|
+
}
|
43
|
+
|
44
|
+
VALUE
|
45
|
+
render_to_rb_str(EVT_HANDLE handle, DWORD flags)
|
46
|
+
{
|
47
|
+
VALUE vbuffer;
|
48
|
+
WCHAR* buffer;
|
49
|
+
ULONG bufferSize = 0;
|
50
|
+
ULONG bufferSizeUsed = 0;
|
51
|
+
ULONG count;
|
52
|
+
BOOL succeeded;
|
53
|
+
VALUE result;
|
54
|
+
|
55
|
+
if (flags != EvtRenderEventXml && flags != EvtRenderBookmark) {
|
56
|
+
return Qnil;
|
57
|
+
}
|
58
|
+
|
59
|
+
// Get the size of the buffer
|
60
|
+
EvtRender(nullptr, handle, flags, 0, NULL, &bufferSize, &count);
|
61
|
+
|
62
|
+
// bufferSize is in bytes, not characters
|
63
|
+
buffer = (WCHAR*)ALLOCV(vbuffer, bufferSize);
|
64
|
+
|
65
|
+
succeeded =
|
66
|
+
EvtRender(nullptr, handle, flags, bufferSize, buffer, &bufferSizeUsed, &count);
|
67
|
+
if (!succeeded) {
|
68
|
+
DWORD status = GetLastError();
|
69
|
+
ALLOCV_END(vbuffer);
|
70
|
+
raise_system_error(rb_eWinevtQueryError, status);
|
71
|
+
}
|
72
|
+
|
73
|
+
result = wstr_to_rb_str(CP_UTF8, buffer, -1);
|
74
|
+
ALLOCV_END(vbuffer);
|
75
|
+
|
76
|
+
return result;
|
77
|
+
}
|
78
|
+
|
79
|
+
EVT_HANDLE
|
80
|
+
connect_to_remote(LPWSTR computerName, LPWSTR domain, LPWSTR username, LPWSTR password,
|
81
|
+
EVT_RPC_LOGIN_FLAGS flags, DWORD *error_code)
|
82
|
+
{
|
83
|
+
EVT_HANDLE hRemote = NULL;
|
84
|
+
EVT_RPC_LOGIN Credentials;
|
85
|
+
|
86
|
+
RtlZeroMemory(&Credentials, sizeof(EVT_RPC_LOGIN));
|
87
|
+
|
88
|
+
Credentials.Server = computerName;
|
89
|
+
Credentials.Domain = domain;
|
90
|
+
Credentials.User = username;
|
91
|
+
Credentials.Password = password;
|
92
|
+
Credentials.Flags = flags;
|
93
|
+
|
94
|
+
hRemote = EvtOpenSession(EvtRpcLogin, &Credentials, 0, 0);
|
95
|
+
if (!hRemote) {
|
96
|
+
*error_code = GetLastError();
|
97
|
+
return hRemote;
|
98
|
+
}
|
99
|
+
|
100
|
+
SecureZeroMemory(&Credentials, sizeof(EVT_RPC_LOGIN));
|
101
|
+
|
102
|
+
return hRemote;
|
103
|
+
}
|
104
|
+
|
105
|
+
static std::wstring
|
106
|
+
guid_to_wstr(const GUID& guid)
|
107
|
+
{
|
108
|
+
LPOLESTR p = nullptr;
|
109
|
+
if (FAILED(StringFromCLSID(guid, &p))) {
|
110
|
+
return nullptr;
|
111
|
+
}
|
112
|
+
std::wstring s(p);
|
113
|
+
CoTaskMemFree(p);
|
114
|
+
return s;
|
115
|
+
}
|
116
|
+
|
117
|
+
static VALUE
|
118
|
+
extract_user_evt_variants(PEVT_VARIANT pRenderedValues, DWORD propCount)
|
119
|
+
{
|
120
|
+
VALUE userValues = rb_ary_new();
|
121
|
+
VALUE rbObj;
|
122
|
+
|
123
|
+
for (DWORD i = 0; i < propCount; i++) {
|
124
|
+
switch (pRenderedValues[i].Type) {
|
125
|
+
case EvtVarTypeNull:
|
126
|
+
rb_ary_push(userValues, Qnil);
|
127
|
+
break;
|
128
|
+
case EvtVarTypeString:
|
129
|
+
if (pRenderedValues[i].StringVal == nullptr) {
|
130
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr("(NULL)"));
|
131
|
+
} else {
|
132
|
+
std::wstring wStr(pRenderedValues[i].StringVal);
|
133
|
+
rbObj = wstr_to_rb_str(CP_UTF8, &wStr[0], -1);
|
134
|
+
rb_ary_push(userValues, rbObj);
|
135
|
+
}
|
136
|
+
break;
|
137
|
+
case EvtVarTypeAnsiString:
|
138
|
+
if (pRenderedValues[i].AnsiStringVal == nullptr) {
|
139
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr("(NULL)"));
|
140
|
+
} else {
|
141
|
+
rb_ary_push(
|
142
|
+
userValues,
|
143
|
+
rb_utf8_str_new_cstr(const_cast<char*>(pRenderedValues[i].AnsiStringVal)));
|
144
|
+
}
|
145
|
+
break;
|
146
|
+
case EvtVarTypeSByte:
|
147
|
+
rbObj = INT2NUM(static_cast<UINT32>(pRenderedValues[i].SByteVal));
|
148
|
+
rb_ary_push(userValues, rbObj);
|
149
|
+
break;
|
150
|
+
case EvtVarTypeByte:
|
151
|
+
rbObj = INT2NUM(static_cast<UINT32>(pRenderedValues[i].ByteVal));
|
152
|
+
rb_ary_push(userValues, rbObj);
|
153
|
+
break;
|
154
|
+
case EvtVarTypeInt16:
|
155
|
+
rbObj = INT2NUM(static_cast<INT32>(pRenderedValues[i].Int16Val));
|
156
|
+
rb_ary_push(userValues, rbObj);
|
157
|
+
break;
|
158
|
+
case EvtVarTypeUInt16:
|
159
|
+
rbObj = UINT2NUM(static_cast<UINT32>(pRenderedValues[i].UInt16Val));
|
160
|
+
rb_ary_push(userValues, rbObj);
|
161
|
+
break;
|
162
|
+
case EvtVarTypeInt32:
|
163
|
+
rbObj = INT2NUM(pRenderedValues[i].Int32Val);
|
164
|
+
rb_ary_push(userValues, rbObj);
|
165
|
+
break;
|
166
|
+
case EvtVarTypeUInt32:
|
167
|
+
rbObj = UINT2NUM(pRenderedValues[i].UInt32Val);
|
168
|
+
rb_ary_push(userValues, rbObj);
|
169
|
+
break;
|
170
|
+
case EvtVarTypeInt64:
|
171
|
+
rbObj = LONG2NUM(pRenderedValues[i].Int64Val);
|
172
|
+
rb_ary_push(userValues, rbObj);
|
173
|
+
break;
|
174
|
+
case EvtVarTypeUInt64:
|
175
|
+
rbObj = ULONG2NUM(pRenderedValues[i].UInt64Val);
|
176
|
+
rb_ary_push(userValues, rbObj);
|
177
|
+
break;
|
178
|
+
case EvtVarTypeSingle: {
|
179
|
+
CHAR sResult[256];
|
180
|
+
_snprintf_s(
|
181
|
+
sResult, _countof(sResult), _TRUNCATE, "%f", pRenderedValues[i].SingleVal);
|
182
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr(sResult));
|
183
|
+
break;
|
184
|
+
}
|
185
|
+
case EvtVarTypeDouble: {
|
186
|
+
CHAR sResult[256];
|
187
|
+
_snprintf_s(
|
188
|
+
sResult, _countof(sResult), _TRUNCATE, "%lf", pRenderedValues[i].DoubleVal);
|
189
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr(sResult));
|
190
|
+
break;
|
191
|
+
}
|
192
|
+
case EvtVarTypeBoolean:
|
193
|
+
rbObj = pRenderedValues[i].BooleanVal ? Qtrue : Qfalse;
|
194
|
+
rb_ary_push(userValues, rbObj);
|
195
|
+
break;
|
196
|
+
case EvtVarTypeGuid:
|
197
|
+
if (pRenderedValues[i].GuidVal != nullptr) {
|
198
|
+
const GUID guid = *pRenderedValues[i].GuidVal;
|
199
|
+
std::wstring wstr = guid_to_wstr(guid);
|
200
|
+
rbObj = wstr_to_rb_str(CP_UTF8, wstr.c_str(), -1);
|
201
|
+
rb_ary_push(userValues, rbObj);
|
202
|
+
} else {
|
203
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr("?"));
|
204
|
+
}
|
205
|
+
break;
|
206
|
+
case EvtVarTypeSizeT:
|
207
|
+
rbObj = SIZET2NUM(pRenderedValues[i].SizeTVal);
|
208
|
+
rb_ary_push(userValues, rbObj);
|
209
|
+
break;
|
210
|
+
case EvtVarTypeFileTime: {
|
211
|
+
LARGE_INTEGER timestamp;
|
212
|
+
CHAR strTime[128];
|
213
|
+
FILETIME ft;
|
214
|
+
SYSTEMTIME st;
|
215
|
+
timestamp.QuadPart = pRenderedValues[i].FileTimeVal;
|
216
|
+
ft.dwHighDateTime = timestamp.HighPart;
|
217
|
+
ft.dwLowDateTime = timestamp.LowPart;
|
218
|
+
if (FileTimeToSystemTime(&ft, &st)) {
|
219
|
+
_snprintf_s(strTime,
|
220
|
+
_countof(strTime),
|
221
|
+
_TRUNCATE,
|
222
|
+
"%04d-%02d-%02d %02d:%02d:%02d.%dZ",
|
223
|
+
st.wYear,
|
224
|
+
st.wMonth,
|
225
|
+
st.wDay,
|
226
|
+
st.wHour,
|
227
|
+
st.wMinute,
|
228
|
+
st.wSecond,
|
229
|
+
st.wMilliseconds);
|
230
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr(strTime));
|
231
|
+
} else {
|
232
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr("?"));
|
233
|
+
}
|
234
|
+
break;
|
235
|
+
}
|
236
|
+
case EvtVarTypeSysTime: {
|
237
|
+
CHAR strTime[128];
|
238
|
+
SYSTEMTIME st;
|
239
|
+
if (pRenderedValues[i].SysTimeVal != nullptr) {
|
240
|
+
st = *pRenderedValues[i].SysTimeVal;
|
241
|
+
_snprintf_s(strTime,
|
242
|
+
_countof(strTime),
|
243
|
+
_TRUNCATE,
|
244
|
+
"%04d-%02d-%02d %02d:%02d:%02d.%dZ",
|
245
|
+
st.wYear,
|
246
|
+
st.wMonth,
|
247
|
+
st.wDay,
|
248
|
+
st.wHour,
|
249
|
+
st.wMinute,
|
250
|
+
st.wSecond,
|
251
|
+
st.wMilliseconds);
|
252
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr(strTime));
|
253
|
+
} else {
|
254
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr("?"));
|
255
|
+
}
|
256
|
+
break;
|
257
|
+
}
|
258
|
+
case EvtVarTypeSid: {
|
259
|
+
WCHAR* tmpWChar = nullptr;
|
260
|
+
if (ConvertSidToStringSidW(pRenderedValues[i].SidVal, &tmpWChar)) {
|
261
|
+
rbObj = wstr_to_rb_str(CP_UTF8, tmpWChar, -1);
|
262
|
+
rb_ary_push(userValues, rbObj);
|
263
|
+
LocalFree(tmpWChar);
|
264
|
+
} else {
|
265
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr("?"));
|
266
|
+
}
|
267
|
+
break;
|
268
|
+
}
|
269
|
+
case EvtVarTypeHexInt32:
|
270
|
+
rbObj = rb_sprintf("%#x", pRenderedValues[i].UInt32Val);
|
271
|
+
rb_ary_push(userValues, rbObj);
|
272
|
+
break;
|
273
|
+
case EvtVarTypeHexInt64:
|
274
|
+
uint32_t high;
|
275
|
+
uint32_t low;
|
276
|
+
|
277
|
+
high = pRenderedValues[i].UInt64Val >> 32;
|
278
|
+
low = pRenderedValues[i].UInt64Val & 0x00000000FFFFFFFF;
|
279
|
+
rbObj = rb_sprintf("0x%08x%08x", high, low);
|
280
|
+
rb_ary_push(userValues, rbObj);
|
281
|
+
break;
|
282
|
+
case EvtVarTypeEvtXml:
|
283
|
+
if (pRenderedValues[i].XmlVal == nullptr) {
|
284
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr("(NULL)"));
|
285
|
+
} else {
|
286
|
+
rbObj = wstr_to_rb_str(CP_UTF8, pRenderedValues[i].XmlVal, -1);
|
287
|
+
rb_ary_push(userValues, rbObj);
|
288
|
+
}
|
289
|
+
break;
|
290
|
+
default:
|
291
|
+
rb_ary_push(userValues, rb_utf8_str_new_cstr("?"));
|
292
|
+
break;
|
293
|
+
}
|
294
|
+
}
|
295
|
+
|
296
|
+
return userValues;
|
297
|
+
}
|
298
|
+
|
299
|
+
VALUE
|
300
|
+
get_values(EVT_HANDLE handle)
|
301
|
+
{
|
302
|
+
VALUE vbuffer;
|
303
|
+
PEVT_VARIANT pRenderedValues;
|
304
|
+
ULONG bufferSize = 0;
|
305
|
+
ULONG bufferSizeUsed = 0;
|
306
|
+
DWORD propCount = 0;
|
307
|
+
BOOL succeeded;
|
308
|
+
VALUE userValues = Qnil;
|
309
|
+
|
310
|
+
EVT_HANDLE renderContext = EvtCreateRenderContext(0, nullptr, EvtRenderContextUser);
|
311
|
+
if (renderContext == nullptr) {
|
312
|
+
rb_raise(rb_eWinevtQueryError, "Failed to create renderContext");
|
313
|
+
}
|
314
|
+
|
315
|
+
// Get the size of the buffer
|
316
|
+
EvtRender(
|
317
|
+
renderContext, handle, EvtRenderEventValues, 0, NULL, &bufferSize, &propCount);
|
318
|
+
|
319
|
+
// bufferSize is in bytes, not array size
|
320
|
+
pRenderedValues = (PEVT_VARIANT)ALLOCV(vbuffer, bufferSize);
|
321
|
+
|
322
|
+
succeeded = EvtRender(renderContext,
|
323
|
+
handle,
|
324
|
+
EvtRenderEventValues,
|
325
|
+
bufferSize,
|
326
|
+
pRenderedValues,
|
327
|
+
&bufferSizeUsed,
|
328
|
+
&propCount);
|
329
|
+
if (!succeeded) {
|
330
|
+
DWORD status = GetLastError();
|
331
|
+
ALLOCV_END(vbuffer);
|
332
|
+
EvtClose(renderContext);
|
333
|
+
raise_system_error(rb_eWinevtQueryError, status);
|
334
|
+
}
|
335
|
+
|
336
|
+
userValues = extract_user_evt_variants(pRenderedValues, propCount);
|
337
|
+
|
338
|
+
ALLOCV_END(vbuffer);
|
339
|
+
EvtClose(renderContext);
|
340
|
+
|
341
|
+
return userValues;
|
342
|
+
}
|
343
|
+
|
344
|
+
static std::vector<WCHAR>
|
345
|
+
get_message(EVT_HANDLE hMetadata, EVT_HANDLE handle)
|
346
|
+
{
|
347
|
+
#define BUFSIZE 4096
|
348
|
+
std::vector<WCHAR> result;
|
349
|
+
ULONG status;
|
350
|
+
ULONG bufferSizeNeeded = 0;
|
351
|
+
LPVOID lpMsgBuf;
|
352
|
+
std::vector<WCHAR> message(BUFSIZE);
|
353
|
+
|
354
|
+
if (!EvtFormatMessage(hMetadata,
|
355
|
+
handle,
|
356
|
+
0xffffffff,
|
357
|
+
0,
|
358
|
+
nullptr,
|
359
|
+
EvtFormatMessageEvent,
|
360
|
+
message.size(),
|
361
|
+
&message[0],
|
362
|
+
&bufferSizeNeeded)) {
|
363
|
+
status = GetLastError();
|
364
|
+
|
365
|
+
if (status != ERROR_EVT_UNRESOLVED_VALUE_INSERT) {
|
366
|
+
switch (status) {
|
367
|
+
case ERROR_EVT_MESSAGE_NOT_FOUND:
|
368
|
+
case ERROR_EVT_MESSAGE_ID_NOT_FOUND:
|
369
|
+
case ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
|
370
|
+
case ERROR_RESOURCE_LANG_NOT_FOUND:
|
371
|
+
case ERROR_MUI_FILE_NOT_FOUND:
|
372
|
+
case ERROR_EVT_UNRESOLVED_PARAMETER_INSERT: {
|
373
|
+
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
374
|
+
FORMAT_MESSAGE_IGNORE_INSERTS,
|
375
|
+
nullptr,
|
376
|
+
status,
|
377
|
+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
378
|
+
reinterpret_cast<WCHAR*>(&lpMsgBuf),
|
379
|
+
0,
|
380
|
+
nullptr) == 0)
|
381
|
+
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
|
382
|
+
FORMAT_MESSAGE_IGNORE_INSERTS,
|
383
|
+
nullptr,
|
384
|
+
status,
|
385
|
+
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
|
386
|
+
reinterpret_cast<WCHAR*>(&lpMsgBuf),
|
387
|
+
0,
|
388
|
+
nullptr);
|
389
|
+
|
390
|
+
std::wstring ret(reinterpret_cast<WCHAR*>(lpMsgBuf));
|
391
|
+
std::copy(ret.begin(), ret.end(), std::back_inserter(result));
|
392
|
+
LocalFree(lpMsgBuf);
|
393
|
+
|
394
|
+
goto cleanup;
|
395
|
+
}
|
396
|
+
}
|
397
|
+
|
398
|
+
if (status != ERROR_INSUFFICIENT_BUFFER)
|
399
|
+
rb_raise(rb_eWinevtQueryError, "ErrorCode: %lu", status);
|
400
|
+
}
|
401
|
+
|
402
|
+
if (status == ERROR_INSUFFICIENT_BUFFER) {
|
403
|
+
message.resize(bufferSizeNeeded);
|
404
|
+
message.shrink_to_fit();
|
405
|
+
|
406
|
+
if (!EvtFormatMessage(hMetadata,
|
407
|
+
handle,
|
408
|
+
0xffffffff,
|
409
|
+
0,
|
410
|
+
nullptr,
|
411
|
+
EvtFormatMessageEvent,
|
412
|
+
message.size(),
|
413
|
+
&message.front(),
|
414
|
+
&bufferSizeNeeded)) {
|
415
|
+
status = GetLastError();
|
416
|
+
|
417
|
+
if (status != ERROR_EVT_UNRESOLVED_VALUE_INSERT) {
|
418
|
+
switch (status) {
|
419
|
+
case ERROR_EVT_MESSAGE_NOT_FOUND:
|
420
|
+
case ERROR_EVT_MESSAGE_ID_NOT_FOUND:
|
421
|
+
case ERROR_EVT_MESSAGE_LOCALE_NOT_FOUND:
|
422
|
+
case ERROR_RESOURCE_LANG_NOT_FOUND:
|
423
|
+
case ERROR_MUI_FILE_NOT_FOUND:
|
424
|
+
case ERROR_EVT_UNRESOLVED_PARAMETER_INSERT:
|
425
|
+
if (FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
426
|
+
FORMAT_MESSAGE_FROM_SYSTEM |
|
427
|
+
FORMAT_MESSAGE_IGNORE_INSERTS,
|
428
|
+
nullptr,
|
429
|
+
status,
|
430
|
+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
431
|
+
reinterpret_cast<WCHAR*>(&lpMsgBuf),
|
432
|
+
0,
|
433
|
+
nullptr) == 0)
|
434
|
+
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
435
|
+
FORMAT_MESSAGE_FROM_SYSTEM |
|
436
|
+
FORMAT_MESSAGE_IGNORE_INSERTS,
|
437
|
+
nullptr,
|
438
|
+
status,
|
439
|
+
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
|
440
|
+
reinterpret_cast<WCHAR*>(&lpMsgBuf),
|
441
|
+
0,
|
442
|
+
nullptr);
|
443
|
+
|
444
|
+
std::wstring ret(reinterpret_cast<WCHAR*>(lpMsgBuf));
|
445
|
+
std::copy(ret.begin(), ret.end(), std::back_inserter(result));
|
446
|
+
LocalFree(lpMsgBuf);
|
447
|
+
|
448
|
+
goto cleanup;
|
449
|
+
}
|
450
|
+
|
451
|
+
rb_raise(rb_eWinevtQueryError, "ErrorCode: %lu", status);
|
452
|
+
}
|
453
|
+
}
|
454
|
+
}
|
455
|
+
}
|
456
|
+
|
457
|
+
result = message;
|
458
|
+
|
459
|
+
cleanup:
|
460
|
+
|
461
|
+
return result;
|
462
|
+
|
463
|
+
#undef BUFSIZE
|
464
|
+
}
|
465
|
+
|
466
|
+
WCHAR*
|
467
|
+
get_description(EVT_HANDLE handle, LANGID langID, EVT_HANDLE hRemote)
|
468
|
+
{
|
469
|
+
#define BUFSIZE 4096
|
470
|
+
std::vector<WCHAR> buffer(BUFSIZE);
|
471
|
+
ULONG bufferSizeNeeded = 0;
|
472
|
+
ULONG status, count;
|
473
|
+
std::vector<WCHAR> result;
|
474
|
+
EVT_HANDLE hMetadata = nullptr;
|
475
|
+
|
476
|
+
static PCWSTR eventProperties[] = { L"Event/System/Provider/@Name" };
|
477
|
+
EVT_HANDLE renderContext =
|
478
|
+
EvtCreateRenderContext(1, eventProperties, EvtRenderContextValues);
|
479
|
+
if (renderContext == nullptr) {
|
480
|
+
rb_raise(rb_eWinevtQueryError, "Failed to create renderContext");
|
481
|
+
}
|
482
|
+
|
483
|
+
if (EvtRender(renderContext,
|
484
|
+
handle,
|
485
|
+
EvtRenderEventValues,
|
486
|
+
buffer.size(),
|
487
|
+
&buffer.front(),
|
488
|
+
&bufferSizeNeeded,
|
489
|
+
&count) != FALSE) {
|
490
|
+
status = ERROR_SUCCESS;
|
491
|
+
} else {
|
492
|
+
status = GetLastError();
|
493
|
+
}
|
494
|
+
|
495
|
+
if (status != ERROR_SUCCESS) {
|
496
|
+
raise_system_error(rb_eWinevtQueryError, status);
|
497
|
+
}
|
498
|
+
|
499
|
+
// Obtain buffer as EVT_VARIANT pointer. To avoid ErrorCide 87 in EvtRender.
|
500
|
+
const PEVT_VARIANT values = reinterpret_cast<PEVT_VARIANT>(&buffer.front());
|
501
|
+
|
502
|
+
// Open publisher metadata
|
503
|
+
hMetadata = EvtOpenPublisherMetadata(
|
504
|
+
hRemote,
|
505
|
+
values[0].StringVal,
|
506
|
+
nullptr,
|
507
|
+
MAKELCID(langID, SORT_DEFAULT),
|
508
|
+
0);
|
509
|
+
if (hMetadata == nullptr) {
|
510
|
+
// When winevt_c cannot open metadata, then give up to obtain
|
511
|
+
// message file and clean up immediately.
|
512
|
+
goto cleanup;
|
513
|
+
}
|
514
|
+
|
515
|
+
result = get_message(hMetadata, handle);
|
516
|
+
|
517
|
+
#undef BUFSIZE
|
518
|
+
|
519
|
+
cleanup:
|
520
|
+
|
521
|
+
if (renderContext)
|
522
|
+
EvtClose(renderContext);
|
523
|
+
|
524
|
+
if (hMetadata)
|
525
|
+
EvtClose(hMetadata);
|
526
|
+
|
527
|
+
return _wcsdup(result.data());
|
528
|
+
}
|
529
|
+
|
530
|
+
VALUE
|
531
|
+
render_system_event(EVT_HANDLE hEvent, BOOL preserve_qualifiers)
|
532
|
+
{
|
533
|
+
DWORD status = ERROR_SUCCESS;
|
534
|
+
EVT_HANDLE hContext = NULL;
|
535
|
+
DWORD dwBufferSize = 0;
|
536
|
+
DWORD dwBufferUsed = 0;
|
537
|
+
DWORD dwPropertyCount = 0;
|
538
|
+
VALUE vRenderedValues;
|
539
|
+
PEVT_VARIANT pRenderedValues = NULL;
|
540
|
+
WCHAR wsGuid[50];
|
541
|
+
LPSTR pwsSid = NULL;
|
542
|
+
ULONGLONG ullTimeStamp = 0;
|
543
|
+
ULONGLONG ullNanoseconds = 0;
|
544
|
+
SYSTEMTIME st;
|
545
|
+
FILETIME ft;
|
546
|
+
CHAR buffer[32];
|
547
|
+
VALUE rbstr;
|
548
|
+
DWORD EventID;
|
549
|
+
VALUE hash = rb_hash_new();
|
550
|
+
|
551
|
+
hContext = EvtCreateRenderContext(0, NULL, EvtRenderContextSystem);
|
552
|
+
if (NULL == hContext) {
|
553
|
+
rb_raise(
|
554
|
+
rb_eWinevtQueryError, "Failed to create renderContext with %lu\n", GetLastError());
|
555
|
+
}
|
556
|
+
|
557
|
+
if (!EvtRender(hContext,
|
558
|
+
hEvent,
|
559
|
+
EvtRenderEventValues,
|
560
|
+
dwBufferSize,
|
561
|
+
pRenderedValues,
|
562
|
+
&dwBufferUsed,
|
563
|
+
&dwPropertyCount)) {
|
564
|
+
status = GetLastError();
|
565
|
+
if (ERROR_INSUFFICIENT_BUFFER == status) {
|
566
|
+
dwBufferSize = dwBufferUsed;
|
567
|
+
pRenderedValues = (PEVT_VARIANT)ALLOCV(vRenderedValues, dwBufferSize);
|
568
|
+
if (pRenderedValues) {
|
569
|
+
EvtRender(hContext,
|
570
|
+
hEvent,
|
571
|
+
EvtRenderEventValues,
|
572
|
+
dwBufferSize,
|
573
|
+
pRenderedValues,
|
574
|
+
&dwBufferUsed,
|
575
|
+
&dwPropertyCount);
|
576
|
+
status = GetLastError();
|
577
|
+
} else {
|
578
|
+
EvtClose(hContext);
|
579
|
+
rb_raise(rb_eRuntimeError, "Failed to malloc memory with %lu\n", status);
|
580
|
+
}
|
581
|
+
}
|
582
|
+
|
583
|
+
if (ERROR_SUCCESS != status) {
|
584
|
+
EvtClose(hContext);
|
585
|
+
ALLOCV_END(vRenderedValues);
|
586
|
+
|
587
|
+
rb_raise(rb_eWinevtQueryError, "EvtRender failed with %lu\n", status);
|
588
|
+
}
|
589
|
+
}
|
590
|
+
|
591
|
+
// EVT_VARIANT value with EvtRenderContextSystem will be decomposed
|
592
|
+
// as the following enum definition:
|
593
|
+
// https://docs.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_system_property_id
|
594
|
+
rbstr = wstr_to_rb_str(CP_UTF8, pRenderedValues[EvtSystemProviderName].StringVal, -1);
|
595
|
+
rb_hash_aset(hash, rb_str_new2("ProviderName"), rbstr);
|
596
|
+
if (NULL != pRenderedValues[EvtSystemProviderGuid].GuidVal) {
|
597
|
+
const GUID* Guid = pRenderedValues[EvtSystemProviderGuid].GuidVal;
|
598
|
+
StringFromGUID2(*Guid, wsGuid, _countof(wsGuid));
|
599
|
+
rbstr = wstr_to_rb_str(CP_UTF8, wsGuid, -1);
|
600
|
+
rb_hash_aset(hash, rb_str_new2("ProviderGuid"), rbstr);
|
601
|
+
} else {
|
602
|
+
rb_hash_aset(hash, rb_str_new2("ProviderGuid"), Qnil);
|
603
|
+
}
|
604
|
+
|
605
|
+
EventID = pRenderedValues[EvtSystemEventID].UInt16Val;
|
606
|
+
if (preserve_qualifiers) {
|
607
|
+
if (EvtVarTypeNull != pRenderedValues[EvtSystemQualifiers].Type) {
|
608
|
+
rb_hash_aset(hash, rb_str_new2("Qualifiers"),
|
609
|
+
INT2NUM(pRenderedValues[EvtSystemQualifiers].UInt16Val));
|
610
|
+
} else {
|
611
|
+
rb_hash_aset(hash, rb_str_new2("Qualifiers"), rb_str_new2(""));
|
612
|
+
}
|
613
|
+
|
614
|
+
rb_hash_aset(hash, rb_str_new2("EventID"), INT2NUM(EventID));
|
615
|
+
} else {
|
616
|
+
if (EvtVarTypeNull != pRenderedValues[EvtSystemQualifiers].Type) {
|
617
|
+
EventID = MAKELONG(pRenderedValues[EvtSystemEventID].UInt16Val,
|
618
|
+
pRenderedValues[EvtSystemQualifiers].UInt16Val);
|
619
|
+
}
|
620
|
+
|
621
|
+
rb_hash_aset(hash, rb_str_new2("EventID"), ULONG2NUM(EventID));
|
622
|
+
}
|
623
|
+
|
624
|
+
rb_hash_aset(hash,
|
625
|
+
rb_str_new2("Version"),
|
626
|
+
(EvtVarTypeNull == pRenderedValues[EvtSystemVersion].Type)
|
627
|
+
? INT2NUM(0)
|
628
|
+
: INT2NUM(pRenderedValues[EvtSystemVersion].ByteVal));
|
629
|
+
rb_hash_aset(hash,
|
630
|
+
rb_str_new2("Level"),
|
631
|
+
(EvtVarTypeNull == pRenderedValues[EvtSystemLevel].Type)
|
632
|
+
? INT2NUM(0)
|
633
|
+
: INT2NUM(pRenderedValues[EvtSystemLevel].ByteVal));
|
634
|
+
rb_hash_aset(hash,
|
635
|
+
rb_str_new2("Task"),
|
636
|
+
(EvtVarTypeNull == pRenderedValues[EvtSystemTask].Type)
|
637
|
+
? INT2NUM(0)
|
638
|
+
: INT2NUM(pRenderedValues[EvtSystemTask].UInt16Val));
|
639
|
+
rb_hash_aset(hash,
|
640
|
+
rb_str_new2("Opcode"),
|
641
|
+
(EvtVarTypeNull == pRenderedValues[EvtSystemOpcode].Type)
|
642
|
+
? INT2NUM(0)
|
643
|
+
: INT2NUM(pRenderedValues[EvtSystemOpcode].ByteVal));
|
644
|
+
_snprintf_s(buffer,
|
645
|
+
_countof(buffer),
|
646
|
+
_TRUNCATE,
|
647
|
+
"0x%llx",
|
648
|
+
pRenderedValues[EvtSystemKeywords].UInt64Val);
|
649
|
+
rb_hash_aset(hash,
|
650
|
+
rb_str_new2("Keywords"),
|
651
|
+
(EvtVarTypeNull == pRenderedValues[EvtSystemKeywords].Type)
|
652
|
+
? Qnil
|
653
|
+
: rb_str_new2(buffer));
|
654
|
+
|
655
|
+
ullTimeStamp = pRenderedValues[EvtSystemTimeCreated].FileTimeVal;
|
656
|
+
ft.dwHighDateTime = (DWORD)((ullTimeStamp >> 32) & 0xFFFFFFFF);
|
657
|
+
ft.dwLowDateTime = (DWORD)(ullTimeStamp & 0xFFFFFFFF);
|
658
|
+
|
659
|
+
FileTimeToSystemTime(&ft, &st);
|
660
|
+
ullNanoseconds =
|
661
|
+
(ullTimeStamp % 10000000) *
|
662
|
+
100; // Display nanoseconds instead of milliseconds for higher resolution
|
663
|
+
_snprintf_s(buffer,
|
664
|
+
_countof(buffer),
|
665
|
+
_TRUNCATE,
|
666
|
+
"%02d/%02d/%02d %02d:%02d:%02d.%llu",
|
667
|
+
st.wYear,
|
668
|
+
st.wMonth,
|
669
|
+
st.wDay,
|
670
|
+
st.wHour,
|
671
|
+
st.wMinute,
|
672
|
+
st.wSecond,
|
673
|
+
ullNanoseconds);
|
674
|
+
rb_hash_aset(hash,
|
675
|
+
rb_str_new2("TimeCreated"),
|
676
|
+
(EvtVarTypeNull == pRenderedValues[EvtSystemKeywords].Type)
|
677
|
+
? Qnil
|
678
|
+
: rb_str_new2(buffer));
|
679
|
+
_snprintf_s(buffer,
|
680
|
+
_countof(buffer),
|
681
|
+
_TRUNCATE,
|
682
|
+
"%llu",
|
683
|
+
pRenderedValues[EvtSystemEventRecordId].UInt64Val);
|
684
|
+
rb_hash_aset(hash,
|
685
|
+
rb_str_new2("EventRecordID"),
|
686
|
+
(EvtVarTypeNull == pRenderedValues[EvtSystemEventRecordId].UInt64Val)
|
687
|
+
? Qnil
|
688
|
+
: rb_str_new2(buffer));
|
689
|
+
|
690
|
+
if (EvtVarTypeNull != pRenderedValues[EvtSystemActivityID].Type) {
|
691
|
+
const GUID* Guid = pRenderedValues[EvtSystemActivityID].GuidVal;
|
692
|
+
StringFromGUID2(*Guid, wsGuid, _countof(wsGuid));
|
693
|
+
rbstr = wstr_to_rb_str(CP_UTF8, wsGuid, -1);
|
694
|
+
rb_hash_aset(hash, rb_str_new2("ActivityID"), rbstr);
|
695
|
+
}
|
696
|
+
|
697
|
+
if (EvtVarTypeNull != pRenderedValues[EvtSystemRelatedActivityID].Type) {
|
698
|
+
const GUID* Guid = pRenderedValues[EvtSystemRelatedActivityID].GuidVal;
|
699
|
+
StringFromGUID2(*Guid, wsGuid, _countof(wsGuid));
|
700
|
+
rbstr = wstr_to_rb_str(CP_UTF8, wsGuid, -1);
|
701
|
+
rb_hash_aset(hash, rb_str_new2("RelatedActivityID"), rbstr);
|
702
|
+
}
|
703
|
+
|
704
|
+
rb_hash_aset(hash,
|
705
|
+
rb_str_new2("ProcessID"),
|
706
|
+
UINT2NUM(pRenderedValues[EvtSystemProcessID].UInt32Val));
|
707
|
+
rb_hash_aset(hash,
|
708
|
+
rb_str_new2("ThreadID"),
|
709
|
+
UINT2NUM(pRenderedValues[EvtSystemThreadID].UInt32Val));
|
710
|
+
rbstr = wstr_to_rb_str(CP_UTF8, pRenderedValues[EvtSystemChannel].StringVal, -1);
|
711
|
+
rb_hash_aset(hash, rb_str_new2("Channel"), rbstr);
|
712
|
+
rbstr = wstr_to_rb_str(CP_UTF8, pRenderedValues[EvtSystemComputer].StringVal, -1);
|
713
|
+
rb_hash_aset(hash, rb_str_new2("Computer"), rbstr);
|
714
|
+
|
715
|
+
if (EvtVarTypeNull != pRenderedValues[EvtSystemUserID].Type) {
|
716
|
+
if (ConvertSidToStringSid(pRenderedValues[EvtSystemUserID].SidVal, &pwsSid)) {
|
717
|
+
rbstr = rb_utf8_str_new_cstr(pwsSid);
|
718
|
+
rb_hash_aset(hash, rb_str_new2("UserID"), rbstr);
|
719
|
+
LocalFree(pwsSid);
|
720
|
+
}
|
721
|
+
}
|
722
|
+
|
723
|
+
EvtClose(hContext);
|
724
|
+
ALLOCV_END(vRenderedValues);
|
725
|
+
|
726
|
+
return hash;
|
727
|
+
}
|