We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


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

winfred_lu asked
Medium Priority
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 vb.net.

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?

Watch Question

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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.