How to create Fortran DLL compatible with Visual Basic.Net

498 views
Skip to first unread message

MikeC

unread,
Jul 27, 2006, 6:26:46 AM7/27/06
to gg95
I am trying to link fortran math routines so they can be called from
within visual basis.

Unable to accomplish this without getting runtime errors

Have tried creating the DLL's using the g95 compiler and need to see if
the problem is on the g95 side or the VBNet side.

Can the g95 compiler create a windows VB.net compatible DLL and if so
can anyone please send me a typical command sequence, a sample snippet,
or point me to some tech literature.

Any help is appreciated as VB.Net is pathetic when it comes to math
operations and fortran is the only way to go. A typical problem I run
takes less than a minute under Fortran and almost two hours under
''compiled" basic.

Thanks

Mike C.

ep...@juno.com

unread,
Jul 27, 2006, 7:01:22 PM7/27/06
to gg95
MikeC wrote:
> I am trying to link fortran math routines so they can be called from
> within visual basis.
>
> Unable to accomplish this without getting runtime errors
>
> Have tried creating the DLL's using the g95 compiler and need to see if
> the problem is on the g95 side or the VBNet side.
>
> Can the g95 compiler create a windows VB.net compatible DLL and if so
> can anyone please send me a typical command sequence, a sample snippet,
> or point me to some tech literature.

Yes. See the examples below. This is adapted from a set of programs
that I wrote for VB or VBA. It has a form with 3 text boxes and 1
command button. Pressing the button puts the difference between the two
(integer) numbers in the first two boxes into the third box.

To work with VB/VBA you need to use the MinGW version of G95. I do not
know if this applies to .NET but I have stuck with MinGW instead of
Cygwin.

I created a form using vb.net 2005 express which has some differences
from VB/VBA:

controls must be on a panel
integer is 32 bits (not long)
ByVal is default (not ByRef)
TextBoxN.text (not TextN.text)
requires dot net framework 2.0

Here is the VB side:

module declaration:

Declare Function isub2 Lib "test.dll" (ByRef i As Integer, ByRef j As
Integer) As Integer

event procedure:

TextBox3.Text = Str(isub2(Val(TextBox1.Text), Val(TextBox2.Text)))

Here is the g95 side:

use MinGW version of g95 (I used the experimental 4.1)

test.f95:

function isub2(i,j)
isub2=i-j
end

test.def:

EXPORTS isub2=isub2_

make.bat:

g95 -s -shared -mrtd -o test.dll test.def test.f95

-mrtd sets the calling convention to stdcall. I have not read the docs
in detail for VB.NET but dlls created for VB/VBA generally work with
.NET. Note that passing strings and probably arrays or array slices may
be a different story. I do know that you can pass reals as well as
integers. Name mangling is different if you use modules or module
procedures.

Note that you are also limited under VB/VBA in what you can call in the
Fortran Run time library. Console I/O causes the program to quit early
but file I/O is fine. I don't know about .NET but I suppose it is the
same.

-- elliot

MikeC

unread,
Jul 28, 2006, 9:23:59 AM7/28/06
to gg95
Elliot

Thanks for the help

Dis everything as you outlined and checked the DLL with Microsoft
Depends - the Export data, ordinal etc all checked out OK.

However, when running as soon as the call occurs I get an "Unable to
load DLL filename" exception

Something must be wrong on the VB side so I tried referencing it within
Vb but can't as it is not a valid net or COM object.

Tried copying the DLL to the system and root paths along with the
project path - still no good.

Any idea on how to get net to recognize it?? What did you do in VB
6(?) to "register" the dll ??

Thanks

Mike C.

ep...@juno.com

unread,
Jul 28, 2006, 5:50:40 PM7/28/06
to gg95

I can not remember where I put the DLL as part of a project. When I
built an .EXE I put the DLL in the same folder as the .EXE and it
worked there. That's all I had to do. Try that first.

-- elliot

MikeC

unread,
Jul 28, 2006, 6:00:50 PM7/28/06
to gg95
Elliot

Thanks, figured out the problem. Trial and error. Finally determined
that when working within the NET IDE development environment the dll
had to be placed in the bin folder - not the project folder where
everything else is. Typical of Microsoft.

Your instructions worked great - thanks so much. Was able to link about
20 fortran subs into one dll.

I also figured out how to pass a 2D arrays from VB.Net to FORTRAN and
return the results. What a difference. The math test running under VB
took in some cases 2+hours. The same test case I just ran using the
Fortran dll called from basic took less than 30 seconds and gave me the
right results.

Thanks again.

Mike C.

John E.

unread,
Aug 3, 2006, 2:20:47 PM8/3/06
to gg95
Do you know anyway of passing the path and name of an input file to
FORTRAN from vb2k5?

MikeC

unread,
Aug 20, 2006, 5:59:13 PM8/20/06
to gg95
John

Sorry to not answer sooner. Out of town for a while

I have the same problem trying to work with strings.I've been able to
deal with arrays, int, dbls, and characters but not strings so far.

Will let you know if I can find a way

Mike C.

MikeC

unread,
Aug 20, 2006, 6:00:11 PM8/20/06
to gg95

John E.

unread,
Aug 20, 2006, 6:24:15 PM8/20/06
to gg95
Thanks Mike, Its actually a double problem I'm running into though, the
first is the one I mentioned, the second is finding the entry point the
fortran dll. I think I've solved the first one, but I wont' be able to
check until I can get the second to cooperate. If you find a solution
let me know, but as far as I can tell, the string solution is solved
easily enough by simply declaring a recieving variable on the Fortran
end that is larger than you'll ever need. Yeah its clunky, but its
simple and it should work.

John

Reply all
Reply to author
Forward
0 new messages