Skip to content

Commit

Permalink
Drop Windows 7
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Jan 25, 2024
1 parent 0674d7d commit 2ed6783
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 36 deletions.
6 changes: 1 addition & 5 deletions crates/libs/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<RoOriginateError>(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 }
}
Expand Down Expand Up @@ -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<crate::imp::IErrorInfo> {
unsafe { crate::imp::GetErrorInfo(0) }
}
5 changes: 5 additions & 0 deletions crates/libs/core/src/imp/bindings.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 14 additions & 21 deletions crates/libs/core/src/imp/factory_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,21 @@ pub fn factory<C: crate::RuntimeName, I: Interface>() -> crate::Result<I> {
let mut factory: Option<I> = None;
let name = crate::HSTRING::from(C::NAME);

let code = if let Some(function) = unsafe { delay_load::<RoGetActivationFactory>(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::<CoIncrementMTAUsage>(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.
Expand Down Expand Up @@ -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)]
Expand Down
10 changes: 0 additions & 10 deletions crates/tests/standalone/src/b_none.rs
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 3 additions & 0 deletions crates/tools/core/bindings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 2ed6783

Please sign in to comment.