Software Archive
Read-only legacy content
17061 Discussions

Linking a Delphi DLL into Visual Fortran 97

Intel_C_Intel
Employee
388 Views
Hi,

My name is Edwin Anand and I am from India, a part of the world where Fortran was taught in schools way back...

And now I find myself having to do it !!! I am using Visual Fortran 97 from Microsoft, in the Developer Studio environment.
So with a knowledge of F77 and a documentation as "good" as MSDN's Fortran literature , I got stuck in this situation. Can anyone, pleeeeease bail me out? :-)

I have a DLL which I wrote in Delphi. Now I want my Fortran program to use this DLL. So I tried the following:

1. Explicit linking.
Very grandly, I wrote the following code and then got stuck:

USE DFWIN

INTEGER*4 hWnd, hProc

hWnd = LoadLibrary("MyDelphiDLL")
if (hWnd .NOT. = 0) then
hProc = GetProcAddress( hWnd, "MyProc" )
......

CALL ???? ( (function pointer) hProc )

And I am stuck here... How can I use 'hProc'? Meaning, how can I tell Fortran to use it as a function? Is there a function pointer concept in Fortran? If so what is the syntax. If no, then is there any other way of using the above calls?
Also, how come there are no samples for explicit linking in Fortran?
Please, some code will be very, very useful.....

So, with some disappointment, but with some hope, I tried 'Implicit linking'. Its like this:

2.Implicit linking:
a) Creating a LIB file: For this, I manually made a .DEF file (MYDEF.DEF) this way:

LIBRARY 'MYDELPHIDLL.DLL'
EXPORTS
MyProc

Now I used the following command at the command prompt: "LIB /DEF:MYDEF.DEF /MACHINE:I386"
to get the LIB file as MYDELPHIDLL.LIB

b) Set the LIB file in project settings ( y nowhere in the help or examples do they talk about it!!)
From the Projects -> Settings -> Link tab, I added the name MYdelphidll.lib beside the existing Kernel32.lib

c) Now, I added the following code in my Fortran program:

       
      MODULE MYMOD 
 
      INTERFACE MYINTERFACE 
      SUBROUTINE MYPROC 
      !DEF$ ATTRIBUTES STDCALL, ALIAS : '_MyProc@0' :: MYPROC 
      END SUBROUTINE 
      END INTERFACE 
 
      CONTAINS 
        SUBROUTINE TESTSTUB 
	  CALL MYPROC 
        END SUBROUTINE 
      ENDMODULE 
 
....... 
      
      PROGRAM MYFORTRANPROGRAM 
 
      USE MYMOD 
 
      CALL MYPROC 
 
      END PROGRAM 



Sigh! Ever since, linker error : 2001 has become my best friend. The more I try to manipulate the code ( Like removed the USE MYMOD and hoped that FORTRAN will find MyProc directely from the lib file, and some more of these antiques...) the linker error is bringing his friends along... had no time to note names... went mad by then !!!!

At my wits end ( which takes a short distance to reach ! ), I would REALLY appreciate some help from any of the Fortran gurus out there. And I will promise never ever again to touch Fortran!!!!

In eager anticipation....

Anand.
0 Kudos
1 Reply
Jugoslav_Dujic
Valued Contributor II
388 Views
1. (Dynamic binding) -- which I prefer. You have to specify INTERFACE to
MyProc and declare a Cray pointer to it:

 
INTERFACE  
      SUBROUTINE MyProc() 
      !MS$ATTRIBUTES STDCALL::   MyProc 
      END SUBROUTINE 
END INTERFACE 
POINTER(lpfnMyProc,MyProc) 


and then, use it in the following manner:

 
INTEGER:: hLib, iSt 
 
hLib=LoadLibrary('MyDelphiDll.dll'C) 
lpfnMyProc=GetProcAddress(hLib,'MyProc'C) 
CALL MyProc() 
iSt=FreeLibrary(hLib) 


Take care of the case of the argument to GetProcAddress. It must match
the exported name from the DLL char-by-char (the one obtained by
dumpbin /export MyDelphiDll.dll). If you provided .def file, it should be
just MyProc. Take care when to LoadLibrary, when to FreeLibrary.

2. (Static binding). You misspelled several things. First, it should be
just INTERFACE (not INTERFACE MYINTERFACE -- it means something
else). Second, it should be !DEC$, not !DEF$. Third, name behind
ALIAS must match exactly exported function name (as described above).
Also, you should add MyDelphiDll.lib into project (as you did).
I had troubles with static binding with Delphi DLLs, though -- linking
and compiling passed OK, but Win2000 complained "Dynamic link library
cannot be located in the following path..." wherever I put it... dunno why.

HTH

Jugoslav
0 Kudos
Reply