C++ – How to call a function from binary data

cfunction-pointers

I have some binary data which contains a bunch of functions and want to call one of it.
I know the signature of these functions along with the offset relative to the start of the file. Calling convention is the default one: __cdecl.
The file was already loaded into a memory page with executing permissions.

For example (A, B, C being some types)

void myFunction (A *arg1, B arg2, C arg3); // Signature
int myOffset = 0x42; // Offset

How can I specify that myOffset points to myFunction?

Best Answer

// define a function pointer
typedef __cdecl void (*your_function) (A *arg1, B arg2, C arg3); 
your_function ftr;

char * memory = 0x123456; // base segment address

fptr = (your_function)(memory + 0x42); //calculate memory address

(*ftpr)(a,b,b); // call function