DllImport vs LoadLibrary, What is the best way?

爱⌒轻易说出口 提交于 2020-01-11 09:41:08

问题


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 ?


回答1:


Most of the Win32 API is available through managed abstractions. Otherwise, declare the ones you need using 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.




回答2:


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.



来源:https://stackoverflow.com/questions/548382/dllimport-vs-loadlibrary-what-is-the-best-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!