Set a pointer to a static address

I am inserting a DLL into another process and want to call a function that is in this binary format based on this address (0x54315).

How can I actually declare a function and then set it to this address?

#define FUNC 0x54315

void *myFuncPtr;

int main()
{
 myFuncPtr = FUNC;  // pretty sure this isn't how

 myFuncPtr(); // call it?
}
+3
source share
3 answers

Existing answers work, but you don’t even need a variable for a function pointer. You can simply:

#define myfunc ((void (*)(void))0x54315)

and then call it the myfunc()same as a regular function. Note that you must change the type of the cast according to the actual argument and the return types of the function.

+3
source

You need to define myFuncPtras a function pointer, void*cannot be called.

typedef :

typedef void (*funptr)(void);
funprt myFuncPtr;

(, .)

- , "" , , .

, , , , , -, .

+5

, . . .

Mat, :

void (*myFuncPtr)(void) = (void (*)(void)) FUNC;

typedef, C .

In addition, you must be sure that the called function is at the same exact address every time your embedded DLL starts. I'm not sure how you can be sure of that, though ...

In addition, you will need to pay attention to the calling conventions and any arguments that the function can expect FUNC, because if you make a mistake, you will probably end up with a stack corruption.

+3
source

All Articles