Skip to content

Commit

Permalink
Merge pull request #31 from ohadravid/use-com-crate
Browse files Browse the repository at this point in the history
Use com crate to implement async feature
  • Loading branch information
ohadravid authored Mar 10, 2021
2 parents a8d5b62 + a7d27d3 commit 5d589aa
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 124 deletions.
8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ default-target = "x86_64-pc-windows-msvc"

[features]
test = ["lazy_static", "async-std"]
async-query = ["com-impl", "wio", "async-channel", "futures"]
async-query = ["async-channel", "futures", "com"]

[target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3.9", features = ["objbase", "wbemcli", "objidlbase", "oaidl", "oleauto", "errhandlingapi", "wtypes", "wtypesbase"] }
Expand All @@ -29,12 +29,10 @@ serde = { version = "1.0", features = ["derive"] }
chrono = { version = "0.4", features = ["serde"] }
lazy_static = { version = "1.4.0", optional = true }
thiserror = "^1"
# dependencies for async queries
com-impl = { git = "https://github.com/Connicpu/com-impl", optional = true}
wio = {version = "0.2.2", optional = true}
async-channel = {version = "1.5.1", optional = true}
async-std = { version = "1.8.0", features = ["attributes"], optional = true }
futures = {version = "0.3", optional = true}
futures = { version = "0.3", optional = true}
com = { version = "0.4", features = ["production"], optional = true }

[dev-dependencies]
lazy_static = "1.4.0"
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ steps:
displayName: Install rust (windows)
condition: eq( variables['Agent.OS'], 'Windows_NT' )
- script: cargo build --release
- script: cargo build --all-features
displayName: Cargo build

- script: cargo test --all-features
Expand Down
26 changes: 14 additions & 12 deletions src/async_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ use crate::{
utils::check_hres,
WMIResult,
};
use crate::query_sink::QuerySink;
use crate::query_sink::{QuerySink, IWbemObjectSink};
use std::{collections::HashMap, ptr};
use com::production::ClassAllocation;
use com::AbiTransferable;
use winapi::{
um::{
wbemcli::IWbemObjectSink,
wbemcli::{
WBEM_FLAG_BIDIRECTIONAL,
},
},
};
use wio::com::ComPtr;
use serde::de;
use futures::stream::{Stream, TryStreamExt, StreamExt};

Expand All @@ -48,20 +48,22 @@ impl WMIConnection {
let query = BStr::from_str(query.as_ref())?;

let (tx, rx) = async_channel::unbounded();
// ComPtr keeps an internal RefCount with initial value = 1
let p_sink: ComPtr<IWbemObjectSink> = QuerySink::new(tx);

// The internal RefCount has initial value = 1.
let p_sink: ClassAllocation<QuerySink> = QuerySink::allocate(Some(tx));
let p_sink_handel = p_sink.query_interface::<IWbemObjectSink>().unwrap();

unsafe {
// As p_sink.RefCount = 1 before this call,
// As p_sink's RefCount = 1 before this call,
// p_sink won't be dropped at the end of ExecQueryAsync
check_hres((*self.svc()).ExecQueryAsync(
query_language.as_bstr(),
query.as_bstr(),
WBEM_FLAG_BIDIRECTIONAL as i32,
ptr::null_mut(),
p_sink.as_raw(),
p_sink_handel.get_abi().as_ptr() as * mut _,
))?;
}

Ok(rx)
}

Expand Down Expand Up @@ -158,7 +160,7 @@ mod tests {
use futures::stream::StreamExt;

#[async_std::test]
async fn _async_it_works_async() {
async fn async_it_works_async() {
let wmi_con = wmi_con();

let result = wmi_con
Expand All @@ -171,7 +173,7 @@ mod tests {
}

#[async_std::test]
async fn _async_it_handles_invalid_query() {
async fn async_it_handles_invalid_query() {
let wmi_con = wmi_con();

let result = wmi_con
Expand All @@ -180,11 +182,11 @@ mod tests {
.collect::<Vec<_>>()
.await;

assert_eq!(result.len(), 0);
assert_eq!(result.len(), 0);
}

#[async_std::test]
async fn _async_it_provides_raw_query_result() {
async fn async_it_provides_raw_query_result() {
let wmi_con = wmi_con();

let results: Vec<HashMap<String, Variant>> =
Expand Down
2 changes: 1 addition & 1 deletion src/de/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::de::{self, Deserialize, Deserializer, Visitor};
use serde::forward_to_deserialize_any;

/// Return the fields of a struct.
/// Taken directly from https://github.com/serde-rs/serde/issues/1110
/// Taken directly from <https://github.com/serde-rs/serde/issues/1110>
///
pub fn struct_name_and_fields<'de, T>(
) -> Result<(&'static str, &'static [&'static str]), serde::de::value::Error>
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
//! # Internals
//!
//! [`WMIConnection`](WMIConnection) is used to create and execute a WMI query, returning
//! [`IWbemClassWrapper`](query::IWbemClassWrapper) which is a wrapper for a WMI object pointer.
//! [`IWbemClassWrapper`](result_enumerator::IWbemClassWrapper) which is a wrapper for a WMI object pointer.
//!
//! Then, [`from_wbem_class_obj`](de::wbem_class_de::from_wbem_class_obj) is used to create a Rust struct with the equivalent data.
//!
Expand Down
6 changes: 4 additions & 2 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ impl WMIConnection {
))?;
}

let pcls_wrapper = unsafe { IWbemClassWrapper::new(NonNull::new(pcls_obj)) };
let pcls_ptr = NonNull::new(pcls_obj).ok_or(WMIError::NullPointerResult)?;

let pcls_wrapper = unsafe { IWbemClassWrapper::new(pcls_ptr) };

Ok(pcls_wrapper)
}
Expand Down Expand Up @@ -435,7 +437,7 @@ impl WMIConnection {
/// Query all the associators of type T of the given object.
/// The `object_path` argument can be provided by querying an object wih it's `__Path` property.
/// `AssocClass` must be have the name as the conneting association class between the original object and the results.
/// See https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-diskdrivetodiskpartition for example.
/// See <https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/win32-diskdrivetodiskpartition> for example.
///
/// ```edition2018
/// # fn main() -> Result<(), wmi::WMIError> {
Expand Down
Loading

0 comments on commit 5d589aa

Please sign in to comment.