Skip to content

Instantly share code, notes, and snippets.

@KostaCitrix
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KostaCitrix/5ee19ab7e8235b58000e to your computer and use it in GitHub Desktop.
Save KostaCitrix/5ee19ab7e8235b58000e to your computer and use it in GitHub Desktop.
Hello MessageBox
[package]
name = "msgbox"
version = "0.1.0"
authors = ["kosta"]
[dependencies]
winapi = "*"
user32-sys = "*"
encoding = "*"
[profile.release]
opt-level = 3
debug = false
rpath = false
lto = true
debug-assertions = false
codegen-units = 1
extern crate winapi;
extern crate user32;
extern crate encoding;
use std::slice;
use encoding::{Encoding, EncoderTrap};
use encoding::all::UTF_16LE;
fn to_utf16(s: &str) -> Vec<u8> {
let mut result = UTF_16LE.encode(s, EncoderTrap::Ignore).unwrap_or(Vec::new());
result.push(0);
result.push(0);
result
}
#[allow(non_snake_case)]
unsafe fn as_LPCWSTR(v: &Vec<u8>) -> winapi::winnt::LPCWSTR {
let s : &[u16] = slice::from_raw_parts(v.as_ptr() as *const _,
v.len()/2);
s.as_ptr()
}
fn message_box(title: &str, message: &str) {
unsafe {
user32::MessageBoxW(std::ptr::null_mut(),
as_LPCWSTR(&to_utf16(message)),
as_LPCWSTR(&to_utf16(title)),
winapi::MB_ICONASTERISK);
}
}
fn main() {
message_box("Oh Hi!", "This is a very important message.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment