.Net applications can call functions from static DLLs using the [DllImport]
attribute. This attribute has as its argument the name of the DLL in which the target is store. But what does one do if the location of the DLL is not in the paths that the system will search? First, let’s consider where the system looks for DLLs in the order that it searches for them.
- The Application Directory
- The System Directory
- The Windows Directory
- Current Directory
- Directorys in the
PATH
environment variable
If the target DLL isn’t in one of those folders, it won’t be found. There is a Win32 function that let’s an application set an additional folder in which the system will look for resolving a DLL location at runtime. The function has the signature HRESULT SetDllDirectory(LPWSTR pathname
). When this method is called with a valid path the new search path is as follows.
- The Application Directory
- The Directory passed in
SetDllDirectory()
- The System Directory
- The Windows Directory
- The Current Directory
- Directories in the
PATH
environment variable
The statement for adding a declaration for SetDllDirectory
follows.
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);
Mastodon: @j2inet@masto.ai
Instagram: @j2inet
Facebook: @j2inet
YouTube: @j2inet
Telegram: j2inet
Twitter: @j2inet