“使用”与 [DllImport] 相比?
我想知道最顶层声明的引用是什么以及为什么我们仍然需要使用 DllImport?我说的是 C#。
I was wondering what is the very top most declared references and why we still need to use DllImport? I'm talking C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
来自 MDSN 文档:
基本上,当您编写 .NET 应用程序时,并且库没有托管包装器(它是用非托管代码编写的),您需要使用 DllImport 与其进行互操作。否则,您可以使用
using
语句引用托管库,就像通常引用任何基类库一样。From the MDSN documentation:
Basically, when you're writing a .NET application, and a library does not have a managed wrapper (it's written in unmanaged code), you need to use DllImport to interoperate with it. Otherwise, you can reference managed libraries with a
using
statement like you normally would any base class library.当您需要调用非托管代码时使用它。
例如,您可能需要调用 Windows API 函数,因此您可以声明如下内容:
引用:
http://www.csharphelp.com/ 2006/01/call-unmanaged-code-part-1-simple-dllimport/
It's used when you need to call unmanaged code.
For example, you might need to make a call to a windows API function, so you could do declare something like this:
references:
http://www.csharphelp.com/2006/01/call-unmanaged-code-part-1-simple-dllimport/
using
指令包含来自引用的托管程序集的命名空间。DllImport
用于从非托管 DLL 导入方法。The
using
directive includes a namespace from a referenced managed assembly.The
DllImport
is used to import methods from unmanaged DLLs.DLLImport 用于将本机 dll 库导入到托管(.net)应用程序中。例如,可以导入用 C++ 编写的库并在您的 c# 项目中使用。
using 用于引用位于托管引用 dll 之一中的命名空间。这些通常是 .net 程序集。
DLLImport is used to import a native dll library into a managed(.net) application. For example a library written in C++ can be imported and used in your c# project.
Using is used to reference a namespace located in one of the managed referenced dlls. These are normally .net assemblies.
Visual Studio 为您创建基本引用(例如系统)。
除非您想使用本机库,否则不需要 DllImport
Visual Studio creates for you the basic references (System for example).
You don't need DllImport unless you want to make use of native libraries
DllImport 用于导入包含在不受 .NET 管理的 DLL 中的函数。 using 语句允许您的代码轻松引用其他 .NET 程序集,而无需使用完全限定名称。
DllImport is for importing a function that is contained in a DLL that is not managed by .NET. The using statements are for allowing your code to easily reference other .NET assemblies without using the fully qualified name.