4

My managed application has a few dependencies on unmanaged DLLs. Even though my unmanaged DLLs are deployed in my bin folder they don’t get loaded. I already found a (hopefully) outdated SO post which points out two possible solutions:

  • add dlls to a well-known lookup path (e.g. \System32\Inetsrv)
  • add my bin folder to PATH

However, I don’t like either solution as it basically adds a dependency outside my deployment folder (=outside my jurisdiction). Can’t I let IIS know to load an unmanaged DLL (e.g. through web.config)?

Community
  • 1
  • 1
Dunken
  • 8,481
  • 7
  • 54
  • 87

1 Answers1

0

You could load the unmanaged DLL passing the full path where it's located to LoadLibraryEx:

[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);

//...

//get the physical path of the root directory of the application
string rootPath = System.Web.HttpContext.Current.Server.MapPath("~");
string libPath = Path.Combine(rootPath, "bin", "relative path to your DLL");
var h = LoadLibraryEx(libPath, IntPtr.Zero, NativeDlls.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH);
if (h == IntPtr.Zero)
{
    throw new Exception("Error in LoadLibrary: " + Marshal.GetLastWin32Error());
}

Library should be set to be copied to the output directory of the build.

bluish
  • 26,356
  • 27
  • 122
  • 180