C# DllImport 与 LoadLibrary,最好的方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/548382/
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
DllImport vs LoadLibrary, What is the best way?
提问by ebattulga
i'm usually using Win32 API in c#.NET. But not declare all in one application. Sometimes usually using user32, sometimes gdi32 ... I think when i declare all api function, those use lot of memory. What is the best way using API in .NET ?
我通常在 c#.NET 中使用 Win32 API。但不要在一个应用程序中声明所有内容。有时通常使用 user32,有时使用 gdi32 ...我想当我声明所有 api 函数时,那些使用大量内存。在 .NET 中使用 API 的最佳方式是什么?
采纳答案by Zooba
Most of the Win32 API is available through managed abstractions. Otherwise, declare the ones you need using DllImport
.
大多数 Win32 API 可通过托管抽象获得。否则,使用DllImport
.
LoadLibrary
should really only be used where you have provided alternate functionality, that is, your application can work even without that particular API function. If the API function is critical, using DllImport
will let the loader worry about whether the function exists or not.
LoadLibrary
真的应该只在您提供替代功能的地方使用,也就是说,即使没有那个特定的 API 函数,您的应用程序也可以工作。如果 API 函数很关键, usingDllImport
会让加载器担心该函数是否存在。
回答by Toybuilder
LoadLibrary is useful when you are writing code that might be used in an environment that may or may not have the desired dll -- for example, you might have a program that can use a special crypto dll if it is available, but can still operate without it. Using DllImport would require that dll to exist.
当您编写的代码可能在可能有也可能没有所需 dll 的环境中使用时,LoadLibrary 很有用——例如,您可能有一个程序,该程序可以使用特殊的加密 dll(如果可用),但仍然可以运行没有它。使用 DllImport 将需要该 dll 存在。