简体   繁体   English

32位dll导入64位.Net应用程序

[英]32 bit dll importing in 64 bit .Net application

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. 我有一个32位Delphi DLL,我想将其导入.NET WIN应用程序。 This application has to be built on ANY CPU mode. 此应用程序必须基于任何CPU模式构建。 Of course, the exception BadImageFormatException is thrown, which means that 64-bit applications can't load x86 DLLs. 当然,抛出异常BadImageFormatException,这意味着64位应用程序无法加载x86 DLL。 I googled around and found a solution, it said that I have to do wrapper, but it wasn't clear for me. 我google了一下,找到了一个解决方案,它说我必须做包装,但对我来说还不清楚。 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? 任何人都可以告诉我如何解决这个问题,有没有可能的方法,我可以将32位Delphi DLL导入到在任何CPU架构(64位,32位)或其他解决方案下构建的程序?

What you have to do is write a wrapper application that hosts the 32-bit DLL file, in a 32-bit process. 您需要做的是在32位进程中编写一个承载32位DLL文件的包装器应用程序。

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. 然后,您的64位应用程序必须通过网络方式与此32位进程通信,或通过COM对象或类似方式使DLL函数可用。

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. 不能在64位进程中运行32位的DLL,不管你怎么努力,所以你需要在32位进程运行。

If compiling your application for 32-bit only is not an option, you have no choice but to create a host application. 如果仅为32位编译应用程序不是一个选项,则除了创建宿主应用程序之外别无选择。

A general idea could be to wrap your (unmanaged) 32-bit DLL with a managed 32-bit wrapper dll and make it COM visible. 一般的想法可能是使用托管的32位包装器dll包装您的(非托管)32位DLL并使其可见COM。 This allows calls to your wrapper DLL via its COM interface. 这允许通过其COM接口调用包装器DLL。

You can than use a COM surrogate to make your COM dll appear as an out of process COM server. 您可以使用COM代理来使您的COM DLL显示为进程外COM服务器。 Take a look at this SO question for some further information on this topic: Access x86 COM from x64 .NET . 请查看此SO问题以获取有关此主题的更多信息: 从x64 .NET访问x86 COM

As I understand things, you have no way of using a 32-bit DLL from a 64-bit application. 据我所知,你无法使用64位应用程序中的32位DLL。 That said, you may compile your application for X86 only. 也就是说,您可以仅为X86编译应用程序。

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. 您找到的解决方案可能是关于如何在“任何CPU”编译的项目中使用32位和64位版本的DLL,具体取决于应用程序是在32位还是64位环境中运行。

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. 为此,您可以在C#中编写两个包装DLL,一个用于64位,一个用于32位,并根据您是在64位还是32位操作系统上运行使用相应的包装器。

However, this does not work when all you have is a 32-bit DLL. 但是,当您拥有的是32位DLL时,这不起作用。 A 64-bit application can not use 32-bit DLLs, as well as a 32-bit application can not use 64-bit DLLs. 64位应用程序不能使用32位DLL,以及32位应用程序不能使用64位DLL。

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位版本的DLL。

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. 一个解决方案虽然有点混乱,但可能是编写一个单独的32位应用程序,您可以从64位应用程序(例如控制台应用程序)与之交换命令。

Not pretty but may work if you only need the occasional call to it. 不漂亮,但如果您只需要偶尔打电话,可能会有效。

Just compile your .Net Application as Platform x86. 只需将.Net应用程序编译为Platform x86即可。 It will run on x64 machines and it will use your 32bit DLL. 它将在x64机器上运行,它将使用您的32位DLL。 Don't waste time on a wrapper. 不要在包装纸上浪费时间。

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

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