Created
December 3, 2017 18:51
-
-
Save demurgos/ffd687a2bd685b28756ffd63237b6bd1 to your computer and use it in GitHub Desktop.
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
C:/Users/demurgos/.cargo/bin/cargo.exe run --package hello-win --bin hello-win | |
Updating git repository `https://github.com/retep998/winapi-rs` | |
Compiling hello-win v0.1.0 (file:///E:/projects/various/hello-win) | |
error[E0432]: unresolved import `winapi::um::winuser` | |
--> src\main.rs:10:19 | |
| | |
10 | use winapi::um::winuser::{MB_OK, MessageBoxW}; | |
| ^^^^^^^ Could not find `winuser` in `um` | |
error: aborting due to previous error | |
error: Could not compile `hello-win`. | |
To learn more, run the command again with --verbose. | |
Process finished with exit code 101 |
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 = "hello-win" | |
version = "0.1.0" | |
authors = ["demurgos"] | |
[target.'cfg(windows)'.dependencies] | |
winapi = { git = "https://github.com/retep998/winapi-rs" } |
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
#[cfg(windows)] extern crate winapi; | |
use std::io::Error; | |
#[cfg(windows)] | |
fn print_message(msg: &str) -> Result<i32, Error> { | |
use std::ffi::OsStr; | |
use std::iter::once; | |
use std::os::windows::ffi::OsStrExt; | |
use std::ptr::null_mut; | |
use winapi::um::winuser::{MB_OK, MessageBoxW}; | |
let wide: Vec<u16> = OsStr::new(msg).encode_wide().chain(once(0)).collect(); | |
let ret = unsafe { | |
MessageBoxW(null_mut(), wide.as_ptr(), wide.as_ptr(), MB_OK) | |
}; | |
if ret == 0 { Err(Error::last_os_error()) } | |
else { Ok(ret) } | |
} | |
#[cfg(not(windows))] | |
fn print_message(msg: &str) -> Result<(), Error> { | |
println!("{}", msg); | |
Ok(()) | |
} | |
fn main() { | |
print_message("Hello, world!").unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment