Re: mixing calling conventions question



> I have the following situation (simplified): a library (dll) I need
> (for data acquisition) comes in 2 versions. The main difference is
> the calling conventions: one is using stdcall and one is using cdecl.
> Now I have an import unit using dynamic loading, which is able to
> init the functions from either of the dlls. Of course I use some
> "ifdef" to mark the function variables with the proper calling
> convention. But this means I have to decide at compile time. I would
> like to be able to use a program with any of the dlls. Is there a way
> to do this? One solution is to duplicate the whole import unit for
> each situation and to make wrapper calls, but that's a lot new code.
> I was thinking of placing all functions in a table, redirect the
> calls to a small asm code that would unload the stack parameters in
> case of cdecl version. But how can I know the number of parameters??
>
> I am out of ideas, but I don't rush in implementing the wrapper calls
> yet. Maybe someone has a better idea.

I don't know exactly how much calls and how many different functions
we're talking about, but i would probably typecast the pointers to
function pointers at the last moment, using an if statement to
determine the type. This way you can decide at runtime.

This would mean declaring the function types twice:

TFooC = function (Bar: Integer): Integer; cdecl;
TFooS = function (Bar: Integer): Integer; stdcall;

and the calling lines themselves would be duplicated (for the if
statement). It would require no other changes.

--
The Fastcode Project: http://www.fastcodeproject.org/
.



Relevant Pages

  • Re: keyboard/mouse programming
    ... procedure local auto variables (created in a new stackframe each time) ... parameters returned to the calling procedure via pointer (copied into ... "cdecl" calling convention is what's normally used for C on x86. ... "stdcall" is what is normally used for Windows, ...
    (alt.lang.asm)
  • Re: English interpretation of this code?
    ... > calling procedure for 2 arguments, ... > to follow normal calling conventions, ... Nothing different to using a function pointer with any other function. ... > way allowed for by the standard, one gets into icky territory. ...
    (comp.lang.c)
  • Re: English interpretation of this code?
    ... >>within the program results in undefined behaviour, ... > for recusively calling main. ... > Which of the two calling hypothetically-incompatible calling conventions ... > that compatibility might not be needed for any other routine. ...
    (comp.lang.c)
  • Re: Hooking to stop file move(ment)
    ... by default in C and CPP calling conversions are "cdecl" that means that the ... when stdcall is used the calling function will clean the stack. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: English interpretation of this code?
    ... I do not find anything in C89 that would indicate undefined behaviour ... for recusively calling main. ... If the main function executes a return ... Which of the two calling hypothetically-incompatible calling conventions ...
    (comp.lang.c)