I have a native/unmanaged C++ library with a number of classes that I would like to use from C#. Most of the solutions I've read (like this one and this one) suggest that I should create a C++/CLI wrapper, and use the wrapper in my C# project. Most of these suggestions, however, ignore platform. As far as I am aware, if the unmanaged DLL is 32-bit, my wrapper DLL will have to be 32-bit, and that will force my C# project to use the x86 platform, even if I have both 32- and 64-bit versions of the unmanaged DLL available.
I've solved this problem before with C APIs by using P/Invoke with LoadLibrary()
and Marshal.GetDelegateForFunctionPointer()
, but I think wrapping every method call of the C++ objects would be error-prone and difficult to maintain. I also don't think I should attempt to rely on discovering the mangled name of the exports in the C++ DLL either.
Incidentally, the C++ library I'm attempting to use is the Google V8 JavaScript VM (http://code.google.com/p/v8/) which can be compiled for x86 or x64, so porting the C++ source code to straight C# is out of the question. And yes, I'm aware of several existing projects that wrap V8 for use with managed code, such as v8sharp (http://v8sharp.codeplex.com/) and Javascript .NET (http://javascriptdotnet.codeplex.com/). However, to my knowledge, all of them use a C++/CLI wrapper that is platform-specific. For interop with other managed code libraries, I need my managed code component to use AnyCPU.
Is there a good way to accomplish this?