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
use serde::{Serialize, de};
use windows::core::BSTR;
use crate::{
Variant, WMIConnection, WMIError, WMIResult, de::meta::struct_name_and_fields,
result_enumerator::IWbemClassWrapper, ser::variant_ser::VariantSerializer,
};
impl WMIConnection {
/// Wrapper for WMI's [ExecMethod](https://learn.microsoft.com/en-us/windows/win32/api/wbemcli/nf-wbemcli-iwbemservices-execmethod) function.
///
/// This function is used internally by [`WMIConnection::exec_class_method`] and [`WMIConnection::exec_instance_method`],
/// which are a higher-level abstraction, dealing with Rust data types instead of raw Variants, that should be preferred to use.
///
/// In the case of a class ("static") method, `object_path` should be name of the class.
///
/// Returns `None` if the method has no out parameters and a `void` return type, and an [`IWbemClassWrapper`] containing the output otherwise.
/// A method with a return type other than `void` will always have a generic property named `ReturnValue` in the output class wrapper with the return value of the WMI method call.
///
/// ```edition2021
/// # use std::collections::HashMap;
/// # use wmi::{Variant, WMIConnection, WMIResult};
/// # fn main() -> WMIResult<()> {
/// # let wmi_con = WMIConnection::new()?;
/// let in_params = wmi_con
/// .get_object("Win32_Process")?
/// .get_method("Create")?
/// .unwrap()
/// .spawn_instance()?;
/// in_params.put_property("CommandLine", "explorer.exe".to_string())?;
///
/// // Because Create has a return value and out parameters, the Option returned will never be None.
/// // Note: The Create call can be unreliable, so consider using another means of starting processes.
/// let out = wmi_con.exec_method("Win32_Process", "Create", Some(&in_params))?.unwrap();
/// println!("The return code of the Create call is {:?}", out.get_property("ReturnValue")?);
/// # Ok(())
/// # }
/// ```
pub fn exec_method(
&self,
object_path: impl AsRef<str>,
method: impl AsRef<str>,
in_params: Option<&IWbemClassWrapper>,
) -> WMIResult<Option<IWbemClassWrapper>> {
let object_path = BSTR::from(object_path.as_ref());
// In the case of a method with no out parameters and a VOID return type, there will be no out-parameters object
let method = BSTR::from(method.as_ref());
let mut output = None;
unsafe {
self.svc.ExecMethod(
&object_path,
&method,
Default::default(),
&self.ctx.0,
in_params.as_ref().map(|param| ¶m.inner),
Some(&mut output),
None,
)?;
}
Ok(output.map(IWbemClassWrapper::new))
}
/// Executes a method of a WMI class not tied to any specific instance. Examples include
/// [Create](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/create-method-in-class-win32-process) of `Win32_Process`
/// and [AddPrinterConnection](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/addprinterconnection-method-in-class-win32-printer) of `Win32_Printer`.
///
/// A method with a return type other than `void` will always try to populate a generic property named `ReturnValue` in the output object with the return value of the WMI method call.
/// If the method call has a `void` return type and no out parameters, the only acceptable type for `Out` is `()`.
///
/// Arrays, Options, unknowns, and nested objects cannot be passed as input parameters due to limitations in how variants are constructed by `windows-rs`.
///
/// This function uses [`WMIConnection::exec_method`] internally, with the name of the method class being the instance path, as is expected by WMI.
///
/// ```edition2021
/// # use serde::{Deserialize, Serialize};
/// # use wmi::{Variant, WMIConnection, WMIResult};
/// #[derive(Serialize)]
/// # #[allow(non_snake_case)]
/// struct CreateInput {
/// CommandLine: String
/// }
///
/// #[derive(Deserialize)]
/// # #[allow(non_snake_case)]
/// struct CreateOutput {
/// ReturnValue: u32,
/// ProcessId: u32
/// }
///
/// #[derive(Deserialize)]
/// # #[allow(non_camel_case_types)]
/// struct Win32_Process;
///
/// # fn main() -> WMIResult<()> {
/// # let wmi_con = WMIConnection::new()?;
/// // Note: The Create call can be unreliable, so consider using another means of starting processes.
/// let input = CreateInput {
/// CommandLine: "explorer.exe".to_string()
/// };
/// let output: CreateOutput = wmi_con
/// .exec_class_method::<Win32_Process, _>("Create", input)?;
///
/// println!("The return code of the Create call is {}", output.ReturnValue);
/// println!("The ID of the created process is: {}", output.ProcessId);
/// # Ok(())
/// # }
/// ```
pub fn exec_class_method<Class, Out>(
&self,
method: impl AsRef<str>,
in_params: impl Serialize,
) -> WMIResult<Out>
where
Class: de::DeserializeOwned,
Out: de::DeserializeOwned,
{
let (class, _) = struct_name_and_fields::<Class>()?;
self.exec_instance_method::<Class, _>(class, method, in_params)
}
/// Executes a WMI method on a specific instance of a class. Examples include
/// [GetSupportedSize](https://learn.microsoft.com/en-us/windows-hardware/drivers/storage/msft-Volume-getsupportedsizes) of `MSFT_Volume`
/// and [Pause](https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/pause-method-in-class-win32-printer) of `Win32_Printer`.
///
/// `object_path` is the `__Path` variable of the class instance on which the method is being called, which can be obtained from a WMI query.
///
/// A method with a return type other than `void` will always try to populate a generic property named `ReturnValue` in the output object with the return value of the WMI method call.
/// If the method call has a `void` return type and no out parameters, the only acceptable type for `Out` is `()`.
///
/// Arrays, Options, unknowns, and nested objects cannot be passed as input parameters due to limitations in how variants are constructed by `windows-rs`.
///
/// ```edition2021
/// # use serde::{Deserialize, Serialize};
/// # use wmi::{FilterValue, Variant, WMIConnection, WMIResult};
/// #[derive(Deserialize)]
/// # #[allow(non_snake_case)]
/// struct PrinterOutput {
/// ReturnValue: u32
/// }
///
/// #[derive(Deserialize)]
/// # #[allow(non_camel_case_types, non_snake_case)]
/// struct Win32_Printer {
/// __Path: String
/// }
///
/// # fn main() -> WMIResult<()> {
/// # let wmi_con = WMIConnection::new()?;
/// let printers: Vec<Win32_Printer> = wmi_con.query()?;
///
/// for printer in printers {
/// let output: PrinterOutput = wmi_con.exec_instance_method::<Win32_Printer, _>(&printer.__Path, "Pause", ())?;
/// println!("Pausing the printer returned {}", output.ReturnValue);
///
/// let output: PrinterOutput = wmi_con.exec_instance_method::<Win32_Printer, _>(&printer.__Path, "Resume", ())?;
/// println!("Resuming the printer returned {}", output.ReturnValue);
/// }
/// # Ok(())
/// # }
/// ```
pub fn exec_instance_method<Class, Out>(
&self,
object_path: impl AsRef<str>,
method: impl AsRef<str>,
in_params: impl Serialize,
) -> WMIResult<Out>
where
Class: de::DeserializeOwned,
Out: de::DeserializeOwned,
{
let (class, _) = struct_name_and_fields::<Class>()?;
let method = method.as_ref();
// See https://learn.microsoft.com/en-us/windows/win32/api/wbemcli/nf-wbemcli-iwbemclassobject-getmethod
// GetMethod can only be called on a class definition, so we retrieve that before retrieving a specific object.
let instance = match self.get_object(class)?.get_method(method)? {
None => None,
Some(method_class) => {
let instance = method_class.spawn_instance()?;
let serializer = VariantSerializer {
wmi: self,
instance: Some(instance),
};
match in_params.serialize(serializer) {
Ok(Variant::Object(instance)) => Some(instance),
Ok(other) => {
return Err(WMIError::ConvertVariantError(format!(
"Unexpected serializer output: {:?}",
other
)));
}
Err(e) => return Err(WMIError::ConvertVariantError(e.to_string())),
}
}
};
let output = self.exec_method(object_path, method, instance.as_ref())?;
match output {
Some(class_wrapper) => Ok(class_wrapper.into_desr()?),
None => Out::deserialize(Variant::Empty),
}
}
}
#[cfg(test)]
mod tests {
use crate::Variant;
use crate::tests::fixtures::wmi_con;
use serde::{Deserialize, Serialize};
use std::thread::sleep;
use std::time::Duration;
#[derive(Deserialize)]
struct Win32_Process {
__Path: String,
HandleCount: u32,
}
#[derive(Debug, Serialize, Default)]
pub struct Win32_ProcessStartup {
CreateFlags: u32,
}
#[derive(Serialize)]
struct CreateInput {
CommandLine: String,
ProcessStartupInformation: Win32_ProcessStartup,
}
#[derive(Deserialize)]
struct CreateOutput {
ReturnValue: u32,
ProcessId: u32,
}
#[test]
fn it_exec_methods_native() {
let wmi_con = wmi_con();
let in_params = wmi_con
.get_object("Win32_Process")
.unwrap()
.get_method("Create")
.unwrap()
.unwrap()
.spawn_instance()
.unwrap();
in_params
.put_property("CommandLine", "explorer.exe".to_string())
.unwrap();
let out = wmi_con
.exec_method("Win32_Process", "Create", Some(&in_params))
.unwrap();
let return_value = out.unwrap().get_property("ReturnValue").unwrap();
assert!(matches!(return_value, Variant::UI4(0)));
}
#[test]
fn it_exec_methods() {
let wmi_con = wmi_con();
const CREATE_SUSPENDED: u32 = 4;
let in_params = CreateInput {
CommandLine: "explorer.exe".to_string(),
ProcessStartupInformation: Win32_ProcessStartup {
CreateFlags: CREATE_SUSPENDED,
},
};
let out: CreateOutput = wmi_con
.exec_class_method::<Win32_Process, _>("Create", &in_params)
.unwrap();
assert_eq!(out.ReturnValue, 0);
let query = format!(
"SELECT * FROM Win32_Process WHERE ProcessId = {}",
out.ProcessId
);
let process = &wmi_con.raw_query::<Win32_Process>(&query).unwrap()[0];
// Since we started the process as suspended, it will not have any open handles.
assert_eq!(process.HandleCount, 0);
let _: () = wmi_con
.exec_instance_method::<Win32_Process, _>(&process.__Path, "Terminate", ())
.unwrap();
// It can take a moment for the process to terminate, so we retry the query a few times.
for _ in 0..10 {
if wmi_con.raw_query::<Win32_Process>(&query).unwrap().len() == 0 {
break;
}
sleep(Duration::from_millis(100));
}
assert!(wmi_con.raw_query::<Win32_Process>(&query).unwrap().len() == 0);
}
#[test]
fn it_exec_with_u8_arrays() {
let wmi_con = wmi_con();
#[derive(Deserialize)]
struct StdRegProv;
#[derive(Deserialize, Serialize)]
struct GetBinaryValue {
sSubKeyName: String,
sValueName: String,
}
#[derive(Deserialize)]
struct GetBinaryValueOut {
uValue: Vec<u8>,
}
let get_binary_value_params = GetBinaryValue {
sSubKeyName: r#"SYSTEM\CurrentControlSet\Control\Windows"#.to_string(),
sValueName: "FullProcessInformationSID".to_string(),
};
let value: GetBinaryValueOut = wmi_con
.exec_class_method::<StdRegProv, _>("GetBinaryValue", &get_binary_value_params)
.unwrap();
assert!(value.uValue.len() > 0, "Expected to get a non-empty value");
#[derive(Deserialize, Serialize)]
struct SetBinaryValue {
sSubKeyName: String,
sValueName: String,
uValue: Vec<u8>,
}
#[derive(Deserialize)]
struct SetBinaryValueOut {
ReturnValue: u32,
}
let test_value_name = format!("{}.test", get_binary_value_params.sValueName);
let test_value = vec![0, 1, 2, 3];
let set_binary_value_params = SetBinaryValue {
sSubKeyName: get_binary_value_params.sSubKeyName,
sValueName: test_value_name,
uValue: test_value,
};
let value: SetBinaryValueOut = wmi_con
.exec_class_method::<StdRegProv, _>("SetBinaryValue", &set_binary_value_params)
.unwrap();
assert_eq!(value.ReturnValue, 0);
let get_test_binary_value_params = GetBinaryValue {
sSubKeyName: set_binary_value_params.sSubKeyName,
sValueName: set_binary_value_params.sValueName,
};
let value: GetBinaryValueOut = wmi_con
.exec_class_method::<StdRegProv, _>("GetBinaryValue", &get_test_binary_value_params)
.unwrap();
assert_eq!(value.uValue, set_binary_value_params.uValue);
wmi_con
.exec_class_method::<StdRegProv, ()>("DeleteValue", &get_test_binary_value_params)
.unwrap();
}
#[test]
fn it_exec_with_object_arrays() {
let wmi_con = wmi_con();
#[derive(Deserialize)]
struct StdRegProv;
#[derive(Serialize)]
struct GetSecurityDescriptor {
sSubKeyName: String,
}
#[derive(Deserialize, Debug)]
struct __Trustee {
Name: String,
}
#[derive(Deserialize, Debug)]
struct __ACE {
Trustee: __Trustee,
}
#[derive(Deserialize, Debug)]
struct __SecurityDescriptor {
DACL: Vec<__ACE>,
}
#[derive(Deserialize, Debug)]
struct GetSecurityDescriptorOut {
Descriptor: Option<__SecurityDescriptor>,
}
let params = GetSecurityDescriptor {
sSubKeyName: r#"SECURITY"#.to_string(),
};
let value: GetSecurityDescriptorOut = wmi_con
.exec_class_method::<StdRegProv, _>("GetSecurityDescriptor", ¶ms)
.unwrap();
let dacl = value.Descriptor.unwrap().DACL;
assert!(dacl.len() > 0, "Expected to get a non-empty value");
assert!(dacl.iter().find(|ac| ac.Trustee.Name == "SYSTEM").is_some());
}
}