using .net dll in visual fortran

using .net dll in visual fortran

hi there.

first of all i have to say that my english is not the best so please be patient hehe.

now to my problem:
normallyi am a .net developer so i am completely new to the fortran world.
but i have to learn it for my job :)

i wrote a class library in to get,send and delete files from a server over ssh. in the library works well.

now i want to use this dll. within fortran and so i wrote an interface to load the dll and uses the specific methods.
heres the code example i already wrote as an console application:

program test

use dfwin, only: LoadLibrary, GetProcAddress

integer(INT_PTR_KIND()) hk32Lib


function SendFileToSSH (host,port,user,passWord,path,fileName,loadFromPath) Result (res)









character(50) host

integer(4) port

character(50) user

character(50) passWord


character(50) loadFromPath

character(1024) res

end function SendFileToSSH

end interface



lpSendFileToSSH = GetProcAddress(hk32Lib,"SendFileToSSH"C)

end program test

if i debugs this code i can see that lpSendFileToSSH getting 0 as value which is probably wrong i think.
can you help me finding out what i have done wrong?

best regards

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

Apparently you have the name aliasing and decoration correct since your program clearly compiles and links OK.

Try terminating all the string arguments with CHAR(0) and see if that works. Visual Basic expects and delivers null-terminated strings.

Please show us the description of the SendFileToSSH routine from VB. I suspect that VB.NET wants a BSTRING rather than a normal character string. You'd have to call routines for creating BSTRINGs that can be found in module IFCOM (or DFCOM if you are using CVF.)

Steve - Intel Developer Support

Hi all and thank you all for your answers.The program itselfs compiles and links correct so there is no syntax error hehe.@Steve:I am sorry but i cant copy the code to the Forum before wednesday. But what i can tell you is, that all parameters are expected as a string except for the port (its an 32bit integer), and the function returns a string with error message if there is any error or "success" if everything worked fine during the transfer.BSTRING means binary string right? does support this as i can set the incoming parameters as a binary string and also can return the string as binary i right with my understanding?

BSTR is a 'Bit string' which, for an n character string, as well as being stored as n characters as 2xn bytes, 2 per character (unicode I believe), stores the number of characters as 4 bytes immediately before the first character. So a BSTR consists of a 4-byte integer string length and a contiguous 2xn byte block of bytes encoding the characters.

Regarding your problem,

what value do you find for the handle given by LoadLibrary ? Is it a valid one i.e. was the library module found?

ALso, do a DUMPBIN /EXPORTS on the DLL listing the exported symbols to check that you are asking for the correct symbol for the routine you want to load.

hi again

loadlibrary does deliver a value to the hk32Lib variable. it looks like an integer value

here you have my function SendFileToSSH:


''' Sends a File to a SSH Server from a local Drive Directory


''' The SSH Server. It can be an Hostname or an IP Address as string

''' The Port where the SSH Connection should go through as integer

''' The Username to connect to SSH Server as string

''' The Password to connect to SSH Server as string

''' The Path where the file should be stored on the SSH Server as string

''' The filename of the file which will be stored on the SSH Server as string.

''' The full Path of the File which should be uploaded to the SSH Server. NOTE: Must be like c:\temp\file.txt

''' Any errormessage as String if anything went wrong. Otherwise Success


Public Shared Function SendFileToSSH(ByVal _host As String, ByVal _port As Integer, ByVal _user As String, ByVal _passWord As String, _

ByVal _path As String, ByVal _fileName As String, ByVal _loadFromPath As String) As String

' Important: It is helpful to send the contents of the

' sftp.LastErrorText property when requesting support.

Dim sftp As New Chilkat.SFtp()

' Set some timeouts, in milliseconds:

sftp.ConnectTimeoutMs = 5000

sftp.IdleTimeoutMs = 10000

' Connect to the SSH server.

' The standard SSH port = 22

' The hostname may be a hostname or IP address.

Dim port As Long

Dim hostname As String

hostname = _host

port = _port

success = sftp.Connect(hostname, port)

If (success <> True) Then

Return (sftp.LastErrorText)

End If

' Authenticate with the SSH server. Chilkat SFTP supports

' both password-based authenication as well as public-key

' authentication. This example uses password authenication.

success = sftp.AuthenticatePw(_user, _passWord)

If (success <> True) Then

Return (sftp.LastErrorText)

End If

' After authenticating, the SFTP subsystem must be initialized:

success = sftp.InitializeSftp()

If (success <> True) Then

Return (sftp.LastErrorText)

End If

' Open a file for writing on the SSH server.

' If the file already exists, it is overwritten.

' (Specify "createNew" instead of "createTruncate" to

' prevent overwriting existing files.)

Dim handle As String

handle = sftp.OpenFile(_path & _fileName, "writeOnly", "createTruncate")

If (handle = vbNullString) Then

Return (sftp.LastErrorText)

End If

' Upload from the local file to the SSH server.

success = sftp.UploadFile(handle, _loadFromPath)

If (success <> True) Then

Return (sftp.LastErrorText)

End If

' Close the file.

success = sftp.CloseHandle(handle)

If (success <> True) Then

Return (sftp.LastErrorText)

End If

Return "success"

End Function

i guess your thinking, that i did not get a value from the function by calling it.

but the general problem is, that the function itself wont be found by calling GetProcAddress.

calling this function always returns 0.

i think that the main problem is, that does not make that .dll "open" to call it from outside the .net framework

After doing a bit of reading around, I find that your VB .Net class library DLL does not export symbols, which is why you cannot find them with GetProcAddress.

I disovered this method for using your class library via a C++ wrapper library detailed here:

You could also rewrite your code using Visual Basic, in which case you can create a Windows DLL from VB as described here (using ChilKatFtp2.dll instead of ChilKatDotnet2.dll)

thx for your answers.

wow...pretty hard stuff isnt it :)

only thing i want to do is directly using ssh connection and transfer files over sftp out of fortran *cry* and theres no easy way to handle this *cry* :)

well...i will read and search through your given links...maybe i will find any solution that works :)

Well, you are trying to do something securely (as that is the purpose of ssh and sftp) and there is nothing easy about software security.



yeah thats right but having some modules that brings the functionality with it would be a lot easier that switching between those programming languages.

but there arent some modules available so doing this way will be the only way...even if its hard :)

Hm, it might be an idea for my Flibs project ( - I just do not know what libraries to use
(yet). Indeed, a decent interface that can be used from a Fortran program would make this a lot more convenient.



i think that would generally be a good idea cause i am sure, that much fortran developers would wish to have a ssh connection in there programs without those "hacking" "wrapping" "switching" methods :)

youll get hero status then hehe

I won't promise anything, but a quick look at the available documentation reveals that it probably
isn't all that complicated. I will have to take a closer look and find some time ...



Have you thought of exposing your VB .NET SSH code via COM interfaces in a Class Library DLL?
You can generate a type library .TLB file from the .DLL and then use the Fortran Module Wizard on the .TLB file to generate code to call the methods. It is quite straightforward as the attached code should show. As a demonstration I built the simplest Class library Dll I could imagine, which contains a single function to return the name of the class.
The VB .Net code for the application (which I called ClassLibrary1) is

Public Interface AnInterface
Function WhoAmI() As String
End Interface
Public Class Class1
Implements AnInterface
Public Function WhoAmI() As String Implements AnInterface.WhoAmI
Return "Class1"
End Function

End Class

In Visual Studio, under Application properties...assembly information.. tick the "Make assembly COM-visible" box and build the application, which creates the ClassLibrary1.DLL.
Then you must use the tool REGASM.EXE (normally found in one of the Framework folders in C:\windows\Microsoft.NET\) to both generate the type library .TLB file and register the TLB and add the DLL to the codebase using

REGASM /TLB /CODEBASE ClassLibrary1.dll

To access this class using Fortran, I created the simple console program that is attached. First though, I used the Fortran Module wizard on the ClassLibrary1.TLB file to generate the code required to handle the calls to the Class1 methods exposed to COM (the only one being WhoAmI). This generated module I called Class1Library and the code is in file Class1Library.f90. By using the CLSID and IID values given in this module, made available via the USE Class1Library statement, it is possible to create an instance of Class1 and call the 'AnInterface' method . As you should see, the code worked OK, the character string 'Class1' being returned, as desired.

All you now have to do is create a VB .NET class containing your SSH routine(s) and expose them using 'Interface' and 'Implements' statements in the VB code. After generating the Class Library .DLL, generating the TLB and registering it, then generating a Fortran Module using the Fortran Module wizard, you should be able to call your SSH functions from Fortran by using appropriate calls to the module code.

Hope this helps!

P.S. I used Visual Basic 2008 Express edition to generate the class library .DLL


Download testvbcom.zip227.64 KB

I was referring to the library "libssh" that I found on the Internet - it is a (probablyhigh-level) interface
to the SSL protocol with some interesting components. A Fortran interface to that library should not be
too much work and you would get access to things like running a remote command or copying data without
too much fuss. (Right now though, I am missing a few auxiliary libraries that are use by libssh, so I could
not make progress.)



My post was aimed at the original poster.

Sure, I understood that and I was replying to my own post ;). The sequential display hid that (mine was reply #16 to reply #14).



Just to show that what I posted was not a fluke, I added a new Interface to access the ChilKat SFTP object after adding a reference to the ChilkatDotNet2.dll and the ChilKat licence .DLL to the VB .NET project so that I could unlock the SFTP object (since I am using it for the free trial period). The new code is:

Public Interface AnInterface
WhoAmI() As String
SendFileToSSH(ByVal a_host As String, ByVal a_port As Integer, ByVal a_user
As String, ByVal a_passWord As String, _
a_path As String,
ByVal a_fileName As
String, ByVal
a_loadFromPath As String)
As String

End Interface

Public Class Class1
Public Function WhoAmI() As String Implements
function WhoAmI entered OK")
Return "Class1"
End Function

Public Function SendFileToSSH(ByVal
a_host As String,
ByVal a_port As
Integer, ByVal
a_user As String,
ByVal a_passWord As
String, _
ByVal a_path As
String, ByVal
a_fileName As String,
ByVal a_loadFromPath As
String) As String Implements

sftp As New
isUnlocked As Boolean
arguments As String
Dim cr As String
cr = Chr(10) + Chr(13)
arguments = "Hostname="
+ a_host + cr + "Port number=" +
Str(a_port) + cr + "User=" +
a_user + cr + "Password=" +
arguments = arguments + cr + "Path=" + a_path + cr + "Filename=" + a_fileName + cr + "Loadfrompath=" + a_loadFromPath
MsgBox(arguments, MsgBoxStyle.OkOnly, "Function SendFileToSSH Entered")

isUnlocked = sftp.UnlockComponent("AnythingWorksFor30DayTrial")
' Set some
timeouts, in milliseconds:
sftp.ConnectTimeoutMs = 5000
sftp.IdleTimeoutMs = 10000
(isUnlocked <> True) Then
"Failed to unlock Chilkat SFtp object!"
"Chilkat SFtp object Unlocked OK!"
End If
End Function

End Class

After re-building the class library DLL, reregistering it and recreating the type library file, then reprocessing the .TLB file using the Fortran Module Wizard and adding the new module to my Fortran project, could access the new interface to SendFileToSSH using the code

! Call the Class1 method 'SendFileToSSH'
a_path="c:\program files"
iret=$AnInterface_SendFileToSSH(iobject, a_host, a_port, a_user, a_passWord, a_path, a_fileName,
a_loadFromPath, retchar_SendFileToSSH)

print *, "SendFileToSSH Returned character string =", TRIM(retchar_SendFileToSSH)

The console output, attached, showed that the call to SendFileToSSH returned OK (the diagnostic messagebox opened by SendFileToSSH had already confirmed that the function had been successfully accessed).


Download consoleoutput.jpg11.68 KB

hi again.

first of all a BIG BIG thank you to anthonyrichards.
thats really a great solution and seems pretty easy to me.

but the problem that i have now is that the intel fortran module wizard always breaks with the errormessage "attempted an unsupported operatio" on step 2/2.

i am a bit unsure which members i have to select to add but i tried many ways, always with the same result that the wizard breaks with the errormessage.

any ideas?

seems to be a win7 problem.
tried it on a win xp machine and the module was created by using the .dll file
using the .tlb file also not worked.

Steve Lionel will have to help with the wizard error message I'm afraid.

I have to admit I still do most of my work using Compaq Visual Fortran 6.6 (which uses the Developer Studio IDE, AKA Microsoft Visual Studio) so I used the Fortran module Wizard that came with CVF. I also am running WIndows XP pro. I used the module WIzard on the .TLB file.

I will bite the bullet and try and create an IVF version of the module as part of an IVF project. Perhaps I will just import the CVF project and see what happens. I will keep this thread posted.

P.S. When using the CVF Module wizard, when offered several choices (of .DLL, of .TLB and of the type of information it contained e.g. COM), I ticked the box that indicated I was targeting a .TLB containing COM information.. Were you presented with similar choices? What choice did you make?

Well this is what happened:

Firstly, I created an empty Console project in IVF.
Then I added the original Fortran files, unchanged, to the project.
I tried to compile the original Module file (that I had created using the CVF module wizard on the .TLB type library file output by REGASM operating on the VB .NET .DLL)

There were 17 error messages.

These were all caused by IVF objecting to my using the Fortran code generated by the
CVF Module wizard. In particular it objected to every use of the REFERENCE
attribute to the character arguments 'on this platform', whatever the latter means.

So I used the IVF module wizard to
generate a new module, using the original .TLB type library.

I followed the instructions, browsed to the TLB file and had no problem doing this.

You can see that the IVF wizard generates code
which gives the VALUE attribute to the character variables instead of a REFERENCE attribute. I removed the original module from the project and added the new module to my new IVF console project, to accompany my
original UNCHANGED Fortran calling program, and
compiled and built the solution, which built OK this time with no error or warning messages.

When I ran the application (start without debugging), it ran
Ok, somewhat surprisingly given the change in ATTRIBUTE for the character
string arguments! The console output is attached as proof.

P.S. Bothe DEBUG and RELEASE configurations built and ran OK.


Download IVFModulewizardoutput.pdf220.84 KB

Just in case your Module Wizard problems are finger problems, the attached file shows the exact steps I took to regenerate the module from the .TLB file using the IVF Module Wizard:


Download IVFMODULEUSE.pdf111.45 KB

The Microsoft* Visual Studio 2008 Shell does not include the ability to convert projects from Compaq* Visual Fortran. Is it possible this is the version of Visual Studio you are using?



Attaching or including files in a post

sorry for my very very late response about that topic.

i already done it with sharpSSH which is a free open source library written in c# from tamir gal.

it works great now.
i can send, get, rename and delete files directly out from fortran which are all requirements we need.

if anyone needs the module so please let me now :)

Leave a Comment

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