简体   繁体   中英

MFC class to C#

I have implemented a library known as GDAL in C++/MFC, this project also implement gdiplus for drawing bit map.

So in short: i incapsulated everything in a class that takes some int, string and other pretty common types and returns some of those type plus some gdiplus::Bitmap. All of this nicely packed in a .dll and .lib.

So now i would like to use this library in a C# project to use all the power of the forms.

I searched across the whole interweb for an answer but couldn't find anything very specific with tutorials and other helpfull documention.

But there was one solution that caught my eyes but wasn't very well described, it was the c++/clr wrapper. I created one that C# finnely accepted as a reference, but it does not let me instanciate that class nor see the namespace or anything related trough intellisence.

If someone could help me down that road or give me a similar good one that would be much appreciated.

You can't use C++ classes in a C# project.

You have two options.

The first approach would be to create a managed wrapper in VC++, which would create a managed class which accesses the unmanaged class and aggregates the calls to the various properties/methods on the class.

See this article for how to do this

Once you have the wrapper, you can just set a reference, and then use your class from C#.

The second approach is to wrap the MFC object in a COM object. You would have to define your interface, then create the class factory, then instantiate the object... It is more "object oriented", but it is a lot of work, see the tutorial

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