Skip to content

Commit 9ca0e2d

Browse files
authored
Merge pull request #14 from TerranMechworks/windows-rs
Replace winapi with windows-rs
2 parents 654a772 + f78cbeb commit 9ca0e2d

4 files changed

Lines changed: 189 additions & 29 deletions

File tree

Cargo.lock

Lines changed: 164 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/zipfixup/Cargo.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@ publish.workspace = true
1515
name = "zipfixup"
1616
crate-type = ["cdylib"]
1717

18+
test = false
19+
1820
[dependencies]
1921
retour = "0.3.1"
2022
region = "3.0.2"
2123

22-
[dependencies.winapi]
23-
version = "0.3.9"
24+
[dependencies.windows]
25+
version = "0.62.0"
2426
default-features = false
25-
features = ["debugapi", "libloaderapi"]
27+
features = [
28+
"Win32_System_Diagnostics_Debug", # OutputDebugStringA/OutputDebugStringW
29+
"Win32_System_LibraryLoader", # DisableThreadLibraryCalls
30+
"Win32_System_SystemServices", # DLL_PROCESS_ATTACH/DLL_PROCESS_DETACH
31+
]
2632

2733
[build-dependencies]
2834
cc = "1.2"

crates/zipfixup/src/dbg.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! [`output!`] macro to log messages via the Debug API, to be viewed in e.g.
22
//! [DebugView](https://learn.microsoft.com/en-us/sysinternals/downloads/debugview).
3-
use ::winapi::um::debugapi::{OutputDebugStringA, OutputDebugStringW};
3+
use windows::Win32::System::Diagnostics::Debug::{OutputDebugStringA, OutputDebugStringW};
4+
use windows::core::{PCSTR, PCWSTR};
45

56
macro_rules! output {
67
($fmt:literal $(, $args:expr)* $(,)?) => {{
@@ -36,22 +37,18 @@ fn encode_unicode(msg: &str) -> Vec<u16> {
3637
/// version may be slightly faster.
3738
pub(crate) fn output_debug_string_w(msg: &str) {
3839
let v: Vec<u16> = encode_unicode(msg);
39-
let p: *const u16 = v.as_ptr();
40+
let p = PCWSTR::from_raw(v.as_ptr());
4041
unsafe { OutputDebugStringW(p) };
4142
// paranoia: ensure `v` is valid until after `OutputDebugStringW`
4243
drop(v);
4344
}
4445

45-
fn encode_ascii(msg: &str) -> Vec<i8> {
46+
fn encode_ascii(msg: &str) -> Vec<u8> {
4647
const ZF: &[u8] = b"[ZF] ";
4748
let msg = msg
4849
.chars()
4950
.map(|c| if c.is_ascii() { c as u8 } else { b'?' });
50-
ZF.iter()
51-
.copied()
52-
.chain(msg.chain(Some(0)))
53-
.map(|b| b as i8)
54-
.collect()
51+
ZF.iter().copied().chain(msg.chain(Some(0))).collect()
5552
}
5653

5754
/// Output an ASCII debug string.
@@ -61,8 +58,8 @@ fn encode_ascii(msg: &str) -> Vec<i8> {
6158
/// Microsoft Unicode ineptness).
6259
#[allow(dead_code, reason = "Use Unicode version by default")]
6360
pub(crate) fn output_debug_string_a(msg: &str) {
64-
let v: Vec<i8> = encode_ascii(&msg);
65-
let p: *const i8 = v.as_ptr();
61+
let v: Vec<u8> = encode_ascii(&msg);
62+
let p = PCSTR::from_raw(v.as_ptr());
6663
unsafe { OutputDebugStringA(p) };
6764
// paranoia: ensure `v` is valid until after `OutputDebugStringA`
6865
drop(v);

crates/zipfixup/src/dll_main.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22
//! executable that loaded the DLL.
33
use crate::{Result, output};
44
use std::sync::Mutex;
5-
use winapi::shared::minwindef::{BOOL, DWORD, HINSTANCE, LPVOID, TRUE};
6-
use winapi::um::libloaderapi::DisableThreadLibraryCalls;
7-
use winapi::um::winnt::{DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH};
5+
use windows::Win32::Foundation::{HMODULE, TRUE};
6+
use windows::Win32::System::LibraryLoader::DisableThreadLibraryCalls;
7+
use windows::Win32::System::SystemServices::{DLL_PROCESS_ATTACH, DLL_PROCESS_DETACH};
88

99
const VERSION: &str = env!("CARGO_PKG_VERSION");
1010

1111
#[unsafe(no_mangle)]
1212
#[allow(non_snake_case)]
13-
extern "system" fn DllMain(dll_module: HINSTANCE, call_reason: DWORD, _reserved: LPVOID) -> BOOL {
13+
extern "system" fn DllMain(
14+
hlibmodule: HMODULE,
15+
call_reason: u32,
16+
_reserved: *mut std::ffi::c_void,
17+
) -> windows::core::BOOL {
1418
match call_reason {
1519
DLL_PROCESS_ATTACH => {
1620
output!("DLL_PROCESS_ATTACH");
1721
// disable DLL_THREAD_ATTACH/DLL_THREAD_DETACH notifications, since
1822
// we don't need them, and may help with spawning threads
19-
unsafe { DisableThreadLibraryCalls(dll_module) };
23+
let _ = unsafe { DisableThreadLibraryCalls(hlibmodule) };
2024
// it's unclear what is allowed to be done in DllMain.
2125
// theoretically, even spawning a thread is not "recommended":
2226
// https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-best-practices

0 commit comments

Comments
 (0)