【发布时间】:2017-12-18 13:02:46
【问题描述】:
我有一个在运行时加载 DLL 的 C# 应用程序。 DLL 是 32 位的,所以它是应用程序。
我尝试使用以下来源的 LoadLibrary:
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);
但没有结果;它总是返回IntPtr.Zero。我也尝试过使用
[DllImport("%windir%\\SysWOW64\\kernel32.dll")]
private static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("%windir%\\SysWOW64\\kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("%windir%\\SysWOW64\\kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);
但这样应用程序在调用 LoadLibrary 时会收到“System.Windows.Markup.XamlParseException”异常。
以前有人遇到过这个问题吗?
【问题讨论】:
-
使用
[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)],然后当您的LoadLibrary结果为IntPtr.Zero时,请查看Marshal.GetLastWin32Error()的结果以了解原因。 -
你可以试试打电话给GetLastError吗?
-
@AlexK.,错误代码是0x000000c1
-
那是 ERROR_BAD_EXE_FORMAT 所以你正在尝试将 32 位库加载到 64 位进程中。
-
听起来,您的应用程序也是为 32 位平台编译的。请说明您的环境。如果您的问题是:如何将 32 位 DLL 加载到 64 位进程中,但是,答案很简单:您不能。