Last active
August 29, 2015 14:24
-
-
Save KostaCitrix/5ee19ab7e8235b58000e to your computer and use it in GitHub Desktop.
Hello MessageBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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