Passing array from VBA to DLL

T

Thivierge.M

Hi, I have a DLL in C which expects an array of short. I need to pass the
array from a VBA application. It seems not to work. Could you help me please?

My DLL is written in C++ and compiled with the __stdcall calling convention.
I exported my function in myDLL.def file.
The interface is defined as follows:

extern "C" __declspec(dllexport) short __stdcall myDLLfunc(unsigned short
*nWords, unsigned short *pBuf);

nWords is the number of words,
pBuf is an array of unsigned short values.

In my VBA code I declared myDLLfunc as follows:

Public Declare Function myDLLfunc "myDLL" (ByRef pNbWords As Integer, ByRef
pBuf() As Integer)

Then I use the following to call myDLLfunc:

Dim nWords As Integer
Dim data(1 To 32) As Integer

Call myDLLfunc(nWords, data())

With this, it seems that the address of the data() array is not right since
the values in the data() array are not as expected.

What did I miss?

Thanks for your help.

Michel
 
T

Thivierge.M

OK. I was just informed that my profile was not properly configured with a
valid no-spam alias, which I have just done. In the mean time, I was able to
find my solution.

Here it is:

In order to pass an array from VBA to a C++ DLL, I have to define the
parameter as Any (void pointer) and pass the first element (or any element)
of the array. As long as the DLL function recognized what to do with the void
pointer we are in business.

In VBA:
Public Declare Function myDLLfunc "myDLL" (ByRef pNbWords As Integer, ByRef
pBuf As Any)

Then I use the following to call myDLLfunc:

Dim nWords As Integer
Dim data(1 To 32) As Integer

Call myDLLfunc(nWords, data(1))

The DLL function is exported as follows:

extern "C" __declspec(dllexport) short __stdcall myDLLfunc(unsigned short
*nWords, unsigned short *pBuf);

Voilà, and it works just fine.

Michel
 

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