views:

1777

answers:

5

Hi, i have in fact two unamaged c++ libraries, one of them makes use of the other. Both are pretty big, so rewriting is not an option for me.

I read some stuff about creating a managed c++ library which wraps arround the unmanaged code. But I don't realy get how to get started, and how to combine all this stuff in one Project...

are there any step by step guides or good easy to understand examples on how to do this?

TIA

+1  A: 

I think it depends on how much of the library functionality you actually need to expose.

I don't know about creating a managed C++ wrapper, but I've personally used two other approaches to solving this problem:

  1. Use SWIG to automatically generate C# PInvoke wrappers for your C++ classes. This sort of works but is really only worth it if you need to expose a lot of classes and members. In my experience, the auto-generated code still needed some tweaking by hand.

  2. Write unmanaged C wrappers for just the functionality you need, export them from a DLL, and hand-code the few PInvoke wrappers you need to import into C#. I've found this is more appropriate in some cases, e.g. there's a large C++ class library that performs different types of image file conversions, but all you really want to expose is a single function DoTheConversion(LPWSTR inputFile). My C# program didn't need to know all the intricacies of the underlying class library.

snowcrash09
A: 

Reading some of these articles might be a start - it's a rather broad subject, and there is no 'perfect' way to do it:

http://www.ondotnet.com/pub/a/dotnet/2004/03/29/mcpp_part3.html http://www.codeguru.com/cpp/cpp/cpp_managed/interop/article.php/c6867

None of these approaches uses p/invoke, and it's an approach we use in the projects I work at where we need to interface an insanely old c++-service library.

J. Steen
+1  A: 

You can write managed and unmanaged C++ in the same project. So you can write a wrapper in Managed C++ that calls your native c++ classes etc. Then in C# your managed C++ classes will appear just like any other .net reference. No need to use P/Invoke in this case.

Steven
A: 

you have two options, one is called ijw "it just works" where you can write managed c++ and call unmanaged c++. The other option requires the use of pinvoke.

if you use pinvoke you'll have something like this

C#

somefunction("str1", "str2", "str3", "str4"); 

[DllImport(@"myproj.dll", EntryPoint = "somefunction")]
public static extern IntPtr SomeFunction([MarshalAs(UnmanagedType.LPWStr)]string jarg1, [MarshalAs(UnmanagedType.LPWStr)]string jarg2, [MarshalAs(UnmanagedType.LPWStr)]string jarg3, [MarshalAs(UnmanagedType.LPWStr)]string jarg4);

c++

extern "C" __declspec(dllexport) void* __stdcall somefunction(wchar_t * jarg1, wchar_t * jarg2, wchar_t * jarg3, wchar_t * jarg4) 
{
//do some stuff with strings
}

if you use SWIG, swig will try to autogenerate the above code, but it is a harsh master.

i used managed c++ once, but i don't remember quite what i thought of it.

iterationx
+1  A: 

You might consider writing a COM wrapper for your C++ code using ATL. I show how to do this in a blog post. This is how Microsoft exposes functionality that they wrote in C++ (e.g. Windows 7 Libraries features). COM is easily consumed by .NET/C# (see this post). If you go this route, you might consider registration free COM if you don't need to share your wrapper with others.

Jeff Moser