Link to home
Start Free TrialLog in
Avatar of brl8
brl8Flag for United States of America

asked on

Get type single or string results back from the same C++ dll using VBA - how to update function declaration?

Good morning all,

I have some VBA code where I am calling a function from a C++ dll.  It has been working perfectly, bringing back results of type single.  However, I now need to bring back results of type string from this same function.  I am not sure how to change my function declaration to achieve this.  Here are the details:

Private Declare Function D2_VBGetResult Lib "D2Res.dll" (ByVal a As String, _
    ByVal b As String, _
    ByVal c As Long, _
    ByRef d As Single, _
    ByVal e As Long, _
    ByVal f As String, _
    ByVal g As String _
) As Long

Dim a As String
Dim b As String
Dim c As Long
Dim result As Single
Dim f As String
Dim g As String

If c <> 0 Then
            derr = D2_VBGetResult(a, b, c, result, 1, f, g)
End if

Open in new window

All of the above is working perfectly.

But, I also need to accommodate this situation where the function returns an 8 character string which I assume is null terminated:

Dim strres as string

strres = Space$(9)
derr = D2_VBGetResult(a, b, c, strres, 2, f, g)

Open in new window



How do I modify the function declaration above to accommodate this situation?  Any help will be greatly appreciated!!
Avatar of HooKooDooKu
HooKooDooKu

The first thing that comes to mind that will allow you to return BOTH types (strings... VBA strings at that, and Longs) is to change the return type from "Long" to "Variant".  In the C++ code, the "Variant" would be a COleVariant.
The fourth parameter of the call returns the Single value into the variable. In this case it appears that the return value you refer to would always be a pointer to a string. You can allocate a buffer then copy the data into the buffer using the string pointer value. You can use RtlMoveMemory() to copy the data into the buffer.
Avatar of brl8

ASKER

Sorry, I should be more clear.

I can't change the dll that I am calling in any way.  What I am trying to do will have to be done in VBA.

I am right now trying to use the alias parameter in my declare statements to declare the function two different ways.
ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial