Click here to Skip to main content
13,256,446 members (51,500 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 23 Apr 2002

Passing an array from VC++ DLL to VB

, 24 Apr 2002
Rate this:
Please Sign up or sign in to vote.
This article shows how to pass an array from a VC++ DLL to VB


This article shows how to pass an array from a VC++ DLL to a VB application

The steps

  • Create an ATL project named ArrayTry; Select DLL; Click Finish.
  • Use ATL Object Wizard to add a simple object.
  • Give the short name as myarray, click OK.
  • The next step is to add a method which takes an array as well as returns it after filling it. You can always add a method using the wizard but VC6 throws an error in this case as we are using SAFEARRAY. So we will do it manually which I think is more logical.
  • So go to your .idl file and add the following below the UUID declaration of your interface
  • interface Imyarray : IDispatch
        [id(1), helpstring("method LongArray")] 
            HRESULT LongArray([in,out] SAFEARRAY(long)* pLarr);
  • Go to your class's .h file and add the following at the very end,
  • // Imyarray
        STDMETHOD(LongArray)(/*[in,out]*/ SAFEARRAY** pLarr);
  • Go to your class' .cpp file and add the following
  • STDMETHODIMP Cmyarray::LongArray(SAFEARRAY** pLarr)
        long lElements; // number of elements in the array
        long iCount;
        HRESULT lResult; // return code for OLE functions
        // pointer to the elements of the array
        long *pArrayElements; 
        long Larray[3] = {4,2,3};//existing array
        //checking if it is an one-dimensional array
        if ( (*pLarr)->cDims != 1 ) return(1);
        // checking if it is an array of longs
        if ( (*pLarr)->cbElements != 4 ) return(2);
        //Getting the number of elements of the array
        lElements = (*pLarr)->rgsabound[0].cElements;
        // locking the array before using its elements
        // using the array
        pArrayElements=(long*) (*pLarr)->pvData;
        for (iCount=0; iCount<lElements; iCount++)
            pArrayElements[iCount] = Larray[iCount];
        // releasing the array
        if (lResult) return(lResult);
        return S_OK;
  • In the above function we take an array , which will be passed ( by a VB application ) & fill it with the values from an existing array.
  • Press F7 to build the DLL , go to Tools and register your DLL. Your DLL is now ready for use.
  • Now Create a VB application as an EXE; Add a command button, double click to add the following code
  • Private Sub Command1_Click()
    Dim x As New TRYARRLib.larray
        Dim ArrayOfLongs(3) As Long 'Declare the array
        x.LongArray ArrayOfLongs  'Get the array
        For Counter = 0 To 2
            'print the array contents
            Debug.Print ArrayOfLongs(Counter) 
    End Sub
  • Don't forget to add a reference to your DLL ( Go to projects/references and point to your .tlb file )

That's It

Watch the results in the immediate window. Please write to me for any explanations/doubts , better way to do the same thing.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Amol Kakhandki
Software Developer (Senior)
Netherlands Netherlands
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionPassing an array from VC++ DLL to VB using Windows 7 and Visual Studio 2008 Pin
John Huseby 224-Sep-10 7:28
memberJohn Huseby 224-Sep-10 7:28 
Questionwhat can be the reasion? Pin
JigarThakkar22-Dec-03 0:59
memberJigarThakkar22-Dec-03 0:59 
GeneralPassing an array from VC++ DLL to VB Pin
coolkoo13-Nov-03 23:58
membercoolkoo13-Nov-03 23:58 
GeneralProblems with vb Pin
nastanet15-Sep-03 6:29
membernastanet15-Sep-03 6:29 
GeneralKEyboard Hook dll Pin
percyvimal5-Sep-03 0:05
memberpercyvimal5-Sep-03 0:05 
GeneralAbsolutely perfect! Pin
HongKongPhooey30-Jan-03 17:55
sussHongKongPhooey30-Jan-03 17:55 
GeneralI want a simple method to realize the string safearray passed from vb to vc(or from vc to vb) Pin
Anonymous12-Nov-02 5:26
sussAnonymous12-Nov-02 5:26 
GeneralRe: I want a simple method to realize the string safearray passed from vb to vc(or from vc to vb) Pin
Bard4-Aug-03 1:06
memberBard4-Aug-03 1:06 
QuestionIs this applicable to ".NET" Framework? Pin
Hyungwook Park26-Jul-02 8:54
memberHyungwook Park26-Jul-02 8:54 
GeneralIs this applicable to ".NET" Pin
Anonymous25-Jul-02 13:35
sussAnonymous25-Jul-02 13:35 
QuestionByte Array? Pin
Anonymous27-Apr-02 11:42
memberAnonymous27-Apr-02 11:42 
GeneralAnother good sample Pin
Rashid Thadha25-Apr-02 1:02
memberRashid Thadha25-Apr-02 1:02 
GeneralSafeArrayAccessData vs. SafeArrayLock Pin
Maximilian Hänel24-Apr-02 2:22
memberMaximilian Hänel24-Apr-02 2:22 
GeneralRe: SafeArrayAccessData vs. SafeArrayLock Pin
Amol Kakhandki24-Apr-02 3:06
memberAmol Kakhandki24-Apr-02 3:06 
GeneralRe: SafeArrayAccessData vs. SafeArrayLock Pin
Maximilian Hänel24-Apr-02 10:15
memberMaximilian Hänel24-Apr-02 10:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 25 Apr 2002
Article Copyright 2002 by Amol Kakhandki
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid