-
Notifications
You must be signed in to change notification settings - Fork 30
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
35d9c57
Update to windows 0.58
Jasper-Bekkers 06aecd8
Compile tests
Jasper-Bekkers 6271aa6
Feedback
Jasper-Bekkers 7443a52
Use `IUnknown::from_raw_borrowed` to avoid double free
ohadravid 6bfcdda
To be safe, clone and leak the `BSTR`
ohadravid c4ba53a
fmt
Jasper-Bekkers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
||
|
@@ -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)? | ||
|
@@ -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) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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 }; | ||
|
@@ -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 => { | ||
|
@@ -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) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤯