Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
352 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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 Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

596 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