- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am trying to pass a VBA string to fortran, modify it and return the result and am having some trouble.
I thought I could have VBA pass the size of the string and a reference to the string to Fortran and then use ConvertBSTRToString to obtain the Fortran equivalent, but I am struggling to do this correctly.
The lenght of the string comes across OK, but ConvertBSTRToString result does not match and the string content in Fortran does not match. Any help woudl be appreciated.
The Fortran routine and VBA code are attached.
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This post may be of limited value to you since what is below is prescriptive. But if interested, take a look at this thread which has Fortran with VB.NET (a different beast of course) : https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/td-p/1373313
But you can consume the same Fortran subroutine shown in it in Microsoft Office VBA as follows: the key aspects are ByVal attribute for the string parameter and (preferably) LongPtr type to match with preferably c_size_t kind for the integer on the Fortran side, this one too as ByVal:
Option Explicit
Public Declare PtrSafe Sub Fstr Lib "c:\temp\Fdll.dll" (ByVal Str As String, ByVal LenStr As LongPtr)
And a trivial button example below to see how the subroutine can be invoked in Excel:
Private Sub cmdBtn_Click()
Dim LenStr As LongPtr
Dim Str As String
On Error GoTo StrErr
Range("Str").Clear
LenStr = Range("LenStr").Value
If (LenStr > 0) Then
Str = String(CLng(LenStr), " ")
Call Fstr(Str, LenStr)
Range("Str").Value = Str
Else
Range("Str").Value = xlErrNA
End If
Exit Sub
StrErr:
Range("Str").Value = xlErrNA
End Sub
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This post may be of limited value to you since what is below is prescriptive. But if interested, take a look at this thread which has Fortran with VB.NET (a different beast of course) : https://community.intel.com/t5/Intel-Fortran-Compiler/x64-string-passing/td-p/1373313
But you can consume the same Fortran subroutine shown in it in Microsoft Office VBA as follows: the key aspects are ByVal attribute for the string parameter and (preferably) LongPtr type to match with preferably c_size_t kind for the integer on the Fortran side, this one too as ByVal:
Option Explicit
Public Declare PtrSafe Sub Fstr Lib "c:\temp\Fdll.dll" (ByVal Str As String, ByVal LenStr As LongPtr)
And a trivial button example below to see how the subroutine can be invoked in Excel:
Private Sub cmdBtn_Click()
Dim LenStr As LongPtr
Dim Str As String
On Error GoTo StrErr
Range("Str").Clear
LenStr = Range("LenStr").Value
If (LenStr > 0) Then
Str = String(CLng(LenStr), " ")
Call Fstr(Str, LenStr)
Range("Str").Value = Str
Else
Range("Str").Value = xlErrNA
End If
Exit Sub
StrErr:
Range("Str").Value = xlErrNA
End Sub
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks FortranFan, that gave me exactly what I was after!
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page