?
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
Medium Priority
?
349 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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!
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

762 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