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 / July 2006

Tip: Looking for answers? Try searching our database.

pinvoke - C++ struct array to C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jason Newell - 28 Jul 2006 17:34 GMT
All,

I'm authoring a C++ .dll & C# .dll and need a little help.  I am
experienced in interop but this one has me stumped.  Does anyone have a
simple example of how to declare a C++ exported function that can return
a struct array in one of the parameters to C#?  The C# .dll would have
no idea how many elements would be returned so the C++ .dll would need
to dynamically allocate the array and populate it.  I fully expect to be
told that I'm doing things wrong but I'm just looking for advice on how
to do this correctly.

Here's what I have now.

[C++ .h]
struct MyStruct {
    DWORD dwA;
    double dA;
    double dB;
    DWORD dwB;
    DWORD dwC;
};

extern "C" __declspec(dllexport) HRESULT GetStructArray(MyStruct* structs);

[C++ .cpp]
extern "C" __declspec(dllexport) HRESULT GetStructArray(MyStruct* structs)
{
    structs = (MyStruct)CoTaskMemAlloc(sizeof(MyStruct) * 4);
    /* structs gets populated */
}

[C# .cs]
[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
    public uint dwA;
    public double dA;
    public double dB;
    public uint dwB;
    public uint dwC;
};

[DllImport(@"CPlus.dll"]
private static extern int GetStructArray(out MyStruct structs);

public void Test()
{
    MyStruct[] structs;
    GetStructArray(out structs);
}

I'm even open to returning an array of pointers and using
Marshal.PtrToStructure() if need be.

Thanks so much!

Jason Newell
pigeonrandle - 30 Jul 2006 23:19 GMT
Jason,

http://www.opennetcf.org/forums/post.asp?method=ReplyQuote&REPLY_ID=9380&TOPIC_I
D=5360&FORUM_ID=16


have a look at the bottom of this post. one of the structure members is
a pointer to an array of (other) structures, and another is a count of
how many structures have been returned.

HTH,
James.

> All,
>
[quoted text clipped - 53 lines]
>
> Jason Newell

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.