Wrapping your own C ++ structure in C ++ / CLI

I have never worked with either C ++ or C ++ / CLI, but I would like to use my own C ++ library in a C # project. I did a bit of work with Google, learned something about C ++ / CLI and decided to give it a start. But I'm not sure that I am doing everything right.

I have the following struct in my own namespace:

avcodec.h

 typedef struct AVCodecDescriptor { enum AVCodecID id; enum AVMediaType type; const char *name; const char *long_name; int props; } AVCodecDescriptor; 

Now I want to wrap this struct in C ++ / CLI for use in C #:

 public ref struct AVCodecDescriptorWrapper { private: struct AVCodecDescriptor *_avCodecDescriptor; public: AVCodecDescriptorWrapper() { _avCodecDescriptor = new AVCodecDescriptor(); } ~AVCodecDescriptorWrapper() { delete _avCodecDescriptor; } // what do? property AVCodecID Id { AVCodecID get() { return; // what comes here ???? } } }; 

However, it seems like I'm doing something terribly wrong, since I can't even see the struct AVCodecDescriptor fields in C ++ / CLI. Does enum need to be wrapped in a structure? I need to copy them to C ++ / CLI and apply it (well, enum AVCodecID has 350 values ​​- copy / paste seems ridiculous.)

+7
c # clr wrapper c ++ - cli
source share
3 answers

You need to create an equivalent CLI class for the native structure and Enums.

 public ref class ManagedAVCodecDescriptor { ManagedAVCodecIDClass id; ManagedAVMediaTypeClass type; String^ name; String^ long_name; int props; }; 

An equivalent Enum class will look like this:

 public enum class ManagedAVCodecIDClass { xyx = 1, abc = 2, ... } 

Now in AVCodecDescriptorWrapper you should get the AVCodecDescriptor object and put all the information in the ManagedAVCodecDescriptor object and pass it to the C # class. p>

 ManagedAVCodecDescriptor^ mObject = gcnew ManagedAVCodecDescriptor(); mObject.setSomeValue(nativeStruct->somevalue); 

and pass mObject to C # code as a generic C # object.

+5
source share

I prefer to use the .NET framework inside my own code. You define:

 public enum class AVCodecID : long { mp4 = 1, mpeg2 = 2, ... 

}

 public ref class AVCodecDescriptor { AVCodecID id, ... System::String name ... } 

and whenever you need in C ++ code, you call

 AVCodecDescriptor cd = gcnew AVCodecDescriptor(); 

It's not always convenient, you need files compiled with the / clr switch, and sometimes you can get angry with converting managed / unmanaged strings, but many times it was quite suitable for me. In any case, you can easily use .NET enums in native C ++ code by typing enum values ​​for int.

+1
source share

Does this work?

  property int Id { int get() { return _avCodecDescriptor->id; } } 

By the way, you should use some smart pointer to control the life cycle of your own object, since right now you will be a memory leak if the .NET object is a finalizer and not a remote one. For example, see scoped_ptr for C ++ / CLI (make sure the managed object correctly releases its own object)

+1
source share

All Articles