Topic: Calling fortran dll from Visual Basic.net (VS 2019)
OK, it is 2022 and I am trying to relearn how to interface fortran dynamic link libraries with visual basic.net (relearn as I used to be able to do this, but it was decades ago). The overall objective is to send an integer to the function in the fortran dll, add 1 to it and return the value (and of course the real objective is to figure out how to get VB.NET to play nicely with a fortran dll).
So I have a fortran dll that complies with no errors or warnings; here is the source code:
Function dllinteger(i) bind(c)
use ISO_C_BINDING
implicit none
cGCC$ ATTRIBUTES STDCALL, DLLEXPORT :: dllinteger
integer(kind=c_int)::dllinteger !must declare the function type
integer(kind=c_int), value::i !declare integer
dllinteger=i+1 !increment i by 1
end function dllinteger
Note that the Microsoft specific complier flags mentioned in the Simply Fortran blog about creating fortran dlls that work with Microsoft compilers were used when compiling.
Now here is the VB.NET code (under Visual Studio 2019):
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnCallDLL.Click
Dim n As Int16 'input integer
Dim response As Int16 'response from dllinteger function
n = CInt(txtInteger.Text) 'convert integer in text format to an integer
response = dllinteger(n) 'response = value returned by dllinteger function
lblResult.Text = CStr(response) 'convert response integer to text and display in a label
End Sub
Private Function dllinteger(n As Short) As Short
Return dllinteger
End Function
End Class
So Form1 has a textbox that accepts an integer, converts it from text to integer, calls the function and then converts the response to text and shows the result in a label. Note that the fortran-library.dll has been added to the VB solution by adding via Solution > Add > Existing item. Likewise the 'dllinteger' function is also specifically noted in the solution file. It compiles with no errors or warnings.
When you run the VB solution, you can enter an integer, hit the button, but the value of 'response' is always zero. But, no runtime errors are noted.
Any input appreciated, particularly if it turns out I made a dumb mistake (or more). I will note that the blog post on creating a fortran dll to work with Microsoft compilers specifically noted the use of lib.exe to create a .lib file, but the details were somewhat lacking on exactly how to do this, and where/how the .lib was supposed to be stored/implemented.
Thanks.