简体   繁体   中英

MASM and C jump to function

I have a pointer to a __stdcall function in C and in both x86 and x64 assembly what I'd like to do is have an asm function that I can use to jump to that function.

For example take the windows API function MessageBoxW

void *fn = GetProcAddress(GetModuleHandle("kernel32.dll"), MessageBoxW);

Then in C I'll have a call to the ASM, like

void foo()
{
MessageBoxW_asmstub(NULL, "test", "test", NULL);
}

Assume fn is global. Then in assembly I'd like to have a function that just forwards to MessageBoxW, not calling it. In other words I want MessageBoxW to clean up the variables passed to MessageBoxW_asmstub and then return to foo

jump (fn) ?

I don't know how to do this.

Assuming that MessageBoxW_asmstub is declared to the C compiler as having the correct calling convention (ie __stdcall for x86; for x64 there is thankfully only one calling convention), then as the comment from Ross Ridge said, this is as simple as jumping to the target function which will then return directly to the caller. Since you have an indirect reference (ie fn refers to a pointer to the target), you probably need another load instruction (although my knowledge of x86 is limited here -- I wouldn't be at all surprised if there is some double-indirect form of jmp). You can use any volatile registers in the calling convention to do this, eg for x64 you might use something along the lines of:

extern fn:qword

MessageBoxW_asmstub:
  mov rax, fn
  jmp rax

BTW, if you use a debugger to step through calls to delay-loaded DLL imports, you'll probably see a similar pattern used in the linker-generated stub functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM