Calling Fortran subroutine dll from VB.NET 2010

Calling Fortran subroutine dll from VB.NET 2010

Hi Everyone,

VB.NET

Public Declare Auto Sub MyDll Lib "MyDll.dll" (ByRef STR_IN As String, ByRef DBL_OUT As String)

Dim str as String="Praveen"

Dim str1 as String

Call MyDll(str,str1)

Fortran

subroutine MyDll(STRING_IN, DBL_OUT,strl)
IMPLICIT NONE
!DEC$ ATTRIBUTES DLLEXPORT::MyDll
!DEC$ ATTRIBUTES STDCALL,ALIAS:'MyDll' :: MyDll
CHARACTER(10), INTENT(IN) :: STRING_IN
!DEC$ ATTRIBUTES REFERENCE :: STRING_IN
CHARACTER(10), INTENT(OUT) :: DBL_OUT
DBL_OUT=STRING_IN // " A"
RETURN
end subroutine MyDll

The above code is not working and I am getting system violation exception...

Any Help Plz............

Praveen

Praveen
6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

This is the correct subroutine

subroutine MyDll(STRING_IN, DBL_OUT)
IMPLICIT NONE
!DEC$ ATTRIBUTES DLLEXPORT::MyDll
!DEC$ ATTRIBUTES STDCALL,ALIAS:'MyDll' :: MyDll
CHARACTER(10), INTENT(IN) :: STRING_IN
!DEC$ ATTRIBUTES REFERENCE :: STRING_IN
CHARACTER(10), INTENT(OUT) :: DBL_OUT
DBL_OUT=STRING_IN // " A"
RETURN
end subroutine MyDll

Praveen

You need to use ByVal for the strings on the VB side. You also need to either make sure that the VB passed string length matches what you declare on the Fortran side, or pass the length separately.

Steve - Intel Developer Support

Edited Code

FORTRAN:

subroutine MyDll(STRING_IN, DBL_OUT,STR_LEN)
IMPLICIT NONE
!DEC$ ATTRIBUTES DLLEXPORT::MyDll
!DEC$ ATTRIBUTES STDCALL,ALIAS:'MyDll' :: MyDll
INTEGER*4, INTENT(IN) :: STR_LEN
CHARACTER(LEN=STR_LEN), INTENT(IN) :: STRING_IN
CHARACTER(LEN=STR_LEN), INTENT(OUT) :: DBL_OUT
!DEC$ ATTRIBUTES REFERENCE :: STRING_IN,DBL_OUT
DBL_OUT="helllo"
RETURN
end subroutine MyDll

VB.NET:

Public Declare Auto Sub MyDll Lib "MyDll.dll" _
(ByVal STR_IN As String, ByVal DBL_OUT As String, ByVal STR_LEN As Integer)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Try
Debug.Print("started")
Dim str As String = "Praveen"
Dim str1 As New String("@", 7)
Call MyDll(str, str1, str.Length)

Debug.Print(str1)
Debug.Print("completed")
Catch ex As Exception
Debug.Print(ex.ToString)
End Try
End Sub

OUTPUT:

started
敨汬潬† @@
completed

What's wrong in the code? Help me Plzzzzzzzzzzz

Praveen

It looks as if VB has changed considerably since the VB-Calls-Fortran sample was written. In testing your program, as well as the sample, I see that contrary to the sample comments, VB does NOT send a NUL-terminated character string. In fact it sends a UNICODE string that Fortran won't be able to deal with directly. You'd need to use the various UNICODE conversion procedures in module IFNLS.

I suggest that you look at the sample VB.NET-Safearrays for some examples of this. In the meantime, we need to redo the VB-Calls-Fortran sample.

Steve - Intel Developer Support

I ran across this thread while doing a related search, and I happened
to find a solution...

VB sent unicode characters to the fortran dll because of the "auto"
character set modifier in the Declare statement. Simply removing the
word "auto" from the Declare statement allows the program to work
properly.  Or, changing "auto" to "ansi" (which is the default) also works.

-- Curtis

 

Leave a Comment

Please sign in to add a comment. Not a member? Join today