Solved

Passing VB array to VC++ DLL's

Posted on 1998-06-29
7
479 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

810 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