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
177 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
ID: 10940823
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
ID: 10941067
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

937 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

11 Experts available now in Live!

Get 1:1 Help Now