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
343 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

863 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

23 Experts available now in Live!

Get 1:1 Help Now