LoadLibraryEx方法抛出异常加载dll期间出错:D:\ xulRunner \ freebl3.dll,错误 - 18.但文件存在......
任何人都知道这意味着什么?
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_AS_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);
}
}
答案 0 :(得分:1)
您是否正在加载本机库/ DLL或.NET库/ DLL?如果你正在做本地工作,我强烈建议你添加一个标签。
这篇文章可能对http://www.codeproject.com/KB/cs/dyninvok.aspx有帮助。虽然它有点旧,但在这方面并没有太大变化。我认为你可以通过使用动态来改善解决方案,但这不是我尝试过的。
祝你好运, 埃里克