是否可以通过 COM 对象进行通信?
2 个 exe 是否可以通过 COM (ActiveX?) 接口进行通信? COM DLL 可以协调两个独立进程之间的数据共享吗?
Is it possible for 2 exes to communicate through a COM (ActiveX?) interface? Can a COM DLL coordinate data-sharing between 2 seperate processes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果要在两个进程之间进行通信,请使用 命名管道。
(可以调用远程 COM 对象并以这种方式共享数据,但它不必要地复杂。)
If you want to communicate between two processes, use a named pipe.
(It is possible to call a remote COM object and share data that way, but it's unnecessarily complex.)
显然,你的问题的答案是肯定的。
后续是:
#1 的答案将为#2 提供信息。
但可以说 COM 是最适合您的解决方案。如果进程 A 中有一些代码想要在进程 B 中执行某些操作,则可以在 b.exe 中注册一个 COM 对象,然后让进程 A CoCreateInstance() 该对象。 COM 将启动 b.exe,创建由传递给 CoCreateInstance() 的 CLSID 指定的对象,然后为您提供一个指向 CoCreateInstance() 的 IID 参数中请求的指定接口的指针。现在,您可以从进程 A 调用进程 B 中对象的方法。
如果您有进一步的问题或澄清,请随时跟进。
COM 将为您整理基本数据类型(基本上是 VARIANT 支持的所有数据类型)。
The answer to your question, obviously, is yes.
The follow-up is:
The answers to #1 will inform #2.
But lets say COM is the best solution for you. If you have some code in process A that wants to do something in process B, you register a COM object in b.exe and then have process A CoCreateInstance() the object. COM will start b.exe, create the object specified by the CLSID you pass to CoCreateInstance() and then give you a poitner to the specified interface you requested in the IID parameter to CoCreateInstance(). Now you can call methods on the object in process B from process A.
If you have further questions or clarifications, feel free to follow-up.
COM will marshal basic data types (basically everything a VARIANT supports) for you.
您可以查看共享 CoffeeMonitor用VB6编写的简单例子。不过,这对于多方通信可能最有用,而不是简单的一对一场景。
另一种方法可能是使用 Mailslots与命名管道不同,它可以以类似于 UDP 广播的方式使用广播。
You might look at Sharing the CoffeeMonitor for a simple example written in VB6. This is probably most useful for n-way communication though, rather than simple one-on-one scenarios.
Yet another approach might be to use Mailslots which unlike Named Pipes can use broadcasts in a manner analogous to UDP broadcasts.
注意:该 DLL 将有两个实例,每个进程一个。如果 DLL 拥有/管理数据,则 DLL 的每个实例都将拥有自己的数据:该数据不会在进程之间共享。
两个 exe 可以通过 com 接口进行通信,其中 COM 接口支持类似我不知道的方法,
putData
和getData
,但是我认为您可能希望将此 COM 对象构建/打包/安装为进程外 (*.exe
) COM 对象,而不是作为进程内 (*.dll
) COM 对象。或者,如果您确实使用DLL,则必须实现它们以应对它们的两个单独实例:例如,它应该使用跨进程互斥体而不是进程内临界区,并且应该使用跨进程共享内存而不是进程内共享内存。 -进程私有堆内存。
这可能不是协调数据共享的最佳方式,但它可能是一种方式。
Caution: there will be two instances of that DLL, one in each process. If the DLL has/manages data, each instance of the DLL will have its own data: that data won't be shared between processes.
It is possible for 2 exes to communicate through a com interface, where the COM interface supports methods like, I don't know,
putData
andgetData
, however I think you may want to build/package/install this COM object as an out-of-process (*.exe
) COM object, and not as an in-process (*.dll
) COM object.Or if you do use DLLs, you'll have to implement them to cope with there being two separate instance of them: e.g. it should use cross-process mutexes instead of in-process critical section, and cross-process shared memory instead of in-process private heap memory.
This may not be the best way to coordinate data-sharing, but it could be a way.
是的,2 个 exe 可以通过 COM 接口进行通信。
COM 客户端和 COM 服务器可以位于同一个进程中,即
同一台计算机或两台计算机上的两个独立进程
不同的计算机。
Yes, it is possible for 2 exes to communicate through a COM interface.
A COM client and a COM server can be in the same process, in
two separate processes on the same computer or on two
different computers.
DCOM(分布式 COM)是与 COM 对象通信的技术在单独的进程(甚至机器)中运行。尽管详细说明您的场景会很有帮助,但对于您想要做的事情可能有更好的选择。
DCOM (Distributed COM) is the technology for communicating with a COM object running in a seperate process (or even machine). Though it would be helpful if you elaborated on your scenario, there might be a better option for what you're trying to do.