How to build a .net wrapper for a c++ library?

2bcb31f7e572b93182bf8a1fce8d2115
0
Kochol 102 Apr 16, 2007 at 20:11

Hello all.
I built a C++ DLL and it works well but I want to build a wrapper for dot net 2.0 with C++.Net.
I tried many ways to build but never succeed.
Is there any tutorial or article about how to write some .net wrapper for C++ DLLs? or is there anyone to help me.

4 Replies

Please log in or register to post a reply.

6aa952514ff4e5439df1e9e6d337b864
0
roel 101 Apr 16, 2007 at 20:33

I don’t know the answer, but it can’t be that hard, can it? Almost everything that is worth wrapping has been wrapped, tons of projects have been wrapped, many people were able to do so, many of their results are open source. You just have to be able to call unmanaged APIs of DLLs from your .NET assembly, that’s all a wrapper does, or am I missing something (I’m quite tired at the moment).

So I think (but hey, I’m not an expert), you do something like this:

http://www.c-sharpcorner.com/UploadFile/shrijeetnair/win32api12062005005528AM/win32api.aspx

… and put the API calls in a fancy class, and give that class to your friends and have fun. Sounds near-trivial. But hey, again, I can be wrong.

560c3e97d3d17a61a455b6090b6d2495
0
jff_f 101 Apr 17, 2007 at 00:43

@Kochol

I built a C++ DLL and it works well but I want to build a wrapper for dot net 2.0 with C++.Net.

I recently made a .Net wrapper for my engine. What I’ve done is add .Net classes in the DLL itself. Since you already have your engine in a DLL it would be easy to add the managed part.

Procedure with VS 2005 Pro :
First, create new .cpp and .h files and add them to the project. Second right click on the file and select properties go in C++->General then you’ll see an option called “Compile with Common Language Runtime support” and set it to “Common Language Runtime Support (/clr)” this will compile only this file as a .Net class. In the newly added files add reference to the System namespace if you need to :

#using <System.dll>
using namespace System;

Then create your namespaces like you’d do in C++ .Net … something I noticed though is that “namespace a::b {}” is different than “namespace a { namespace b { } }”.

Make sure you include your non-managed .h files before the managed one or this will cause problems with some code (looks like its messing with win32, directx and ogl includes or redefining symbols).

You’re now ready to create a managed project (like c# for example) and reference the DLL and it intellisense should update the classes. You can now use the managed classes you created that way.

Otherwise, if you don’t want the managed and unmanaged parts in the same DLL you could the pinvoke method. But in the end it would have took me more time to figure out the correct types that fits the unmanaged DLL using pinvoke (I didn’t know .Net types much at that time). Now that it’s done I like the idea of having the two parts together in the same DLL.

Also, as far as I understood .Net can import COM dlls without any wrapper so you could probably make your engine as a COM and make clean .Net like classes for the managed part.

It’s a shame I lost the references I read when doing my wrapper… :sad:

JFF

997e1243eb8e4ee07dff88ebc9bbbe9b
0
STLDude 101 Apr 17, 2007 at 05:19

This is some of my links I used to do the same think before. It is in no particular order, and some of the stuff might not be useful, but it helped me. I have C++ engine and I’m using C# for my editor which communicates back and forth with the engine.

.NET interop marshaling made
Interop (How Do I in Visual C++)
Understanding Classic COM Interoperability With .NET Applications
Mixed (Native and Managed) Assemblies
An Overview of Managed/Unmanaged
Dispose and Destructors
Writing Code in a Natural Way with C++/CLI
New C++ Language Features (.NET 2005)

P.S. jff_f

It’s a shame I lost the references I read when doing my wrapper…

My fix for stuff like this is to run my own personal wiki (I’m using OpenWiki running on an old second machine) and as you know it is very easy to add new content to it, this way I can really be a rat pack.

2bcb31f7e572b93182bf8a1fce8d2115
0
Kochol 102 May 20, 2007 at 17:21

Thank you all.

I want to build a .net wrapper for mono so it can be cross platform.

see this project for an example.

http://irrlichtnetcp.sourceforge.net/index.php/Main_Page