Skip to content

Instantly share code, notes, and snippets.

@Mr-Byte
Created May 28, 2015 22:28
Show Gist options
  • Save Mr-Byte/11ed82e0c94cc6a70e2c to your computer and use it in GitHub Desktop.
Save Mr-Byte/11ed82e0c94cc6a70e2c to your computer and use it in GitHub Desktop.
use std::ffi::OsString;
use std::os::windows::ffi::OsStrExt;
#[link(name = "user32")]
extern "stdcall" {
fn MessageBoxW(hwnd: *mut (), message: *const u16, title: *const u16, kind: u32);
}
struct LPCWSTR {
str: Vec<u16>
}
impl LPCWSTR {
fn as_ptr(&self) -> *const u16 {
self.str.as_ptr()
}
}
impl<T: Into<OsString>> From<T> for LPCWSTR {
fn from(source: T) -> LPCWSTR {
LPCWSTR { str: source.into().as_os_str().encode_wide().chain(Some(0).into_iter()).collect::<Vec<_>>() }
}
}
fn message_box<T: Into<LPCWSTR>>(message: T, title: T) {
unsafe { MessageBoxW(::std::ptr::null_mut(), message.into().as_ptr(), title.into().as_ptr(), 0) };
}
fn main() {
message_box("Hello, world!", "Greetings");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment