Solved

Passing VB array to VC++ DLL's

Posted on 1998-06-29
7
484 Views
Last Modified: 2012-05-04
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
Comment
Question by:dd021197
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 2

Expert Comment

by:rayb
ID: 1464528
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
 
LVL 2

Expert Comment

by:rayb
ID: 1464529
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
 
LVL 6

Expert Comment

by:alamo
ID: 1464530
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:dd021197
ID: 1464531
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
 
LVL 1

Expert Comment

by:cd74013
ID: 1464532
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
 
LVL 1

Author Comment

by:dd021197
ID: 1464533
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
 
LVL 6

Accepted Solution

by:
alamo earned 50 total points
ID: 1464534
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question