LoadLibraryEx method throws me an exception of There was an error during dll loading : D:\xulRunner\freebl3.dll, error - 18. But files exists...
Anyone knows what that means?
CODE:
string XulRunnerPath = @"D:\xulRunner";
string[] files = Directory.GetFiles(XulRunnerPath, "*.dll");
foreach (var file in files)
{
LoadWin32Library(file);
}
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibraryEx(string dllFilePath, IntPtr hFile, uint dwFlags);
[DllImport("kernel32.dll")]
public extern static bool FreeLibrary(IntPtr dllPointer);
static uint LOAD_LIBRARY_A开发者_JAVA百科S_DATAFILE = 0x00000002;
static uint LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040;
static uint LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008;
public static IntPtr LoadWin32Library(string dllFilePath)
{
try
{
System.IntPtr moduleHandle = LoadLibraryEx(dllFilePath, IntPtr.Zero, LOAD_WITH_ALTERED_SEARCH_PATH);
if (moduleHandle == IntPtr.Zero)
{
// I'm gettin last dll error
int errorCode = Marshal.GetLastWin32Error();
throw new ApplicationException(
string.Format("There was an error during dll loading : {0}, error - {1}", dllFilePath, errorCode)
);
}
return moduleHandle;
}
catch (Exception exc)
{
throw new Exception(String.Format("Couldn't load library {0}{1}{2}", dllFilePath, Environment.NewLine, exc.Message), exc);
}
}
Are you loading a native library/DLL, or a .NET library/DLL? If you're doing native work, I would highly recommend adding a tag to that effect.
This article might help http://www.codeproject.com/KB/cs/dyninvok.aspx. While it is a little old, things haven't changed too much on this front. I would think you could make the solution better via the use of dynamic, but it is not something that I have tried.
Good luck, Erick
精彩评论