C#-LoadLibrary使用32位DLL失败

C#-LoadLibrary使用32位DLL失败,c#,wpf,windows,winapi,dll,C#,Wpf,Windows,Winapi,Dll,我有一个C应用程序,它在运行时加载DLL。DLL是32位的,因此它是应用程序 我尝试从以下位置使用LoadLibrary: [DllImport("kernel32.dll")] private static extern IntPtr LoadLibrary(string dllToLoad); [DllImport("kernel32.dll")] private static extern IntPtr GetProcAddress(IntPtr hModu

我有一个C应用程序,它在运行时加载DLL。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”异常

以前有人遇到过这个问题吗

您的C#exe必须编译为32位

查看此页面以了解错误代码详细信息:

错误\u错误\u EXE\u格式
193(0xC1)
%1不是有效的Win32应用程序


使用
[DllImport(“kernel32.dll”,SetLastError=true,CharSet=CharSet.Auto)]
然后当您的
LoadLibrary
结果为
IntPtr.Zero
时,查看
封送.GetLastWin32Error()
的结果以了解原因。您能尝试调用吗?@AlexK。,错误代码为0x000000c1,这是错误\u BAD\u EXE\u格式,因此您试图将32位库加载到64位进程中。听起来,您的应用程序也是为32位平台编译的。请澄清你的环境。如果您的问题是:如何将32位DLL加载到64位进程中,那么答案很简单:您不能。我将尝试在x86目标下而不是在“AnyCPU”下重新编译我的应用程序,看看它是否可以正常工作。即使以x86为目标,加载也会失败,在尝试加载DLL1时出现相同的错误0xC1。您不必指定
kernel32.dll的路径
,只要
[DllImport(“kernel32.dll”,SetLastError=true,CharSet=CharSet.Auto)]
应该是ok2。您必须通过在项目属性窗口3的
Build
页面中将
Platform target
设置为
AnyCPU,并使用首选32位
x86
将exe编译为32位。您必须确保您的dll是有效的32位PE格式dll