是否可以通过 COM 对象进行通信?

发布于 2024-08-02 12:54:30 字数 164 浏览 10 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不乱于心 2024-08-09 12:54:30

如果要在两个进程之间进行通信,请使用 命名管道

(可以调用远程 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.)

丢了幸福的猪 2024-08-09 12:54:30

显然,你的问题的答案是肯定的。

后续是:

  1. 为什么要共享数据?你想实现什么目标?数据是什么样的?您是否需要编组复杂的结构并进行复杂的 RPC 调用,或者您是否内存中只有一大块数据并且您想让两个人一起使用它?
  2. 为什么您认为 COM 是最好的方法?您是否考虑过仅发送窗口消息,或使用命名管道(如@avakar建议),或使用带有命名互斥体的共享内存?

#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:

  1. Why do you want to share data? What are you trying to accomplish? What does the data look like? Do you need to marshal complex structures and make complex RPC calls, or do you just have a big chunk of data in memory and you want to have two people party on it?
  2. Why do you think COM is the best way to do it? Have you considered just sending Window Messages, or using a Named Pipe (as suggested by @avakar), or using shared memory with named mutexes?

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.

指尖凝香 2024-08-09 12:54:30

您可以查看共享 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.

情深缘浅 2024-08-09 12:54:30

com dll 可以协调两个独立进程之间的数据共享吗?

注意:该 DLL 将有两个实例,每个进程一个。如果 DLL 拥有/管理数据,则 DLL 的每个实例都将拥有自己的数据:该数据不会在进程之间共享。

两个 exe 可以通过 com 接口进行通信,其中 COM 接口支持类似我不知道的方法,putDatagetData,但是我认为您可能希望将此 COM 对象构建/打包/安装为进程外 (*.exe) COM 对象,而不是作为进程内 (*.dll) COM 对象。

或者,如果您确实使用DLL,则必须实现它们以应对它们的两个单独实例:例如,它应该使用跨进程互斥体而不是进程内临界区,并且应该使用跨进程共享内存而不是进程内共享内存。 -进程私有堆内存。

这可能不是协调数据共享的最佳方式,但它可能是一种方式。

Can a com dll coordinate data-sharing between 2 seperate processes?

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 and getData, 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.

-黛色若梦 2024-08-09 12:54:30

是的,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.

燕归巢 2024-08-09 12:54:30

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文