Passing VB array to VC++ DLL's

I would like to pass a VB array declared as

Dim myarray(100) as long

to my VC++ DLL, what type to I declare the function as, how do I get access to the elements in the array, and
can I dynamically resize this array ??

is it done using the VBArrayElement etc.. function , if so
how - any sample programs welcome
Who is Participating?
alamoConnect With a Mentor Commented:
Declare the call inside VB as:

Declare Function VBQuickHigh Lib "....l" (ByVal control As Integer, ByVal strPhrase, firstarrayelement as Long) As Integer

You'd then call it with:

VBQuickHigh Ter1.hWnd, astring, an_array(0)

This will work because the array's first element is being passed by reference (as a pointer), not by value, and a pointer to the first element is the same as a pointer to the entire array, since all elements are contiguous in memory.

This is by far the easiest method, but it has a limitation: the DLL can't resize the array. Most of the time this isn't needed, though. The DLL can't even really know how long the array is, and can cause problems if it acccesses memory off the end of the array. For this reason, it's a good thing if the array will ever change size to add another paramater which has the upper bound of the array, so the DLL knows how big it is.

One more thing I noticed: in your DLL you declare the array as an int array (16 bits) but in your VB it is declared as Long (32 bits). You need to make these match!

Hope this helps!
The declaration of you array in VB will work.

In VC, you must be prepared to accept the array as a VARIANT,  whatever exported function you have should have this as the parameter declaration:
const VARIANT FAR& Array

To extract the array of longs from the VARIANT you can use this code:
LONG *GetArray(const VARIANT FAR& var,LONG& nSize)      // Caller must free the pointer!
            *pReturn= NULL;

      nSize= 0;

      case VT_I4:
                  pReturn= new LONG;
                  *pReturn= var.lVal;
                  nSize= 1;


      case (VT_ARRAY | VT_I4):

                  nDims= SafeArrayGetDim(var.parray);

                  if(1 != nDims)

                  nSize= nUBound - nLBound + 1;
                  pReturn= new LONG[nSize];

                  for( nIndex = nLBound ; nIndex <= nUBound ; nIndex++ )
                        VERIFY(SUCCEEDED(SafeArrayGetElement(var.parray,&nIndex,&pReturn[nIndex - nLBound])));


      case VT_NULL:



      return pReturn;

Although I've never tried to resize the array, I'm sure that VB would complain VERY loudly if you tried to do so using this method.

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

You can indeed resize SafeArrays (using SafeArrayRedim). The entire idea behind them is that you are passing an array which is completely self-descriptive and can be safely passed back and forth between apps.

Take a look at, alexo listed some more of the functions you need. Good luck!
dd021197Author Commented:
I am using a C only DLL, under VC++ 1.5 and using VB4 16bit.
The VARIANT structure seems not to have a vt member. Is there
any differences under the environment I am using.
If you declare your dll routine as

far __pascal long RoutineName( far int *PassedArray )
(you need to export it as well)

Access the elements as
PassedArray[0]... upto the upper bound of the vb array

Declare it in VB as

declare function RoutineName lib "...." as ( PassedArray() as long) as long

dd021197Author Commented:
Inside the DLL, it's declared as:

int FAR PASCAL VBQuickHigh(HWND hwndTextControl, char* pcPhrase, int far* hTermsArray)

Inside VB, it's declared as:

Declare Function VBQuickHigh Lib "....l" (ByVal control As Integer, ByVal strPhrase, array() As Long) As Integer

Inside the routine that calls the VB function it's as follows:

Dim an_array(100) As Long
Dim astring as string

astring = "the"

an_array(1) = 1
an_array(2) = 2

VBQuickHigh Ter1.hWnd, astring, an_array

This does not seem to work, the pointer inside the DLL contains

Whats wrong?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.