简体   繁体   中英

Wrapping c++ class so I can use it in c#

I have this c++ header:

// BRepPrimAPI_MakeSphere.hxx file
#include <Standard.hxx>
#include <Standard_DefineAlloc.hxx>
#include <BRepPrim_Sphere.hxx>
#include <BRepPrimAPI_MakeOneAxis.hxx>

class gp_Pnt;
class gp_Ax2;

class BRepPrimAPI_MakeSphere : public BRepPrimAPI_MakeOneAxis
{
public:
    DEFINE_STANDARD_ALLOC

    Standard_EXPORT BRepPrimAPI_MakeSphere(const Standard_Real R);
    Standard_EXPORT Standard_Address OneAxis();
    Standard_EXPORT BRepPrim_Sphere& Sphere();

protected:


private:
    BRepPrim_Sphere mySphere;
};

and this c++ source:

// BRepPrimAPI_MakeSphere.cxx file
#include <BRepBuilderAPI.hxx>
#include <BRepPrimAPI_MakeSphere.hxx>
#include <gp.hxx>
#include <gp_Ax2.hxx>
#include <gp_Dir.hxx>
#include <gp_Pnt.hxx>

BRepPrimAPI_MakeSphere::BRepPrimAPI_MakeSphere(const Standard_Real R) :
       mySphere(gp::XOY(),R)
{
}

Standard_Address  BRepPrimAPI_MakeSphere::OneAxis()
{
  return &mySphere;
}

BRepPrim_Sphere&  BRepPrimAPI_MakeSphere::Sphere()
{
  return mySphere;
}

Standard_EXPORT is defined as follows:

# define Standard_EXPORT __declspec( dllexport )

DEFINE_STANDARD_ALLOC is defined as follows:

# define DEFINE_STANDARD_ALLOC  
void* operator new (size_t theSize)                                  
  {                                                                    
    return Standard::Allocate (theSize);                               
  }                                                                    
void  operator delete (void* theAddress)                             
  {                                                                    
    Standard::Free (theAddress);
  }
void* operator new[] (size_t theSize)                               
  {                                                                   
    return Standard::Allocate (theSize);                              
  }                                                                   
void  operator delete[] (void* theAddress)                          
  {                                                                   
    Standard::Free (theAddress);
  }
void* operator new (size_t, void* theAddress)                       
  {                                                                   
    return theAddress;                                                
  }                                                                   
void operator delete (void*, void*)                                 
  {                                                                   
  }

I want to use them in my c# code.

I have tried three methods that didn't work for me:

  1. Wrapping the c++ code with CLR to manage it and then use it in the c# code.
  2. SWIG
  3. P/Invoke

Now the issue is that this is one class in a huge c++ library. What I am trying to do is automatically generate the warppers for every single class so I can have the library's full functionality.

The main problem I'm having is handling return types and arguments.

This is what I'm trying right now:

extern "C" __declspec(dllexport) void Call_BRepPrimAPI_MakeSphere(BRepPrimAPI_MakeSphere* p, const Standard_Real R) {
    p->BRepPrimAPI_MakeSphere::BRepPrimAPI_MakeSphere(R);
}

But I obviously can't use this in c# because the two argument types don't exist in c# right?

Basically my question is how and what would be the best way to wrap this class?

Basically my question is how and what would be the best way to wrap this class?

I would use c++/cli, that should let you define.Net wrappers that can interact directly with native c++ objects.

What I am trying to do is automatically generate the warppers for every single class so I can have the library's full functionality.

I do not think this can be done in general, at least not without extra knowledge about resource management and other details not expressed in the c++ source. Just consider if a C# object is disposed, should you delete the underlying C++ object? To decide this you need to know if the object is shared or not, and there is no general way to know this.

But creating and using c++/cli code is kind of a pain. You need to have a fair bit of in depth knowledge of c#, c++ and c++/cli. In some cases there are just no alternative, but I would consider other solutions first, like.Net native libraries, or rewriting the required functions in C#.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM