Accessing 32-bit DLLs from 64-bit code

此生再无相见时 提交于 2019-12-21 23:51:43

问题


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° 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()
    {
      ......
    }
}

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° 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° 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;

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.


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


回答1:


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.

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.

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.

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



来源:https://stackoverflow.com/questions/39948944/accessing-32-bit-dlls-from-64-bit-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!