且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法将我的Visual-C ++ DLL添加到我的VB.NET Windows窗体GUI应用程序

更新时间: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:


  1. 中右键单击项目解决方案资源管理器并转到添加>现有项目...



  • 找到您的本机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添加为链接(快捷方式),您始终可以引用原始文件。因此,如果您曾经更新/重新编译该DLL,则不会必须将其重新添加到您的VB.NET项目中。

    • By adding the DLL as a link (shortcut) you always refer to the original file. Thus if you ever update/recompile the DLL you won't have to re-add it to your VB.NET project.


  • 解决方案资源管理器中选择您的DLL。



  • 进入属性窗口并将复制到输出目录更改为始终复制


    • 这将确保您的DLL始终复制到输出目录( bin\Debug bin\发布)。

    • This will make sure that your DLL is always copied to the output directory (bin\Debug or bin\Release) every time you compile the project.


  • 完成!

  • Done!


    li>