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
Mattias Sjögren - 30 Mar 2007 23:59 GMT
>int CaptureMultiple( CapBuffer ** capBufferArr, unsigned int
>numBuffer ..)
[quoted text clipped - 6 lines]
>internal static extern int CaptureMultiple( [In, Out] CapBuffer[]
>capBufferArr, UInt32 numBuffers...)
Using CapBuffer[] as the parameter type will effectively give you a
CapBuffer* on the native side. You need one more level of indirection
to match the original signature.
>[MarshalAs(UnmanagedType.SysUInt)]
>public IntPtr Buffer1;
This is what I'd use.
Mattias

Signature
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.