Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Fortran DLL calling DELPHI DLL

sabalan
New Contributor I
1,003 Views

I am trying to make function calls to a DLL coded in DELPHI from my own Fortran DLL. This Fortran DLL is then called by a VB executable. I have no problem with the communication between VB and my Fortran DLL, but I get Unhandled exception : 0x0000005: Access violation inside my DLL when a function call to the DELPHI DLL is executed. I try to attach a zip file containing 2 DLL-s: REF_CALC32.DLL and VAR_LIB32.DLL freely provided by the refrigerant vendor. The first DLL is dependent of the second one.

My DLL looks like the following and any VB or other application can be used to call it. Refr should be null-terminated string at function call, but the length is not given. Therefore I dont know how to declare it inside the interface. I have tried with CHARACTER*(*) and CHARACTER(*), and different given lengths, always getting the same access violation at RetLog=S_molmas(R134aC,Res). The function itself should be declared as Boolean while calling directly from VB. I have tried both with logical and integer declarations in my DLL, and all(?) alternatives with STDCALL, ALIAS, REFERENCE, etc inside the interface, without success.

Subroutine MyDLL(OUTPUT)

!DEC$ ATTRIBUTES DLLEXPORT,STDCALL :: MyDLL

!DEC$ ATTRIBUTES ALIAS: 'MyDLL' :: MyDLL

Use Kernel32

Implicit None

Interface

Logical Function S_molmas(Refr, Res)

!DEC$ ATTRIBUTES Alias:'S_molmas':: S_molmas

!DEC$ ATTRIBUTES REFERENCE:: S_molmas

Character(5) Refr

Real(8) Res

End Function

End Interface

INTEGER(4) hLib

REAL(8) Res, Output(1)

Logical RetLog

< p="">

Pointer (Smol, S_molmas)

hLib = LoadLibrary("REF_CALC32.DLL"C)

Smol = GetProcAddress(hLib, 'S_molmas'C)

RetLog = S_molmas('R134a'C, Res)

Output(1) = Res

RetLog = FreeLibrary(hLib)

Return

End

I have been searching this forum in 2 days, without finding a solution for such particular problem. Please! What am I doing wrong?

Sabalan

PS. I can not find how to attach files here :-(

0 Kudos
6 Replies
Steven_L_Intel1
Employee
1,003 Views
To attach files here, use the Options tab when posting.

The interface you have will have Fortran pass the character length at the end of the argument list which could cause stack corruption due to a mismatch in number of arguments. Try adding:

!DEC$ ATTRIBUTES REFERENCE :: Refr

and see if it helps. It would be even more of a problem if you were using CVF or compiled with /iface:cvf.

You also need STDCALL for S_molmas.
0 Kudos
sabalan
New Contributor I
1,003 Views

Thanks Steve for your reply. But, yes, it is CVF 6.6C! Your suggestions and different combinations of them and others didn't help. What is that "more of a problem" exactly? Would the LIB-way be helpful? I found an oldmessage here from youtalking about MAKILIB.EXE but the link to download it is broken now. Can I find it on old DVF or CVF CD-s?

I attach the DELPHI DLL-s this time and would appreciate thankfully any hints and suggestions from all forum members.

Sabalan

0 Kudos
anthonyrichards
New Contributor III
1,003 Views
I use CVF6.6c also. I know nothing about DELPHI and how it handles arguments except
what I can glean from a DELPHI primer found by Googling.

1) Can you say how DELPHI expects the arguments to be supplied? Are they passed by reference or by value?

If you know, then add the appropriate compiler directives: namely
!DEC$ ATTRIBUTES REFERENCE :: REALVAR
!DEC$ ATTRIBUTES REFERENCE :: STRING
REAL(8) REALVAR
CHARACTER*(*) STRING
I would also try
!DEC$ ATTRIBUTES STDCALL:: S_molmas

2) When writing mixed language calls, I would always place real arguments first, then integer, then put any character argument at the end. I would always pass character strings by reference. In fact I would pass all arguments by reference.

3) Failing everything else, rewrite your DELPHI routine Smolmas to use
a record structure for the argument list, e.g.
type
Myrecstruc = record

realvar: double;
chars : string[30];
end;
var
arglist : Myrecstruc
and match this on the Fortran side with
TYPE MYRECSTRUC
SEQUENCE
REAL(8) REALVAR
CHARACTER(30) STRING
END TYPE
and call the routine thus:
TYPE(MYRECSTRUC) ARGLIST
ARGLIST%STRING='R134a'C
RetLog = S_molmas(ARGLIST)

Hope this helps.

0 Kudos
sabalan
New Contributor I
1,003 Views

Anthony, unfortunately I do not have access to the source code of DELPHI DLL-s. Those are downloaded from the Solvay Fluor company's web site. What I can see in a suplied VB sample code for the functionis the following:

Declare Function S_molmas Lib "REF_CALC32.DLL" _
(ByVal Refr As String, ByRef Res As Double) As Boolean

I am out of possible combinations of REFERENCE, ALIAS, STDCALL, logical or integer, DLLimport, etc.!

Sabalan

0 Kudos
anthonyrichards
New Contributor III
1,003 Views

Panic no more. The attached program works fine (see the screen shot included in the zip file...).

program delphidll
 Use Kernel32
Implicit None
 Interface
Logical Function S_molmas(Refr, Res)
!DEC$ ATTRIBUTES STDCALL :: S_molmas
!DEC$ ATTRIBUTES Alias:'S_molmas':: S_molmas
!DEC$ ATTRIBUTES REFERENCE:: Refr, Res
Character(*) Refr
Real(8) Res
End Function
End Interface
 INTEGER(4) hLib
REAL(8) Res, Output(1)
Logical RetLog
Pointer (Smol, S_molmas)
 hLib = LoadLibrary("REF_CALC32.DLL"C)
Smol = GetProcAddress(hLib, 'S_molmas'C)
print *,'Smol= ',Smol
RetLog = S_molmas('R134a'C, Res)
print *,'RetLog= ',Retlog
Output(1) = Res
PRINT *, 'OUTPUT(1)= ',Res
RetLog = FreeLibrary(hLib)
end program

					
				
			
			
				
			
			
			
			
			
			
			
		
0 Kudos
sabalan
New Contributor I
1,003 Views

Thanks a lot Anthony. My problem is solved now thanks to your sample code. And you know, Anthony and Steve: This is..., I can not find a word! May be "tragic"! Because the only permanent error in my code, while I was trying different combinations (but just not that one which I posted here first), was existens of a comma between the word ATTRIBUTES and the next directive! And this gave the "access violation..." error message all the time! Ghaharrrrharr...

Sabalan

0 Kudos
Reply