开发者

C++ How to make two programs communicate?

开发者 https://www.devze.com 2023-03-02 07:57 出处:网络
Question: What is the best way to make two programs (both running on the same computer) communicate and interact? (Windows, C++)

Question: What is the best way to make two programs (both running on the same computer) communicate and interact? (Windows, C++)

Theoretical situation:

  1. 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.

  2. 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.

  3. 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:

  1. 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?

  2. 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?

  3. 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.


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


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.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号