Click here to Skip to main content
Licence 
First Posted 23 Apr 2002
Views 120,401
Bookmarked 29 times

Passing an array from VC++ DLL to VB

By | 24 Apr 2002 | Article
This article shows how to pass an array from a VC++ DLL to VB

Introduction

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
    public:
        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
        lResult=SafeArrayLock(*pLarr);
        if(lResult)return(lResult);
    
        // using the array
        pArrayElements=(long*) (*pLarr)->pvData;
        for (iCount=0; iCount<lElements; iCount++)
            pArrayElements[iCount] = Larray[iCount];
    
        // releasing the array
        lResult=SafeArrayUnlock(*pLarr);
        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) 
        Next
          
    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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Amol Kakhandki

Web Developer

India India

Member

Amol is currently working for a software company in India.His background is an engineering degree in Industrial Electronics.
He has been implementing projects in COM,DCOM,LDAP using VC++ ,MFC,ATL.
This has to be one of my favorite card - Reward Hotel Starwood Preferred Guest Credit Card. Thanks and enjoy!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionPassing an array from VC++ DLL to VB using Windows 7 and Visual Studio 2008 PinmemberJohn Huseby 26:28 24 Sep '10  
Questionwhat can be the reasion? PinmemberJigarThakkar23:59 21 Dec '03  
GeneralPassing an array from VC++ DLL to VB Pinmembercoolkoo22:58 13 Nov '03  
GeneralProblems with vb Pinmembernastanet5:29 15 Sep '03  
GeneralKEyboard Hook dll Pinmemberpercyvimal23:05 4 Sep '03  
GeneralAbsolutely perfect! PinsussHongKongPhooey16:55 30 Jan '03  
GeneralI want a simple method to realize the string safearray passed from vb to vc(or from vc to vb) PinsussAnonymous4:26 12 Nov '02  
GeneralRe: I want a simple method to realize the string safearray passed from vb to vc(or from vc to vb) PinmemberBard0:06 4 Aug '03  
QuestionIs this applicable to ".NET" Framework? PinmemberHyungwook Park7:54 26 Jul '02  
GeneralIs this applicable to ".NET" PinsussAnonymous12:35 25 Jul '02  
QuestionByte Array? PinmemberAnonymous10:42 27 Apr '02  
GeneralAnother good sample PinmemberRashid Thadha0:02 25 Apr '02  
GeneralSafeArrayAccessData vs. SafeArrayLock PinmemberMaximilian Hänel1:22 24 Apr '02  
GeneralRe: SafeArrayAccessData vs. SafeArrayLock PinmemberAmol Kakhandki2:06 24 Apr '02  
You are right,SafeArrayAccessData() is a better way of doing it.
its just that i prefer the Lock,Unlock functions.
GeneralRe: SafeArrayAccessData vs. SafeArrayLock PinmemberMaximilian Hänel9:15 24 Apr '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120529.1 | Last Updated 25 Apr 2002
Article Copyright 2002 by Amol Kakhandki
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid