Solved

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

Posted on 2004-04-28
4
176 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:satyaswarup
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
vancouver_jones earned 125 total points
Comment Utility
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:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/vcconclasslibrarytemplate.asp

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:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmex/html/vcconconvertingmanagedextensionsforcprojectsfrompureintermediatelanguagetomixedmode.asp

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:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp12192002.asp

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.
0
 
LVL 1

Expert Comment

by:vancouver_jones
Comment Utility
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:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkPlatformInvokeTutorial.asp
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now