Solved

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

Posted on 2014-12-03
4
467 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

920 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

13 Experts available now in Live!

Get 1:1 Help Now