Migrating Fortan call from VBA to .NET

Sneezo

New member
Joined
Nov 1, 2009
Messages
3
Programming Experience
1-3
Hello all

I'm a newbie to the forum and somethign of an amatuer - programming is not my primary discipline although I dabble occassionally.

I'm currently trying to interface some Fortran routines into a 3rd party environment by creating a VB.NET DLL. I don't have accessto the Fprtran code because its considered proprietary.

As a starting point, I demonstrated that I could make the call satisfactorily from VBA using the following code:

======================================================
Declare Sub INTERFACE_VB Lib "C:\FortDir\FortFile.dll" _
(ByVal SV As String, ByVal intSV As Long, _
ByRef IV As Long, ByRef RV As Single, ByRef intIERR As Long)

Dim SV As String * 5120
Dim IV(1 To 1280) As Long
Dim RV(1 To 16000) As Single
Dim IERR As Long

[set up arrays]

Call INTERFACE_VB(SV, Len(SV), IV(1), RV(1), IERR)
======================================================

Now, to get the same thing working in .NET, I nede to make some changes:
1. Arrays rebased to 0
2. Use Integer instead of Long because of the change in size
3. No fixed length strings

However, I just can't get the Fortran to return the same values! I have made many attempts, with the following being typical:

======================================================
Declare Sub INTERFACE_VB Lib "C:\FortDir\FortFile.dll" _
(ByVal SV As String, ByVal intSV As Integer, _
ByRef IV As Integer, ByRef RV As Single, ByRef intIERR As Integer)

Dim SV As String
Dim IV(1279) As Integer
Dim RV(15999) As Single
Dim IERR As Integer

[set up arrays]

Call INTERFACE_VB(SV, Len(SV), IV(0), RV(0), IERR)
======================================================

Because I am unable to debug the Fortran I am finding it very difficult to work out where the error is:

- Is it to do with string characters - Unicode vs ANSI?
- Is it to do with array handing?
- Do I need to specify marshaling?

Thanks in advance for any suggestions!
 
It might be an issue with the fixed-length string. As you know, there's no such thing as a fixed-length string in VB.NET but there is an attribute that you can apply to a string to make it appear fixed-length to external functions that require it. Try declaring your string like this and see if it makes a difference:
VB.NET:
<VBFixedString(5120)> Dim SV As String
 
Thanks. I suspect this is the right track. I didn't include these lines in my original post but the (inherited) VBA string array is set up very unusually:

StartInt = 73
EndInt = 136
Mid(SV, StartInt, EndInt) = FileDir

I'm really not sure what this is doing (for a start the third argument should be the length rather than the end-point), but this line does produce a SV that is accepatble to Fortran. If I try to examine SV after this line:

Mid(SV, StartInt, EndInt) 'returns the correct string without leading spaces, as expected
SV 'doesn't return anything!!!
SV(73) 'doesn't return anything!!!
Mid(PYROSV, 72, 80) 'doesn't return anything
Mid(PYROSV, 73, xx) 'returns the correct string without leading spaces, if xx is > Len(FileDir)

I can't use this syntax in VB.NET so I need an alternative. It is likely that my alternatives are sending different strings to Fortran...

I'll try the VBFixedString approach but will still need to establish an equivalent of the odd looking Mid function.
 
What that Mid call is doing is setting the values of the characters from index 73 to index (73+136) to the values of the characters in the FileDir variable. You can do the same thing in VB.NET as the Mid statement is supported. Normally I'd recommend against it but in this case it seems appropriate. The thing is, you have to have characters at those positions in order to set them and if you have assigned a value to 'SV' yet then you've got a problem. Maybe try actually creating a string of the desired size first.
VB.NET:
<VBFixedString(5120)> Private SV As New String(" "c, 5120)
Note that it must be a member as local variables cannot have attributes applied.
 
Thanks.

The key was to initialise the fixed length strings. I tried this line previously but without initialising the string, it didn't compile. I wrongly concluded that it wasn't supported.

I was also somewhat confused by the VBA version that didn't include initialisation of the strings but still worked.

In VBA, if after:

Mid(SV, 73, 137) = FileDir

I couldn't work out why:

SV => seemed to be blank
Mid(SV, 72, 80) => seemed to be blank

I guess the unitialised characters got in the way.

More confusing still is that if I try to create SV in other ways, the Fortran call wouldn't work. For example:

SV = " "
SV = SV & FileDir

SV in this expression was defiend as a String, which I guess was the difference. What is it about fixed length strings that Fortran might interpret differently from a normal string?


Anyway, I think I'm good to go now - thanks for your help.
 
Back
Top