Calling a function that can be either cdecl or stdcall

我的梦境 提交于 2019-12-23 10:51:22

问题


I need to write code that calls an external function that can be either stdcall call or cdecl in a 32bit windows application.
My code, the caller, can't know in advance which of these its going to be. Right now, if I try to call a cdecl function from a call site that was defined as stdcall, I get a checkEsp exception dialog, and I'm guessing that's there for a good reason.
Is there any way to do it?


回答1:


It can be done following way:

          mov     esi, esp

          push    arg3
          push    arg2
          push    arg1
          call    [SomeExternalProc]

          mov     esp, esi   ; now the stack is always properly cleaned 

The external procedure should preserve esi. Or you can use any other register preserved by the external procedure or even memory variable - local or global.

Good, the order of the arguments is the same for CDECL and STDCALL - in reverse order.




回答2:


You can also use alloca() which has the side effect of saving and restoring the stack pointer:

{
    alloca( (uintptr_t)callback & 2 );
    callback();
}



回答3:


cdecl and stdcall are by definition incompatible. In cdecl, the caller cleans up the stack, in stdcall the callee cleans up the stack. If you assume stdcall, but it is in fact cdecl, no one cleans up the stack. That means your ESP (stack pointer) is going to be screwed up after the call. Maybe if you give more details, there maybe a work around, but there is no way to call a function without knowing it's calling convention without messing up your stack.

See : http://en.wikipedia.org/wiki/X86_calling_conventions for a definition of the difference.



来源:https://stackoverflow.com/questions/17625790/calling-a-function-that-can-be-either-cdecl-or-stdcall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!