"&" to a Function in an argument

Hey, guys im trying to find out how to pass a reference to a function in an argument in this example.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void test(char *szMessage, char *szTitle)
{
	MessageBoxA(NULL, szMessage, szTitle, MB_OK);
}

void getfunc(void &func, DWORD funcsize, byte *pbOut)
{
	CopyMemory(pbOut, func, funcsize);
}

int main()
{
...
getfunc(test, testSize, pbData);
...
}


I know it errors but its basicly what i need help with. I've been trying to figure it out and i cant.

Thank you :D
A function pointer argument would be written as void(*func)(char*, char*) or simply my_fun_ptr func if you write a typedef first : typedef void(*my_fun_ptr)(char*, char*);
1
2
3
4
void getfunc(void &func, DWORD funcsize, byte *pbOut)
{
    CopyMemory(pbOut, func, funcsize);
}


So, what exactly are you trying to copy here? You're only going to be able to copy the address of the function, not its guts.
Hello, woohoo.

void cannot be a reference, only a pointer. Try converting getfunc( ) to a template function. Then, when you pass func to CopyMemory( ), cast it to void *. For example:

1
2
3
4
5
template< typename T >
void getfunc( T( &Function )( /* Args */ ), DWORD funcsize, byte *pbOut )
{
    CopyMemory( ( void * )pbOut, &Function, funcsize );
}

Your conversion style may vary.
Last edited on
: D, Woohoo thank you guys I got it. I was staring at it in the face and didnt see it.

1
2
3
4
void getfunc(void (*myfunction), DWORD funcsize, byte *pbOut)
{
	CopyMemory(pbOut, myfunction, funcsize);
}
that's just a void*, not a function pointer though. A function pointer would look more like this
void(*func)(void)//a function that takes no argument and doesn't return anything

though this would also work:
1
2
//requires <functional>
std::function<void(void)> func;
Now im confused out of my mind lol. For some reason a pointer is returning a reference to a function

GetFunc
-----------------------------------------------------
http://i.minus.com/iVf76uqhnzM97.PNG

test
-----------------------------------------------------
http://i.minus.com/iSGe2uloD8TPh.PNG

And when i reference the function it returns a pointer (i think)
Last edited on
?? Currently your function is returning nothing so...?
Oh, woops sorry i mean when i use

1
2
3
4
void getfunc(void (*myfunction), DWORD funcsize, byte *pbOut)
{
	CopyMemory(pbOut, myfunction, funcsize);
}


isnt "myfunction" supposed to be a pointer but in a debugger its a reference. (Seen in the picture)
Last edited on
Topic archived. No new replies allowed.