Closed ValentinGrim closed 3 months ago
How about something like this? Can't guarantee this is the most idiomatic, maybe others have some suggestions.
[dependencies.windows]
version = "0.57.0"
features = [
"Win32_Foundation",
"Win32_Graphics_Gdi",
"Win32_Graphics_Printing"
]
use std::{
alloc::{alloc, alloc_zeroed, dealloc, Layout},
mem::align_of,
};
use windows::{
core::w,
Win32::{
Foundation::{HANDLE, HWND},
Graphics::{
Gdi::{DEVMODEW, DM_OUT_BUFFER},
Printing::{DocumentPropertiesW, OpenPrinterW},
},
},
};
fn main() -> windows::core::Result<()> {
unsafe {
let device_name = w!("Brother MFC-L2740DW series");
let mut handle = HANDLE::default();
OpenPrinterW(device_name, &mut handle, None)?;
let size = DocumentPropertiesW(HWND::default(), handle, device_name, None, None, 0);
let layout = Layout::from_size_align(size as usize, align_of::<DEVMODEW>()).unwrap();
let mut devmode = alloc_zeroed(layout) as *mut DEVMODEW;
let written = DocumentPropertiesW(
HWND::default(),
handle,
device_name,
Some(devmode),
None,
DM_OUT_BUFFER.0,
);
dbg!((*devmode).dmYResolution);
// ...
}
Ok(())
}
@riverar.
Adapted the sample you gave me and it work just as intended! Thank you very much.
Summary
Hi there,
I'm facing some challenges while trying to retrieve the DEVMODE of a printer using
DocumentPropertiesW
in Rust. I have existing C++ code that successfully accomplishes this using the standard Windows API. Here's the C++ code for reference:I'm trying to reproduce this functionality in Rust, but I've encountered a couple of challenges:
Here's my Rust code:
Crate manifest
No response
Crate code
No response