mirror of
https://github.com/ankitects/anki.git
synced 2026-01-10 12:33:55 -05:00
refactor windows ffi macro
This commit is contained in:
parent
f037e260c0
commit
2aa9108046
1 changed files with 14 additions and 16 deletions
|
|
@ -11,7 +11,6 @@ use anyhow::anyhow;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use widestring::u16cstr;
|
use widestring::u16cstr;
|
||||||
use windows::core::PCSTR;
|
|
||||||
use windows::core::PCWSTR;
|
use windows::core::PCWSTR;
|
||||||
use windows::Wdk::System::SystemServices::RtlGetVersion;
|
use windows::Wdk::System::SystemServices::RtlGetVersion;
|
||||||
use windows::Win32::Foundation::FreeLibrary;
|
use windows::Win32::Foundation::FreeLibrary;
|
||||||
|
|
@ -19,7 +18,6 @@ use windows::Win32::Foundation::HMODULE;
|
||||||
use windows::Win32::System::Console::AttachConsole;
|
use windows::Win32::System::Console::AttachConsole;
|
||||||
use windows::Win32::System::Console::GetConsoleWindow;
|
use windows::Win32::System::Console::GetConsoleWindow;
|
||||||
use windows::Win32::System::Console::ATTACH_PARENT_PROCESS;
|
use windows::Win32::System::Console::ATTACH_PARENT_PROCESS;
|
||||||
use windows::Win32::System::LibraryLoader::GetProcAddress;
|
|
||||||
use windows::Win32::System::LibraryLoader::LoadLibraryExW;
|
use windows::Win32::System::LibraryLoader::LoadLibraryExW;
|
||||||
use windows::Win32::System::LibraryLoader::LOAD_LIBRARY_FLAGS;
|
use windows::Win32::System::LibraryLoader::LOAD_LIBRARY_FLAGS;
|
||||||
use windows::Win32::System::Registry::RegCloseKey;
|
use windows::Win32::System::Registry::RegCloseKey;
|
||||||
|
|
@ -285,22 +283,22 @@ impl Drop for PyFfi {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! load_sym {
|
|
||||||
($lib:expr, $name:expr) => {
|
|
||||||
std::mem::transmute(
|
|
||||||
GetProcAddress($lib, PCSTR::from_raw($name.as_ptr().cast()))
|
|
||||||
.ok_or_else(|| anyhow!("failed to load {}", $name.to_string_lossy()))?,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! ffi {
|
macro_rules! ffi {
|
||||||
($lib:expr, $exec:expr, $($field:ident),* $(,)?) => {
|
($lib:expr, $exec:expr, $($field:ident),* $(,)?) => {
|
||||||
#[allow(clippy::missing_transmute_annotations)] // they're not missing
|
#[allow(clippy::missing_transmute_annotations)]
|
||||||
PyFfi { exec: $exec, $($field: {
|
$crate::platform::PyFfi {
|
||||||
let sym = ::std::ffi::CString::new(stringify!($field)).map_err(|_| anyhow::anyhow!("failed to construct symbol CString"))?;
|
exec: $exec,
|
||||||
load_sym!($lib, sym)
|
$($field: {
|
||||||
},)* lib: $lib.0, }
|
let sym = ::std::ffi::CString::new(stringify!($field)).map_err(|_| ::anyhow::anyhow!("failed to construct sym"))?;
|
||||||
|
::std::mem::transmute(
|
||||||
|
::windows::Win32::System::LibraryLoader::GetProcAddress(
|
||||||
|
$lib,
|
||||||
|
::windows::core::PCSTR::from_raw(sym.as_ptr().cast()),
|
||||||
|
)
|
||||||
|
.ok_or_else(|| anyhow!("failed to load {}", stringify!($field)))?,
|
||||||
|
)
|
||||||
|
},)*
|
||||||
|
lib: $lib.0, }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue