Solved

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

Posted on 2014-12-03
4
471 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
  • 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 33

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

786 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