更新时间:2023-12-06 16:59:04
向DLL添加引用和P /调用它之间存在区别。您只能添加对.NET DLL的引用,因为它是一种以.NET友好的方式直接访问DLL成员的方法。由于两个程序集都使用相同的语言(IL),因此DLL可以轻松地由常规应用程序编译和引用。
There's a difference between adding a reference to the DLL and P/Invoking it. You can only add a reference to .NET DLLs because it is a way of directly accessing the DLL's members in a .NET-friendly way. Since both assemblies are of the same language (IL) the DLL can easily be compiled and referenced by the regular application.
P /调用与之完全不同,因为您可以编组调用到本机DLL,该DLL已被编译为纯机器代码。不能将其添加为引用,因为编译器无法将其链接到.NET代码,因为它仅理解.NET语言和IL。
P/Invoking however is completely different because there you are marshalling calls to a native DLL, which is already compiled into pure machine code. It cannot be added as a reference because the compiler cannot link it to the .NET code since it only understands .NET languages and IL.
将DLL添加到项目中您必须改为将其添加为宽松文件:
To add the DLL to your project you have to add it as a loose file instead:
在中右键单击项目解决方案资源管理器
并转到添加>现有项目...
。
找到您的本机DLL并将其选中(但不要添加)。然后按添加
按钮上的小箭头,然后选择添加为链接
。
Locate your native DLL and select it (but do not add it). Then press the little arrow on the Add
button and select Add As Link
.
在解决方案资源管理器
中选择您的DLL。
进入属性窗口
并将复制到输出目录
更改为始终复制
。
bin\Debug
或 bin\发布
)。bin\Debug
or bin\Release
) every time you compile the project.
完成!
Done!
li>