C++如何让两个程序通信?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5839132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ How to make two programs communicate?
提问by Jake Wilson
Question: What is the best way to make two programs (both running on the same computer) communicate and interact? (Windows, C++)
问题:使两个程序(都在同一台计算机上运行)进行通信和交互的最佳方法是什么?(视窗,C++)
Theoretical situation:
理论情况:
On a Windows PC, I have a 3rd party softwarethat you use to do stuff (like open/edit/save files...it's not really important). This 3rd party softwarehas an available C++ SDKso you can develop plugins for it.
I develop a separate standaloneWindows C++ program (most likely using Qt) with a GUI. This program is made to run on the same Windows computer as the 3rd party software. This program would act as a remote controlor sender.
Using the 3rd party software's SDK, I develop a tiny pluginfor the 3rd party software. This program acts as a receiver, so that my Qt GUI can send commands that this plugin would receive and thus remote control the 3rd party software accordingly.
在 Windows PC 上,我有一个3rd 方软件,你用来做一些事情(比如打开/编辑/保存文件……这并不重要)。这个第 3 方软件有一个可用的C++ SDK,因此您可以为其开发插件。
我使用 GUI开发了一个单独的独立Windows C++ 程序(很可能使用 Qt)。该程序与第 3 方软件在同一台 Windows 计算机上运行。该程序将充当遥控器或发送器。
使用 3rd 方软件的 SDK,我为3rd 方软件开发了一个小插件。该程序充当接收器,因此我的 Qt GUI 可以发送该插件将接收的命令,从而相应地远程控制 3rd 方软件。
So for a basic example, I could press a button on my standalone GUI that would communicate with the plugin that would then open a specified file in the 3rd party software.
因此,对于一个基本示例,我可以按下独立 GUI 上的一个按钮,该按钮将与插件进行通信,然后该插件将在 3rd 方软件中打开指定的文件。
What I'm essentially looking for is to develop standalone software that would communicate back and forth with a plugin that I would develop for the 3rd party software.
我本质上在寻找的是开发独立软件,该软件可以与我为第 3 方软件开发的插件来回通信。
What is the best approach to this? I really have no clue where to start or what to look at. Are there common C++ libraries that make this type of thing easy?
什么是最好的方法?我真的不知道从哪里开始或看什么。有没有通用的 C++ 库可以让这类事情变得简单?
I'm relatively new to C++ and very new to Qt, so please try to elaborate. Please don't just say "Use Shared Memory" or something like that cause I'm not gonna know exactly what you are talking about.
我对 C++ 比较陌生,对 Qt 也很陌生,所以请尽量详细说明。请不要只说“使用共享内存”或类似的话,因为我不知道你在说什么。
The program would NOT be run on a separate computer, so no network stuff is necessary here I don't think.
该程序不会在单独的计算机上运行,因此我认为这里不需要网络内容。
Some questions I was thinking about:
我在想的一些问题:
What if user has multiple copies of 3rd party software open at the same time. How does my standalone software know which one to communicate with?
If I already have my standalone software running and open the 3rd party software, does the plugin somehow have to register or find my standalone software to communicate with it?
If I already have the 3rd party software running and then open my standalone software does it have to scan for the plugin to see if its running or how does it find it?
如果用户同时打开多个 3rd 方软件副本会怎样。我的独立软件如何知道与哪一个通信?
如果我已经运行了我的独立软件并打开了 3rd 方软件,插件是否必须以某种方式注册或找到我的独立软件才能与之通信?
如果我已经运行了 3rd 方软件,然后打开我的独立软件,它是否必须扫描插件以查看它是否正在运行或如何找到它?
Any help would be greatly appreciated. I've done a search on the topic on SO but didn't see many questions for C++ specifically and most of them didn't have many answers or weren't very clear or didn't specifically refer to software communication with a plugin for a 3rd party program.
任何帮助将不胜感激。我已经在 SO 上对该主题进行了搜索,但没有看到很多专门针对 C++ 的问题,而且其中大多数问题没有很多答案,或者不是很清楚,或者没有专门提到与插件的软件通信第 3 方程序。
采纳答案by SteelBytes
there are many solutions with many pros and cons. you will have to do some reading on each to work out which is best for you. no solution is best for everyone
有许多解决方案,各有利弊。你将不得不对每一个都做一些阅读,以找出最适合你的。没有对每个人都是最好的解决方案
here is a good starting place http://msdn.microsoft.com/en-us/library/aa365574(v=vs.85).aspx
这是一个很好的起点http://msdn.microsoft.com/en-us/library/aa365574(v=vs.85).aspx
回答by Demian Brecht
Honestly, I'd most likely take the network-based approach. It decouples the standalone software and the plugin and as such, you could easily hook your plugin into any number of other utility programs or scripts as long as the communication protocol matches.
老实说,我很可能会采用基于网络的方法。它分离了独立软件和插件,因此,只要通信协议匹配,您就可以轻松地将插件挂接到任意数量的其他实用程序或脚本中。
So, I'd probably develop the plugin to act as a server, receiving commands and responding with statuses (hey, you could even follow the HTTP standard if you really wanted to ;)).
所以,我可能会开发插件来充当服务器,接收命令并以状态响应(嘿,如果你真的想要,你甚至可以遵循 HTTP 标准;))。
And ifyou ever wanted to do cross-machine communication, it comes for free :)
而如果你曾经想要做跨机器的通信,它来自免费:)
Of course, depending on the software and the usage, I'd encrypt the communication.
当然,根据软件和使用情况,我会对通信进行加密。
回答by Simon Mourier
If you're using Qt, I believe it has the notion of Plugin: How to Create Qt Plugins
如果你使用 Qt,我相信它有插件的概念:如何创建 Qt 插件
Otherwise, COM works fine. With C++ , I would use the ATL library, but there is also COM support in Qt.
否则,COM 工作正常。对于 C++ ,我会使用 ATL 库,但 Qt 中也有 COM 支持。
If you want to go more low level, I would use sockets or named pipes (since you're working with Windows)
如果您想要更底层,我会使用套接字或命名管道(因为您使用的是 Windows)