Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for executing WMI methods #104

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

TechnoPorg
Copy link

@TechnoPorg TechnoPorg commented Dec 23, 2024

WMIConnection now supports calling both instance and class methods with an idiomatic Rust API that automatically serializes and deserializes to/from Rust structs.

The WMI method calling code is based on the example from the windows-rs crate: microsoft/windows-rs#2794

Comments and documentation are still WIP, and there is an awful hack in struct serialization that I'd like to get rid of, which is why this PR is currently a draft.

Closes #18
Closes #58

- Adds conversion function from wmi-rs' Variant to windows-rs' VARIANT
- Adds custom serializer to take a struct and convert it to a HashMap of (field name, field value) pairs
- Can execute either instance or class methods, but instance methods require the __PATH of the WMI object.
- Rust struct conversion to and from IWbemClassObjects is handled by VariantStructSerializer
src/ser/variant_ser.rs Outdated Show resolved Hide resolved
This resolves the unsafe memory hack that was previously being used, and makes the technique much more scalable.
@TechnoPorg
Copy link
Author

After looking at how serde_json does it, I pushed a fix to serialize fields much more cleanly by calling serialize on individual keys to isolate behaviour for each supported type.
I feel much more comfortable with the state of the PR now :)

Copy link
Owner

@ohadravid ohadravid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi TechnoPorg 👋

Thanks for working on this! This is a great addition to the crate and the impl looks very good.

I left some minor comments, the bigger stuff are:

  1. Process create test seems flaky on my machine
  2. Consider exposing exec_method_native_wrapper
  3. Consider allowing In = () / Out = () (by default?)

Again, thank you for taking the time to implement this, great work!

src/method.rs Show resolved Hide resolved
src/variant.rs Outdated Show resolved Hide resolved
src/ser/variant_ser.rs Show resolved Hide resolved
src/method.rs Show resolved Hide resolved
src/method.rs Outdated Show resolved Hide resolved
src/method.rs Outdated Show resolved Hide resolved
src/ser/variant_ser.rs Show resolved Hide resolved
src/method.rs Outdated
}

#[test]
fn it_exec_instance_method() {
Copy link
Owner

@ohadravid ohadravid Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running locally, this seems to be flaky for some reason:

failures:

---- method::tests::it_exec_class_method stdout ----
thread 'method::tests::it_exec_class_method' panicked at src\method.rs:162:14:
called `Result::unwrap()` on an `Err` value: SerdeError("invalid type: Option value, expected u32")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- method::tests::it_exec_instance_method stdout ----
thread 'method::tests::it_exec_instance_method' panicked at src\method.rs:177:14:
called `Result::unwrap()` on an `Err` value: SerdeError("invalid type: Option value, expected u32")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran it a few more times on my machine and was able to reproduce this. I'm not sure what's causing it, but the process create method is failing with the generic error code 8 ("unknown failure").

This seems to be happening totally unpredictably, so I don't believe it's tied to this implementation, but I haven't had the chance to test whether it occurs with WMI bindings in other languages. We could ignore the test, or rework it to use another WMI method instead.

Cargo.toml Outdated Show resolved Hide resolved
src/method.rs Outdated Show resolved Hide resolved
@TechnoPorg TechnoPorg marked this pull request as ready for review December 28, 2024 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IWbemServices::ExecMethod support for WMIConnection Support for methods in IWbemClassWrapper
2 participants