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

Passing a string to a sub

juanflozoya
Beginner
596 Views

Dear Intel Comunity

I am trying to run or calling a dll in VB6, my FORTRAN listing is:

SUBROUTINE fsub (x [VALUE])

!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:'fsub' :: fsub

INTEGER*4 x

x = x + 1

END SUBROUTINE fsub

integer*2 FUNCTION ffunc (y )

!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:'ffunc' :: ffunc

INTEGER*2 y

ffunc = y + 1

END function ffunc

SUBROUTINE fstring (fstr)

!DEC$ ATTRIBUTES DLLEXPORT,STDCALL,REFERENCE,ALIAS:'fstring' :: fstring

CHARACTER*20 fstr

fstr = 'Juan el bueno'

END SUBROUTINE fstring

Linker line:

/OUT:"Debug/Dll6.dll" /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:"D:Basura2Dll6Dll6debugdll6.dll.intermediate.manifest" /DEBUG /PDB:"Debug/Dll6.pdb" /SUBSYSTEM:WINDOWS /IMPLIB:"$(OUTDIR)/Dll6.lib" /DLL

And my VB code is:

Public Class Form1

Public Declare Sub fsub Lib "D:Basura2Dll6Dll6Debugdll6.dll" (ByRef x As Long)

Public Declare Function ffunc Lib "D:Basura2Dll6Dll6Debugdll6.dll" (ByRef n As Integer) As Integer

Public Declare Sub fstring Lib "D:Basura2Dll6Dll6Debugdll6.dll" (ByRef fstr As String)

Dim text1, text2

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim lngTest As Long

lngTest = CLng(Val("5"))

Call fsub(lngTest)

MsgBox(CStr(lngTest))

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim intCheck As Integer

intCheck = CInt(Val("8"))

intCheck = ffunc(intCheck)

MsgBox(CStr(intCheck))

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim fstr As String

fstr = Space$(20) ' Pad string before making call.

Call fstring(fstr)

MsgBox(fstr) ' Display the results.

End Sub

End Class

The first 2 subroutines work well by the third have problems with passing the string an give me the next error message:

The runtime has encountered a fatal error. The address of the error was at 0x7a0b317c, on thread 0xb64. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshalin g errors for COM-interop or PInvoke, which may corrupt the stack.

Thanks for your help

0 Kudos
2 Replies
Steven_L_Intel1
Employee
596 Views

Two things.

In the Fortran routine fstring, add:

!DEC$ ATTRIBUTES REFERENCE ::fstr

Putting REFERENCE on the routine declaration is not sufficient - you need to tell the compiler to not expect a length to be passed.

In the VB declaration for fstring, use ByVal for the fstr argument. If you don't, you get, I think, a BSTR which you need different Fortran code to decipher.

0 Kudos
juanflozoya
Beginner
596 Views
Thank you very much, it works fine with this.
0 Kudos
Reply