As Kayaker mentioned, using the address-of operator on functions presents a problem in msvc debug mode builds.
All function references are indirected by a jump stub.
I've recently been trying to hunt down the setting which controls this behaviour but I've had no luck.
Even when copying the release compiler settings into the debug section the result was the same.
So I strongly suspect that it's controlled by a linker setting.
My workaround was to put the function in a seperate masm source file.
Another option would be to hardcode markers into the function pro and epi-log.
And then doing a binary search for those markers to determine the start and length of the function.
Ie.:
Code:
struct args {
void* ptr;
void (__stdcall* pfn)(void*);
};
void __declspec(naked) __stdcall foo(void* bar) {
// Start marker
__asm __emit 0x11
__asm __emit 0x11
__asm __emit 0x11
__asm __emit 0x11
// Prolog code
// This is required if you want to access arguments
__asm push ebp
__asm mov ebp, esp
//
// Your code goes here.
// NOTE: You cannot use local variables since the compiler
// has not way of allocating space on the stack for them.
//
register args* pargs = reinterpret_cast<args*>(bar);
if (args && args->pfn) {
args->pfn(args->ptr);
}
// Epilog code
__asm pop ebp
// Pop the argument(s) on return
__asm retn 4
// End marker
__asm __emit 0x22
__asm __emit 0x22
__asm __emit 0x22
__asm __emit 0x22
}
Yet another possibility would be to detect the jump stubs and following them.
Depending on what you need, dll-injection might also be an option.