Solved

Passing VB array to VC++ DLL's

Posted on 1998-06-29
7
477 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now