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

locate dll and load it into memory in FORTRAN

orangecccc
Beginner
739 Views
Question:
I'm using CVF6.6. I'd like to locate dll and load it into memory.The dll was provided by someone. There was no error to compile and link it. But the program was stopped on the Call Statement (call DefineProblem300(strDir,IE,ErrorMsg)). The dll wasn't loaded. My FORTRAN program is in below. Do you have any suggestions to locate dll and load it properly? Thanks in advance.
.
ProgramTest
use kernel32
pointer (p,DefineProblem300)
! Declare an interface block to the routine to be called.
interface
Subroutine DefineProblem300(FName,IE,ErrorMsg)
!DEC$ ATTRIBUTES DLLIMPORT:: DefineProblem300
character(80) FName, ErrorMsg
integer(4) :: IE
end
end interface
! First, locate the dll and load it into memory
p = loadlibrary("e:/test1/blm.dll")
if (p == 0) then
type *, "Error occurred opening blm.dll"
type *, "Program aborting"
goto 1000
endif
! Set up a pointer to the routine of interest
q1 = getprocaddress(p, "DefineProblem300")
if (q1 == 0) then
type *, "Error occurred finding DefineProblem300 in blm.dll"
type *, "Program aborting"
goto 1000
endif
!
strDir = "e:/test1/dll.dat"
call DefineProblem300(strDir,IE,ErrorMsg)
....
end

Message Edited by orangecccc on 04-05-200603:22 PM

0 Kudos
10 Replies
Steven_L_Intel1
Employee
739 Views
Two problems I see are that your filespec has forward slashes instead of Windows backslashes and you're not using C-strings so the Win32 APIs have no idea how long the strings are. Remember also that when you do put backslashes in a C-string, you have to double them, like so: "e:test1blm.dll"C

Also, do not use DLLIMPORT on the interface to a routine you access through LoadLibrary/GetProcAddress. You have not declared q1.
0 Kudos
orangecccc
Beginner
739 Views

Steve,

Your suggestion was very helpful. Now the DLL was loaded.

But when I debugged the program, I've got an error 'Unhandled exception in test.exe (BLM.DLL): 0xC0000005:Access Violation' on the call statement (call DefineProblem300(strDir,IE,ErrorMsg)).' Do you have any idea for the error?

How can I declair q1?

The dll.dat is input data for the DLL. Do you think my program is O.K?

Thank you for your help again.

Message Edited by orangecccc on 04-06-200609:44 AM

Message Edited by orangecccc on 04-06-200609:47 AM

0 Kudos
Steven_L_Intel1
Employee
739 Views
Replace:
pointer (p,DefineProblem300)

with:
pointer (q1,DefineProblem300)
integer(int_ptr_kind()) p
0 Kudos
orangecccc
Beginner
739 Views

Steve,

When I changed my code from your suggestion, I've got an compile error as follows:

Error: Multiple declaration of a DEC Fortran pointee.[DEFINEPROBLEM300]
pointer (q1,DefineProblem300)

0 Kudos
Steven_L_Intel1
Employee
739 Views
You must have added a declaration of q1 before, then. Remove the one you added earlier.
0 Kudos
orangecccc
Beginner
739 Views

Thank you Steve.

Please advise me how to link(?) the DLL and input data (dll.dat). Please see my Fortran code in my previous message. I'm almost getting there. Thanks,

Message Edited by orangecccc on 04-06-200611:18 AM

0 Kudos
Steven_L_Intel1
Employee
739 Views
You don't link the DLL, because you're loading it dynamically. I don't understand the question about the data file - it seems to be just a filename you're passing to the routine. I assume the routine opens the file. Be sure that the called routine has an interface compatible with what the call is supplying. That means, based on what you've coded here, the C calling mechanism and the character length at the end of the argument list.
0 Kudos
orangecccc
Beginner
739 Views
Steve,
Thanksfor your quick reply. Attached please find the zip file, DLL.zip. There are two input files and af90 code. Please review them. There was no compile and link error, but the program was stopped with the following error message:
fort1:severe<157>:Program Exception - access violation
Thanks,

Message Edited by orangecccc on 04-06-200604:05 PM

0 Kudos
Steven_L_Intel1
Employee
739 Views
I can't do anything with this. You didn't supply the DLL nor do I have the faintest idea what the DLL is expecting to be passed. You should be able to tell in the debugger if it even gets into the DLL routine, and which call is causing the problem.
0 Kudos
orangecccc
Beginner
739 Views

Steve,

Here is the dll file as an attached. Could you check what I have a problem?

When I debugged the code, the program was stopped the following line:

call DefineProblem300(strDir,IE,ErrorMsg)

This call is to read the input data file, *.dat. Many thanks,

Message Edited by orangecccc on 04-06-200604:14 PM

Message Edited by orangecccc on 04-07-200610:22 AM

0 Kudos
Reply