?
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
Medium Priority
?
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
vancouver_jones earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

764 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