-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathWEPExplorer.cpp
2141 lines (1895 loc) · 66.1 KB
/
WEPExplorer.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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Windows Events Providers Explorer / Command line
// By Elias Bachaalany <[email protected]>
// Based on the sample code from: https://msdn.microsoft.com/en-us/library/windows/desktop/dd996925(v=vs.85).aspx
//
/* -----------------------------------------------------------------------------
* Copyright (c) Elias Bachaalany <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* -----------------------------------------------------------------------------
*/
#include "stdafx.h"
//--------------------------------------------------------------------------
namespace std_utils
{
// http://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string
static void str_replace(
std::wstring &str,
const std::wstring &oldStr,
const std::wstring &newStr)
{
size_t pos = 0;
while ((pos = str.find(oldStr, pos)) != std::wstring::npos)
{
str.replace(pos, oldStr.length(), newStr);
pos += newStr.length();
}
}
static void html_entities(std::wstring &str)
{
static std::wstring tbl[] =
{
L"&", L"&",
L"<", L"<",
L">", L">"
};
for (size_t i = 0; i < _countof(tbl); i += 2)
str_replace(str, tbl[i], tbl[i + 1]);
}
}
//--------------------------------------------------------------------------
// XML constants begin
static LPCWSTR XML_PROVIDERS = L"Providers";
static LPCWSTR XML_PROVIDER = L"Provider";
static LPCWSTR XML_NAME = L"Name";
static LPCWSTR XML_GUID = L"Guid";
static LPCWSTR XML_METADATA = L"Metadata";
static LPCWSTR XML_EVENT_METADATA = L"EventMetadata";
static LPCWSTR XML_RESOURCEFILEPATH = L"ResourceFilePath";
static LPCWSTR XML_PARAMETERFILEPATH = L"ParameterFilePath";
static LPCWSTR XML_PUBLISHER_MESSAGE = L"PublisherMessage";
static LPCWSTR XML_MESSAGEFILEPATH = L"MessageFilePath";
static LPCWSTR XML_HELPLINK = L"HelpLink";
static LPCWSTR XML_CHANNEL = L"Channel";
static LPCWSTR XML_CHANNELS = L"Channels";
static LPCWSTR XML_MESSAGE = L"Message";
static LPCWSTR XML_INDEX = L"Index";
static LPCWSTR XML_ID = L"Id";
static LPCWSTR XML_PATH = L"Path";
static LPCWSTR XML_IMPORTED = L"Imported";
static LPCWSTR XML_TASKS = L"Tasks";
static LPCWSTR XML_LEVELS = L"Levels";
static LPCWSTR XML_LEVEL = L"Level";
static LPCWSTR XML_OPCODES = L"Opcodes";
static LPCWSTR XML_OPCODE = L"Opcode";
static LPCWSTR XML_VALUE = L"Value";
static LPCWSTR XML_VERSION = L"Version";
static LPCWSTR XML_KEYWORDS = L"Keywords";
static LPCWSTR XML_KEYWORD = L"Keyword";
static LPCWSTR XML_EVENT = L"Event";
static LPCWSTR XML_TEMPLATE = L"Template";
static LPCWSTR XML_TASK = L"Task";
// XML constants end
static LPCWSTR OPT_NAME = L"/name";
static LPCWSTR STR_NONE = L"None";
static LPCWSTR OPT_OUT = L"/out";
static LPCWSTR OPT_META = L"/meta";
static LPCWSTR OPT_EVENTMETA = L"/eventmeta";
static LPCWSTR OPT_FORCE = L"/force";
//--------------------------------------------------------------------------
struct COptions
{
LPWSTR ProviderFilter;
bool bGetMeta;
bool bGetEventMeta;
bool bForce;
LPWSTR OutFileName;
COptions(): bGetMeta(false), OutFileName(nullptr), bForce(false),
ProviderFilter(nullptr), bGetEventMeta(false)
{
}
};
//--------------------------------------------------------------------------
struct CXmlOutput
{
size_t m_nLevel;
std::wostream &m_osOut;
CXmlOutput(std::wostream &Out) : m_nLevel(0), m_osOut(Out)
{
}
void Tabs()
{
for (size_t i = 0; i < m_nLevel; ++i)
m_osOut << L" ";
}
void OpenTag(LPCWSTR TagName)
{
Tabs();
m_osOut << L"<" << TagName << L">" << std::endl;
++m_nLevel;
}
void CloseTag(LPCWSTR TagName)
{
--m_nLevel;
Tabs();
m_osOut << L"</" << TagName << L">" << std::endl;
}
void TabText(LPCWSTR Txt)
{
Tabs();
m_osOut << Txt << std::endl;
}
void TagText(
LPCWSTR TagName,
LPCWSTR Text)
{
Tabs();
std::wstring s(Text);
std_utils::html_entities(s);
m_osOut << L"<" << TagName << L">"
<< s.c_str()
<< L"</" << TagName << L">"
<< std::endl;
}
void TagCData(
LPCWSTR TagName,
LPCWSTR Text)
{
Tabs();
m_osOut << L"<" << TagName << L"><![CDATA[" << std::endl << Text << L"]]></" << TagName << L">" << std::endl;
}
};
//--------------------------------------------------------------------------
class CProvidersExplorer
{
private:
// Contains the value and message string for a type, such as
// an opcode or task that the provider defines or uses. If the
// type does not specify a message string, the message member
// contains the value of the type's name attribute.
typedef struct _msgstring
{
union
{
DWORD dwValue; // Value attribute for opcode, task, and level
UINT64 ullMask; // Mask attribute for keyword
};
LPWSTR pwcsMessage; // Message string or name attribute
} MSG_STRING, *PMSG_STRING;
// Header for the block of value/message string pairs. The dwSize member
// is the size, in bytes, of the block of MSG_STRING structures to which
// the pMessage member points.
typedef struct _messages
{
DWORD dwSize;
PMSG_STRING pMessage;
} MESSAGES, *PMESSAGES;
// Global variables.
EVT_HANDLE m_hMetadata;
MESSAGES m_ChannelMessages;
MESSAGES m_LevelMessages;
MESSAGES m_TaskMessages;
MESSAGES m_OpcodeMessages;
MESSAGES m_KeywordMessages;
CXmlOutput *m_Out;
COptions *m_Opt;
void DumpPublisherMetadata(LPWSTR pwszPublisherName);
DWORD DumpProviderEvents(EVT_HANDLE hMetadata);
DWORD DumpEventProperties(EVT_HANDLE hEvent);
DWORD DumpEventProperty(
EVT_HANDLE hMetadata,
int Id,
PEVT_VARIANT pProperty);
DWORD DumpProviderProperties(
EVT_HANDLE hMetadata,
DWORD PropIdFilter = -1);
static DWORD GetMetadataProperty(
EVT_HANDLE hMetadata,
EVT_PUBLISHER_METADATA_PROPERTY_ID Id,
PEVT_VARIANT *ppProperty);
DWORD DumpProviderProperty(
EVT_HANDLE hMetadata,
int Id,
PEVT_VARIANT pProperty);
DWORD DumpChannelProperties(
EVT_HANDLE hChannels,
DWORD dwIndex,
PMESSAGES pMessages);
DWORD DumpLevelProperties(
EVT_HANDLE hLevels,
DWORD dwIndex,
PMESSAGES pMessages);
DWORD DumpTaskProperties(
EVT_HANDLE hTasks,
DWORD dwIndex,
PMESSAGES pMessages);
DWORD DumpOpcodeProperties(
EVT_HANDLE hOpcodes,
DWORD dwIndex,
PMESSAGES pMessages);
DWORD DumpKeywordProperties(
EVT_HANDLE hKeywords,
DWORD dwIndex,
PMESSAGES pMessages);
LPWSTR GetMessageString(
EVT_HANDLE hMetadata,
DWORD dwMessageId);
PEVT_VARIANT GetProperty(
EVT_HANDLE handle,
DWORD dwIndex,
EVT_PUBLISHER_METADATA_PROPERTY_ID PropertyId);
LPWSTR GetPropertyName(
EVT_HANDLE hMetadata,
PMESSAGES pMessages,
DWORD dwValue);
LPWSTR GetOpcodeName(
EVT_HANDLE hMetadata,
PMESSAGES pMessages,
DWORD dwOpcodeValue,
DWORD dwTaskValue);
LPWSTR GetKeywordName(
EVT_HANDLE hMetadata,
PMESSAGES pMessages,
UINT64 ullKeyword);
void FreeMessages(PMESSAGES pMessages);
void CleanMetadataWorkVars();
public:
int Dump();
CProvidersExplorer(
COptions *opt,
CXmlOutput *out) : m_Opt(opt), m_Out(out)
{
}
};
//--------------------------------------------------------------------------
void CProvidersExplorer::CleanMetadataWorkVars()
{
m_hMetadata = NULL;
memset(&m_ChannelMessages, 0, sizeof(m_ChannelMessages));
memset(&m_LevelMessages, 0, sizeof(m_LevelMessages));
memset(&m_TaskMessages, 0, sizeof(m_TaskMessages));
memset(&m_OpcodeMessages, 0, sizeof(m_OpcodeMessages));
memset(&m_KeywordMessages, 0, sizeof(m_KeywordMessages));
}
//--------------------------------------------------------------------------
void CProvidersExplorer::DumpPublisherMetadata(LPWSTR pwszPublisherName)
{
CleanMetadataWorkVars();
DWORD status = ERROR_SUCCESS;
// Get a handle to the provider's metadata. You can specify the provider's name
// if the provider is registered on the computer or the full path to an archived
// log file (archived log files contain the provider's metadata). Specify the locale
// identifier if you want the localized strings returned in a locale other than
// the locale of the current thread.
m_hMetadata = EvtOpenPublisherMetadata(
NULL,
pwszPublisherName,
NULL,
0,
0);
if (m_hMetadata == NULL)
{
wprintf(L"ERROR: EvtOpenPublisherMetadata failed with %d\n", GetLastError());
goto cleanup;
}
m_Out->OpenTag(XML_METADATA);
status = DumpProviderProperties(
m_hMetadata,
m_Opt->bGetMeta ? -1 : EvtPublisherMetadataPublisherGuid);
m_Out->CloseTag(XML_METADATA);
if (m_Opt->bGetEventMeta)
{
m_Out->OpenTag(XML_EVENT_METADATA);
status = DumpProviderEvents(m_hMetadata);
m_Out->CloseTag(XML_EVENT_METADATA);
}
cleanup:
if (m_hMetadata != nullptr)
EvtClose(m_hMetadata);
if (m_ChannelMessages.pMessage != nullptr)
FreeMessages(&m_ChannelMessages);
if (m_LevelMessages.pMessage != nullptr)
FreeMessages(&m_LevelMessages);
if (m_TaskMessages.pMessage != nullptr)
FreeMessages(&m_TaskMessages);
if (m_OpcodeMessages.pMessage != nullptr)
FreeMessages(&m_OpcodeMessages);
if (m_KeywordMessages.pMessage != nullptr)
FreeMessages(&m_KeywordMessages);
}
//--------------------------------------------------------------------------
// Free the memory for each message string in the messages block
// and then free the messages block.
void CProvidersExplorer::FreeMessages(PMESSAGES pMessages)
{
DWORD dwCount = pMessages->dwSize / sizeof(MSG_STRING);
for (DWORD i = 0; i < dwCount; i++)
{
free(((pMessages->pMessage) + i)->pwcsMessage);
((pMessages->pMessage) + i)->pwcsMessage = nullptr;
}
free(pMessages->pMessage);
pMessages->pMessage = nullptr;
}
//--------------------------------------------------------------------------
// Get an enumerator to the provider's events and enumerate them.
// Call this function after first calling the PrintProviderProperties
// function to get the message strings that this section uses.
DWORD CProvidersExplorer::DumpProviderEvents(EVT_HANDLE hMetadata)
{
EVT_HANDLE hEvents = NULL;
EVT_HANDLE hEvent = NULL;
DWORD status = ERROR_SUCCESS;
// Get a handle to the provider's events.
hEvents = EvtOpenEventMetadataEnum(hMetadata, 0);
if (NULL == hEvents)
{
wprintf(L"ERROR: EvtOpenEventMetadataEnum failed with %lu\n", GetLastError());
goto cleanup;
}
// Enumerate the events and print each event's metadata.
while (true)
{
hEvent = EvtNextEventMetadata(hEvents, 0);
if (NULL == hEvent)
{
if (ERROR_NO_MORE_ITEMS != (status = GetLastError()))
{
wprintf(L"ERROR: EvtNextEventMetadata failed with %lu\n", status);
}
break;
}
m_Out->OpenTag(XML_EVENT);
status = DumpEventProperties(hEvent);
if (status != ERROR_SUCCESS)
break;
m_Out->CloseTag(XML_EVENT);
EvtClose(hEvent);
hEvent = NULL;
}
cleanup:
if (hEvents != nullptr)
EvtClose(hEvents);
if (hEvent != nullptr)
EvtClose(hEvent);
return status;
}
//--------------------------------------------------------------------------
// Print the metadata for the event.
DWORD CProvidersExplorer::DumpEventProperties(EVT_HANDLE hEvent)
{
PEVT_VARIANT pProperty = NULL; // Contains a metadata value
PEVT_VARIANT pTemp = NULL;
DWORD dwBufferSize = 0;
DWORD dwBufferUsed = 0;
DWORD status = ERROR_SUCCESS;
// Use the EVT_EVENT_METADATA_PROPERTY_ID's enumeration values to loop
// through all the metadata for the event.
for (int Id = 0; Id < EvtEventMetadataPropertyIdEND; Id++)
{
// Get the specified metadata property. If the pProperty buffer is not big enough, reallocate the buffer.
if (!EvtGetEventMetadataProperty(
hEvent,
EVT_EVENT_METADATA_PROPERTY_ID(Id),
0,
dwBufferSize,
pProperty,
&dwBufferUsed))
{
status = GetLastError();
if (ERROR_INSUFFICIENT_BUFFER == status)
{
dwBufferSize = dwBufferUsed;
pTemp = (PEVT_VARIANT)realloc(pProperty, dwBufferSize);
if (pTemp != nullptr)
{
pProperty = pTemp;
pTemp = NULL;
EvtGetEventMetadataProperty(
hEvent,
(EVT_EVENT_METADATA_PROPERTY_ID)Id,
0,
dwBufferSize,
pProperty,
&dwBufferUsed);
}
else
{
wprintf(L"ERROR: realloc failed\n");
status = ERROR_OUTOFMEMORY;
goto cleanup;
}
}
if (ERROR_SUCCESS != (status = GetLastError()))
{
wprintf(L"ERROR: EvtGetEventMetadataProperty failed with %d\n", GetLastError());
goto cleanup;
}
}
status = DumpEventProperty(
m_hMetadata,
Id,
pProperty);
if (status != ERROR_SUCCESS)
break;
RtlZeroMemory(pProperty, dwBufferUsed);
}
cleanup:
if (pProperty != nullptr)
free(pProperty);
return status;
}
//--------------------------------------------------------------------------
// Print the event property.
DWORD CProvidersExplorer::DumpEventProperty(
EVT_HANDLE hMetadata,
int Id,
PEVT_VARIANT pProperty)
{
DWORD status = ERROR_SUCCESS;
static DWORD dwOpcode = 0;
LPWSTR pName = NULL; // The event's name property
LPWSTR pMessage = NULL; // The event's message string
switch (Id)
{
case EventMetadataEventID:
{
wchar_t FormatBuffer[32];
swprintf_s(
FormatBuffer,
_countof(FormatBuffer),
L"%lu",
pProperty->UInt32Val);
m_Out->TagText(
XML_ID,
FormatBuffer);
break;
}
case EventMetadataEventVersion:
{
wchar_t FormatBuffer[32];
swprintf(
FormatBuffer,
_countof(FormatBuffer),
L"%lu",
pProperty->UInt32Val);
m_Out->TagText(XML_VERSION, FormatBuffer);
break;
}
// The channel property contains the value of the channel's value attribute.
// Instead of printing the value attribute, use it to find the channel's
// message string or name and print it.
case EventMetadataEventChannel:
if (pProperty->UInt32Val > 0)
{
pName = GetPropertyName(
hMetadata,
&m_ChannelMessages,
pProperty->UInt32Val);
if (pName != nullptr)
m_Out->TagText(XML_CHANNEL, pName);
}
break;
// The level property contains the value of the level's value attribute.
// Instead of printing the value attribute, use it to find the level's
// message string or name and print it.
case EventMetadataEventLevel:
if (pProperty->UInt32Val > 0)
{
pName = GetPropertyName(
hMetadata,
&m_LevelMessages,
pProperty->UInt32Val);
if (pName != nullptr)
m_Out->TagText(XML_LEVEL, pName);
}
break;
// The opcode property contains the value of the opcode's value attribute.
// Instead of printing the value attribute, use it to find the opcode's
// message string or name and print it.
// The opcode value contains the opcode in the high word. If the opcode is
// task-specific, the opcode value will contain the task value in the low word.
// Save the opcode in a static variable and print it when we get the task
// value, so we can decide if the opcode is task-specific.
case EventMetadataEventOpcode:
dwOpcode = pProperty->UInt32Val;
break;
// The task property contains the value of the task's value attribute.
// Instead of printing the value attribute, use it to find the task's
// message string or name and print it.
case EventMetadataEventTask:
if (pProperty->UInt32Val > 0)
{
pName = GetPropertyName(
hMetadata,
&m_TaskMessages,
pProperty->UInt32Val);
if (pName != nullptr)
m_Out->TagText(XML_TASK, pName);
}
// Now that we know the task, get the opcode string and print it.
if (dwOpcode > 0)
{
pName = GetOpcodeName(
hMetadata,
&m_OpcodeMessages,
dwOpcode,
pProperty->UInt32Val);
if (pName != nullptr)
m_Out->TagText(XML_OPCODE, pName);
}
break;
// The keyword property contains a bit mask of all the keywords.
// Instead of printing the value attribute, use it to find the
// message string or name associated with each keyword and print them (space delimited).
case EventMetadataEventKeyword:
// The upper 8 bits can contain reserved bit values, so do not include them
// when checking to see if any keywords are set.
if ((pProperty->UInt32Val & 0x00FFFFFFFFFFFFFF) > 0)
{
pName = GetKeywordName(
hMetadata,
&m_KeywordMessages,
pProperty->UInt32Val);
if (pName != nullptr)
{
m_Out->TagText(XML_KEYWORD, pName);
free(pName);
}
}
break;
// If the message string is not specified, the value is -1.
case EventMetadataEventMessageID:
if (pProperty->UInt32Val != -1)
{
pMessage = GetMessageString(
hMetadata,
pProperty->UInt32Val);
if (pMessage != nullptr)
{
m_Out->TagCData(XML_MESSAGE, pMessage);
free(pMessage);
}
}
break;
// When you define the event, the template attribute contains the template
// identifier; however, the template metadata contains an XML string of the
// template (includes the data items, not the UserData section).
case EventMetadataEventTemplate:
m_Out->TagCData(XML_TEMPLATE, pProperty->StringVal);
break;
default:
break;
}
return status;
}
//--------------------------------------------------------------------------
// Used to get the message string or name for levels, tasks, and channels.
// Search the messages block sequentially for an item that has the same value
// and return a pointer to the message string.
LPWSTR CProvidersExplorer::GetPropertyName(
EVT_HANDLE hMetadata,
PMESSAGES pMessages,
DWORD dwValue)
{
UNREFERENCED_PARAMETER(hMetadata);
LPWSTR pMessage = NULL;
DWORD dwCount = pMessages->dwSize / sizeof(MSG_STRING);
for (DWORD i = 0; i < dwCount; i++)
{
if ( pMessages->pMessage != nullptr
&& dwValue == ((pMessages->pMessage) + i)->dwValue )
{
pMessage = ((pMessages->pMessage) + i)->pwcsMessage;
break;
}
}
return pMessage;
}
//--------------------------------------------------------------------------
// Used to get the message string or name for an opcode. Search the messages block sequentially
// for an item that has the same opcode value (high word). Opcodes can be defined globally or
// locally (task-specific). All global opcodes must be unique, but multiple tasks can specify the
// same opcode value, so we need to check the low word to see if the task on the event matches
// the task on the opcode.
LPWSTR CProvidersExplorer::GetOpcodeName(
EVT_HANDLE hMetadata,
PMESSAGES pMessages,
DWORD dwOpcodeValue,
DWORD dwTaskValue)
{
UNREFERENCED_PARAMETER(hMetadata);
LPWSTR pMessage = NULL;
DWORD dwCount = pMessages->dwSize / sizeof(MSG_STRING);
DWORD dwOpcodeIndex = 0; // Points to the global opcode (low word is zero)
BOOL fFound = FALSE;
for (DWORD i = 0; i < dwCount; i++)
{
if (pMessages->pMessage != nullptr && dwOpcodeValue == HIWORD(((pMessages->pMessage) + i)->dwValue))
{
if (0 == LOWORD(((pMessages->pMessage) + i)->dwValue))
{
dwOpcodeIndex = i;
}
else if (dwTaskValue == LOWORD(((pMessages->pMessage) + i)->dwValue))
{
pMessage = ((pMessages->pMessage) + i)->pwcsMessage;
fFound = TRUE;
break;
}
}
}
if (FALSE == fFound)
{
if (pMessages->pMessage != nullptr)
pMessage = ((pMessages->pMessage) + dwOpcodeIndex)->pwcsMessage;
}
return pMessage;
}
//--------------------------------------------------------------------------
// Used to get the message strings or names for the keywords specified on the event. The event
// contains a bit mask that has bits set for each keyword specified on the event. Search the
// messages block sequentially for items that have the same keyword bit set. Concatenate all the
// message strings.
LPWSTR CProvidersExplorer::GetKeywordName(
EVT_HANDLE hMetadata,
PMESSAGES pMessages,
UINT64 ullKeywords)
{
UNREFERENCED_PARAMETER(hMetadata);
LPWSTR pMessage = NULL;
LPWSTR pTemp = NULL;
BOOL fFirstMessage = TRUE;
size_t dwStringLen = 0;
DWORD dwCount = pMessages->dwSize / sizeof(MSG_STRING);
for (DWORD i = 0; i < dwCount; i++)
{
if (ullKeywords & ((pMessages->pMessage) + i)->ullMask)
{
// + space delimiter + null-terminator
dwStringLen += wcslen(((pMessages->pMessage) + i)->pwcsMessage) + 1 + 1;
pTemp = (LPWSTR)realloc(pMessage, dwStringLen * sizeof(WCHAR));
if (pTemp)
{
pMessage = pTemp;
pTemp = NULL;
if (fFirstMessage)
{
*pMessage = L'\0'; // Need so first wcscat_s call works
fFirstMessage = FALSE;
}
else
{
wcscat_s(pMessage, dwStringLen, L" "); // Space delimiter
}
wcscat_s(pMessage, dwStringLen, ((pMessages->pMessage) + i)->pwcsMessage);
}
else
{
wprintf(L"ERROR: realloc failed for GetKeywordName\n");
if (pMessage)
{
free(pMessage);
pMessage = NULL;
}
break;
}
}
}
return pMessage;
}
//--------------------------------------------------------------------------
// Use the EVT_PUBLISHER_METADATA_PROPERTY_ID enumeration values to enumerate all the
// provider's metadata excluding event metadata. Enumerates the metadata for channels,
// tasks, opcodes, levels, keywords, and the provider.
DWORD CProvidersExplorer::DumpProviderProperties(
EVT_HANDLE hMetadata,
DWORD PropIdFilter)
{
DWORD status = ERROR_SUCCESS;
for (int Id = 0; Id < EvtPublisherMetadataPropertyIdEND; Id++)
{
if (PropIdFilter != -1 && Id != PropIdFilter)
continue;
PEVT_VARIANT pProperty = NULL;
status = GetMetadataProperty(
hMetadata,
EVT_PUBLISHER_METADATA_PROPERTY_ID(Id),
&pProperty);
if (status != ERROR_SUCCESS)
{
wprintf(L"ERROR: EvtGetPublisherMetadataProperty failed with %d\n", GetLastError());
return status;
}
status = DumpProviderProperty(
hMetadata,
Id,
pProperty);
if (pProperty != nullptr)
free(pProperty);
if (status != ERROR_SUCCESS)
break;
// Skip the type-specific IDs, so the loop doesn't fail. For channels, levels,
// opcodes, tasks, and keywords, you use EvtGetPublisherMetadataProperty
// to get a handle to an array of those objects. You would then use the type
// specific ID (for example, EvtPublisherMetadataLevelValue) to access the metadata from
// the array. Do not call EvtGetPublisherMetadataProperty with a type specific ID or it
// will fail. The switch statement increments to the end of the type specific IDs for
// each type.
switch (Id)
{
case EvtPublisherMetadataChannelReferences:
Id = EvtPublisherMetadataChannelReferenceMessageID;
break;
case EvtPublisherMetadataLevels:
Id = EvtPublisherMetadataLevelMessageID;
break;
case EvtPublisherMetadataOpcodes:
Id = EvtPublisherMetadataOpcodeMessageID;
break;
case EvtPublisherMetadataTasks:
Id = EvtPublisherMetadataTaskMessageID;
break;
case EvtPublisherMetadataKeywords:
Id = EvtPublisherMetadataKeywordMessageID;
break;
}
}
return status;
}
//--------------------------------------------------------------------------
DWORD CProvidersExplorer::GetMetadataProperty(
EVT_HANDLE hMetadata,
EVT_PUBLISHER_METADATA_PROPERTY_ID Id,
PEVT_VARIANT *ppProperty)
{
PEVT_VARIANT pProperty = NULL;
DWORD dwBufferSize = 0;
DWORD dwBufferUsed = 0;
DWORD status = ERROR_SUCCESS;
*ppProperty = nullptr;
// Get the metadata property. If the buffer is not big enough, reallocate the buffer.
if (!EvtGetPublisherMetadataProperty(
hMetadata,
Id,
0,
dwBufferSize,
pProperty,
&dwBufferUsed))
{
status = GetLastError();
if (status == ERROR_INSUFFICIENT_BUFFER)
{
dwBufferSize = dwBufferUsed;
PEVT_VARIANT pTemp = (PEVT_VARIANT)malloc(dwBufferSize);
if (pTemp != nullptr)
{
pProperty = pTemp;
EvtGetPublisherMetadataProperty(
hMetadata,
EVT_PUBLISHER_METADATA_PROPERTY_ID(Id),
0,
dwBufferSize,
pProperty,
&dwBufferUsed);
}
else
{
return ERROR_OUTOFMEMORY;
}
}
status = GetLastError();
if (status != ERROR_SUCCESS)
{
if (pProperty != nullptr)
free(pProperty);
return status;
}
}
*ppProperty = pProperty;
return ERROR_SUCCESS;
}
//--------------------------------------------------------------------------
// Print the metadata properties for the provider and the types that
// it defines or references.
DWORD CProvidersExplorer::DumpProviderProperty(
EVT_HANDLE hMetadata,
int Id,
PEVT_VARIANT pProperty)
{
UNREFERENCED_PARAMETER(hMetadata);
DWORD status = ERROR_SUCCESS;
DWORD dwArraySize = 0;
DWORD dwBlockSize = 0;
LPWSTR pMessage = NULL;
switch (Id)
{
case EvtPublisherMetadataPublisherGuid:
{
WCHAR wszProviderGuid[(sizeof(GUID)+8) * 2 *sizeof(WCHAR)];
StringFromGUID2(
*(pProperty->GuidVal),
wszProviderGuid,
sizeof(wszProviderGuid) / sizeof(WCHAR));
m_Out->TagText(
XML_GUID,
wszProviderGuid);
break;
}
case EvtPublisherMetadataResourceFilePath:
if (pProperty->Type != EvtVarTypeNull)
{
m_Out->TagText(
XML_RESOURCEFILEPATH,
pProperty->StringVal == nullptr ? L"" : pProperty->StringVal);
}
break;
case EvtPublisherMetadataParameterFilePath:
m_Out->TagText(
XML_PARAMETERFILEPATH,
(pProperty->Type == EvtVarTypeNull) ? L"" : pProperty->StringVal);
break;
case EvtPublisherMetadataMessageFilePath:
m_Out->TagText(
XML_MESSAGEFILEPATH,
(pProperty->Type == EvtVarTypeNull) || (pProperty->StringVal == nullptr) ? L"" : pProperty->StringVal);
break;
case EvtPublisherMetadataHelpLink:
m_Out->TagText(
XML_HELPLINK,
(pProperty->Type == EvtVarTypeNull) || (pProperty->StringVal == nullptr) ? L"" : pProperty->StringVal);
break;
// The message string ID is -1 if the provider element does not specify the message attribute.
case EvtPublisherMetadataPublisherMessageID:
if (pProperty->UInt32Val == -1)
{
pMessage = nullptr;
}
else
{
pMessage = GetMessageString(
m_hMetadata,
pProperty->UInt32Val);
}
m_Out->TagText(
XML_PUBLISHER_MESSAGE,
pMessage == nullptr ? L"" : pMessage);
if (pMessage != nullptr)
free(pMessage);
break;
// We got the handle to all the channels defined in the channels section