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

Calling Visual Basic code from Fortran - Anyone?

egil_giertsen
Beginner
4,966 Views

Hi,

I'veended up with a set of formulas originally coded in Visual Basic, that I now need to call from Fortran.

Anyone tried that?

Thanks in advance for any help...

Best regards,

8-) Egil

Egil Giertsen
Senior Research Scientist
Phone : +47 7359 2081
Telefax: +47 7359 2660
Mobile : +47 9305 8672
_____________________________
MARINTEK SINTEF GROUP
Department of Structural Engineering
Otto Nielsens vei 10
P O Box 4125, Valentinlyst
N-7450 Trondheim
NORWAY
_____________________________
http://www.marintek.sintef.no

0 Kudos
37 Replies
van_der_merwe__ben
New Contributor II
1,755 Views
Assuming your VB dlls is not exposing the code you want to call inside objects, I would personally try and do a LoadLibrary call on it to load it. You can then call GetProcAddress to get the function address, and there is a way to do that using the function ordinal (shown in dumpbin) rather than the name, btw.

We dynamically load DLLs (havent tried VB) and call into them this way and it works fine.

Just make sure you declare your Fortran prototype to be compatible but judging on the mangled name I doubt you have complex arguments. Also, you may have to try a few different calling conventions to get the right one.

Alternatively (and maybe cleaner) create a simple little COM wrapper around the DLL (you can quickly create a COM wrapper using the activescript wizard etc.). Then create an instance of the COM object and call into that. If you have trouble doing that from Fortran code, you can link in a little C code to do and easily call the C code from Fortran.
0 Kudos
g_f_thomas
Beginner
1,755 Views

Sadly, none of this will work with a VB DLL which is a COM thingy or ActiveX DLL as opposed to a classic DLL with exports that can be gotten at via GetProcAddress. So you have to use COM protocol with the assistance of the Module Wizard or figure out how to change an ActiveX DLL into a classic DLL as discussed earlier in this thread.

Gerry

0 Kudos
forall
Beginner
1,755 Views
The Module Wizard handles your item #4. It is not an oversight. I'll try to work up an example, though I'm not experienced at VB or C#.

steve, if you decide to do this example, I now have working code that calls (hardcoded) VB2008 scripts from both C++ and Fortran (both using the COM interface, which for C++ comes naturally but for Fortran has to be extracted using the Wizard).

It took me a while, but its pretty neat! I am quite sure the C# version could be worked out realtively easily by analogy.

I will try to document it better and post it (maybe as some kind of short knowledge-base article? this forum has been very helpful for me and so would be nice to contribute something back if its deemed useful).

The whole experience with writing the VB/C++/Fortran connections was rather instructive! I do wish there was some way of learning this more or less systematically rather than by trial and error. Are these matters subjects of any courses, or books? or is the subject too fast-developing for the written word? I certainly found the VB conventions changed sugnificantly over the last few years so some of the "help files" were more like "confusion files" ..
0 Kudos
Steven_L_Intel1
Employee
1,755 Views
We would be delighted to have you write an article for our Knowledge Base with examples. Let me know how I can help.
0 Kudos
forall
Beginner
1,755 Views
We would be delighted to have you write an article for our Knowledge Base with examples. Let me know how I can help.

Hi Steve,

I sent you a 'message' a couple of days ago - just wondering if it got thru as I had some difficulties making it work... I also tried to attach a very rough draft with a working example. Let me know if its of worth continuing..

thanks,
d
0 Kudos
Steven_L_Intel1
Employee
1,755 Views
I don't get email when private messages arrive, so I didn't see it. Unfortunately, attachments don't work in private messages. Please attach the ZIP to a reply here and I'll be glad to look at it.
0 Kudos
forall
Beginner
1,755 Views
I don't get email when private messages arrive, so I didn't see it. Unfortunately, attachments don't work in private messages. Please attach the ZIP to a reply here and I'll be glad to look at it.

Hi Steve,
see attached. Its rather rough but the example works - I thought I'd let you see it before trying to do much more.
cheers,dmitri
0 Kudos
forall
Beginner
1,755 Views
Quoting - forall

Hi Steve,
see attached. Its rather rough but the example works - I thought I'd let you see it before trying to do much more.
cheers,dmitri

cant seem the get the attachment working here either ...
0 Kudos
Steven_L_Intel1
Employee
1,755 Views
Got it - thanks.
0 Kudos
forall
Beginner
1,755 Views

Hi Steve,

just wondering if you had a chance to have a look at the files and have any feedback?

cheers,

d

0 Kudos
abhimodak
New Contributor I
1,755 Views

While Steve will give you an expert's feedback, this has been quite helpful to me. I encapsulated it in a module and can call any COM object I created using VB.

Currently I am struggling with passing arrays "from" Fortran "to" VB and on keeping the VB form "alive" even after the Fortran program quits. I need to figure out my way through the safe-array business for the former while the latter has stumped me quite a bit.

Abhi

0 Kudos
Steven_L_Intel1
Employee
1,755 Views
Sorry, have not had cycles for this. I have not forgotten you.
0 Kudos
seth_king
Beginner
1,755 Views
Sorry to drag up an old topic, but I am having the same issue and I didn't see the resolution in this forum. I have a DLL, created in VB6 with the functions I want exported, that I need to link into my Fortran code. I can see the functions I want in the dumpbin output and I have created a DEF file, and used that to create a LIB file that I put into my project, but I still get a linker error of unresolved external symbols. Any ideas?

Thanks!
0 Kudos
Steven_L_Intel1
Employee
1,755 Views
You cannot link to a VB DLL - or any DLL. The linker requires an "export library" (.LIB) which VB does not create.

If you want to call a routine in a DLL without an export library, you need to use LoadLibrary and GetProcAddress as demonstrated in the sample DLL\DynamicLoad
0 Kudos
anthonyrichards
New Contributor III
1,755 Views
Created a new console project with the .F90 and the LIB added to the project.
DUMPBIN on the .LIB shows symbols with leading underscores.
Correcting the ALIAS to show this allows me to compile and link.
Registered the DLL and added it to C:\WIndows\system32\ so that it could be found.
Program runs but crashes with access error on the call to PrintTiltlesName
Can you show your Visual Basic function prototype declarations?
0 Kudos
seth_king
Beginner
1,755 Views

Thank you, I guess I should have seen that but couldn't.

I think the crash is related to passing a string into the DLL. Do I need to do this as a safearray? Or a BSTR pointer?

Private Declare Function ApplyNNet Lib "NNetApply.dll" (MyNNetType As String, MyAddress As String, MyInput() As Double) As Variant

Private Declare Sub PrintTitlesName Lib "NNetApply.dll" (MyAddress As String)

0 Kudos
anthonyrichards
New Contributor III
1,755 Views
Passing strings from VB to a Fortran DLL I can do. Getting a string back from a Fortran DLL I can do also.
Going the other way is a real pain. Strings are horrible compard to integers, reals etc.

You want to 'send' a string from Fortran to VB. This means that the memory location (buffer) for the string is allocated on the Fortran side so you need to send the address of the start of that buffer to VB. If VB needs/assumes/stores strings a BITSTRINGS, then you would be wise to cast your character string on the Fortran side as a BITSTRING. There are functions available that do this which will give you a pointer (address) to the BITSTRING. Then all you need to do is pass this pointer to VB.

I find that dealing with adresses makes it easier to see what's happening in memory.
I would try making MyAdress ByRef for a start (and make the argument the pointer with a VALUE attribute on the Fortran side), then post a new DLL. Can you supply a Debug version for test purposes?

P.S. What does PrintTitlesName do?
Can you arrange for the function to display a message box showing what turns up as the argument?


0 Kudos
Reply