Using an exported Class defined in an MFC dll in Posted on: 04/27/2004 23:41:54

I have a dll MyMFCDLL.dll in which a class MyExportedClass is defined. The dll is created using VC6.0. It is exported as _declspec(dllexport). I want to use this class in my C# application. how do I use it?
Who is Participating?
vancouver_jonesConnect With a Mentor Commented:
A good way to use native C++ code in C# is to de-export your C++ class and wrap it into a managed C++ class that will serve as an interface to your native C++ class. Then you'll import the managed C++ class into your C# code. Here is what you'll need to do:

First create a Managed C++ Class Library Template project in Visual Studio. Before you do, be sure to read the following carefully to understand the template:

You'll note that you need to convert the project to mixed mode and link in the MFC libraries as needed. (I'll leave it up to you to figure out what MFC stuff needs to be linked in) Read the following carefully:

Finally, you'll need to include most of your native C++ files into the project and create new managed C++ classes to wrap the classes you want to expose. The following article describes how:

Then you'll need to either include the new mixed DLL as a reference in your C# project or dynamically load it in some other way which is beyond the scope of this answer.

This is not the most straightfoward thing to do. It's a multi-step process. I've had to do it before which is why I can provide this information, but it took me a while to implement it in what I was doing, so keep trying if things don't go smoothly the first time.
This is even better. I just found it. My situation that I described above was slightly different than yours because I had to adapt an unmanaged C++ class (not a dll) into my enviornment. This should be a TON easier for you:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.