Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-03

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

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Seth Grover

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 referenced in
function _wmain".

If I put "stdcallSquare name ''" 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?

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.

Thanks,

-SG

This email is fiction. Any resemblance to actual events or persons living or dead is purely coincidental. Seth Grover sethdgrover[at]gmail[dot]com

Reply by email

Henry Vermaak
to Seth Grover
Seth Grover wrote 5 lines
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.

Show quoted text 35 lines
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 referenced in function _wmain". If I put "stdcallSquare name ''" 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):

Henry

Reply by email

Jeff Wormsley
to Seth Grover: I appreciated Henry's comment regarding my last question (generating a .lib file from a…
Seth Grover wrote 6 lines
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.

Not exactly on topic, but I'm curious why you want to link statically like this and not dynamically. AFAIK, VS can call LoadLibrary and GetProcAddress just fine.

Jeff.

Reply by email