@@ -71,6 +71,26 @@ rb_winevt_query_alloc(VALUE klass)
71
71
return obj ;
72
72
}
73
73
74
+ static DWORD
75
+ get_evt_query_flag_from_cstr (char * flag_str )
76
+ {
77
+ if (strcmp (flag_str , "channel" ) == 0 )
78
+ return EvtQueryChannelPath ;
79
+ else if (strcmp (flag_str , "file" ) == 0 )
80
+ return EvtQueryFilePath ;
81
+ else if (strcmp (flag_str , "forward" ) == 0 )
82
+ return EvtQueryForwardDirection ;
83
+ else if (strcmp (flag_str , "reverse" ) == 0 )
84
+ return EvtQueryReverseDirection ;
85
+ else if (strcmp (flag_str , "tolerate_query_errors" ) == 0 ||
86
+ strcmp (flag_str , "tolerate_errors" ) == 0 )
87
+ return EvtQueryTolerateQueryErrors ;
88
+ else
89
+ rb_raise (rb_eArgError , "Unknown query flag: %s" , flag_str );
90
+
91
+ return 0 ;
92
+ }
93
+
74
94
/*
75
95
* Initalize Query class.
76
96
*
@@ -85,15 +105,15 @@ static VALUE
85
105
rb_winevt_query_initialize (VALUE argc , VALUE * argv , VALUE self )
86
106
{
87
107
PWSTR evtChannel , evtXPath ;
88
- VALUE channel , xpath , session ;
108
+ VALUE channel , xpath , session , rb_flags ;
89
109
struct WinevtQuery * winevtQuery ;
90
110
struct WinevtSession * winevtSession ;
91
111
EVT_HANDLE hRemoteHandle = NULL ;
92
- DWORD len ;
112
+ DWORD len , flags = 0 ;
93
113
VALUE wchannelBuf , wpathBuf ;
94
114
DWORD err = ERROR_SUCCESS ;
95
115
96
- rb_scan_args (argc , argv , "21 " , & channel , & xpath , & session );
116
+ rb_scan_args (argc , argv , "22 " , & channel , & xpath , & session , & rb_flags );
97
117
Check_Type (channel , T_STRING );
98
118
Check_Type (xpath , T_STRING );
99
119
@@ -111,6 +131,23 @@ rb_winevt_query_initialize(VALUE argc, VALUE *argv, VALUE self)
111
131
}
112
132
}
113
133
134
+ switch (TYPE (rb_flags )) {
135
+ case T_SYMBOL :
136
+ flags = get_evt_query_flag_from_cstr (RSTRING_PTR (rb_sym2str (rb_flags )));
137
+ break ;
138
+ case T_STRING :
139
+ flags = get_evt_query_flag_from_cstr (StringValuePtr (rb_flags ));
140
+ break ;
141
+ case T_FIXNUM :
142
+ flags = NUM2LONG (rb_flags );
143
+ break ;
144
+ case T_NIL :
145
+ flags = EvtQueryChannelPath | EvtQueryTolerateQueryErrors ;
146
+ break ;
147
+ default :
148
+ rb_raise (rb_eArgError , "Expected a String, a Symbol, a Fixnum, or a NilClass instance" );
149
+ }
150
+
114
151
// channel : To wide char
115
152
len =
116
153
MultiByteToWideChar (CP_UTF8 , 0 , RSTRING_PTR (channel ), RSTRING_LEN (channel ), NULL , 0 );
@@ -128,7 +165,7 @@ rb_winevt_query_initialize(VALUE argc, VALUE *argv, VALUE self)
128
165
TypedData_Get_Struct (self , struct WinevtQuery , & rb_winevt_query_type , winevtQuery );
129
166
130
167
winevtQuery -> query = EvtQuery (
131
- hRemoteHandle , evtChannel , evtXPath , EvtQueryChannelPath | EvtQueryTolerateQueryErrors );
168
+ hRemoteHandle , evtChannel , evtXPath , flags );
132
169
err = GetLastError ();
133
170
if (err != ERROR_SUCCESS ) {
134
171
if (err == ERROR_EVT_CHANNEL_NOT_FOUND ) {
@@ -613,6 +650,37 @@ Init_winevt_query(VALUE rb_cEventLog)
613
650
* @see https://msdn.microsoft.com/en-us/windows/desktop/aa385575#EvtSeekStrict
614
651
*/
615
652
rb_define_const (rb_cFlag , "Strict" , LONG2NUM (EvtSeekStrict ));
653
+
654
+ /*
655
+ * EVT_QUERY_FLAGS enumeration: EvtQueryChannelPath
656
+ * @since 0.10.3
657
+ * @see https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_query_flags
658
+ */
659
+ rb_define_const (rb_cFlag , "ChannelPath" , LONG2NUM (EvtQueryChannelPath ));
660
+ /*
661
+ * EVT_QUERY_FLAGS enumeration: EvtQueryFilePath
662
+ * @since 0.10.3
663
+ * @see https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_query_flags
664
+ */
665
+ rb_define_const (rb_cFlag , "FilePath" , LONG2NUM (EvtQueryFilePath ));
666
+ /*
667
+ * EVT_QUERY_FLAGS enumeration: EvtQueryForwardDirection
668
+ * @since 0.10.3
669
+ * @see https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_query_flags
670
+ */
671
+ rb_define_const (rb_cFlag , "ForwardDirection" , LONG2NUM (EvtQueryForwardDirection ));
672
+ /*
673
+ * EVT_QUERY_FLAGS enumeration: EvtQueryReverseDirection
674
+ * @since 0.10.3
675
+ * @see https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_query_flags
676
+ */
677
+ rb_define_const (rb_cFlag , "ReverseDirection" , LONG2NUM (EvtQueryReverseDirection ));
678
+ /*
679
+ * EVT_QUERY_FLAGS enumeration: EvtSeekOriginMask
680
+ * @since 0.10.3
681
+ * @see https://learn.microsoft.com/en-us/windows/win32/api/winevt/ne-winevt-evt_query_flags
682
+ */
683
+ rb_define_const (rb_cFlag , "TolerateQueryErrors" , LONG2NUM (EvtQueryTolerateQueryErrors ));
616
684
/* clang-format on */
617
685
618
686
rb_define_method (rb_cQuery , "initialize" , rb_winevt_query_initialize , -1 );
0 commit comments