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

Seth Grover sethdgrover at gmail.com
Wed Mar 24 16:14:08 CET 2010

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.

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

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?

I know I could just use cdecl and forget about stdcall altogether, but
I'd like to learn how to do it with stdcall but I'm just curious to
see if there's a way to get stdcall to work without having to manually
put the decorations on the exported names myself.



This email is fiction. Any resemblance to actual events
or persons living or dead is purely coincidental.

Seth Grover

More information about the fpc-pascal mailing list