简体   繁体   English

在C#项目中,如何引用用C ++编写并使用/ clr编译的项目?

[英]In a C# project, how can I refer to a project written in C++ and compiled with /clr?

I have a fairly large project written in unmanaged C++ and generating a DLL. 我有一个用非托管C ++编写并生成DLL的相当大的项目。 I would like to be able to use the DLL in a .Net project (in this case, just a bare-bones WinForm application). 我希望能够在.Net项目中使用DLL(在这种情况下,只是一个准WinForm应用程序)。 I rebuilt the DLL with /clr set. 我用/ clr设置重建了DLL。 It built successfully. 它构建成功。 In my WinForm project, I selected Add Reference, selected the Project tab, and selected the DLL project. 在我的WinForm项目中,选择“添加引用”,选择“项目”选项卡,然后选择DLL项目。 It was successfully added as a reference in my WinForm project. 它已成功添加为我的WinForm项目中的参考。 However, I am having trouble using the DLL. 但是,我在使用DLL时遇到问题。 I want to add a using directive for it, but I do not know what name the DLL has. 我想为其添加一个using指令,但是我不知道DLL的名字。 I tried using the same name as the file, but it didn't work. 我尝试使用与文件相同的名称,但是没有用。 How do I find out what its name is for use in a using directive? 如何找出在using指令中使用的名称?

Thanks very much! 非常感谢!

RobR ROBR

The /clr switch enables you to mix .NET types ( ref class , interface class ) with the other C++ code. /clr开关使您可以将.NET类型( ref classinterface class )与其他C ++代码混合在一起。 It doesn't magically make native C++ code usable from C#. 它并没有神奇地使本地C ++代码可用于C#。

Among other reasons, C++ code heavily relies on object addresses remaining constant, but in .NET they do not. 除其他原因外,C ++代码在很大程度上依赖于保持不变的对象地址,但在.NET中则并非如此。 .NET requires that all objects have a v-table so that the garbage collector can find out what data type the object is (and therefore where within the object pointers are contained) but most native types have no v-table at all. .NET要求所有对象都具有一个v表,以便垃圾回收器可以找出对象是什么数据类型(以及对象指针在其中的位置),但是大多数本机类型根本没有v表。 The compiler can't make native types .NET-compatible without changing the layout and breaking compatibility with native code. 在不更改布局和破坏与本机代码的兼容性的情况下,编译器无法使本机类型与.NET兼容。 Which would defeat the purpose. 这会失败的目的。 So while the compiler can generate metadata for the native types, it's not useful to C#. 因此,尽管编译器可以为本机类型生成元数据,但对于C#而言却没有用。 Instead it lets you include .NET-compatible classes and native types together in the same DLL and internally call between them seamlessly. 相反,它使您可以将.NET兼容类和本机类型一起包含在同一DLL中,并在它们之间进行内部无缝调用。

To answer your other question "How can I find out what namespaces are defined within an arbitrary .NET assembly?" 回答另一个问题“我如何找出在任意.NET程序集中定义了哪些名称空间?” use an assembly viewer/decompiler such as dotPeek, .NET Reflector, etc. 使用程序集查看器/反编译器,例如dotPeek,.NET Reflector等。

you need to write managed wrapper classes in C++. 您需要使用C ++编写托管包装类。 Check this msdn article . 检查此msdn文章

Actually you dont need to compile the entire dll as /clr, only the managed wrappers. 实际上,您不需要将整个dll编译为/ clr,而只需将托管包装程序编译为。 Not all c++ code works with /clr and there is a modarate slowdown. 并非所有的c ++代码都可与/ clr一起使用,并且速度适中。

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

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