简体   繁体   English

在C#中使用C ++ dll

[英]Using a C++ dll in C#

I'm attempting to consume a dll written in C++ from a C# application. 我正在尝试使用C#应用程序中用C ++编写的dll。 I have 3rd party source code for the C++ dll (the Cyclone physics engine) and do not want to manually port it over to C#. 我有C ++ dll(Cyclone物理引擎)的第三方源代码,并且不想手动将其移植到C#。

In the C++ project 在C ++项目中
I changed it to output a dll. 我将其更改为输出dll。 I changed it to use the /clr flag. 我将其更改为使用/ clr标志。 I changed it to use Multi-threaded Debug DLL (/MDd) because that was the only one compatible with /clr and also compiled. 我将其更改为使用多线程调试DLL(/ MDd),因为那是唯一与/ clr兼容并进行编译的。

In the C# project 在C#项目中
I added a reference to the dll. 我添加了对dll的引用。 I'm using the cyclone namespace. 我正在使用旋风命名空间。

At first there was absolutely nothing under the namespace. 最初,名称空间下绝对没有任何内容。 I think this is because in the C++ code, all classes were declared with no access modifiers and the default is private. 我认为这是因为在C ++代码中,所有类都声明为没有访问修饰符,并且默认为private。 So for the class "Particle" I changed the definition to: 因此,对于“ Particle”类,我将定义更改为:

public class Particle
{
//...
}

Now I can successfully declare a variable of type Particle from the C# code. 现在,我可以从C#代码中成功声明一个类型为Particle的变量。 However, intellesense and the object browser report Particle to be a struct (?) and it doesn't contain any methods at all. 但是,智能感知和对象浏览器将粒子报告为结构(?),并且根本不包含任何方法。 The c++ code declares a bunch of methods after "public:" access modifiers so I don't know what the problem is. C ++代码在“ public:”访问修饰符之后声明了一堆方法,因此我不知道问题出在哪里。

For example, 例如,

public:
void integrate(real duration);

What am I doing wrong? 我究竟做错了什么?

The Particle class is not a managed class, hence it is treated as a struct. 粒子类不是托管类,因此将其视为结构。 You need to use the ref keyword to make it managed and garbage collected. 您需要使用ref关键字来对其进行管理和垃圾收集。 You also need to do the same to every other class that references it which might be a problem. 您还需要对引用它的其他所有类进行相同的操作,这可能是一个问题。 The best solution I think, is to create a managed wrapper class that uses the Particle class internally. 我认为最好的解决方案是创建一个在内部使用Particle类的托管包装类。 This wrapper class can then be referenced by .net. 然后,.net可以引用该包装器类。

See here : 这里

I have not actually run into your exact problem before, but I have used C++ dlls in c# before. 之前我还没有真正遇到过您的确切问题,但是我以前在c#中使用过C ++ dll。 Typically when calling a dll like that you would use the dllImport keyword. 通常,当调用这样的dll时,将使用dllImport关键字。 Basically you can define a class that imports all the types and methods from the c++ dll. 基本上,您可以定义一个从c ++ dll导入所有类型和方法的类。 You can then call those wrapper classes. 然后,您可以调用这些包装器类。

I am far from an expert on it, but I have used it to get access to win32 methods and some other libraries that I needed to use. 我还不是专家,但是我已经使用它来访问win32方法和我需要使用的其他一些库。

This link on codeplex has a few links tools that might help. Codeplex上的链接具有一些可能有用的链接工具。 But the most important is probably the Interop Assistant. 但是最重​​要的可能是Interop Assistant。 It can generate the C# wrappers for your c++ dll. 它可以为您的c ++ dll生成C#包装器。

This isn't exactly what you are asking, but I thought it might help to look at a different direction. 这并不是您要问的,但我认为这可能有助于寻找另一个方向。

尝试使用ref关键字声明c ++类:

public ref class Particle

您对COM Interop有参考吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM