简体   繁体   English

使用COM在Windows上的本地计算机上进行进程间通信

[英]Interprocess communication on local machine on Windows using COM

I was investigating the option of doing interprocess communication on Windows using COM and C++ . 我正在研究使用COM和C ++在Windows上进行进程间通信的选项。

I found this article on MSDN , offering a list of interprocess communication options for Windows, and COM is one of them. 在MSDN上发现了这篇文章 ,提供了Windows的进程间通信选项列表,COM就是其中之一。
But, unfortunately, the COM option is just listed without much details. 但是,不幸的是,刚刚列出的COM选项没有太多细节。

Does someone have any pointers about doc or other resources on how to use COM for interprocess communication on Windows? 有人对如何在Windows上使用COM进行进程间通信有任何关于doc或其他资源的指针吗?

I'm not interested in communicating with remote machines (so: no DCOM ); 我对与远程机器通信不感兴趣(所以: 没有DCOM ); I'm just interested in interprocess communication on the same local machine. 我只对同一本地机器上的进程间通信感兴趣。

The idea would be to define some custom COM interfaces implementing some custom communication protocol, and then have a server program and a client program (each one in its own process, running on the same local machine), and use COM to communicate between the two (eg the client makes requests to the server, and the server returns proper answers, everything using COM interfaces). 这个想法是定义一些实现一些自定义通信协议的自定义COM接口,然后有一个服务器程序和一个客户端程序(每个都在自己的进程中,在同一个本地机器上运行),并使用COM在两者之间进行通信(例如,客户端向服务器发出请求,服务器返回正确的答案,所有内容都使用COM接口)。

So, for example: are there predefined COM interfaces to implement an interprocess communication? 那么,例如:是否有预定义的COM接口来实现进程间通信? If so, what are them? 如果是这样,他们是什么?

It would be helpful to have some tutorial or a more detailed guide about this subject. 有一些关于这个主题的教程或更详细的指南会很有帮助。

If you have a COM interface that both sides know about then one process can register some object implementing that with the Running Object Table using a moniker. 如果你有一个双方都知道的COM接口,那么一个进程可以使用一个名字对象注册一个使用运行对象表实现它的对象 The other process can then retrieve the object from this interprocess table using the moniker identifier and query it for the known interface. 然后,另一个进程可以使用名字对象标识符从此进程间表中检索对象,并查询已知接口。 Now the client process has a reference to something existing in the other process and calls will be marshalled by COM. 现在,客户端进程引用了其他进程中存在的内容,并且调用将由COM进行编组。

There are lots of things to go wrong though, particularly with ensuring your interfaces are being marshalled correctly. 但是有很多事情要出错,尤其是确保正确地编组接口。 Marshalling is often not well tested until you start using multiple processes or you have been using .Net with your COM interfaces. 在您开始使用多个进程或者您的COM接口使用.Net之前,编组通常不会经过良好测试。 Using oleautomation compatible types and marking the interface in IDL with the [oleautomation] attribute can help to ensure typelibrary marshalling will work but also attention to the other attributes used with arrays is important. 使用oleautomation兼容类型并使用[oleautomation]属性在IDL中标记接口可以帮助确保类型库编组工作,但是注意与数组一起使用的其他属性也很重要。 We found this with the IPropertyBag2 interface some years ago. 几年前我们用IPropertyBag2接口找到了它。 The Visual Studio 6 IDL description looks like this in ocidl.idl: Visual Studio 6 IDL描述在ocidl.idl中如下所示:

HRESULT Read(
            [in] ULONG cProperties,
            [in] PROPBAG2 * pPropBag,
            [in] IErrorLog * pErrLog,
            [out] VARIANT * pvarValue,
            [out] HRESULT * phrError
        );

and does not marshall more than a single VARIANT from the array provided. 并且不会从提供的阵列中编组多个VARIANT。 The newer version looks like this: 较新的版本如下所示:

HRESULT Read(
           [in] ULONG cProperties,
           [in, size_is(cProperties)] PROPBAG2 * pPropBag,
           [in, unique] IErrorLog * pErrLog,
           [out, size_is(cProperties)] VARIANT * pvarValue,
           [in, out, unique, size_is(cProperties)] HRESULT * phrError
       );

which correctly associates the size of the pvarValue array with the size specified by the cProperties parameter. 它正确地将pvarValue数组的大小与cProperties参数指定的大小相关联。 Assuming a typelibrary with the second definition has been registered then this interface should marshal properly now but a few years ago those missing parameters cost us a few brain cells working out why persistence was failing. 假设已经注册了具有第二个定义的类型库,那么这个界面现在应该正确编组,但几年前这些缺失的参数花费了我们一些脑细胞来解决为什么持久性失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM