1

I have an already built static library on Windows VS2012 (.lib), compiled with the stdcall convention. When I say "already built", I mean unfortunatedly I cannot rebuild it with the calling convention of my choice.

Is it possible to call a function from this library from a code compiled with cdecl convention ? Or at least do some kind of wrapper ?

I am not sure it is possible (after browsing SO, I am in fact almost convinced this is totally impossible) but some guy here vaguely mentioned there might be a kludge. Any idea ?

Community
  • 1
  • 1
GaTTaCa
  • 459
  • 6
  • 18
  • 3
    Edit the library's .h file(s) and put __stdcall in the function prototypes. Send your changes back to the library owner so you only have to do this once. – Hans Passant Apr 29 '15 at 18:37
  • Maybe I wasn't clear: the library's functions already have the __stdcall qualifier in the header and were compiled with it. I wish to call them from a __cdecl function in another module (mine). – GaTTaCa Apr 30 '15 at 08:36
  • Then you only have an imaginary problem. – Hans Passant Apr 30 '15 at 08:37
  • No. I will be more specific: I would like to reuse some thirdparty libraries (namely Nvidia's implementation of OpenCL libraries - found in the drivers) in my own project. Alas the libs are built with the stdcall convention thus not working with my cdecl project. – GaTTaCa Apr 30 '15 at 08:45
  • Then there must be a tooling issue -- there is no problem for modern compilers to compile cdecl functions that call stdcall functions and vice versa. – Björn Lindqvist Aug 31 '15 at 00:47
  • As a note, the compiler will handle calling conventions for you if you specify (or omit) appropriate compiler-specific keywords as necessary (this is most evident when calling the (stdcall) Windows API, where user code is typically cdecl). At most, the only difference you see should is the function protype (which will contain the appropriate compiler keyword) and mangled name (where MSVC encodes calling conventions with single-letter codes as part of the function type for functions, pointers-to-function, etc.). – Justin Time - Reinstate Monica Jul 28 '19 at 19:33

1 Answers1

2

In the header for your static library, declare the imported functions as extern <return_type> __stdcall. See more about calling convention attributes on MSDN: https://msdn.microsoft.com/en-us/library/zxk0tw93.aspx

peterdn
  • 2,386
  • 1
  • 23
  • 24
  • The linker won't be able to match the imported functions with their call as stdcall and cdecl have different decorating conventions. – GaTTaCa Apr 30 '15 at 08:33
  • But you said the library is compiled with stdcall. Then if they're declared stdcall the linker should find them. It shouldn't matter what calling convention your own code uses unless this library expects callbacks, in which case you declare *them* stdcall. So... what's the issue? – peterdn Apr 30 '15 at 10:03