Created
June 2, 2016 22:43
-
-
Save mrobakowski/8c48ebd72f2131dc63e2207eacceff49 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
#![feature(plugin)] | |
#![plugin(clippy)] | |
extern crate comdlg32; | |
extern crate winapi; | |
const PNGS: &'static str = "PNG Image Files\0*.png\0\0"; | |
const MAX_FILE_NAME_LENGTH: u32 = 260; | |
fn main() { | |
println!("{:?}", get_filename()); | |
} | |
fn get_filename() -> Option<String> { | |
unsafe { | |
use comdlg32::*; | |
use winapi::*; | |
use std::ptr::null; | |
let mut filename = [0 as c_char; (MAX_FILE_NAME_LENGTH+1) as usize]; | |
let null_ptr = null::<c_void>(); | |
let mut opts = OPENFILENAMEA { | |
lStructSize: std::mem::size_of::<OPENFILENAMEW>() as u32, | |
hwndOwner: null_ptr as _, | |
hInstance: null_ptr as _, | |
lpstrFilter: PNGS.as_ptr() as _, | |
lpstrFile: filename.as_mut_ptr(), | |
nMaxFile: MAX_FILE_NAME_LENGTH, | |
lpstrTitle: "Select A File To Open\0".as_ptr() as _, | |
nFilterIndex: 0, | |
nMaxCustFilter: 0, | |
lpstrCustomFilter: null_ptr as _, | |
nFileExtension: 0, | |
nFileOffset: 0, | |
lpstrInitialDir: null_ptr as _, | |
Flags: 0, | |
nMaxFileTitle: 0, | |
lpstrFileTitle: null_ptr as _, | |
lCustData: 0, | |
lpstrDefExt: null(), | |
lpfnHook: None, | |
pvReserved: null_ptr as _, | |
lpTemplateName: null(), | |
dwReserved: 0, | |
FlagsEx: 0 | |
}; | |
let res = GetOpenFileNameA(&mut opts as _); | |
if res == 1 { | |
let filename = &filename as &[i8]; | |
Some(String::from_utf8(filename.iter().map(|x|{*x as u8}).take_while(|x|{*x != 0}).collect()).expect("Couldn't decode")) | |
} else { | |
None | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment