|
| 1 | +fn main() { |
| 2 | + #[cfg(all(target_arch = "x86_64", target_os = "windows", target_env = "msvc"))] |
| 3 | + x86_64_pc_windows_msvc::test(); |
| 4 | +} |
| 5 | + |
| 6 | +#[cfg(all(target_arch = "x86_64", target_os = "windows", target_env = "msvc"))] |
| 7 | +mod x86_64_pc_windows_msvc { |
| 8 | + #![allow(clippy::upper_case_acronyms)] |
| 9 | + |
| 10 | + // Expanded windows_sys, with --cfg windows_raw_dylib, on not(target_arch = "x86"). |
| 11 | + // |
| 12 | + // With target_arch = "x86", #[link] needs import_name_type = "undecorated" for windows APIs for |
| 13 | + // windows APIs - and the extern abi depends on the specific API. |
| 14 | + |
| 15 | + // use windows_sys::core::PWSTR; |
| 16 | + // use windows_sys::{Win32::Foundation::*, Win32::UI::WindowsAndMessaging::*}; |
| 17 | + type PWSTR = *mut u16; |
| 18 | + type BOOL = i32; |
| 19 | + type HWND = isize; |
| 20 | + type LPARAM = isize; |
| 21 | + type WNDENUMPROC = Option<unsafe extern "system" fn(hwnd: HWND, param: LPARAM) -> BOOL>; |
| 22 | + |
| 23 | + #[link(name = "user32.dll", kind = "raw-dylib", modifiers = "+verbatim")] |
| 24 | + extern "system" { |
| 25 | + fn EnumWindows(lpenumfunc: WNDENUMPROC, lparam: LPARAM) -> BOOL; |
| 26 | + |
| 27 | + fn GetWindowTextW(hwnd: HWND, buf: PWSTR, buflen: i32) -> i32; |
| 28 | + } |
| 29 | + |
| 30 | + pub fn test() { |
| 31 | + unsafe { EnumWindows(Some(enum_window), 0) }; |
| 32 | + } |
| 33 | + |
| 34 | + extern "system" fn enum_window(window: HWND, _: LPARAM) -> BOOL { |
| 35 | + let mut text: [u16; 512] = [0; 512]; |
| 36 | + |
| 37 | + let len = unsafe { GetWindowTextW(window, text.as_mut_ptr(), text.len() as i32) }; |
| 38 | + let text = String::from_utf16_lossy(&text[..len as usize]); |
| 39 | + |
| 40 | + 1 // TRUE |
| 41 | + } |
| 42 | +} |
0 commit comments