从64位代码访问32位DLL [英] Accessing 32-bit DLLs from 64-bit code

查看:129
本文介绍了从64位代码访问32位DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要迁移32位dll才能在64位C#(以及C ++)应用程序中使用它. dll是用非托管的delphi代码编写的.我无法重新编译dll,唯一的方法是使用进程间通信(IPC).我搜索了很长时间,但发现的信息却很少.我在此链接中找到了最好的指南:从64位代码访问32位DLL .

I need to migrate a 32bit dll in order to use it in a 64bit C# (and also C++) applications. The dll is write in unmanaged delphi code. I can't recompile the dll and the only way is to use the interprocess communication (IPC). I search for a long time but i found not many information about. The best guide i found is in this link: Accessing 32-bit DLLs from 64-bit code.

我遵循本指南来实现自己的目标,因为该论坛中经常引用它.因此,指南说明了我必须执行的三个步骤:

I followed this guide for achieve my goal because very often is quoted in this forum. So the guide explain I have to make three steps:

1°步骤-创建一个实现COM对象的32位组件,该对象加载并调用32位DLL,并将32位DLL接口公开为COM接口.所以我做了与解释

1° STEP - Create a 32-bit component implementing a COM object which loads and calls into the 32-bit DLL, and exposes the 32-bit DLL interface as a COM interface. So I made the same thing as explained here (example taken from previous link):

[ComVisible(true), GuidAttribute("137AD71F-4657-4362-B9E4-C6D734F1F530")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IGetMyString
{
   string GetMyString();
}

[ComVisible(true), GuidAttribute("89BB4535-5A89-43a0-89C5-19A4697E5C5C")]
[ProgId("CallPDW.Class1")]
[ClassInterface(ClassInterfaceType.None)]
public class Class1 : IGetMyString
{
    string GetMyString()
    {
      ......
    }
}

然后,我必须注册.NET COM程序集,因此必须使用Regasm.exe,但是我创建了Visual Studio安装和部署项目,该项目构建MSI文件并进行与Regasm相同的操作.然后,我控制了注册表编辑器,并找到了HKEY,因此它可以正常工作. 因此,在执行此步骤之前,一切正常或似乎一切正常.

Then i have to register the .NET COM assembly so i have to use Regasm.exe, but instead I created Visual Studio Setup and Deployment project that builds an MSI file and make the same things Regasm do. Then i controlled the Registry Editor and I found the HKEYs so it works. So until this step all is ok or seems to be ok.

2°步骤-通过创建标准COM + OOP应用程序(使用dllhost作为代理过程),将该COM组件配置为进程外(OOP);

2° STEP - Configure this COM components for out-of-process (OOP) by either creating a standard COM+ OOP application (using dllhost as the surrogate process);

3°步骤-创建一个64位包装DLL,该DLL实现与 原始的32位DLL,导入上面创建的COM对象的COM接口, 将当前对公开接口的调用转换为对COM对象接口的调用,传输调用参数,接收返回值并委托 他们给呼叫者;

3° STEP - Create a 64-bit wrapper DLL which implements the same interface as the original 32-bit DLL, imports the COM interface of the COM object created above, translates current calls to the exposed interface into calls to the COM object interface, transfers the call parameters, receives return values and delegates them to the callers;

问题在于我不知道如何实现2°和3°步骤,因为我没有在网上或论坛上找到有关此的任何信息.因此,我需要帮助来实现步骤2°和3°.还可以,也可以链接到其他帖子等. 预先感谢.

The problem is that the 2° and the 3° STEPs I have no idea how to achieve because I don't found anything on the web or in the forum about this. So I need help to achieve step 2° and 3°. Is ok also some link to other posts etc. Thanks in advance.

其他信息(2016年12月10日-晚上11:10): 此DLL通过两个接口将其功能暴露给32位本机环境: 1.)具有函数指针(WINAPI *)的C/C ++头 2.)带有P/Invoke的.NET界面

OTHER INFORMATIONS (12/10/2016 - 11:10 PM): This DLL expose its functions to the 32bit native environment through two interfaces: 1.) a C/C++ header with function pointers (WINAPI*) 2.) a .NET interface with P/Invoke

推荐答案

您处在正确的轨道上.您已经创建并注册了COM可见包装DLL,这对于公开Delphi库的方法是必需的.

You're on the right track. You've already created and registered a COM visible wrapper DLL, which is necessary to expose the methods of your Delphi library.

由于您的x64进程无法调用此x86包装器,因此需要一个替代进程.但是,如果您不打算使用DLL的任何服务,则将DLL转换为完整的COM+服务器几乎没有任何意义.

Since your x64 process can't call into this x86 wrapper, there's the need for a surrogate process. However, it makes little sense to transform your DLL to a full fledged COM+ server, if you're not going to use any of its services.

相反,我建议将包装DLL托管到默认的COM代理中.为此,请调用基于x86的OLE/COM对象查看器,它是Windows SDK的一部分.展开所有对象"节点,然后选择您的COM可见类.从那里,切换到实施"选项卡,然后选中使用代理过程".切换到注册表"选项卡,并确认"AppID"节点现在包含[DllSurrogate]条目.注意:如果需要自动执行此步骤,则需要自己手动或通过设置过程将适当的值写入注册表.

Instead, I'd recommend hosting your wrapper DLL into the default COM surrogate. To do so, invoke the x86 based OLE/COM Object Viewer, which is part of the Windows SDK. Expand the "All Objects" node and select your COM visible class. From there, switch to the "Implementation" tab and check "Use Surrogate Process". Switch to the "Registry" tab and confirm that the "AppID" node now contains a [DllSurrogate] entry. Note: if you need to automate this step, you'll need to write the appropriate values to the registry yourself, either manually or via your setup procedure.

您现在应该可以从x64主机创建COM可见包装类的实例.

You should now be able to create an instance of the COM visible wrapper class from your x64 host.

这篇关于从64位代码访问32位DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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