32 位 dll 导入 64 位 .Net 应用程序
我遇到了问题,从昨天开始我一直在尝试解决它,但没有成功。 我有一个 32 位 Delphi DLL,我想将其导入到 .NET WIN 应用程序中。该应用程序必须构建在ANY CPU模式上。当然,会抛出异常BadImageFormatException,这意味着64位应用程序无法加载x86 DLL。 我用谷歌搜索并找到了一个解决方案,它说我必须做包装器,但我不清楚。谁能告诉我如何解决这个问题,是否有任何可能的方法可以将 32 位 Delphi DLL 导入到在任何 CPU 架构(64 位、32 位)下构建的程序中,或者可能是其他解决方案?
I'm having a problem, I've been trying to solve it since yesterday but no luck.
I have a 32-bit Delphi DLL which I want to import it in to a .NET WIN Application. This application has to be built on ANY CPU mode. Of course, the exception BadImageFormatException is thrown, which means that 64-bit applications can't load x86 DLLs.
I googled around and found a solution, it said that I have to do wrapper, but it wasn't clear for me. Can anyone tell me how to solve this problem, is there any possible way that I can import a 32-bit Delphi DLL in to a program built under any CPU architecture (64-bit, 32-bit) or maybe another solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您所要做的就是编写一个包装应用程序,在 32 位进程中托管 32 位 DLL 文件。
然后,您的 64 位应用程序必须通过网络方式或通过 COM 对象或类似方式使 DLL 函数可用,与该 32 位进程进行对话。
无论您如何努力,都无法在 64 位进程中运行 32 位 DLL,因此您需要在 32 位进程中运行它。
如果无法仅编译 32 位应用程序,则您别无选择,只能创建一个主机应用程序。
What you have to do is write a wrapper application that hosts the 32-bit DLL file, in a 32-bit process.
Your 64-bit application then has to talk to this 32-bit process, through network means, or by making the DLL functions available through a COM object, or similar.
You can not run a 32-bit DLL inside a 64-bit process, no matter how hard you try, so you need to run it in a 32-bit process.
If compiling your application for 32-bit only is not an option, you have no choice but to create a host application.
一般的想法可能是使用托管 32 位包装器 DLL 来包装(非托管)32 位 DLL,并使其对 COM 可见。这允许通过 COM 接口调用您的包装 DLL。
然后,您可以使用 COM 代理使 COM dll 显示为进程外 COM 服务器。
看看这个SO问题,了解有关此主题的更多信息: 从 x64 访问 x86 COM .NET。
A general idea could be to wrap your (unmanaged) 32-bit DLL with a managed 32-bit wrapper dll and make it COM visible. This allows calls to your wrapper DLL via its COM interface.
You can than use a COM surrogate to make your COM dll appear as an out of process COM server.
Take a look at this SO question for some further information on this topic: Access x86 COM from x64 .NET.
据我了解,您无法从 64 位应用程序使用 32 位 DLL。也就是说,您只能为 X86 编译您的应用程序。
您找到的解决方案可能是关于如何在“任何 CPU”编译的项目中使用 32 位和 64 位版本都存在的 DLL,具体取决于应用程序是在 32 位还是 64 位环境中运行。
为此,您可以用 C# 编写两个包装器 DLL,一个用于 64 位,一个用于 32 位,并根据您运行的是 64 位还是 32 位操作系统来使用相应的包装器。
但是,当您只有 32 位 DLL 时,这不起作用。 64 位应用程序不能使用 32 位 DLL,32 位应用程序也不能使用 64 位 DLL。
因此,您要么需要编译 32 位应用程序,要么必须创建 DLL 的 64 位版本。
As I understand things, you have no way of using a 32-bit DLL from a 64-bit application. That said, you may compile your application for X86 only.
The solution you found may be about how to use a DLL that exists for both 32- and 64-bit versions in an "Any CPU"-compiled project depending on whether the application is running in a 32- or 64-bit environment.
To do that, you could write two wrapper DLLs in C#, one for 64-bit and one for 32-bit and use the respective wrapper depending on whether you're running on a 64-bit or 32-bit OS.
However, this does not work when all you have is a 32-bit DLL. A 64-bit application can not use 32-bit DLLs, as well as a 32-bit application can not use 64-bit DLLs.
So you either need to compile your application for 32-bit, or you have to create a 64-bit version of your DLL.
尽管有点混乱,但解决方案可能是编写一个单独的 32 位应用程序,您可以与 64 位应用程序(例如您发送命令的控制台应用程序)进行通信。
不太漂亮,但如果您只需要偶尔调用它,则可能会起作用。
A solution although a bit of a mess could be to write a separate 32-bit application that you can talk to from your 64-bit application such as a console application you send commands to/from.
Not pretty but may work if you only need the occasional call to it.
只需将 .Net 应用程序编译为平台 x86 即可。它将在 x64 机器上运行,并且将使用您的 32 位 DLL。不要在包装纸上浪费时间。
Just compile your .Net Application as Platform x86. It will run on x64 machines and it will use your 32bit DLL. Don't waste time on a wrapper.