加载 32 位 DLL 时出现 BadImageFormatException,目标是 x86 [英] BadImageFormatException when loading 32 bit DLL, target is x86

查看:27
本文介绍了加载 32 位 DLL 时出现 BadImageFormatException,目标是 x86的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 32 位的 DLL (FreeType)(头文件:IMAGE_FILE_MACHINE_I386).

I have a DLL (FreeType) which is certainly 32-bit (header: IMAGE_FILE_MACHINE_I386).

我想从 C# 代码中使用它,使用 DllImport.

I want to use it from C# code, using DllImport.

我的应用程序的目标是 x86,IntPtr.Size 是 4,进程是 32 位.

Target of my application is x86, IntPtr.Size is 4, process is 32-bit.

但是我得到了 BadImageFormatException(来自 HRESULT 的异常:0x8007000B).有什么问题?

But I get BadImageFormatException (Exception from HRESULT: 0x8007000B). What can be wrong?

我当然使用 64 位 Windows 7.

Of course I use 64-bit Windows 7.

推荐答案

据我了解,专为 x86 构建并在 64 位操作系统中运行的程序集只能加载为 x86 构建的库,否则将抛出 BadImageFormatException.在 64 位操作系统中,为 Any CPU 或 x64 构建的程序集在尝试加载 x86 库时将抛出相同的异常.

From what I understand, an assembly specifically built for x86 and running in a 64-bit operating system can only load libraries built for x86 or a BadImageFormatException will be thrown. In a 64-bit OS, an assembly built for Any CPU or x64 will throw the same exception when trying to load an x86 library.

因此,假设没有发生任何异常奇怪的事情,我会确保您已通过打开项目属性并单击构建"选项卡将应用程序设置为构建为 x86.确保平台目标"设置为x86"而不是任何 CPU.

So, assuming nothing incredibly weird is going on, I would ensure that you've set your application to build as x86 by opening the project properties and clicking on the Build tab. Ensure 'Platform Target' is set as 'x86' and not Any CPU.

或者,您可以尝试查找 64 位版本的 DLL 以进行测试.

Alternatively, you could try to find a 64-bit version of the DLL for testing purposes.

这篇关于加载 32 位 DLL 时出现 BadImageFormatException,目标是 x86的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆