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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

840 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