Solved

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

Posted on 2010-11-17
4
337 Views
Last Modified: 2012-05-10
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!!
0
Comment
Question by:brl8
  • 2
4 Comments
 
LVL 16

Expert Comment

by:HooKooDooKu
ID: 34156759
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.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 34156889
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.
0
 
LVL 1

Author Comment

by:brl8
ID: 34156969
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.
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 34157084
You won't have to change the DLL. If you change the fourth parameter to type Long all should be fine. The question I have is do you know when it's suppose to return a string value and a single value is this based on the other parameters you specify or is it completley random?

There is actually a couple different ways you could do this here is one that may work. If you have the C++ definition that would help.




Private Declare Function D2_VBGetResult Lib "D2Res.dll" (ByVal a As String, _

    ByVal b As String, _

    ByVal c As Long, _

    Byval d As Long, _

    ByVal e As Long, _

    ByVal f As String, _

    ByVal g As String _

) As Long



' For character string

dim Buffer(64) As Byte

derr = D2_VBGetResult(a, b, c, varptr(buffer(0)), 2, f, g)

debug.print buffer ' you want to strip null chars later.





' For single in the same call

dim result as Single

derr = D2_VBGetResult(a, b, c, varptr(result ), 1, f, g)

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now