64 to 32 bit Interop - how?

南楼画角 提交于 2019-11-30 01:25:28

问题


I need to integrate some legacy 32-bit code - for which I don't have the source code, into a project in such a way that it can be called from a 64-bit .NET assembly. The original code is implemented as a 32-bit COM object in a DLL. Windows doesn't allow direct calls from 64 to 32-bit objects, so I'm looking for inspiration on how to deal with this situation.

How can a legacy 32-bit COM object be accessed from a 64-bit .NET assembly?

UPDATE: We discovered that the COM component was itself a wrapper around some ANSI C, which we founf the original source for. We were able to compile that in Visual Studio as a native 64-bit dll, and import that into .NET - sorry to move the goalposts!


回答1:


The best approach is to make an out of process COM server that wraps your 32-bit DLL. You can then call this from 64bit code.

Here is an explanation of the basic concepts.




回答2:


What you need to do is create two processes communicating with IPC. This way, one can be 32 bit, and one can be 64 bit. You need to create a 32 program which links with the COM object and exposes its API through some IPC mechanism such as a named pipe. This way your .NET program can access it from another process.




回答3:


Check out this blog post. You can reference a 32 bit COM assembly from a 64 bit .NET application using a runtime callable wrapper. The short version is the following...

  1. Use tlbimp.exe to create a 64 bit Runtime Callable Wrapper:

    tlbimp.exe foo.dll /machine:x64 /out:Interop.Foo.dll

  2. Register the COM assembly (not the RCW) if you haven't already:

    regsvr32.exe foo.dll

  3. Reference the RCW (eg. Interop.Foo.dll) from your application. Change your Build Configuration to x64 and let 'er rock.



来源:https://stackoverflow.com/questions/875112/64-to-32-bit-interop-how

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