passing byte array from VC++ .NET dll to VB.NET

Posted on 2006-03-25
Last Modified: 2012-05-05
I am writing a vc++ .Net dll, and this dll will be used under vb.Net.

The function in vc++ would be look like:
  int _stdcall AbcCapData(char * pkt_data) {...}
I wish to fill bytes in pkt_data that is going to be used in

I searched the EE site and found a similar question:
Unfortunately,  I couldn't understand the answer because lacking knowledge of COM Server.

I also saw an example using LPSAFEARRAY like:
  int _stdcall AbcCapData(LPSAFEARRAY FAR * pkt_data) {...}
But I have no idea about LPSAFEARRAY, either.
Besides, it seems that LPSAFEARRAY is for vb6? (I'm not quite sure)

My question is,
which of these technologies should I use? Marshal, LPSAFEARRAY or neither?
Where should I start?

Question by:winfred_lu
    LVL 48

    Accepted Solution

    The following Marhal.Copy overload copies data from an unmanaged memory pointer to a byte array:

    Marshal.Copy Method (IntPtr, Byte[], Int32, Int32)

    (IntPtr is char*)

    If you write managed C++ Dll, convert char * pkt_data to Byte[] array and expose byte array to VB .NET client.
    If you write unmanaged C++ Dll called using PInvoke, this conversion must be done in the client VB .NET code.

    LPSAFEARRAY is ised in COM, it is not related to this case.
    LVL 48

    Expert Comment

    Some more details. If this unmanaged function is called directly from VB .NET client, client must allocate array of required size using Marshal.AllocHGlobal Method. IntPtr returned by AllocHGlobal points to unmanaged memory block. This pointer is passed to AbcCapData function. Function fills it using some way like strcpy, memcpy etc. After function returns, client copies this data by Byte array as described in my first post.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    732 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

    18 Experts available now in Live!

    Get 1:1 Help Now