P/invoke an array of structs with embedded char* buffers
- From: "Lionel" <lioneljlobo@xxxxxxxxx>
- Date: 30 Mar 2007 09:32:49 -0700
Hi,
I've tried several approaches for a few days now & feel its time to
ask the gurus.
I am making a call to a C++ dll from C# via p/invoke, the api expects
an array of structs with pre-allocated buffers. The api populates the
buffers with raw byte data from a device.
The C++ struct definition is :
typedef struct {
unsigned char * Buffer1;
unsigned int Buffer1size;
unsigned char * Buffer2;
unsigned int Buffer2size;
unsigned int Type;
unsigned char * Buffer3;
unsigned int Buffer3size;
unsigned int PacketNum;
} CapBuffer;
The C++ dll exported method has the following signature and expects
the above struct as an array of structs.
int CaptureMultiple( CapBuffer ** capBufferArr, unsigned int
numBuffer ..)
Buffer's 1, 2 & 3 are to be preallocated to 20000 bytes, and the dll
will populate these buffers.
I pass the arrray to structs parameter as:
internal static extern int CaptureMultiple( [In, Out] CapBuffer[]
capBufferArr, UInt32 numBuffers...)
I'm not sure how the individual buffers should be marshalled. I've
tried several techniques, some of which are as follows, in each case I
allocate memory (with a padded string buffer for strings or do a
Marshal.AllocHGlobal() for IntPtr)
[MarshalAs(UnmanagedType.LPStr, SizeConst = BUFFER_SIZE)]
public string Buffer1;
[MarshalAs(UnmanagedType.SysUInt)]
public IntPtr Buffer1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = BUFFER_SIZE)]
public byte[] Buffer1;
What am I doing wrong? What is the standard approach to pass a struct
with an embedded const char*?
Any advice would be much appreciated.
Thanks,
Lionel
.
- Follow-Ups:
- Re: P/invoke an array of structs with embedded char* buffers
- From: Mattias Sjögren
- Re: P/invoke an array of structs with embedded char* buffers
- Prev by Date: Re: How would you marshal this?
- Next by Date: Re: C# to C++ Dll with a pointer to string.
- Previous by thread: Value Class Reference
- Next by thread: Re: P/invoke an array of structs with embedded char* buffers
- Index(es):
Relevant Pages
|