Passing array from C++ dll to VBA via xl_array

N

norman.dillon

hi

I'm trying to write a C++ dll that passes an array to VBA via the
xl_array structure described in Steve Dalton's book - "Excel add-in
development in C++ "


Here is the C++ cpp file code......


#include <iostream.h>
#include <windows.h>


//define the xl_array structure
typedef struct
{
WORD rows;
WORD columns;
double array[1];


}


xl_array;

xl_array * __stdcall xl_array_example1(int rows, int columns);


xl_array * __stdcall xl_array_example1(int rows, int columns)
{
int size=rows*columns;
xl_array *p_myarray;


//allocate memory, and fill the array
size_t mem_size = sizeof(xl_array) + (size-1)*sizeof(double);
if((p_myarray=(xl_array*)malloc(mem_size)))
{
p_myarray->rows=rows;
p_myarray->columns=columns;
for (int i=0;i<size;i++)
{
p_myarray->array=20;
}
}


//return the pointer
return p_myarray;



};


....& here is the VBA code

Private Declare Function xl_array_example1 Lib "M:\Steve Dalton Book
\Compiled DLLs\dll_xl_array.dll" (ByVal a As Integer, ByVal b As
Integer) As Double()


Public Sub xlarraycaller()
Dim a As Integer, b As Integer, c() As Double, d As String
a = 3
b = 3
c = xl_array_example1(a, b)
End Sub


The program compiles and runs fine but, at the end, there is no value
in the c array. Can you help?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top