Solved

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

Posted on 2014-12-03
4
464 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
Comment Utility
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 32

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 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

14 Experts available now in Live!

Get 1:1 Help Now