Passing & returning VB Net strings to Fortran dll

411 views
Skip to first unread message

MikeC

unread,
May 18, 2007, 5:03:27 PM5/18/07
to gg95
For Info

Found a simple way to pass a string that can be modified within
Fortran and returned

'within VB Class
'declare the exrternal dll
'Public Declare Sub ProcedureName Lib "ProcedureName.dll" _
' ( ByRef TRANSFER As Byte, etc)
'
'within body of VB net Program
'create a byte [char] array from string variable & terminate with a
null character
'Dim CHRSTRING As Byte() =
System.Text.Encoding.ASCII.GetBytes(TESTSTRING & Chr(0))

'pass pointer to first element
'Call Class.ProcedureName(CHRSTRING(0), etc)

'The Pass must be ByRef.
'
'Upon return from the Sub FORTRAN returns the byte array [string]
'with the null character [Chr(0)] replaced with Chr(32).

'FORTRAN [dll]
'SUBROUTINE ProcedureName( TRANS, etc )
'* .. Scalar Arguments ..
' CHARACTER*( * ) TRANS

If anyone has an easier or better way let me know

Regards

MikeC

Ken

unread,
May 23, 2007, 3:22:52 PM5/23/07
to gg95
Interesting idea. I can't seem to get it to work in VB/VBA - I keep
getting a 'BAD DLL calling convention' error. I need to play with it
more...

Question - doesn't just sending the string + hidden length work?

MikeC

unread,
May 31, 2007, 9:20:12 AM5/31/07
to gg95
Ken

Can't send a string directly as a net string is Unicode 2 byte based
so it must first get decoded into a single byte ASCII array.

You got me thinking so now I pass the array by ref to the first
element AND a length

Dim SourceErrorString As String = "--NONE--"
Dim LenCS As Integer = SourceErrorString.Length
Dim SourceErrorChar As Byte() =
System.Text.Encoding.ASCII.GetBytes(SourceErrorString)

Call LAPACK.dgesvx(SourceErrorChar(0), LenCS, etc, etc)

In Fortran the string is declared as a byte (CHAR) array with the
length as passed ie

SUBROUTINE DGESVX( FACT, TRANS, N, NRHS, A, LDA, AF, LDAF, IPIV,
$ EQUED, R, C, B, LDB, X, LDX, RCOND, FERR,
BERR,
$ WORK, IWORK, NOITER, INFO, ERRINFO, LEN )

CHARACTER (LEN) ERRINFO

within the Fortran sub I do whatever I want to a local
CHARACTER(say 10) LocalSTRING

and when done assign it to the passed string for return

ERRINFO = LocalSTRING

If the Local string is shorter, the ERRINFO is padded to its LEN with
CHR(32) by Fortran and returned

If its longer its chopped to the length of LEN and returned

When its returned I just decode it back to a Unicode string and chop
the blanks added

Dim enc As Encoding = Encoding.ASCII
SourceErrorString = enc.GetString(SourceErrorChar, 0, LenCS)

This might work in VB as well if you can find a way to convert from
two byte strings to single byt vs a vs,

Mike

Reply all
Reply to author
Forward
0 new messages