Solved

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

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

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…
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 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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

821 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