Skip to content

Commit fd997b9

Browse files
authored
Remove old Windows 7 support code (#2817)
1 parent 0674d7d commit fd997b9

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

crates/libs/core/src/error.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ impl Error {
1515
/// point of failure.
1616
pub fn new(code: HRESULT, message: HSTRING) -> Self {
1717
unsafe {
18-
if let Some(function) = crate::imp::delay_load::<RoOriginateError>(s!("combase.dll"), s!("RoOriginateError")) {
19-
function(code, std::mem::transmute_copy(&message));
20-
}
18+
crate::imp::RoOriginateError(code.0, std::mem::transmute_copy(&message));
2119
let info = GetErrorInfo().and_then(|e| e.cast()).ok();
2220
Self { code, info }
2321
}
@@ -153,8 +151,6 @@ impl std::fmt::Display for Error {
153151

154152
impl std::error::Error for Error {}
155153

156-
type RoOriginateError = extern "system" fn(code: HRESULT, message: *mut std::ffi::c_void) -> i32;
157-
158154
fn GetErrorInfo() -> Result<crate::imp::IErrorInfo> {
159155
unsafe { crate::imp::GetErrorInfo(0) }
160156
}

crates/libs/core/src/imp/bindings.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Bindings generated by `windows-bindgen` 0.52.0
22

33
#![allow(non_snake_case, non_upper_case_globals, non_camel_case_types, dead_code, clippy::all)]
4+
::windows_targets::link!("api-ms-win-core-winrt-error-l1-1-0.dll" "system" fn RoOriginateError(error : HRESULT, message : HSTRING) -> BOOL);
5+
::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);
46
::windows_targets::link!("kernel32.dll" "system" fn CloseHandle(hobject : HANDLE) -> BOOL);
57
::windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes : *const SECURITY_ATTRIBUTES, bmanualreset : BOOL, binitialstate : BOOL, lpname : PCWSTR) -> HANDLE);
68
::windows_targets::link!("kernel32.dll" "system" fn EncodePointer(ptr : *const ::core::ffi::c_void) -> *mut ::core::ffi::c_void);
@@ -14,6 +16,7 @@
1416
::windows_targets::link!("kernel32.dll" "system" fn LoadLibraryExA(lplibfilename : PCSTR, hfile : HANDLE, dwflags : LOAD_LIBRARY_FLAGS) -> HMODULE);
1517
::windows_targets::link!("kernel32.dll" "system" fn SetEvent(hevent : HANDLE) -> BOOL);
1618
::windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WAIT_EVENT);
19+
::windows_targets::link!("ole32.dll" "system" fn CoIncrementMTAUsage(pcookie : *mut CO_MTA_USAGE_COOKIE) -> HRESULT);
1720
::windows_targets::link!("ole32.dll" "system" fn CoTaskMemAlloc(cb : usize) -> *mut ::core::ffi::c_void);
1821
::windows_targets::link!("ole32.dll" "system" fn CoTaskMemFree(pv : *const ::core::ffi::c_void));
1922
::windows_targets::link!("ole32.dll" "system" fn PropVariantClear(pvar : *mut PROPVARIANT) -> HRESULT);
@@ -347,6 +350,7 @@ impl ::core::clone::Clone for CLIPDATA {
347350
*self
348351
}
349352
}
353+
pub type CO_MTA_USAGE_COOKIE = isize;
350354
#[repr(C)]
351355
pub union CY {
352356
pub Anonymous: CY_0,
@@ -543,6 +547,7 @@ pub type HANDLE = isize;
543547
pub type HEAP_FLAGS = u32;
544548
pub type HMODULE = isize;
545549
pub type HRESULT = i32;
550+
pub type HSTRING = *mut ::core::ffi::c_void;
546551
#[repr(C)]
547552
pub struct IDLDESC {
548553
pub dwReserved: usize,

crates/libs/core/src/imp/factory_cache.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,21 @@ pub fn factory<C: crate::RuntimeName, I: Interface>() -> crate::Result<I> {
5353
let mut factory: Option<I> = None;
5454
let name = crate::HSTRING::from(C::NAME);
5555

56-
let code = if let Some(function) = unsafe { delay_load::<RoGetActivationFactory>(crate::s!("combase.dll"), crate::s!("RoGetActivationFactory")) } {
57-
unsafe {
58-
let mut code = function(std::mem::transmute_copy(&name), &I::IID, &mut factory as *mut _ as *mut _);
59-
60-
// If RoGetActivationFactory fails because combase hasn't been loaded yet then load combase
61-
// automatically so that it "just works" for apartment-agnostic code.
62-
if code == CO_E_NOTINITIALIZED {
63-
if let Some(mta) = delay_load::<CoIncrementMTAUsage>(crate::s!("ole32.dll"), crate::s!("CoIncrementMTAUsage")) {
64-
let mut cookie = std::ptr::null_mut();
65-
let _ = mta(&mut cookie);
66-
}
67-
68-
// Now try a second time to get the activation factory via the OS.
69-
code = function(std::mem::transmute_copy(&name), &I::IID, &mut factory as *mut _ as *mut _);
70-
}
71-
72-
code
56+
let code = unsafe {
57+
let mut get_com_factory = || crate::HRESULT(RoGetActivationFactory(std::mem::transmute_copy(&name), &I::IID as *const _ as _, &mut factory as *mut _ as *mut _));
58+
let mut code = get_com_factory();
59+
60+
// If RoGetActivationFactory fails because combase hasn't been loaded yet then load combase
61+
// automatically so that it "just works" for apartment-agnostic code.
62+
if code == CO_E_NOTINITIALIZED {
63+
let mut cookie = 0;
64+
CoIncrementMTAUsage(&mut cookie);
65+
66+
// Now try a second time to get the activation factory via the OS.
67+
code = get_com_factory();
7368
}
74-
} else {
75-
CLASS_E_CLASSNOTAVAILABLE
69+
70+
code
7671
};
7772

7873
// If this succeeded then return the resulting factory interface.
@@ -124,8 +119,6 @@ unsafe fn get_activation_factory(library: crate::PCSTR, name: &crate::HSTRING) -
124119
function(std::mem::transmute_copy(name), &mut abi).from_abi(abi)
125120
}
126121

127-
type CoIncrementMTAUsage = extern "system" fn(cookie: *mut *mut std::ffi::c_void) -> crate::HRESULT;
128-
type RoGetActivationFactory = extern "system" fn(hstring: *mut std::ffi::c_void, interface: &crate::GUID, result: *mut *mut std::ffi::c_void) -> crate::HRESULT;
129122
type DllGetActivationFactory = extern "system" fn(name: *mut std::ffi::c_void, factory: *mut *mut std::ffi::c_void) -> crate::HRESULT;
130123

131124
#[cfg(test)]

crates/tools/core/bindings.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Windows.Win32.Foundation.SysAllocStringLen
1212
Windows.Win32.Foundation.SysFreeString
1313
Windows.Win32.Foundation.SysStringLen
14+
Windows.Win32.System.Com.CoIncrementMTAUsage
1415
Windows.Win32.System.Com.CoTaskMemAlloc
1516
Windows.Win32.System.Com.CoTaskMemFree
1617
Windows.Win32.System.Com.StructuredStorage.PROPVARIANT
@@ -67,3 +68,5 @@
6768
Windows.Win32.System.Variant.VT_UI4
6869
Windows.Win32.System.Variant.VT_UI8
6970
Windows.Win32.System.Variant.VT_UNKNOWN
71+
Windows.Win32.System.WinRT.RoGetActivationFactory
72+
Windows.Win32.System.WinRT.RoOriginateError

0 commit comments

Comments
 (0)