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

Update to windows 0.58 #98

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ default = ["chrono"]
test = []

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.52", features = [
windows-core = { version = "0.58" }
windows = { version = "0.58", features = [
"implement",
"Win32_Foundation",
"Win32_Security",
Expand Down
2 changes: 1 addition & 1 deletion src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl COMLibrary {
/// `CoInitialize`s the COM library for use by the calling thread, but without setting the security context.
///
pub fn without_security() -> WMIResult<Self> {
unsafe { CoInitializeEx(None, COINIT_MULTITHREADED)? }
unsafe { CoInitializeEx(None, COINIT_MULTITHREADED).ok()? }
Copy link
Owner

Choose a reason for hiding this comment

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

This will lose the original error code - would you mind adding the needed variant to the WMIError?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's slightly misleading but this calls https://docs.rs/windows-core/latest/windows_core/struct.HRESULT.html#method.ok which does retain the error code.

Copy link
Owner

Choose a reason for hiding this comment

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

🤯


let instance = Self {
_phantom: PhantomData,
Expand Down
4 changes: 2 additions & 2 deletions src/query_sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub struct QuerySink {
/// receives asynchronously the result of the query, through Indicate calls.
/// When finished,the SetStatus method is called.
/// # <https://docs.microsoft.com/fr-fr/windows/win32/wmisdk/example--getting-wmi-data-from-the-local-computer-asynchronously>
impl IWbemObjectSink_Impl for QuerySink {
impl IWbemObjectSink_Impl for QuerySink_Impl {
fn Indicate(
&self,
lObjectCount: i32,
Expand Down Expand Up @@ -209,7 +209,7 @@ mod tests {
use super::*;
use crate::tests::fixtures::*;
use futures::StreamExt;
use windows::core::{ComInterface, IUnknown, Interface};
use windows::core::{IUnknown, Interface};

#[async_std::test]
async fn async_it_should_send_result() {
Expand Down
3 changes: 2 additions & 1 deletion src/result_enumerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use serde::{
Serialize,
};
use std::ptr;
use windows::core::VARIANT;
use windows::Win32::System::Ole::SafeArrayDestroy;
use windows::Win32::System::Variant::{VariantClear, VARIANT};
use windows::Win32::System::Variant::VariantClear;
use windows::Win32::System::Wmi::{
IEnumWbemClassObject, IWbemClassObject, CIMTYPE_ENUMERATION, WBEM_FLAG_ALWAYS,
WBEM_FLAG_NONSYSTEM_ONLY, WBEM_INFINITE,
Expand Down
38 changes: 23 additions & 15 deletions src/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
};
use serde::Serialize;
use std::convert::TryFrom;
use windows::core::{ComInterface, IUnknown, BSTR};
use windows::Win32::Foundation::{VARIANT_FALSE, VARIANT_TRUE};
use windows::core::{IUnknown, Interface, BSTR, VARIANT};
use windows::Win32::Foundation::{VARIANT_BOOL, VARIANT_FALSE, VARIANT_TRUE};
use windows::Win32::System::Variant::*;
use windows::Win32::System::Wmi::{self, IWbemClassObject, CIMTYPE_ENUMERATION};

Expand Down Expand Up @@ -79,15 +79,19 @@ impl Variant {
///
/// This function is unsafe as it is the caller's responsibility to ensure that the VARIANT is correctly initialized.
pub fn from_variant(vt: &VARIANT) -> WMIResult<Variant> {
let vt = vt.as_raw();
let variant_type = unsafe { vt.Anonymous.Anonymous.vt };

// variant_type has two 'forms':
// 1. A simple type like `VT_BSTR` .
// 2. An array of certain type like `VT_ARRAY | VT_BSTR`.
if variant_type.0 & VT_ARRAY.0 == VT_ARRAY.0 {
let array = unsafe { vt.Anonymous.Anonymous.Anonymous.parray };
if variant_type & VT_ARRAY.0 == VT_ARRAY.0 {
let array = unsafe {
vt.Anonymous.Anonymous.Anonymous.parray
as *const windows::Win32::System::Com::SAFEARRAY
};

let item_type = VARENUM(variant_type.0 & VT_TYPEMASK.0);
let item_type = VARENUM(variant_type & VT_TYPEMASK.0);

return Ok(Variant::Array(unsafe {
safe_array_to_vec(&*array, item_type)?
Expand All @@ -97,11 +101,13 @@ impl Variant {
// See https://msdn.microsoft.com/en-us/library/cc237865.aspx for more info.
// Rust can infer the return type of `vt.*Val()` calls,
// but it's easier to read when the type is named explicitly.
let variant_value = match variant_type {
let variant_value = match VARENUM(variant_type) {
VT_BSTR => {
let bstr_ptr: &BSTR = unsafe { &vt.Anonymous.Anonymous.Anonymous.bstrVal };

Variant::String(bstr_ptr.try_into()?)
let bstr_ptr = unsafe { BSTR::from_raw(vt.Anonymous.Anonymous.Anonymous.bstrVal) };

Choose a reason for hiding this comment

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

Consider replacing this with

let bstr_borrow = ManuallyDrop::new(unsafe { BSTR::from_raw(vt.Anonymous.Anonymous.Anonymous.bstrVal) });
Variant::Sting(bstr_borrow.to_string())

This creates a non-owning BSTR from the get-go, so you do not have to remember and cannot forget to manually release ownership later.

let bstr_as_str = bstr_ptr.to_string();
// We don't want to be the ones freeing the BSTR.
let _ = bstr_ptr.into_raw();
Variant::String(bstr_as_str)
}
VT_I1 => {
let num = unsafe { vt.Anonymous.Anonymous.Anonymous.cVal };
Expand Down Expand Up @@ -136,10 +142,10 @@ impl Variant {
VT_BOOL => {
let value = unsafe { vt.Anonymous.Anonymous.Anonymous.boolVal };

match value {
match VARIANT_BOOL(value) {
VARIANT_FALSE => Variant::Bool(false),
VARIANT_TRUE => Variant::Bool(true),
_ => return Err(WMIError::ConvertBoolError(value.0)),
_ => return Err(WMIError::ConvertBoolError(value)),
}
}
VT_UI1 => {
Expand All @@ -165,11 +171,13 @@ impl Variant {
VT_EMPTY => Variant::Empty,
VT_NULL => Variant::Null,
VT_UNKNOWN => {
let unk = unsafe { &vt.Anonymous.Anonymous.Anonymous.punkVal };
let ptr = unk.as_ref().ok_or(WMIError::NullPointerResult)?;
Variant::Unknown(IUnknownWrapper::new(ptr.clone()))
let ptr = unsafe {
IUnknown::from_raw_borrowed(&vt.Anonymous.Anonymous.Anonymous.punkVal)
};
let ptr = ptr.cloned().ok_or(WMIError::NullPointerResult)?;
Variant::Unknown(IUnknownWrapper::new(ptr))
}
_ => return Err(WMIError::ConvertError(variant_type.0)),
_ => return Err(WMIError::ConvertError(variant_type)),
};

Ok(variant_value)
Expand Down
Loading