我使用了xxxA版本的调用来使用CString
编写此代码。现在,我想使用xxxW版本,无法解决如何将OsString
传递给调用的问题。
let cname = OsString::from("my backend class");
let brush = CreateSolidBrush(RGB(0, 0, 0));
let cl = WNDCLASSW {
style: 0,
lpfnWndProc: Some(callback),
cbClsExtra: 0,
cbWndExtra: std::mem::size_of::<&i32>() as i32,
hInstance: handle,
hIcon: 0 as HICON,
hCursor: LoadCursorW(std::ptr::null_mut(), IDC_ARROW),
hbrBackground: brush, //(COLOR_WINDOW + 1) as HBRUSH,
lpszMenuName: 0 as LPWSTR,
lpszClassName: cname.as_ptr(), <<<=======
};
OsString
没有as_ptr()
方法。我不应该使用OsString
吗?如果没有,我应该怎么用?
最佳答案
On Windows,
OsStr
implements thestd::os::windows::ffi::OsStrExt
trait, which provides anencode_wide
method. This provides an iterator that can becollect
ed into a vector ofu16
.
encode_wide
的文档还指出:Note that the encoding does not add a final null terminator.
因此:
use std::os::windows::ffi::OsStrExt;
let cname = OsStr::new("my backend class")
.encode_wide()
.chain(Some(0)) // add NULL termination
.collect::<Vec<_>>();
// and then call cname.as_ptr() as you are currently doing
关于windows - 如何将OsString传递给Win32 API调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64175598/