Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 508
  • Last Modified:

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
0
dd021197
Asked:
dd021197
  • 2
  • 2
  • 2
  • +1
1 Solution
 
raybCommented:
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!
{
      LONG
            *pReturn= NULL;

      nSize= 0;

      switch(var.vt)
      {
      case VT_I4:
            {
                  pReturn= new LONG;
                  *pReturn= var.lVal;
                  nSize= 1;
            }

            break;

      case (VT_ARRAY | VT_I4):
            {
                  LONG
                        nValue,
                        nDims,
                        nLBound,
                        nUBound,
                        nIndex;

                  nDims= SafeArrayGetDim(var.parray);

                  if(1 != nDims)
                  {
                        BAD_TYPE;
                  }

                  VERIFY(SUCCEEDED(SafeArrayGetLBound(var.parray,1,&nLBound)));
                  VERIFY(SUCCEEDED(SafeArrayGetUBound(var.parray,1,&nUBound)));
                  
                  nSize= nUBound - nLBound + 1;
                  pReturn= new LONG[nSize];

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

            break;

      case VT_NULL:
            break;

      default:
            BAD_TYPE;

      }

      return pReturn;
}


0
 
raybCommented:
PS:
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.

0
 
alamoCommented:
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 http://www.experts-exchange.com/topics/comp/os-ms-windows/programming/owl/Q.10060181, alexo listed some more of the functions you need. Good luck!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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.
0
 
cd74013Commented:
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
PassedArray[ubound?]

Declare it in VB as

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

0
 
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
ziltch.

Whats wrong?
0
 
alamoCommented:
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!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now