I've never worked with either C++ or C++/CLI, but I would like to use a native C++ library in a C# project. I googled a bit, learnt a few things about C++/CLI and decided to give it a head start. But I'm not sure if I'm doing any of it right.
I have the following struct
in a native 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 using it 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 horribly wrong since I'm not even able to see the fields of the struct
AVCodecDescriptor in C++/CLI. Do I have to wrap the enum
s in the struct to? Do I have to copy them into C++/CLI and cast it (well, enum AVCodecID
has like 350 values - a copy/paste seems ridiculus.)
new
anddelete
is rather pointless here.private: AVCodecDescriptor _avCodecDescriptor;
suffices. And yes, you have to wrap everything. Including the enums. You need them to be declared as managed types. And then map them from the unmanaged to managed. As for the way you are wrapping, should you really do it that way? How are you going to manage the lifetime of theconst char*
members. Frankly, it's not obvious to me that C++/CLI is going to be easier than p/invoke. – Kunlun