当前位置: 动力学知识库 > 问答 > 编程问答 >

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?

网友答案:

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

网友答案:

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.

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: