Dereference Raw Memory Address in unsafe rust

Hello there. :wave:
I'm trying to write some sort of dll hooking Library
Currently i have a problem and i don't know how to do it in Rust.
Simply i want to dereference a raw memory address, i know that it is existing, and the dll is injected and running in the same space of memory so i think we can simply access it.
To explain it more look at this example

let addr = 0x00401530; // it's an address to  a function.

I need somehow to get a reference to that function so i can inspect/patch or make a detour to it.

If there's a simple way to do that in unsafe rust, then how ? If not can we make it in C and link to it ?

Thanks :smiley:

0x00401530 as *const u8 gives a pointer. You can also do as fn() -> i32, etc. to cast to a function pointer.

2 Likes

Are *const .. required to obtain the pointer to it ?

Yes, that's the pointer syntax.

1 Like

Hmmm, interesting :thinking:
Thank you, i will try it :smiley:

You may also want to look at the libloading crate. It's essentially a platform-agnostic abstraction over LoadLibrary/dlopen and behind the scenes they do this sort of "cast a raw pointer to a function pointer" thing.

3 Likes