Communication between all running processes of the same application - c++

Is it possible to get HANDLES of all running processes of my application?
I need to send message to all currently running processes of my application. Every process may execute from a different location (folder/filename) and may be a different version of the same application. Other applications should never receive my messages.
Originator itself shouldn't receive a message, only other instances should receive a message. New instances of my application start in future also should communicate with other currently running copies.
Main window class and name may differ, so FindWindow() is not a solution for me.
How can I do this? What is the best way to solve this task?

Related

can I use pipes to communicate with existing application?

I was reading about pipes today, it seems an interesting for me to use it or to start about it.
but it's written there that the processes should be like a parent and child or client server and the communication will be between them.
there are some things I don't understand and before start I should be sure about them.
If I have a running process for example on my machine, and I want to send message to it, of course I can't control it, so how can I make it respond to my messages??
should I create the client and server and run them both??
Or the running process application can be a server for example and I can send some events to it??
if you can help me in this :)
thanks :)
If the running process is reading from stdin, you may write to /proc/the_pid/fd/0 to send your message.

Closing process from service without killing the processes

I am searching for a way to close a process running under any user by a windows service running under the system account. I've nearly tried everything but I could not find any solution except killing the process.
Why can't I kill the process?
I can't kill the process uses a tray icon. If I kill the process, the tray icon won't disappear.
What I've tried so far
I've already tried to use global eventhandles (did not work because the child process got extremely laggy).
I also tried to use PostMessage/SendMessage to communicate with the process. That solution did not work because a windows service can not interact with any user interfaces,...
I found another question (here on stackoverflow) which describes exactly my problem: Close a child process from a windows service.
This question does not contain a nice solution.
First of all I don't use C++ instead of C#. The next problem is, that the child process uses a mouse hook. So it has so run very fast without many overhead. Otherwise it would get laggy which would mean, that the mouse would lag on the whole system.
So is there really no simple solution to close a process from a windows service?
A service can use SetProcessWindowStation() and SetThreadDesktop() to attach to the interactive desktop of the target process before then sending messages to the target process.
Alternatively, the service can use CreateProcessAsUser() to run a new process in the same session as the target process, and then that process can send messages to the target process.

Windows CE centralized message queue between processes

Setup: 4 processes need to monitor a centralized source (I am thinking Message Queue) and be alerted when a message is put into the queue. This is for reporting errors that each process needs to know about to decide what they individually will do about it.
I have a few questions however, since I want to make sure Message Queues are the best way to go about it before I venture to far down that road.
Does the Message Queue API on Windows CE have events/notifications of some sort? If not, does the Windows CE OS itself allow you to tie in to the queue somehow for alerts of new messages arriving? Is there a better Inter Process communication tool for this kind of setup and requirements?
EDIT: Also, this will be a running log of errors that cant be lost, so I will put it on my flash memory.
What always works is a simple TCP connection via loopback. This also makes it easy to debug on a desktop system, in case you want. Concerning CE's message queues, those are always 1:1, so you'd need as many as you have connections between processes. Also, if you want to use them in a TCP stream-like manner, you'll need one for each direction.
There is another way you could use, and that is to monitor the file that you're logging the info to. I'm not sure to what extent CE supports a notification API for file changes, but if you don't need low-latency reactions, you can always poll every now-and-then. Alternatively, use a named event after writing to the logfile.

Simple online game servers monitor

I want to make a tool that runs on win32 to monitor our online game servers. The servers actually are .exe files. I need to know whether they have crashed so I can restart them. Therefore, the tool will have 2 main features:
Frequently check a list of the server programs to see whether they are running or not
Reopen the executable of any server that has crashed
Does anyone have any idea or knows an API to start with?
If you're in Win32, you can start out with C# using System.Diagnostics
using System.Diagnostics;
Then get a process list:
Process[] processlist = Process.GetProcesses();
foreach(Process theprocess in processlist){
Console.WriteLine(“Process: {0} ID: {1}”, theprocess.ProcessName, theprocess.Id);
}
And from there it's up to you what you want to do with the info.
Game servers should produce logs. You should make a service / cron job to monitor the logs. Depends on the contents of logs, your service should act respective actions, e.g. restart service, trigger alarms, etc.
I realize that this is not exactly what you asked for, but what about doing the monitoring with a fully-fledged monitoring tool such as Nagios? You would of course have to "teach" the monitoring tool about the processes that shall be monitored but you would also profit from its more advanced functions. In the case of Nagios, for example, these would comprise automatic e-mail notifications, an online dashboard of process status, notifications via SMS etc.
You should start the game servers using CreateProcess or similar in the win32 api.
You will get back a process handle in the lpProcessInformation parameter.
You can use WaitForSingleObject to WaitForMultipleObjects to wait until that handle is notified which will happen when the process terminates for any reason.

How can I Execute a Function when Windows Shut down

How Can I execute a function when Windows shutdown. Here is my scenario, I am mounting a drive using WNetAddConnection2 function in my application. Now I want user to set the option if the drive will be mounted on next system startup or not.
If he selects , not to mount on next startup , then I need to remove the drive using WNetCancelConnection2 , but this should only happen when user shutdown the system.
I can only think of only solution. Create a service which will check the user option and then decide whether to mount the drive or not.
Are there any other ways to go ahead with it?
If you have a main window (even an invisible one) that can process messages, you can handle the WM_ENDSESSION message.
See: http://msdn.microsoft.com/en-us/library/aa376889(v=VS.85).aspx
If you can make your app into a Windows service (or have your app communicate state with one that you provide) you can perform required actions on receipt of SERVICE_CONTROL_SHUTDOWN in your service control handler function. This would decouple your app that handles user interaction from the shutdown handling, which requires something to be running all the time (what if the user logs off?).
explorer.exe is the GUI process of windows which usually only gets shut down if Windows shuts down (exceptions have to be made for certain error conditions). You could listen on the WM_DESTROY window message for the process ID of explorer.exe and dismount then.
The way I can think of is to:
Register your program to auto Start up (when PC starts). Here's a tutorial on howto.
Store the user option (as mentioned above) in a repository or registry (if you know how). When your app would have started, you can read your registry and act accordingly.
For shutdown, your application will have to hook itself on a SystemEvent to detect shutdown (then you can act accordingly). Here's an example on howto (C#). For C++, you can listen to WM_ENDSESSION message.
I hope that my 2 cents can help you.