Click here to Skip to main content
15,919,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to transfer into a C ++ structure via a DLL , the content of a VB structure (UTD).

My VB code is as follows :

VB
Private Declare Function Cpp_TEST1 Lib "C:\Users\\TEST1.dll" (ByRef iStruct As myStruct) As Double

Type myStruct
    n As Long
    data() As Long
    rData As Boolean
End Type


iStruct.n = 10
iStruct.rData= True
ReDim iStruct.data(1 To 12)


iStruct.data(1) = 1
iStruct.data(2) = 2
iStruct.data(3) = 3
iStruct.data(4) = 4
iStruct.data(5) = 5
iStruct.data(6) = 6
iStruct.data(7) = 7
iStruct.data(8) = 8
iStruct.data(9) = 9
iStruct.data(10) = 10
iStruct.data(11) = 11
iStruct.data(12) = 12


Test1 = Cpp_TEST1(iStruct)



In C++ side i do :

C++
struct myStruct{    
    long n;
    long  *data;
    VARIANT_BOOL rData;
};

XLSTATLINK_API double __stdcall Cpp_TEST1(myStruct *iStruct)
{
    return 1;
}



When i spy iStruct, I get good values ​​for data n et rData.
For the data array, the data obtained are random :


[0] 8388609 long
[1] 4 long
[2] 0 long
[3] 234122136 long
[4] 12 long
[5] 1 long
[6] 634790639 long
[7] -2013222912 long
[8] 352064696 long
[9] 352064792 long
[10] 1771379816 long
[11] 537 long


My problem is that I can't recover the data array contained in the VBA structure from the C++ DLL !!

Thanks you in advance ;-)
Posted
Comments
KarstenK 13-May-15 12:51pm    
It is obvious that the rData part doesnt work. Use some fixed values instead of the Redim() ones.
[no name] 13-May-15 13:32pm    
Notice you used VARIANT_BOOL for the boolean member.

Your long *data declaration should probably be a VARIANT.

Also, the parameter to Cpp_TEST1 is likely also a VARIANT.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms221627%28v=vs.85%29.aspx
Maciej Los 13-May-15 15:08pm    
Do you use Option Base 1?

In VB the arrays are defined as safe arrays, you have to access it as such in C.
C++
struct myStruct{    
    long n;
    SAFEARRAY *data;
    VARIANT_BOOL rData;
};
 
Share this answer
 
Depending on Option Base[^] statement, lower bound of arrays start from 0 (zero) or 1(one). If you do not use this statement, array must starts from 0. I'd suggest to read this article Arrays in VB[^] which might help you to understand how to declare and initilize arrays.

VB
ReDim Preserve iStruct.data(12)
For i = 0 to 11
    iStruct.data(i) = i+1
Next
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900