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

Help with passing VBA string to Fortran

ripittard
新貢獻者 I
1,539 檢視

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,

 

 

0 積分
1 解決方案
FortranFan
榮譽貢獻者 III
1,520 檢視

@ripittard ,

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

 

 

 

在原始文章中檢視解決方案

2 回應
FortranFan
榮譽貢獻者 III
1,521 檢視

@ripittard ,

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

 

 

 

ripittard
新貢獻者 I
1,496 檢視

Thanks FortranFan, that gave me exactly what I was after!

回覆