Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6981893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:13:01+00:00 2026-05-27T18:13:01+00:00

I need to return a list of points i have from a C dll

  • 0

I need to return a list of points i have from a C dll to a C# application using PInvoke. These are points in 3 dimensions [x,y,z]. The number of points varies by what kind of model it is. In C i handle this a linked list of structs. But I don’t see how i can pass this on to C#.

The way I see it, I have to return a flexible two dimensional array, probably in a struct.

Any suggestions to how this can be done? Both ideas on how to return it in C and how to access it in C# are highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:13:01+00:00Added an answer on May 27, 2026 at 6:13 pm

    A linked list of structs could be passed back, but it would be quite a hassle to deal with, as you would have to write code to loop through the pointers, reading and copying the data from native memory into managed memory space. I would recommend a simple array of structs instead.

    If you have a C struct like the following (assuming 32-bit ints)…

    struct Point
    {
        int x;
        int y;
        int z;
    }
    

    … then you’d represent it nearly the same way in C#:

    [StructLayout(LayoutKind.Sequential]
    struct Point
    {
        public int x;
        public int y;
        public int z;
    }
    

    Now to pass an array back, it would be easiest to have your native code allocate the array and pass it back as a pointer, along with another pointer specifying the size in elements.

    Your C prototype might look like this:

    // Return value would represent an error code
    // (in case something goes wrong or the caller
    // passes some invalid pointer, e.g. a NULL).
    // Caller must pass in a valid pointer-to-pointer to
    // capture the array and a pointer to capture the size
    // in elements.
    int GetPoints(Point ** array, int * arraySizeInElements);
    

    The P/Invoke declaration would then be this:

    [DllImport("YourLib.dll")]
    static extern int GetPoints(
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] out Point[] array,
        out int arraySizeInElements);
    

    The MarshalAs attribute specifies that the array should be marshaled using the size specified in the second parameter (you can read more about this at MSDN, “Default Marshaling for Arrays”).

    If you use this approach, note that you must use CoTaskMemAlloc to allocate the native buffer as this is what the .NET marshaler expects. Otherwise, you will get memory leaks and/or other errors in your application.

    Here is a snippet from the simple example I compiled while verifying my answer:

    struct Point
    {
        int x;
        int y;
        int z;
    };
    
    extern "C"
    int GetPoints(Point ** array, int * arraySizeInElements)
    {
        // Always return 3 items for this simple example.
        *arraySizeInElements = 3;
    
        // MUST use CoTaskMemAlloc to allocate (from ole32.dll)
        int bytesToAlloc = sizeof(Point) * (*arraySizeInElements);
        Point * a = static_cast<Point *>(CoTaskMemAlloc(bytesToAlloc));
        *array = a;
    
        Point p1 = { 1, 2, 3 };
        a[0] = p1;
    
        Point p2 = { 4, 5, 6 };
        a[1] = p2;
    
        Point p3 = { 7, 8, 9 };
        a[2] = p3;
    
        return 0;
    }
    

    The managed caller can then deal with the data very simply (in this example, I put all the interop code inside a static class called NativeMethods):

    NativeMethods.Point[] points;
    int size;
    int result = NativeMethods.GetPoints(out points, out size);
    if (result == 0)
    {
        Console.WriteLine("{0} points returned.", size);
        foreach (NativeMethods.Point point in points)
        {
            Console.WriteLine("({0}, {1}, {2})", point.x, point.y, point.z);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using ObjectContext.ExecuteStoreQuery to return a list of entities, about 30K. I need
I need to return a list of counties, but I need to filter out
Given a list I need to return a list of lists of unique items.
I'm working on a project in wich I need to return a list of
I need to return an a generic list in the correct order for my
I need to copy two linked lists recursively and return a new list .
I have a select query which returns a list of data and I need
I have a function that returns two values in a list. Both values need
I have computed the values of the points I need to draw and I
I have a JSON multi-level response that I need to deserialize and from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.