Daniel Dimovski
Daniel Dimovski

Reputation: 592

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?

Upvotes: 6

Views: 2475

Answers (3)

zabulus
zabulus

Reputation: 2523

You don't need to write wrap on QT.

I advice you to write Observer class both in C# & C++, and hide calls of Native functions to it. Schema is like this

Your code in C# -> Observer(C#) -> Native call to Observer(C++) -> Calls to your dll.

Upvotes: 0

Jeff
Jeff

Reputation: 5966

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

Upvotes: 2

Benoit Miller
Benoit Miller

Reputation: 399

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.

Upvotes: 1

Related Questions