Exploring the Rust programming language

> Coding, hacking, computer graphics, game dev, and such...
User avatar
fips
Site Admin
Posts: 170
Joined: Wed Nov 12, 2008 9:49 pm
Location: Prague
Contact:

Exploring the Rust programming language

Post by fips »

I have finally found some time to learn more about the Rust programming language, which has been under my radar for a couple of years now, but I had no chance to get my hands dirty until recently. There's a great book to start with, called: The Rust Programming Language, which is very well written and quite approachable (demonstrating how vital the Rust comunity is). I now begin to feel that Rust is a serious contender to C++, almost as if Rust came into existence to fix all the C++'s mistakes and expand the benefits, which I guess was among other motivations actually the case.

Anyway, my fist Rust program is not quite a typical one. I was rather curious how to dynamically load a library and call an exported function (In my case, I'm invoking 'cosf', which is available in 'msvcr100.dll' on Windows). This is an essential tool a system hacker needs to posses :), so here it is:

Code: Select all

// [CODE BY FIPS @ 4FIPS.COM, (c) 2017 FILIP STOKLAS, MIT-LICENSED]

extern crate libloading;

use libloading::{Library, Symbol};

type CosFunc = fn(f32) -> f32;

fn main() {
    let dll = Library::new("msvcr100.dll").unwrap();
    unsafe {
        let func: Symbol<CosFunc> = dll.get(b"cosf").unwrap();
        println!("cos(0) = {}", func(0.0));
    }
}

// output:
// cos(0) = 1
Some more useful resources: