#windows #wide #string #winapi #api-bindings

yanked lpwstr

Convert between safe types and raw u16 pointers for Windows FFI

Uses old Rust 2015

0.1.0-alpha1 Mar 2, 2019

#25 in #wide

MIT/Apache

8KB
60 lines

Convert between safe types and raw u16 pointers for Windows FFI.

Originally from wio-rs b895086

Examples

Creating a null-terminated wide string

use std::ffi::OsString;
use std::ptr;
use lpwstr::ToWide; // for to_wide_null
#

let class_name = OsString::from("MozillaWindowClass").to_wide_null();

unsafe {
    let window = FindWindowW(class_name.as_ptr(), ptr::null_mut());
}

Reading a null-terminated wide string

use std::ffi::OsString;
use std::mem;
use lpwstr::FromWide; // for from_wide_ptr_null
#

let computer_name;

unsafe {
    let mut buffer = vec![mem::uninitialized(); MAX_COMPUTERNAME_LENGTH + 1];
    GetComputerNameW(buffer.as_mut_ptr(), &mut (buffer.len() as u32));

    computer_name = OsString::from_wide_ptr_null(buffer.as_ptr());
}

assert_eq!(computer_name.to_string_lossy(), "COMPUTRON");

No runtime deps