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

Unable to read text file from Fortran dll

cento68
Beginner
1,623 Views
I am trying to write a fortran dll that simply opens a file and reads its contents. I have been succesfull in calling the dll from vb .net and pass a series of variables (integer, boolean/logical and string). However, when i try to open a file in the dll, i get the following error message:
Code:
An unhandled exception of type 'System.DllNotFoundException' occurred in ExampleDLLcall.exe
Additional information: Unable to load DLL (F:Studio NETTCAT DLL TestExampleDLLDebugExampleDLL.dll)

This is the vb code:
Code:
Friend Declare Auto Sub ExampleDLL _
    Lib "F:Studio NETTCAT DLL TestExampleDLLDebugExampleDLL.dll" _
    (ByRef SuccessFlag As Boolean, ByRef iPrint As Integer, ByVal FilePath As String, ByVal StringLength As Integer)
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim SuccessFlag As Boolean = False
        Dim MatNum As Integer = 0
        Dim iPrint As Integer = 0
        Dim FilePath As String = "F:Documentsinputs.in"
        Dim StringLength As Long = Len(FilePath)


        ' call dll
        Call ExampleDLL(SuccessFlag, iPrint, FilePath, StringLength)
    End Sub

And this is the fortran code:
Code:
subroutine ExampleDLL(SuccessFlag,iprint,InputFile)

  ! Expose subroutine ExampleDLL to users of this DLL
  !
  !DEC$ ATTRIBUTES DLLEXPORT, STDCALL, REFERENCE::ExampleDLL
  !DEC$ ATTRIBUTES ALIAS : "ExampleDLL" :: ExampleDLL

  ! Variables -------------------------------------
  implicit none
  ! determines success of tcat program execution
  logical(2),intent(inout):: SuccessFlag
  integer,intent(inout):: iprint
  logical(2):: TCAT2flag
  CHARACTER(*),intent(in):: InputFile
  
  integer:: matnum
   
  ! Body of ExampleDLL ------------------------------
  ! set success flag to false
  SuccessFlag = .false.

! read from file
  	open(unit=10,file=InputFile,status='old')
	read(10,*) matnum		!/number of materials
	close(10)

  iprint = matnum
  ! change the internal variable
  TCAT2flag = .true.
  
  ! pass result to output variable
  SuccessFlag = TCAT2flag
   
end subroutine ExampleDLL
I have searched the forums and learned that there are no restrictions in reading files from Fortran dll's so i would be very grateful if you could point out what i am doing so wrong. :smileysad:
Thanks you for your time,
Vincent



0 Kudos
9 Replies
Steven_L_Intel1
Employee
1,623 Views
1. Are you running this on a system that has Intel Visual Fortran installed?
2. Does the PATH environment variable include C:Program FilesIntelCompilerFortran9.0IA32Bin ?
0 Kudos
cento68
Beginner
1,623 Views
Sorry, i forgot to mention that I am using Intel Visual Fortran 9for Studio .NET 2003.
I have looked into the PATH environment variable, and included the directory you suggested through the Tools>Options tab in the IDE and also through windows system (I have Fortran installed in a separate partition as that of the operating system, but i don't think that would be an issue). Unfortunately, the change doesn't seem to work as I still get the same error.
Thanks for the suggestion.
0 Kudos
Steven_L_Intel1
Employee
1,623 Views
The Tools..Option tab is not relevant here. The PATH system environment variable needs to include all the paths where needed DLLs are located.

Download Dependency Walker and analyze your DLL with it. Make sure that all DLLs it identifies are available on one of the folders in PATH.
0 Kudos
cento68
Beginner
1,623 Views
I used Dependency Walker on the dll and did not get any errors. I also checked for the paths of any dependent dlls and all seem to be there.
I've included a snapshot of the DW window.
I'm not too sure how to include multiple paths in the PATH system environment variable if it isn't through the IDE. When i try to use the windows (system>advanced>environment variables) i can only add one path...
I updated the PATH environment variables through the Modify option in Intel Visual Fortran add/remove panel, and compiled again. It still doesn't seem to work but i get a different error message. I have included the message in the OutputInfo.jpg file. I'm trying to see if i can figure out something from the message, but so for nothing new.
I'm still puzzled why the open statement forces the dll call to fail :smileysad:
Any further suggestions would be greatly appreciated.
Thanks again.
0 Kudos
Steven_L_Intel1
Employee
1,623 Views
Well! This error mesage is VERY different than the one you reported initially. The default path for open files is the folder the executable is in. You can see the path it is looking for in the error message. You should be able to figure out how to solve this from here.
0 Kudos
cento68
Beginner
1,623 Views

Thanks for the suggestion.

I followed your advice. I placed the file in the active directory (the default one shown in the message). However, it still didn't work. So insead, I changed the current active directory to one with a shorter path string and instead of reading from a file i tried to write this time. There was no error, but i discovered thatwhen i pass thestring variable (the path of the file to read) to the Fortran dll, it is only thefirst letter that it used in the open statement. Hence, instead of'inputs.in' it is using just 'i'.

I must be doing something wrong when i passthestringvariable tothe fortran dll?

Either way, thank you for your constant help.

0 Kudos
Steven_L_Intel1
Employee
1,623 Views
Either the length is not being set correctly or it is not received by Fortran. I would execute this in the debugger (need to enable debugging of unmanaged code in the VB.NET project) and see if the length value is correct in VB and how it is received in Fortran.
0 Kudos
jim_dempsey
Beginner
1,623 Views
>> instead of'inputs.in' it is using just 'i'.
This sounds like you are passing "filename" declared as:
character :: filename(filenamesize)
instead of
character(filenamesize) :: filename
Jim Dempsey
0 Kudos
jim_dempsey
Beginner
1,623 Views

Look at the caller to

subroutine ExampleDLL(SuccessFlag,iprint,InputFile)

to see what InputFile is

0 Kudos
Reply