Last active
March 29, 2018 21:51
-
-
Save fdb/6a2cdab149d5367db3fba0db0c6382e1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 = "procaddress" | |
version = "0.1.0" | |
authors = [] | |
[dependencies] | |
libc = "0.2.33" |
This file contains hidden or 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 libc; | |
use libc::c_void; | |
use std::ptr; | |
#[link(name = "kernel32")] | |
#[link(name = "user32")] | |
extern "stdcall" { | |
pub fn LoadLibraryA(lpFileName: *const u8) -> *const c_void; | |
pub fn GetProcAddress(hModule: *const c_void, lpProcName: *const u8) -> *const c_void; | |
pub fn MessageBoxA(hWnd: *const c_void, lpText: *const u8, lpCaption: *const u8, uType: u32) | |
-> i32; | |
} | |
type FnMessageBox = extern "stdcall" fn(*const c_void, *const u8, *const u8, u32) -> i32; | |
fn main() { | |
unsafe { | |
// Call the MessageBox function directly. | |
MessageBoxA( | |
ptr::null(), | |
MESSAGE.as_ptr() as *const u8, | |
TITLE.as_ptr() as *const u8, | |
0, | |
); | |
// Load the function from user32.dll... | |
let module = LoadLibraryA(USER32_DLL.as_ptr() as *const u8); | |
println!("module {}", module as usize); | |
let h_message_box = GetProcAddress(module, MESSAGE_BOX.as_ptr() as *const u8); | |
println!("MessageBoxA {}", h_message_box as usize); | |
// ...then call it. | |
let message_box: FnMessageBox = std::mem::transmute(h_message_box); | |
message_box( | |
ptr::null(), | |
MESSAGE.as_ptr() as *const u8, | |
TITLE.as_ptr() as *const u8, | |
0, | |
); | |
} | |
} | |
const USER32_DLL: &'static [u8] = b"user32\0"; | |
const MESSAGE_BOX: &'static [u8] = b"MessageBoxA\0"; | |
const TITLE: &'static [u8] = b"GetProcAddress\0"; | |
const MESSAGE: &'static [u8] = b"Hello, World!\0"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment