在另一个进程之间进行内部COM对象共享 [英] In-Proc COM object sharing across another Process

查看:262
本文介绍了在另一个进程之间进行内部COM对象共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我提出这个问题之前,我想说清楚,我知道有进程间通讯的图书馆和技术。这是一个关于COM的学习问题。
我也知道关于out-of-proc服务器,但这不是我正在寻找。

Before I ask this question I would like to make it clear that I know there are libraries and techniques for Inter process communcation. This though, is a learning question about COM. I also do know about out-of-proc servers but that's not what I am looking for.

问题:

我想知道什么,因为我不知道,是可能的,如果是如何,共享一个进程内COM对象(定义在DLL中的对象)生活在一个进程已经在过程中实例化)跨另一个进程?即,如何从进程B中的进程A获取指向进程内对象的指针?

What I want to know, because I don't know this, is it possible, and if yes how, to share an in-proc COM object (object defined in a DLL) living in one process (has been instantiated in the process) across another process? Ie, how do I obtain a pointer to the in-proc object from proces A in process B?

提前感谢。

推荐答案

无论您是在单个进程中还是在单独进程之间共享公寓之间的单个对象实例,基本原则都是相同的。

Yes, it's possible. The underlying principle is the same regardless of whether you are sharing a single object instance between apartments in a single process, or between separate processes.

这里有两种方法:最简单的是使用运行对象表:这本质上是一个命名COM对象的工作站范围表。您有一个进程使用知名的名称向表中添加一个对象,并让另一个进程查找该对象。

There's two approaches here: perhaps the simplest is to use the Running Object Table: this is essentially a workstation-wide table of named COM objects. You have one process add an object to the table with a well-known name, and have the other process look up that object.

另一种方法是使用封送。封送处理是使用COM API来获取描述对象位置的一系列字节的过程。然后,您可以使用任何想要的方式(共享内存,文件,管道等)将该系列字节复制到另一个进程,然后在接收进程中使用另一个COM API来解除对象的集合; COM然后在该过程中创建一个合适的远程代理,与原始通信。查看API CoMarshalIntefcace CoUnmarshal Interter 了解更多详情。

The other approach is to use marshaling. Marshaling is the process of using a COM API to get a series of bytes that describe the location of an object. You can then copy that series of bytes to another process using any means you want to (shared memory, file, pipe, etc), and then use another COM API in the receiving process to unmarshal the object; COM then creates a suitable remoting proxy in that process that communicates back to the original one. Check out the APIs CoMarshalIntefcace and CoUnmarshalInterface for more details.

请注意,这两个都需要您为对象提供合适的远程支持;您使用的接口需要在IDL中进行描述,并进行相应的编译和注册。

Note that both of these require that you have suitable remoting support in place for the object; the interfaces you are using need to be described in IDL and compiled and registered appropriately.

-

对于这些情况,没有代码可用。

I don't have code handy for either of these cases unfortunately.

对于CoMarshalInterface方法,过程如下:

For the CoMarshalInterface approach, the process is something like:


  • 使用CreateStreamOnHGlobal(使用NULL hglobal)创建一个由COM根据需要分配的HGLOBAL支持的IStream
  • 使用CoMarshalInterface来封送流的接口指针
  • 使用GetHGlobalFromStream从流中获取HGLOBAL
  • 使用GlobalLock / GlobalSize锁定HGLOBAL并访问marhaled数据GlobalUnlock完成时)
  • 使用任何方法将字节复制到目标进程。
  • Use CreateStreamOnHGlobal (with NULL hglobal) to create an IStream that's backed by a HGLOBAL that COM allocates as needed
  • Use CoMarshalInterface to marshal the interface pointer to the stream (which in turn writes it to the memory backed by the HGLOBAL)
  • Use GetHGlobalFromStream to get the HGLOBAL from the stream
  • Use GlobalLock/GlobalSize to lock the HGLOBAL and access the marhaled data (GlobalUnlock when done)
  • Use whatever means you want to to copy the bytes to the target process.

在远端,使用:


  • GlobalAlloc / GlobalLock / GlobalUnlock创建一个新的HGLOBAL并使用封送的数据填充它
  • CreateStreamOnHGlobal与您的新HGLOBAL
  • 将此流传递给CoUnmarshalInterface
  • GlobalAlloc/GlobalLock/GlobalUnlock to create a new HGLOBAL and populate it with the marshaled data
  • CreateStreamOnHGlobal with your new HGLOBAL
  • Pass this stream to CoUnmarshalInterface

正常的COM和Windows引用计数/资源规则适用于所有这些; AddRef / Release;使用GlobalFree释放您分配的任何HGLOBAL等。

Normal COM and Windows refcounting/resource rules apply across all of this; AddRef/Release as appropriate; use GlobalFree to free any HGLOBALs that you allocate, etc.

这篇关于在另一个进程之间进行内部COM对象共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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