用托管code包装调用64位管理code的32位非托管code的

用托管code包装调用64位管理code的32位非托管code的

本文介绍了使用托管code包装调用64位管理code的32位非托管code的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与我对面,我必须调用原生32位$ C $从托管的64位处理C的情况即将到来的频率正在增加,因为64位机和应用程序成为prevalent。我不想记住我applciation为32位,我不能获得64位版本的code正被调用的。

The frequency with which I am coming across the situation where I have to call native 32-bit code from a managed 64-bit process is increasing as 64-bit machines and applications become prevalent. I don't want to mark my applciation as 32-bit and I cannot obtain 64-bit versions of of the code that is being calling.

这是我目前使用的解决方案是创建一个加载了程序,使从64位进程的32位调用C ++ COM垫片。

The solution that I currently use is to create C++ COM shims that are loaded out of process to make the 32-bit calls from the 64-bit process.

此COM垫片解决方案运作良好,并跨进程调用的幕后由COM,最大限度地减少这种方法的开销处理。

This COM shim solution works well and the cross process calls are handled behind the scenes by COM, which minimises the overhead of this approach.

不过,我喜欢把我们的一切承担使用C#,想知道如果有,最大限度地减少这样的开销任何框架的新发展。我已经看过IPCChannel但我觉得这种做法是比较整洁的COM垫片的解决方案。

I would however like to keep all the new development that we undertake using C# and wondered if there are any frameworks that minimise the overhead of doing this. I have looked at IPCChannel but I feel that this approach is not as neat as the COM shim solution.

谢谢,埃德

推荐答案

我有同样的问题,我的解决方案是使用的。基本上,项目包括:

I had the same problem and my solution was to use remoting. Basically the project consisted of:

  • 独立于平台的 CalculatorRemote.dll
    • CalculatorNative 静态内部类的32倍的P / Invoke方法
    • RemoteCalculator 从<$c$c>MarshalByRefObject它使用从 CalculatorNative 本地方法;
    • Platform-independent CalculatorRemote.dll library with
      • CalculatorNative internal static class with x32 P/Invoke methods
      • RemoteCalculator class derived from MarshalByRefObject which used native methods from CalculatorNative;

      因此​​,如果主要的应用始于64位模式下它生成一个 RemoteCalculator 主机应用程序并用远程的 RemoteCalculator 实例。 (当在32倍,它只是使用了一个本地实例 RemoteCalculator )。最棘手的部分告诉计算器主机应用程序关闭。

      So if the main application started in x64 mode it was spawning a RemoteCalculator host application and used remoted RemoteCalculator instance. (When in x32 it just used a local instance of RemoteCalculator.) The tricky part was telling calculator-host application to shut down.

      我觉得这是更好的比使用COM,因为:

      I think this it better than using COM because:

      • 您没有在任何地方注册COM类;
      • 与COM互操作应该比NET远程速度较慢;
      • 有时候,如果事情是在COM端,你需要重新启动你的应用程序从该收回去错了; (可能我只是不很熟悉COM)
      • 在X32模式下运行时不会有与远程任何性能损失 - 所有的方法都将在同一个AppDomain中调用

      这篇关于使用托管code包装调用64位管理code的32位非托管code的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 12:24