DllImport vs LoadLibrary, what is the best way?

I usually use the Win32 API in C # .NET. But do not declare everything in one application. Sometimes user32 is usually used, sometimes gdi32 ... I think when I declare all api functions, they use a lot of memory. What is the best way to use the API in .NET?

+3
source share
2 answers

Most of the Win32 API is accessible through managed abstractions. Otherwise, declare the ones you need using DllImport.

LoadLibraryreally should only be used where you have provided alternative functions, that is, your application can work even without this specific API function. If an API function is critical, using it DllImportwill allow the loader to worry about whether the function exists or not.

+5
source

LoadLibrary is useful when you write code that can be used in an environment that may or may not have the required dll - for example, you can have a program that can use special cryptography dll, if available, but can still work without it. Using DllImport will require the existence of a dll.

+9
source

Source: https://habr.com/ru/post/1703211/


All Articles