Solved

How do I wrap C++ unmanaged classes in a managed (C#) DLL?

Posted on 2014-12-03
4
486 Views
Last Modified: 2014-12-14
Hi:

I've got a series of classes implemented in unmanaged C++.

In order to make it easier to use for .Net clients, I need to create a .Net managed (C#) based wrapper around the project. This will take the requirement of dealing with unmanaged calls out of their hands.

Can someone point me to an example/tutorial of doing this?
I've got about a dozen or so classes which I'd like to be able to expose through a DLL.


Thanks,
JohnB
0
Comment
Question by:jxbma
[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 86

Accepted Solution

by:
jkr earned 500 total points
ID: 40479742
There is either P/Invoke that works well with calling unmanaged functions, but since you wrote 'classes', I assume COM Interop will suit you better. Taker a look at the article at http://www.codeproject.com/Articles/5001/NET-COM-Interoperability (".NET - COM Interoperability") and if you can use the newer .NET version, look here: http://www.codeproject.com/Tips/143694/Get-rid-of-COM-Interop-DLL-by-using-the-new-C-dy ("Get rid of COM Interop DLL by using the new C# 4 dynamic keyword")
0
 
LVL 34

Expert Comment

by:sarabande
ID: 40480276
you could create a mixed-code - managed-unmanaged - c++ dll. the dll is mainly managed and can include unmanaged header files and can call into exported classes of unmanaged classes of an unmanaged c++ dll.

you find samples by searching for IJW (it just works) which is the slogan MS created fro that feature.

Sara
0
 
LVL 11

Expert Comment

by:vo1d
ID: 40483717
go with cli, mixedmode interop managed - unmanaged c++.
if you have classes and you need bidirectional communication between the managed and unmanaged world, it is the right choice and faster than interop.
Create a small transition layer otherwise it could get very ugly with the syntax differences of managed and unmanaged c++.

go with pinvoke.
if you have unmanaged dll's and just have to call some methods.
communication between both worlds is not that flexible and is more functional with aligned data.

Eitherway, AnyCPU will not work in these scenarios and you will have to produce 2 builds, x86 and x64.
You should not go with COM, also it is a 3rd option.
0
 
LVL 11

Expert Comment

by:vo1d
ID: 40483731
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

624 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