Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using DirectX DLL (C++) from C#

I have a native C++ DLL using DirectX, that I want to be able to use through C# to be able to create content creation tools.

Adding COM would require quite an effort it seems. Can P/Invoke be used to maintain classes using polymorphism, or would it require me to wrap most of the code to facilitate use of P/Invoke?

It there a better solution is available? Or should I even consider writing the tools in C++ using Qt perhaps?

like image 347
Daniel Dimovski Avatar asked Feb 12 '10 14:02

Daniel Dimovski


2 Answers

I always feel that C++/CLI is always the best option when doing C# to C++ interop. You can easily create thin wrappers for an unmanaged library that will look exactly like a managed library to your C# code. It also gives you more control over how marshaling and pinning is performed.

There are ways to automatically generate C++/CLI I believe, but I've never used them.

More info on C++/CLI here: http://msdn.microsoft.com/en-us/magazine/cc163681.aspx

like image 164
Jeff Avatar answered Oct 19 '22 04:10

Jeff


I presume rendering with D3D directly from C# isn't an option? (because that certainly would be easier!)

A long time ago, I used SWIG to automatically maintain C# "bindings" of my C++ rendering DLL code. It uses P/Invoke to expose the C++ objects on the C# side, and you won't have to maintain that code anymore.

like image 31
Benoit Miller Avatar answered Oct 19 '22 04:10

Benoit Miller