Stefan
Stefan

Reputation: 4206

Specify the search path for DllImport in .NET

Is there a way to specify the paths to be searched for a given assembly that is imported with DllImport?

[DllImport("MyDll.dll")]
static extern void Func();

This will search for the dll in the app dir and in the PATH environment variable. But at times the dll will be placed elsewhere. Can this information be specified in app.config or manifest file to avoid dynamic loading and dynamic invocation?

Upvotes: 59

Views: 42502

Answers (4)

Walter Verhoeven
Walter Verhoeven

Reputation: 4411

I think that the question, even older is still quite relenant however the framework evolved at the moment, perhaps the "best" way to do this is by using the framework itself. any one who is interested, I recommend using `DefaultDllImportSearchPaths attributes on any native/external library. Here is a sample:

[DefaultDllImportSearchPaths(DllImportSearchPath.SafeDirectories)]
[DllImport("Netapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern int NetServerGetInfo(string serverName, int level, out IntPtr pSERVER_INFO_XXX);

Upvotes: 0

Eric
Eric

Reputation: 3147

This might be useful DefaultDllImportSearchPathsAttribute Class
E.g.

[assembly: DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]

Also note you can use AddDllDirectory as well so you aren't screwing up anything already there:

[StructLayout(LayoutKind.Sequential)]
struct DllDirectoryCookie { private nint value; }
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern DllDirectoryCookie AddDllDirectory(string path);

And clean up after you:

[StructLayout(LayoutKind.Sequential)]
struct BOOL { int value; /*...*/ operator bool(BOOL x) => x.value != 0; }
[DllImport("kernel32.dll", SetLastError = true)]
static extern BOOL RemoveDllDirectory(DllDirectoryCookie cookie);

Upvotes: 7

Chris Schmich
Chris Schmich

Reputation: 29456

Call SetDllDirectory with your additional DLL paths before you call into the imported function for the first time.

P/Invoke signature:

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);

To set more than one additional DLL search path, modify the PATH environment variable, e.g.:

static void AddEnvironmentPaths(string[] paths)
{
    string path = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
    path += ";" + string.Join(";", paths);

    Environment.SetEnvironmentVariable("PATH", path);
}

There's more info about the DLL search order here on MSDN.


Updated 2013/07/30:

Updated version of the above using Path.PathSeparator:

static void AddEnvironmentPaths(IEnumerable<string> paths)
{
    var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };

    string newPath = string.Join(Path.PathSeparator.ToString(), path.Concat(paths));

    Environment.SetEnvironmentVariable("PATH", newPath);
}

Upvotes: 71

jvrdev
jvrdev

Reputation: 151

Try calling AddDllDirectory with your additional DLL paths before you call into the imported function for the first time.

If your Windows version is lower than 8 you will need to install this patch, which extends the API with the missing AddDllDirectory function for Windows 7, 2008 R2, 2008 and Vista (there is no patch for XP, though).

Upvotes: 15

Related Questions