I am looking for a way in C++ where I can communicate between 2 programs. I'll sketch below what I am trying to do.
I have a console application running permanently. It has some basic interactions with the user using it.
I want the user to be able to load 'plugins' from a specified file. These plugins keep loaded and what they basically do is they send to the console software what commands they want reserved. The console then checks if that command is taken, and if so returns an error status. The plugin can check itself if it wants to keep running or shut down.
When a command is run, the console will check it's data to see if any plugin is using that command. If so it'll send the command to the plugin with the necessary metadata. The plugin then responds with a code that might signify a message or a error or any response. When the plugin feels it's done it can shut down itself, however it will communicate with the console so the console can remove the commands linked to it. Also the user should be able to disable the plugin by typing a command in the console doing so. The console will communicate with the plugin asking for it to shut down.
It is crucial that the plugins keep running in the background. They might have setup data objects that can be used in later commands, or they might be doing background processing. The communication data send between the console and it's plugins can be multiple MiBs big and it needs to be send as fast as possible.
I cannot use a network connection between the plugins.
Does anyone have any idea how to achieve this? This is not exactly what the program does, but it is a simplified scenario that I can easily port to my application.
Do the plugins have to be actual programs or can they be libs?
You could do something similar to what Winamp does and have the plugins as DLLs (or whatever other dynamic lib your OS uses). Then communicating back and forth between the main program and the plugin is just a matter of designing an API for the plugin.
1. List dynamic libraries e.g. inside 'plugins' sudirectory.
2. For every listed file import pointers for Your API:
- GetComandList
- ProcessCommand
and save them in some table. It can be table of structs with
two function pointers. One struct = one plugin.
(...)
3. Go into your main code. If user gives command:
a) Check does any plugin know it by calling GetCommandList
(You can save this list somewhere at initialization time).
b) If yes, call ProcessCommand() for given plugin (via function pointer).
Thank you very much. This gave me some direction so now I can start Googleing again. I don't think I will need any more help, but if you feel like dropping something go ahead!