• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1834
  • Last Modified:

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

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?

  • 2
1 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.
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now