[fpc-pascal] stdcall vs. cdecl: another "using a FPC-generated .dll with Microsoft Visual C++" question

Henry Vermaak henry.vermaak at gmail.com
Wed Mar 24 16:31:41 CET 2010


On 24 March 2010 15:14, Seth Grover <sethdgrover at gmail.com> wrote:
> I appreciated Henry's comment regarding my last question (generating a
> .lib file from a FPC-generated for linking with VC++). I ended up
> using implib (http://implib.sourceforge.net) to generate the .def file
> with dll2def.exe and then fasm.exe to create the .lib file from that,
> and VC++ seems to link fine against it.

Thanks for sharing this, I'm sure someone else will come across this
problem in time.

> My fight is with calling conventions and exported names. I've read the
> programmers guide chapters 6, 7, and 12, and got a lot of useful
> information out if it. I'm trying now to get a grasp of how different
> calling conventions work, what different compilers generate what name
> decorations with the different calling conventions, what compilers
> require when linking against them, etc.
>
> I've got a little test library with two functions:
>
> function stdcallSquare(value : cint) : cint; stdcall;
> function cdeclSquare(value : cint) : cint; cdecl;
>
> And I'm trying to use them in a Visual C++ project. In my .cpp file, I have:
>
> extern "C" {
>        int __declspec(dllimport) __cdecl cdeclSquare(int value);
>        int __declspec(dllimport) __stdcall stdcallSquare(int value);
> }
>
> In the "exports" section of my FPC library, I can simply export
> cdeclSquare without modifying the name it's exported as, and VC++
> finds and links against and calls it just great.
>
> If I try to export sdtcallSquare without modifying the name it's
> exported as, VC++ complains with "1>testdll.obj : error LNK2019:
> unresolved external symbol __imp__stdcallSquare at 4 referenced in
> function _wmain".
>
> If I put "stdcallSquare name 'stdcallSquare at 4'" in my exports section,
> then VC++ seems to be happy with it and it links and calls the routine
> Ok.
>
> So my question is, is there a way to have FPC automatically generate
> the decoration VC++ is looking for? Or does anyone know of a way to
> make VC++ find and link stdcall functions without the decorations?

It's not possible with fpc.  There seems to be sort of a standard:

http://en.wikipedia.org/wiki/Name_mangling#C_name_decoration_in_Microsoft_Windows

Marco said fpc differs from this because windows system dlls use a
different mangling, too, and fpc matches that (iirc).

See this thread for more info (although this is the other way 'round
from what you are doing):

http://www.mail-archive.com/fpc-pascal@lists.freepascal.org/msg19388.html

Henry



More information about the fpc-pascal mailing list