Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如何将OsString传递给Win32 API调用_Windows_Winapi_Rust - Fatal编程技术网

Windows 如何将OsString传递给Win32 API调用

Windows 如何将OsString传递给Win32 API调用,windows,winapi,rust,Windows,Winapi,Rust,我让这段代码使用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,

我让这段代码使用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(), <<<=======
};
让cname=OsString::from(“我的后端类”);
让笔刷=CreateSolidBrush(RGB(0,0,0));
设cl=WNDCLASSW{
样式:0,
lpfnWndProc:Some(回调),
cbClsExtra:0,
cbWndExtra:std::mem::size_of:()作为i32,
hInstance:手柄,
hIcon:0作为hIcon,
hCursor:LoadCursorW(std::ptr::null_mut(),IDC_箭头),
hbrBackground:刷,//(颜色窗口+1)为HBRUSH,
LPSzMenuame:0作为LPWSTR,
lpszClassName:cname.as_ptr(),根据:

在Windows上,实现了
std::os::Windows::ffi::
特性,它提供了一个方法。它提供了一个迭代器,可以将迭代器分解为

该文件还规定:

请注意,编码并没有添加最终的空终止符

因此:

使用std::os::windows::ffi::OsStrExt;
让cname=OsStr::new(“我的后端类”)
.encode_wide()
.chain(某些(0))//添加空终止
收集::();
//然后按当前操作调用cname.as_ptr()

TY。考虑到OsString的存在是专门用于在windows上将utf16传递给ffi调用的,这相当复杂。与CString的_ptr@pm100:
OsString
在内部使用编码,可以廉价地转换为本地字符串和本地字符串。Windows需要UTF-16,因此需要转换。实际上我不需要We’关于OsString。我只想用“最好”的方法将字符串传递给接受UTF16并带有空终止符的win32调用。有更好的方法吗?@pmt100:没有,这正是最好的方法。这不起作用,因为它不会零终止字符串,所以我选择了“xxxxxxx\0”
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