diff --git a/Cargo.toml b/Cargo.toml index dc1bd8d..bb4f7f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "thound" -version = "0.1.0" +version = "0.1.1" authors = ["Denys Mentiei "] description = "Finds the folders with VC toolchain and Windows SDK" license = "MIT" diff --git a/README.md b/README.md index 0ad4676..32ff955 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Add `thound` to your `Cargo.toml`: ``` toml [dependencies] -thound = "0.1.0" +thound = "0.1" ``` ## Usage diff --git a/src/lib.rs b/src/lib.rs index d77f3e9..d191122 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -123,11 +123,7 @@ pub fn find_vc_and_windows_sdk() -> Option { } fn find_vc_toolchain() -> Option { - let hr = unsafe { CoInitializeEx(null_mut(), COINIT_MULTITHREADED) }; - // S_FALSE means COM has been already initialized. - if hr != S_OK && hr != S_FALSE { - return None; - } + let _com = ComScope::start()?; let mut config = null_mut(); let status = unsafe { diff --git a/src/winapi.rs b/src/winapi.rs index 0039ed0..2e6e765 100644 --- a/src/winapi.rs +++ b/src/winapi.rs @@ -107,6 +107,7 @@ extern "stdcall" { #[link(name = "Ole32")] extern "stdcall" { pub fn CoInitializeEx(pvReserved: LPVOID, dwCoInit: DWORD) -> HRESULT; + pub fn CoUninitialize(); pub fn CoCreateInstance( rclsid: REFCLSID, pUnkOuter: LPUNKNOWN, @@ -400,3 +401,29 @@ pub(crate) fn reg_query_string_value(key: &RegKey, sub_key: &str) -> Option Option { + let hr = unsafe { CoInitializeEx(null_mut(), COINIT_MULTITHREADED) }; + // S_FALSE means COM has been already initialized. + if hr != S_OK && hr != S_FALSE { + return None; + } + + Some(ComScope) + } +} + +impl Drop for ComScope { + fn drop(&mut self) { + // A thread must call CoUninitialize once for each successful + // call it has made to the CoInitialize or CoInitializeEx + // function, including any call that returns S_FALSE. + unsafe { CoUninitialize() }; + } +}