如何使用C#在Visual C ++ DLL中共享类? [英] How to share a class in a Visual C++ DLL with C#?

查看:134
本文介绍了如何使用C#在Visual C ++ DLL中共享类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C ++编写程序并将其导出为DLL.我有一个C#WPF GUI,我想导入DLL进行处理.

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.

我对C#非常陌生.如何在C#中使用C ++类?我知道我不能只使用.h文件.

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.

因为我在C ++中使用了指针,所以在C#中我无法使用指针.这就是为什么我感到困惑.像在C ++中一样,我使用char*而不是string,并且我需要返回double*以获得大量的double数据,诸如此类.

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.

推荐答案

有很多方法:

  1. 导出DLL函数并使用 DllImportAttribute P/调用到DLL.
  2. 如果它是COM DLL,则可以生成运行时可调用包装器通过 TLBIMP.exe
  3. 您可以使用C ++/CLI来创建一个.Net程序集,该程序集将在DLL上本地加载和调用方法.
  1. Export the DLL functions and use DllImportAttribute to P/Invoke into the DLL.
  2. If it is a COM DLL, then you can generate a Runtime Callable Wrapper via TLBIMP.exe
  3. You can use C++/CLI to build create a .Net assembly which loads and calls methods on the DLL natively.


要解决您的其他问题,可以,您可以在C#中使用指针.如果您有一个带有double*参数的函数,则可以在C#中这样声明:


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:

[DllImport("Your.DLL")]
private static extern unsafe void DoProcessing(double* data, int dataSize);

您需要确保选中C#项目的构建"选项卡上的允许不安全代码"复选框,然后,您可以使用指向您内心深处的内容的指针.

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.

但是,请注意,尽管我在这里提供了指针签名,但是由于您显然对指针感到满意,因此还有其他签名可以更安全地使用,并且不需要使用不安全的代码编译C# . CLR中的标准编组程序将负责从指针到C#数组的转换,如果需要的话,您可以给它一些提示:

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:

[DllImport("Your.DLL")]
private static extern void DoProcessing(
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] double[] data, 
        int dataSize);

这指示编组将索引1处的参数(第二个参数)用作第一个参数所指向的double数组的大小.这使您可以避免使用C#中的指针,并使用安全的CLI类型.但是,如果您喜欢指针,我的建议是只使用它们-与不必弄清楚如何在不使用它们的情况下编写方法签名相比,C#对您而言将更加容易.

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.

有关P/Invoke的更多详细信息,请参阅此文档:

For more details on P/Invoke, refer to this documentation: http://msdn.microsoft.com/en-us/library/aa288468(VS.71).aspx

这篇关于如何使用C#在Visual C ++ DLL中共享类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆