1
votes

I am trying to link a Fortran dynamic-link-library (DLL) with a VB.NET application. The subroutine contained in the DLL is supposed to modify a value c provided in the VB side, but it is not doing it: it keeps c as it is.

Here is the Fortran code:

SUBROUTINE prueba_int(m,n,p)
!DEC$ ATTRIBUTES DLLEXPORT, DECORATE, ALIAS : "prueba_int" :: prueba_int
!DEC$ ATTRIBUTES REFERENCE :: m,n
IMPLICIT NONE
INTEGER, INTENT(IN) :: m,n
INTEGER,INTENT(OUT) :: p

p=m*n

END SUBROUTINE

And here is the VB.NET section:

Public Class Form1
Public Declare Sub prueba_int Lib "Dll2.dll path" (ByRef m As Integer, ByRef n As Integer, ByRef p As Integer)
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
    ofd.Filter = "(*txt)|*.txt"
    If (ofd.ShowDialog() = DialogResult.OK) Then
        txtArray.Text = ofd.FileName
    End If
End Sub

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
    Dim a, b, c As Integer
    a = CInt(txt1.Text)
    b = CInt(txt2.Text)
    c = 1
    Call prueba_int(a, b, c)
    MsgBox(c) 'Only to check c's value
End Sub
End Class

I have already tried using LONG instead of INTEGER, but the problem remained. Can anybody give me a clue about this issue?

EDIT: Both the VB.NET and the FORTRAN codes were programmed using Visual Studio 2015 with the Intel Fortran Compiler (Parallel XE 2017). The DLL was built using the STDCALL(/iface:stdcall) calling convention.

1
I'm not big on Fortran, so take what I say with a grain of salt... but you've got m and n marked as IN and p marked as OUT. But in the vb they are all declared ByRef. I would have thought m and n should be ByVal and only p would be ByRef. - Erik Westwig
Thanks for answering, @ErikWestwig! I was using precisely ByVal with m and n, but if I do so, the exception System.AccessViolationException arises, accompanied with a warning about trying to overwrite protected memory. Using ByRef on them was the only way to avoid it. - MSalmer
I think you have to declare p as INTENT(INOUT) if you want to change the value of c. - M. Chinoune
Hi, @M.Chinoune! I have made the change to INOUT, but it is still failing. - MSalmer

1 Answers

0
votes

I have already found the solution. The only thing I did was changing the export line with this one:

!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, REFERENCE, ALIAS : "prueba_int" :: prueba_int

!DEC$ ATTRIBUTES REFERENCE :: m,n

Now it works! This thread was useful, if someone else has the same problem: https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/276310