简体   繁体   English

通过 P/Invoke 在 x86 环境的项目中导入 x86_64 DLL

[英]Importing x86_64 DLL in project with x86 environment through P/Invoke

My current target framework is .NET Framework v4.0.我目前的目标框架是 .NET Framework v4.0。

I'm having issues importing the DLL as I get the "BadImageFormatException" similar to this post BadImageFormatException when loading 32 bit DLL, target is x86我在导入 DLL 时遇到问题,因为我在加载 32 位 DLL 时收到类似于此帖子 BadImageFormatException 的“BadImageFormatException”,目标是 Z8A9DA7865483C5FD359F3ACEF178D2D6

From there I realised that the DLL (retrieved from NuGet) can only be built in x64, but my solution can only be x86 as that is what the team has decided on.从那里我意识到 DLL(从 NuGet 检索)只能在 x64 中构建,但我的解决方案只能是 x86,因为这是团队决定的。 So I can't change the configuration to x64.所以我无法将配置更改为 x64。

I'm currently using P/Invoke to import it (DllImport).我目前正在使用 P/Invoke 来导入它(DllImport)。 My machine is x64.我的机器是x64。 For context, this DLL is built with managed native c++ code and I am trying to convert it to c#.对于上下文,此 DLL 是使用托管本机 c++ 代码构建的,我正在尝试将其转换为 c#。 I cannot touch the dll as it is not my library, but my project is in c#.我无法触摸 dll,因为它不是我的库,但我的项目在 c# 中。

Since I am not allowed to change my project settings, and the DLL can only be built in and run in x64, is there any other way to solve this issue?由于我不允许更改我的项目设置,并且 DLL 只能在 x64 中内置和运行,有没有其他方法可以解决这个问题?

You can not load a 64-bit dll in a 32-bit process.您不能在 32 位进程中加载​​ 64 位 dll。 Your options are:您的选择是:

  1. Update your application to x64 mode.将您的应用程序更新为 x64 模式。 This typically requires all native components to be changed to x64 versions.这通常需要将所有本机组件更改为 x64 版本。 You said you team decided on x86, but decisions can be re-evaluated if you have good enough reasons to.您说您的团队决定使用 x86,但如果您有足够的理由,可以重新评估决定。
  2. Find a way to get a x86 version of your library.找到获取库的 x86 版本的方法。 This might involve creating your own build, so it might be expensive.这可能涉及创建您自己的构建,因此可能很昂贵。
  3. Run your library in a separate process, and use your favorite RPC/IPC method to communicate between the processes.在单独的进程中运行您的库,并使用您最喜欢的 RPC/IPC 方法在进程之间进行通信。 This will typically make deploying and debugging your application at least a bit more difficult, depending on how frequently the library is used.这通常会使部署和调试应用程序变得更加困难,具体取决于库的使用频率。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM