Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2007

Tip: Looking for answers? Try searching our database.

P/invoke an array of structs with embedded char* buffers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Lionel - 30 Mar 2007 17:32 GMT
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.


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.