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 6160773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:21:42+00:00 2026-05-23T21:21:42+00:00

In unmanaged C++ I have a function which I’m trying to call from C#.

  • 0

In unmanaged C++ I have a function which I’m trying to call from C#. This C++ function is as follows:

typedef std::vector<Point> Points;
typedef std::back_insert_iterator<Points> OutputIterator;

namespace MYNAMESPACE{
    DLLEXPORT OutputIterator convexHull(Points::iterator first, Points::iterator last,  OutputIterator result);
}

When called from C++, the function is used as follows:

  Points points, result;

  points.push_back(Point(0,0));
  points.push_back(Point(10,0));
  points.push_back(Point(10,10));
  points.push_back(Point(6,5));
  points.push_back(Point(4,1));

  OutputIterator resultIterator = std::back_inserter(result);

  MYNAMESPACE::convexHull( points.begin(), points.end(), resultIterator);
  std::cout << result.size() << " points on the convex hull" << std::endl;

I’ve started writing the C# code, but I’ve no idea what types I should be passing:

[DllImport("unmanagedCode.dll", EntryPoint = "convexHull", CallingConvention = CallingConvention.StdCall)]
        public static extern ???<Point> convex_hull_2(???<Point> start, ???<Point> last, ???<Point> result);

The Point structure in C# is just:

struct Point{
    double x;
    double y;
}

Is it a case of passing an array or List of Point?

I have the source to the C++ and can make changes to the function parameters; would there be a different type of parameters which would be easier to call from C#?

  • 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-23T21:21:43+00:00Added an answer on May 23, 2026 at 9:21 pm

    Passing C++ types through P/Invoke is not going to work. You don’t know their layout and nothing guarantees they won’t change. P/Invoke is really only meant for inter-operating with C.

    One option is to use C++/CLI instead of C++. This won’t be portable (only supported with VC++/Windows), but it might be the easiest solution depending on how large your C++ code is already.

    If you want to remain portable and use straight P/Invoke from C#, your best bet is to slightly refactor the C++ convexHull and provide a new function callable from C (and thus P/Invoke).

    // C-safe struct.
    struct Results
    {
        Point *points;
        std::size_t num_points;
    };
    
    // Store the real results in a vector, but derive from the C-safe struct.
    struct ResultsImpl : Results
    {
        Points storage;
    };
    
    // convexHull has been refactored to take pointers
    // instead of vector iterators.
    OutputIterator convexHull(Point const *first, Point const *last,
        OutputIterator result);
    
    // The exported function is callable from C.
    // It returns a C-safe Results, not ResultsImpl.
    extern "C" DLLEXPORT Results* convexHullC(Point const *points,
                                              std::size_t num_points)
    {
        try
        {
            std::unique_ptr<ResultsImpl> r(new ResultImpl);
    
            // fill in r->storage.
            convexHull(points, points + num_points,
                std::back_inserter(r->storage));
    
            // fill in C-safe members.
            r->points = &r->storage[0];
            r->numPoints = &r->storage.size();
    
            return r.release();
        }
        catch(...)
        {
            // trap all exceptions!
            return 0;
        }
    }
    
    // needs to be called from C# to clean up the results.
    extern "C" DLLEXPORT void freeConvexHullC(Results *r)
    {
        try
        {
            delete (ResultsImpl*)r;
        }
        catch(...)
        {
            // trap all exceptions!
        }
    }
    

    And then from C#:

    [StructLayout(LayoutKind.Sequential)]
    struct Point
    {
        double x;
        double y;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    struct Results
    {
        IntPtr points;
        IntPtr num_points;
    }
    
    [DllImport("unmanagedCode")]
    IntPtr convexHullC(Point[] points, IntPtr pointCount);
    
    [DllImport("unmanagedCode")]
    void freeConvexHullC(IntPtr results);
    
    Point[] ConvexHull(Point[] points)
    {
        IntPtr pr = convexHull(points, new IntPtr(points.Length));
    
        if(pr == IntPtr.Zero)
        {
            throw new Exception("native error!");
        }
    
        try
        {
            Results r = Marshal.PtrToStructure(pr, typeof(Results));
    
            points = new Point[checked((int)(long)r.num_points)];
    
            for(int i = 0; i < points.Length; ++i)
            {
                points[i] = Marshal.PtrToStructure(
                    r.points + Marshal.Sizeof(typeof(Point)) * i,
                    typeof(Point));
            }
    
            return points;
        }
        finally
        {
            freeConvexHull(pr);
        }
    }
    

    Code not tested!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an unmanaged (C/C++) DLL which I need to call from a C#
I have a wrapper around a C++ function call which I call from C#
I have an unmanaged COM object, and I would like to call it from
I have 2 unmanaged dlls which have exactly same set of function (but slightly
I am using a delegate which calls an unmanaged function pointer. This causes the
We have a C++/CLI class, let us call it class A, from which we
I have a native regular C++ Dll which I want to call from C#
I have an unmanaged dll that contains a function to read a data from
Can any one help me. I have this ‘unmanaged’ .NET code, which works on
I have a managed class which contains unmanaged class pointer: class Managed { public

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.