> I tried to pass two dimension array into C DLL, but the initilized data is
> misplaced in the array!
This is because basic C arrays' dimensions are allocated in the opposite
order to VB. If this is a problem, then use a SAFEARRAY rather than a C
array, because this is what VB uses internally. In fact, you could do the
same with strings by using a BSTR rather than a C string. It is a bit of
extra work, and slightly slower, but it makes for easier interop.
--
Mark Alexander Bertenshaw
Kingston upon Thames
UK
Ben Ben - 29 Dec 2003 03:17 GMT
I use VC6.0 and VB 6.0 to develop the code, could VC use SAFEARRAY? I can
not find any information about SAFEARRAY in MSDN library. Because I have to
use two dimension array to include many digital numbers rather than string.
Is there any other way to deal wiht the allocation issue?
> > I tried to pass two dimension array into C DLL, but the initilized data is
> > misplaced in the array!
[quoted text clipped - 9 lines]
> Kingston upon Thames
> UK
Mark Alexander Bertenshaw - 30 Dec 2003 12:50 GMT
> I use VC6.0 and VB 6.0 to develop the code, could VC use SAFEARRAY?
I would have thought so, since the documentation is all written assuming a
C/C++ programmer.
> I can
> not find any information about SAFEARRAY in MSDN library.
It's definitely there. Look under:
Platform SDK Documentation -> Component Services -> COM -> Automation ->
Conversion and Manipulation Functions -> Array Manipulation API Functions ->
SAFEARRAY Data Type
> Because I have to use two dimension array to include many digital numbers
rather than string.
> Is there any other way to deal wiht the allocation issue?
Not really - there are no other simple data types in C/C++ which would do
this, and you would have to create a COM wrapper for any classes you might
create to do the same thing.
HTH
Mark Alexander Bertenshaw
Kingston upon Thames
UK
Ben Ben - 29 Dec 2003 06:24 GMT
Now I enlarge the size of the array as follows:
Type AUTOCAL_SYSTEM_PARAMETERS
displayFlag As Long
... ...
ExpectedPower(0 To 2, 0 To 16) As Double
End Type
Dim b As AUTOCAL_SYSTEM_PARAMETERS
b.ExpectedPower(0, 0) = 1.5
b.ExpectedPower(0, 1) = 2.3
b.ExpectedPower(0, 2) = 3.4
b.ExpectedPower(0, 3) = 4.5
b.ExpectedPower(0, 4) = 5.6
I pass the parameter into C DLL:
#pragma pack(4)
typedef struct
{
int displayFlag;
... ...
double ExpectedPower[3][17];
}AUTOCAL_SYSTEM_PARAMETERS;
#pragma pack(8) // Back to the default
double __stdcall TestSendWithStructure(AUTOCAL_SYSTEM_PARAMETERS *b)
{
double cout1,cout2,cout3,cout4;
cout1 = b->ExpectedPower[0][0];
cout2 = b->ExpectedPower[0][1];
cout3 = b->ExpectedPower[0][2];
cout4 = b->ExpectedPower[0][3];
return (cout1);
}
The VC debug result shows as follows:
- ExpectedPower 0x0012f178
- [0] 0x0012f178
|- [0] 1.5000000000000
|- [1] 0.00000000000000
|- [2] 0.00000000000000
|- [3] 2.3000000000000
|- [4] 0.00000000000000
|- [5] 0.00000000000000
|- [6] 3.4000000000000
|- [7] 0.00000000000000
|- [8] 0.00000000000000
|- [9] 4.5000000000000
|- [10] 0.00000000000000
|- [11] 0.00000000000000
|- [12] 5.6000000000000
|- [13] 0.00000000000000
|- [14] 0.00000000000000
|- [15] 0.00000000000000
|- [16] 0.00000000000000
How to explain this result and fix the issue?
Mark Alexander Bertenshaw - 30 Dec 2003 12:56 GMT
Ben -
I've already explained the issue: VB packs arrays in the opposite way to the
way C expects them. Your results confirm this. Either reverse the indexes
when you access the array in C, or use SAFEARRAYs.
--
Mark Alexander Bertenshaw
Kingston upon Thames
UK
Ben Ben - 29 Dec 2003 08:22 GMT
I got the VB two dimenstion array store order and C two dimension array
store order!
VB:
b(0 To 2, 0 To 16) As Double
(0,0) (1,0) (2,0)
(0,1) (1,1) (2,1)
(0,2) (1,2) (2,2)
... ...
(0,16) (1,16) (2,16)
VC:
double B[3][17];
(0,0) (0,1) (0,2) (0,3) (0,4) ... (0,16)
(1,0) (1,1) .... .... (1,16)
(2,0) (2,0) ... ... (2,16)
So when VC program try to read the array in above order, it will read the
array from VB as follows
(0,0) (1,0) (2,0) (0,1) (1,1) (2,1) (0,2) (1,2) (2,2) ...
The conversion fomula could be like this B(m,n) :m and n is the size of the
array. When trying to read/write the data
B(i,j) of VB, which is responding to B(i',j'), then i' = (i+j*m)/n, j' =
(i+j*m)%n
How to use SAFEARRAY?