From 2ed678380e72edb9636a10baad35a4d3b91eb4c2 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Thu, 25 Jan 2024 10:11:27 -0600 Subject: [PATCH] Drop Windows 7 --- crates/libs/core/src/error.rs | 6 +--- crates/libs/core/src/imp/bindings.rs | 5 ++++ crates/libs/core/src/imp/factory_cache.rs | 35 +++++++++-------------- crates/tests/standalone/src/b_none.rs | 10 ------- crates/tools/core/bindings.txt | 3 ++ 5 files changed, 23 insertions(+), 36 deletions(-) diff --git a/crates/libs/core/src/error.rs b/crates/libs/core/src/error.rs index 26f6a1336f..91eccbc040 100644 --- a/crates/libs/core/src/error.rs +++ b/crates/libs/core/src/error.rs @@ -15,9 +15,7 @@ impl Error { /// point of failure. pub fn new(code: HRESULT, message: HSTRING) -> Self { unsafe { - if let Some(function) = crate::imp::delay_load::(s!("combase.dll"), s!("RoOriginateError")) { - function(code, std::mem::transmute_copy(&message)); - } + crate::imp::RoOriginateError(code.0, std::mem::transmute_copy(&message)); let info = GetErrorInfo().and_then(|e| e.cast()).ok(); Self { code, info } } @@ -153,8 +151,6 @@ impl std::fmt::Display for Error { impl std::error::Error for Error {} -type RoOriginateError = extern "system" fn(code: HRESULT, message: *mut std::ffi::c_void) -> i32; - fn GetErrorInfo() -> Result { unsafe { crate::imp::GetErrorInfo(0) } } diff --git a/crates/libs/core/src/imp/bindings.rs b/crates/libs/core/src/imp/bindings.rs index ffe27b497b..7ff017f0cc 100644 --- a/crates/libs/core/src/imp/bindings.rs +++ b/crates/libs/core/src/imp/bindings.rs @@ -1,6 +1,8 @@ // Bindings generated by `windows-bindgen` 0.52.0 #![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)] +::windows_targets::link!("api-ms-win-core-winrt-error-l1-1-0.dll" "system" fn RoOriginateError(error : HRESULT, message : HSTRING) -> BOOL); +::windows_targets::link!("api-ms-win-core-winrt-l1-1-0.dll" "system" fn RoGetActivationFactory(activatableclassid : HSTRING, iid : *const GUID, factory : *mut *mut ::core::ffi::c_void) -> HRESULT); ::windows_targets::link!("kernel32.dll" "system" fn CloseHandle(hobject : HANDLE) -> BOOL); ::windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes : *const SECURITY_ATTRIBUTES, bmanualreset : BOOL, binitialstate : BOOL, lpname : PCWSTR) -> HANDLE); ::windows_targets::link!("kernel32.dll" "system" fn EncodePointer(ptr : *const ::core::ffi::c_void) -> *mut ::core::ffi::c_void); @@ -14,6 +16,7 @@ ::windows_targets::link!("kernel32.dll" "system" fn LoadLibraryExA(lplibfilename : PCSTR, hfile : HANDLE, dwflags : LOAD_LIBRARY_FLAGS) -> HMODULE); ::windows_targets::link!("kernel32.dll" "system" fn SetEvent(hevent : HANDLE) -> BOOL); ::windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WAIT_EVENT); +::windows_targets::link!("ole32.dll" "system" fn CoIncrementMTAUsage(pcookie : *mut CO_MTA_USAGE_COOKIE) -> HRESULT); ::windows_targets::link!("ole32.dll" "system" fn CoTaskMemAlloc(cb : usize) -> *mut ::core::ffi::c_void); ::windows_targets::link!("ole32.dll" "system" fn CoTaskMemFree(pv : *const ::core::ffi::c_void)); ::windows_targets::link!("ole32.dll" "system" fn PropVariantClear(pvar : *mut PROPVARIANT) -> HRESULT); @@ -347,6 +350,7 @@ impl ::core::clone::Clone for CLIPDATA { *self } } +pub type CO_MTA_USAGE_COOKIE = isize; #[repr(C)] pub union CY { pub Anonymous: CY_0, @@ -543,6 +547,7 @@ pub type HANDLE = isize; pub type HEAP_FLAGS = u32; pub type HMODULE = isize; pub type HRESULT = i32; +pub type HSTRING = *mut ::core::ffi::c_void; #[repr(C)] pub struct IDLDESC { pub dwReserved: usize, diff --git a/crates/libs/core/src/imp/factory_cache.rs b/crates/libs/core/src/imp/factory_cache.rs index d0202dbf6e..42ba806af1 100644 --- a/crates/libs/core/src/imp/factory_cache.rs +++ b/crates/libs/core/src/imp/factory_cache.rs @@ -53,26 +53,21 @@ pub fn factory() -> crate::Result { let mut factory: Option = None; let name = crate::HSTRING::from(C::NAME); - let code = if let Some(function) = unsafe { delay_load::(crate::s!("combase.dll"), crate::s!("RoGetActivationFactory")) } { - unsafe { - let mut code = function(std::mem::transmute_copy(&name), &I::IID, &mut factory as *mut _ as *mut _); - - // If RoGetActivationFactory fails because combase hasn't been loaded yet then load combase - // automatically so that it "just works" for apartment-agnostic code. - if code == CO_E_NOTINITIALIZED { - if let Some(mta) = delay_load::(crate::s!("ole32.dll"), crate::s!("CoIncrementMTAUsage")) { - let mut cookie = std::ptr::null_mut(); - let _ = mta(&mut cookie); - } - - // Now try a second time to get the activation factory via the OS. - code = function(std::mem::transmute_copy(&name), &I::IID, &mut factory as *mut _ as *mut _); - } - - code + let code = unsafe { + let mut get_com_factory = || crate::HRESULT(RoGetActivationFactory(std::mem::transmute_copy(&name), &I::IID as *const _ as _, &mut factory as *mut _ as *mut _)); + let mut code = get_com_factory(); + + // If RoGetActivationFactory fails because combase hasn't been loaded yet then load combase + // automatically so that it "just works" for apartment-agnostic code. + if code == CO_E_NOTINITIALIZED { + let mut cookie = 0; + CoIncrementMTAUsage(&mut cookie); + + // Now try a second time to get the activation factory via the OS. + code = get_com_factory(); } - } else { - CLASS_E_CLASSNOTAVAILABLE + + code }; // If this succeeded then return the resulting factory interface. @@ -124,8 +119,6 @@ unsafe fn get_activation_factory(library: crate::PCSTR, name: &crate::HSTRING) - function(std::mem::transmute_copy(name), &mut abi).from_abi(abi) } -type CoIncrementMTAUsage = extern "system" fn(cookie: *mut *mut std::ffi::c_void) -> crate::HRESULT; -type RoGetActivationFactory = extern "system" fn(hstring: *mut std::ffi::c_void, interface: &crate::GUID, result: *mut *mut std::ffi::c_void) -> crate::HRESULT; type DllGetActivationFactory = extern "system" fn(name: *mut std::ffi::c_void, factory: *mut *mut std::ffi::c_void) -> crate::HRESULT; #[cfg(test)] diff --git a/crates/tests/standalone/src/b_none.rs b/crates/tests/standalone/src/b_none.rs index 037d27a448..e69de29bb2 100644 --- a/crates/tests/standalone/src/b_none.rs +++ b/crates/tests/standalone/src/b_none.rs @@ -1,10 +0,0 @@ -// Bindings generated by `windows-bindgen` 0.52.0 - -#![allow( - non_snake_case, - non_upper_case_globals, - non_camel_case_types, - dead_code, - clippy::all -)] -::windows_targets::link!("kernel32.dll" "system" fn GetTickCount() -> u32); diff --git a/crates/tools/core/bindings.txt b/crates/tools/core/bindings.txt index 4b1c71cb4c..b459faecb0 100644 --- a/crates/tools/core/bindings.txt +++ b/crates/tools/core/bindings.txt @@ -11,6 +11,7 @@ Windows.Win32.Foundation.SysAllocStringLen Windows.Win32.Foundation.SysFreeString Windows.Win32.Foundation.SysStringLen + Windows.Win32.System.Com.CoIncrementMTAUsage Windows.Win32.System.Com.CoTaskMemAlloc Windows.Win32.System.Com.CoTaskMemFree Windows.Win32.System.Com.StructuredStorage.PROPVARIANT @@ -67,3 +68,5 @@ Windows.Win32.System.Variant.VT_UI4 Windows.Win32.System.Variant.VT_UI8 Windows.Win32.System.Variant.VT_UNKNOWN + Windows.Win32.System.WinRT.RoGetActivationFactory + Windows.Win32.System.WinRT.RoOriginateError