C++如何让两个程序进行通信?
问题:让两个程序(都在同一台计算机上运行)进行通信和交互的最佳方式是什么? (Windows,C++)
理论情况:
在 Windows PC 上,我有一个第三方软件,您可以用它来执行某些操作(例如打开/编辑/保存文件...这并不重要)。此第 3 方软件具有可用的C++ SDK,因此您可以为其开发插件。
我开发了一个带有 GUI 的独立独立 Windows C++ 程序(很可能使用 Qt)。该程序与第 3 方软件在同一台 Windows 计算机上运行。该程序将充当远程控制或发送器。
使用第 3 方软件的 SDK,我为第 3 方软件开发了一个小型插件。该程序充当接收器,以便我的 Qt GUI 可以发送该插件将接收的命令,从而相应地远程控制第 3 方软件。
因此,对于一个基本示例,我可以按下独立 GUI 上的一个按钮,该按钮将与插件进行通信,然后该插件将在第 3 方软件中打开指定的文件。
我本质上正在寻找的是开发独立的软件,该软件可以与我为第三方软件开发的插件来回通信。
对此最好的方法是什么?我真的不知道从哪里开始或看什么。是否有通用的 C++ 库可以使此类事情变得简单?
我对 C++ 比较陌生,对 Qt 也很陌生,所以请尝试详细说明。请不要只说“使用共享内存”或类似的话,因为我不会确切地知道你在说什么。
该程序不会在单独的计算机上运行,因此我认为这里不需要网络东西。
我正在考虑的一些问题:
如果用户同时打开多个第三方软件副本怎么办?我的独立软件如何知道要与哪一个进行通信?
如果我已经运行了独立软件并打开了第 3 方软件,该插件是否必须以某种方式注册或找到我的独立软件才能与之通信?
如果我已经运行了第 3 方软件,然后打开我的独立软件,它是否必须扫描插件以查看其是否正在运行或如何找到它?
任何帮助将不胜感激。我已经在 SO 上对该主题进行了搜索,但没有看到很多专门针对 C++ 的问题,并且大多数问题没有很多答案,或者不是很清楚,或者没有具体提及与插件的软件通信第三方程序。
Question: What is the best way to make two programs (both running on the same computer) communicate and interact? (Windows, C++)
Theoretical situation:
On a Windows PC, I have a 3rd party software that you use to do stuff (like open/edit/save files...it's not really important). This 3rd party software has an available C++ SDK so you can develop plugins for it.
I develop a separate standalone Windows 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 control or sender.
Using the 3rd party software's SDK, I develop a tiny plugin for 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.
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.
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.
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?
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.
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有许多解决方案,各有利弊。您必须对每个内容进行一些阅读才能找出最适合您的内容。没有适合每个人的解决方案,
这里是一个很好的起点 http://msdn.microsoft.com/en-us/library/aa365574(v=vs.85).aspx
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 标准;))。
如果您想要进行跨机器通信,它是免费的:)
当然,根据软件和使用情况,我会对通信进行加密。
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 ;)).
And if you 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.
如果您使用 Qt,我相信它有插件的概念: 如何创建Qt 插件
否则,COM 工作正常。对于 C++,我会使用 ATL 库,但 Qt 中也有 COM 支持。
如果你想更底层,我会使用套接字或命名管道(因为你使用的是 Windows)
If you're using Qt, I believe it has the notion of Plugin: How to Create Qt Plugins
Otherwise, COM works fine. With C++ , I would use the ATL library, but there is also COM support in Qt.
If you want to go more low level, I would use sockets or named pipes (since you're working with Windows)
我会使用套接字在远程控制应用程序和服务器应用程序之间建立通信。
通过在 SO 上搜索这些关键字,您会找到很多很多信息:IPC、RPC,套接字。
I'd go for sockets to establish a communication between your remote control application and the server app.
You'll find lots and lots of information by searching these keywords here on SO: IPC, RPC, sockets.