creating dll to link with delphi program

Brad Finney Brad.Finney@humboldt.edu
Mon Oct 24 18:59:00 GMT 2011



On 10/24/2011 07:16 AM, Tobias Burnus wrote:
> Brad Finney wrote:
>> Yes, I did try his suggestion, but missed the addition of the bind(c,
>> name= " ").
>
> Well, that was the most important part ...


Yes, I know!  Sorry about that.

>
>> Thanks for getting me to read more carefully! That does correct the
>> case issue, but not (for me) the underscore issue, which is very
>> puzzling. I took the exact code you listed and got a leading
>> underscore on Foo and bar.
>>
>> F:\FlowReader>nm foo.o
>> 00000000 T _Foo
>
> The leading _ is added there because the ABI (application binary
> interface) of 32bit Windows *requires* it. If you don't like it, blame
> Microsoft.
>
> Are you sure that you do not need the leading underscore? I do not want
> to rule out that Delphi is broken, but the 32bit Windows ABI makes
> pretty clear that Windows should have a leading underscore, see:
>
> http://msdn.microsoft.com/en-us/library/zkwh89ks%28v=vs.71%29.aspx
> http://msdn.microsoft.com/en-us/library/zxk0tw93%28v=VS.71%29.aspx


Well, if I look at a working version of the .dll (from Fortran code 
compiled with Intel Fortran), all of the Fortran procedure names do not 
have an underscore.  If I produce my .dll with gfortran with leading 
underscores, the Delphi executable reports that it can't find the 
procedures in my .dll.  Based on those two pieces of evidence, I have 
been concluding that the routines are required to not have an underscore.


>
> Thus, I think your problem should be elsewhere. Can you check what
> Delphi actually wants? Does it use STDCALL or CDECL? How does it call
> Windows functions - which all start with a leading underscore?
>
> Regarding CDECL/STDCALL: With CDECL (default in GCC), the caller puts
> the procedure arguments on the the stack, calls the procedure and then
> pops it from the stack. With STDCALL, the caller puts the actual
> arguments on the stack and calls the procedure - but here the called
> procedure pops the stack. STDCALL is the widely used by (32bit)
> Microsoft's DLL - but not by all of their libraries. - It might be that
> Delphi on Windows uses STDCALL by default - but it could also use
> STDCALL (or in principle also FASTCALL). For those, see the two MS links
> above and
> http://gcc.gnu.org/onlinedocs/gfortran/Mixed_002dLanguage-Programming.html
>
The big visible difference I see when using STDCALL is that the 
procedure names are appended with an @4.  The executable still is not 
able to find the procedures.

I appreciate all of the suggestions.  Mix language programming is always 
a mess, and under windows it seems to be especially difficult.

Brad



More information about the Fortran mailing list