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.

How to marshal a variable length array in a callback function from unmanaged to managed code?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gernot - 24 Mar 2007 00:24 GMT
Hi,

I want to marshal the following C++ struct to a managed callback
function:

struct CALLBACK_DATA
{
   LPBYTE    pArray;       // byte array
   UINT         uArrayLen;  // byte count
};

The C++ function expects the following callback type:

typedef BOOL (CALLBACK *CALLBACK_FUNC)(const CALLBACK_DATA *pData,
LPVOID pvParam);

In C# first I import the C++ function:

[DllImport("unmanaged.dll", EntryPoint = "Func")]
static extern bool Func(CallbackFunc callback, IntPtr param);

Where CallbackFunc is declared as a delegate:

public delegate bool CallbackFunc(CallbackData data, IntPtr param);

Then I define the callback and do the call:

// the callback function
public static bool Callback(CallbackData data, IntPtr param)
{
}

CallbackFunc callback = new CallbackFunc(Callback);
Func(callback, param);

The question is how do I have to declare CallbackData?

[StructLayout(LayoutKind.Sequential)]
public class CallbackData
{
   [MarshalAs(UnmanagedType.ByValArray, SizeConst=100)]  // error!
   byte[] array;
   uint    arrayLen;
}

I cannot find attibutes that fit for this situation.
Anyway the array is not const size!

Can anybody help?

Thanks,
Gernot
mehr13@hotmail.com - 25 Mar 2007 14:17 GMT
> Hi,
>
[quoted text clipped - 51 lines]
> Thanks,
> Gernot

Declare the array parameter as IntPtr,
[StructLayout(LayoutKind.Sequential)]
public class CallbackData
{
    IntPtr  array;
    uint    arrayLen;
}

Then use Marshal class to handle the array.

Hope this helps
MH
gernot - 25 Mar 2007 16:44 GMT
On Mar 25, 3:17 pm, meh...@hotmail.com wrote:

> > Hi,
>
[quoted text clipped - 67 lines]
>
> - Show quoted text -

It did help, thank you.

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.