Skip to content

Instantly share code, notes, and snippets.

@deadjakk
Last active January 12, 2021 04:10
Show Gist options
  • Save deadjakk/0ffd592569ab54a76718ecd879eb39fe to your computer and use it in GitHub Desktop.
Save deadjakk/0ffd592569ab54a76718ecd879eb39fe to your computer and use it in GitHub Desktop.
Minimal example, loading rust dll into c to call WinAPI (for reference)
#[link(name="kernel32")]
extern "system" {
fn WinExec(lpstr: *mut u8,cmdshow: u32)->u32;
}
#[no_mangle] // needs to precede every function you wish to call from c
extern "system" fn DllMain(_: *const u8, _: u32, _: *const u8) -> u32 {
let ret = unsafe {
WinExec(['c' as u8 , 'a' as u8 ,'l' as u8, 'c' as u8, '.' as u8, 'e' as u8, 'x' as u8 , 'e' as u8, '\0' as u8].as_mut_ptr(), 1);
};
0
}
//cargo.toml contents:
//[package]
//name = "winapi-as-sc-lib"
//version = "0.1.0"
//authors = ["deadjakk"]
//edition = "2018"
//# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
//[lib]
//name = "fun"
//crate-type = ["cdylib"] # Creates dynamic lib
//[dependencies]
//loadthis.c contents
//#include <Windows.h>
//int main(void) {
// HMODULE hModule = LoadLibraryA("fun.dll"); // should spawn a calc
// return 0;
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment