如何在 Visual C 中共享类C# 的 DLL 吗?
我用 C++ 编写程序并将其导出为 DLL。我有一个 C# WPF GUI,我想导入 DLL 来进行处理。
我对 C# 很陌生。如何在 C# 中使用 C++ 类?我知道我不能只使用 .h 文件。
因为我在 C++ 中使用了指针,所以在 C# 中我无法使用指针。这就是为什么我感到困惑。就像在 C++ 中一样,我使用 char*
而不是 string
,并且我需要返回 double*
以获得 double
的大集合代码>数据,等等。
I wrote my program in C++ and exported it as a DLL. I have a C# WPF GUI and I want to import the DLL to do the processing.
I am very new to C#. How can I use the C++ class in C#? I know I can't just use the .h file.
Because I used pointers in C++, in C# I couldn't use pointers. That's why I got confused. Like in C++ i used char*
instead of string
, and I need to return double*
for a big collection of double
data, and things like that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有多种方法:
DllImportAttribute
到 P/Invoke 到 DLL 中。为了解决您的其他问题,是的,您可以在 C# 中使用指针。如果您有一个带有
double*
参数的函数,您可以在 C# 中声明它,如下所示:您需要确保选中 C# 项目的构建选项卡上的“允许不安全代码”复选框,然后您就可以使用指针来满足您的需求了。
但是,请注意,虽然我在这里提供了指针签名,但由于您显然对指针感到满意,因此还有其他签名可以更安全地使用,并且不需要您使用不安全的代码来编译 C#。 CLR 中的标准编组器将负责从指针到 C# 数组的转换,前提是您在需要长度时给它一些提示:
这指示编组器使用索引 1 处的参数(第二个参数) ) 作为第一个参数指向的双精度数组的大小。这使您可以避免使用 C# 中的指针,并使用安全的 CLI 类型。但是,如果您喜欢指针,我的建议是只使用它们 - C# 对您来说比弄清楚如何在没有它们的情况下编写方法签名更容易。
有关 P/Invoke 的更多详细信息,请参阅此文档: http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx
There are a number of ways:
DllImportAttribute
to P/Invoke into the DLL.To address your additional concerns, yes, you can use pointers in C#. If you have a function which has a
double*
parameter, you can declare that in C# like this:You need to make sure you check the "Allow Unsafe Code" checkbox on the build tab of the C# project, and after that, you can use pointers to your heart's content.
However, note that while I'm providing the pointer signature here, since you are obviously comfortable with pointers, there are other signatures which could be more safe for you to use, and would not require you to compile C# with unsafe code. The standard marshaller in the CLR will take care of the conversion from a pointer to a C# array, provided you give it a bit of a hint if the length is required:
This instructs the marshaller to use the parameter at index 1 (the 2nd parameter) as the size of the array of doubles pointed at by the 1st parameter. This allows you to avoid pointers in C#, and use safe CLI types. However, if you like pointers, my advice is to just use them - the C# will be easier for you than having to figure out how to write the method signature without them.
For more details on P/Invoke, refer to this documentation: http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx
很大程度上取决于 C++ dll 的结构。如果您只有少数函数,并且它们不是类的成员函数,那么您可以将它们设为“extern C”函数并使用 .NET 中的 P/Invoke 功能(由于您使用的属性,有时称为 DllImport)从 C# 访问函数。
A lot depends on the structure of your C++ dll. If you have just a handful of functions, and they are not member functions of a class, then you can make them "extern C" functions and use the P/Invoke capability in .NET (sometimes called DllImport because of the attribute you use) to access the functions from C#.