visual-studio 为什么使用 DllImport 属性来添加引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3321957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:14:31  来源:igfitidea点击:

Why use DllImport Attribute as apposed to adding a reference?

c#visual-studiodllreferencedllimport

提问by m-y

I've seen a couple of examples such as this:

我见过几个这样的例子:

[DllImport("user32.dll")]
static extern bool TranslateMessage([In] ref Message lpMsg);

[DllImport("user32.dll")]
static extern IntPtr DispatchMessage([In] ref Message lpmsg);

But, what I don't understand is why someone would do that as apposed to just referencing the DLL like they do other libraries? The MSDN states: "The DllImport attribute is very useful when reusing existing unmanaged code in a managed application. For instance, your managed application might need to make calls to the unmanaged WIN32 API." But, is that saying it is not useful to reference an unmanaged dll or impossible otherwise?

但是,我不明白的是为什么有人会像引用其他库一样只引用 DLL 呢?MSDN 指出:“在托管应用程序中重用现有的非托管代码时,DllImport 属性非常有用。例如,您的托管应用程序可能需要调用非托管 WIN32 API。” 但是,是说引用非托管 dll 没有用还是不可能?

回答by Will Dean

"But, is that saying it is not useful to reference an unmanaged dll or impossible otherwise?"

“但是,是说引用非托管 dll 没有用还是不可能?”

Yes, exactly so. What you're thinking of as 'referencing a DLL' is actually 'referencing a .NET assembly' - it just so happens that the most common way of packaging the kind of assemblies one tends to reference is in a DLL.

是的,正是如此。您所认为的“引用 DLL”实际上是“引用 .NET 程序集”- 碰巧的是,打包人们倾向于引用的程序集类型的最常见方法是在 DLL 中。

DLLImport is entirely about importing 'traditional DLLs' - i.e. ones which export all their methods using the original Windows DLL export mechanism.

DLLImport 完全是关于导入“传统 DLL”——即使用原始 Windows DLL 导出机制导出所有方法的那些。

Think of DLLImport as actually being called 'UnmanagedImport', and things might be clearer.

将 DLLImport 视为实际上被称为“UnmanagedImport”,事情可能会更清楚。

回答by Matthew Whited

Some libraries such as user32.dll are unmanaged code. Basically this means they do not have the required metadata to allow .Net to talk to them by reference (there is much more that goes into it but hopefully that gives you enough of a head start.)

某些库(例如 user32.dll)是非托管代码。基本上这意味着他们没有允许 .Net 通过引用与他们交谈所需的元数据(还有更多内容,但希望这能给你足够的领先优势。)

回答by slayernoah

In a nutshell:

简而言之:

  • Add Reference is used for: DLL files containing managed code

  • DllImport is used for: DLL files containing unmanaged code

  • 添加引用用于:包含托管代码的DLL 文件

  • DllImport 用于:包含非托管代码的DLL 文件

Definitions:

定义:

Managed Code: code that will run only under the management of a Common Language Runtime (CLR) virtual machine, typically the .NET Framework (or Mono).

托管代码仅在公共语言运行时 (CLR) 虚拟机(通常是 .NET Framework(或Mono))的管理下运行的代码。

Unmanaged Code: any compiled binaries running directly on the OS; DLLs compiled using anything older than Visual Studio .NET 2002.

非托管代码:任何直接在操作系统上运行的编译后的二进制文件;使用早于 Visual Studio .NET 2002 的任何版本编译的 DLL。

More details: Managed, Unmanaged, Native: What Kind of Code Is This?

更多详细信息:托管、非托管、本机:这是什么类型的代码?

回答by Jairo

The .NET platform code compiles into Managed Code and it is stored using Assemblies, this assemblies are .DLL files BUT NOT ALL .DLL files are assemblies containing Managed Code. You only can use Managed Code with 'Add Reference' style.

.NET 平台代码编译为托管代码并使用程序集存储,此程序集是 .DLL 文件,但并非所有 .DLL 文件都是包含托管代码的程序集。您只能使用具有“添加引用”样式的托管代码。

Other languages and development techniques generates .DLL files with unmanaged code, actually you even can interoperate (call methods) with them but you need the DLLImport attribute

其他语言和开发技术使用非托管代码生成 .DLL 文件,实际上您甚至可以与它们互操作(调用方法),但您需要 DLLImport 属性