passing 2d array from VB6 to c++ DLL?

bignateyk

Lifer
Apr 22, 2002
11,288
7
0
I need help passing a 2d array (matrix) from VB6 to a c++ DLL.

Right now, I have it working with a 1d array... here is the code I am using.

long __declspec (dllexport) __stdcall function_name(byte *1dArray,long lElements)
{
//lElements is how big the array is
//do something in here
}

What would I have to modify that to to have it accept 2d arrays?
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
long __declspec(dllexport) __stdcall function_name(SAFEARRAY *array)


NOTE: the indices order of SAFEARRAYs are opposite from normal arrays in every language besides VB. Keep this in mind when trying to access it through array->pvData.

Meaning that if you have a 3x3 array the order elements are stored is:
(0,0),(1,0),(2,0),(0,1),(1,1),(2,1),(0,2),(1,2),(2,2), rather than
(0,0),(0,1),(0,2),(1,0),(1,1),(1,2),(2,0),(2,1),(2,2).

You could pass a pointer to element (0,0) along with the 2 dimension sizes like you are doing with a 1d array, but the thing about the unusual element ordering will still apply (might as well just pass it as a safearray... if you do it as ByRef (SAFEARRAY **), the C++ code can then even resize it).