From df88359acb1cd100d7d7c3db11ac79cbba8f9b3b Mon Sep 17 00:00:00 2001 From: Cameron Rowe Date: Fri, 13 Sep 2019 20:36:05 -0700 Subject: [PATCH 1/2] Added ability to specify a namespace when creating a connection --- Cargo.toml | 2 +- src/connection.rs | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4601118..0a8fd91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wmi" -version = "0.4.4" +version = "0.4.5" authors = ["Ohad Ravid "] edition = "2018" license = "MIT OR Apache-2.0" diff --git a/src/connection.rs b/src/connection.rs index a9fb4e5..1b79e78 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -90,7 +90,17 @@ pub struct WMIConnection { /// Currently does not support remote providers (e.g connecting to other computers). /// impl WMIConnection { + /// Creates a connection with a default `CIMV2` namespace path. pub fn new(com_lib: Rc) -> Result { + Self::with_namespace_path("ROOT\\CIMV2", com_lib) + } + + /// Creates a connection with the given namespace path. + /// + /// ```edition2018 + /// let wmi_con = WMIConnection::with_namespace_path("ROOT\\Microsoft\\Windows\\Storage", com_con.into())?; + /// ``` + pub fn with_namespace_path(namespace_path: &str, com_lib: Rc) -> Result { let mut instance = Self { com_con: com_lib, p_loc: None, @@ -99,7 +109,7 @@ impl WMIConnection { instance.create_locator()?; - instance.create_services()?; + instance.create_services(namespace_path)?; instance.set_proxy()?; @@ -136,13 +146,12 @@ impl WMIConnection { Ok(()) } - fn create_services(&mut self) -> Result<(), Error> { + fn create_services(&mut self, path: &str) -> Result<(), Error> { debug!("Calling ConnectServer"); let mut p_svc = ptr::null_mut::(); - let object_path = "ROOT\\CIMV2"; - let object_path_bstr = WideCString::from_str(object_path)?; + let object_path_bstr = WideCString::from_str(path)?; unsafe { check_hres((*self.loc()).ConnectServer( From 609552b69ce86e1bbdb61c669e1c3f963707a63a Mon Sep 17 00:00:00 2001 From: Cameron Rowe Date: Fri, 13 Sep 2019 20:59:01 -0700 Subject: [PATCH 2/2] Fixed failure in test --- src/connection.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/connection.rs b/src/connection.rs index 1b79e78..c671d4b 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -98,7 +98,12 @@ impl WMIConnection { /// Creates a connection with the given namespace path. /// /// ```edition2018 - /// let wmi_con = WMIConnection::with_namespace_path("ROOT\\Microsoft\\Windows\\Storage", com_con.into())?; + /// # fn main() -> Result<(), failure::Error> { + /// # use wmi::*; + /// # use serde::Deserialize; + /// let wmi_con = WMIConnection::with_namespace_path("ROOT\\Microsoft\\Windows\\Storage", COMLibrary::new()?.into())?; + /// # Ok(()) + /// # } /// ``` pub fn with_namespace_path(namespace_path: &str, com_lib: Rc) -> Result { let mut instance = Self {