0

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?

NoSkill
  • 718
  • 5
  • 15
  • What do you mean by "message"? Different meanings (e.g. a simple notification that something has happened versus sending an arbitrary text string that the recipients have to parse/interpret) will be suited to different methods of sending the message. Other considerations include .... Does the originator of the message need to also receive it? If new instances of your application start in future, do they need to receive previous messages? Does it matter if some instances receive a particular message more than once? – Peter Aug 15 '22 at 09:14
  • I mean window-messages created by `RegisterWindowMessage`, or may be `WM_COPYDATA`. Originator itself doesn't receive a message, only other instances should receive a message. New instances of my application start in future also should communicate (send/receive messages) with other currently running copies. Is there solution without using `FindWindow()`, as of window class and name may differ? – NoSkill Aug 15 '22 at 09:39
  • See [Named Pipes](https://learn.microsoft.com/en-us/windows/win32/ipc/named-pipes) _"...A named pipe is a named, one-way or duplex pipe for communication between the pipe server and __one or more pipe clients__...."_ – Richard Critten Aug 15 '22 at 09:45
  • That's the question you should have asked in the first place. Instead you made it so general, that there are many possible answers. Also, your question is off-topic as C++ - windows API doesn't rely on the program being any particular programming language (and the c++ tag is standard C++, not C++ under windows). Anyway, in the windows API, there are options like `BroadcastSystemMessage()` [from win 2000 I think] and named pipes or message boxes - but your program still needs to set such things up, and know how to handle the messages. – Peter Aug 15 '22 at 09:49
  • This looks fundamentally broken. You expect Windows to know that "DIR1\app.exe" is the same application as "DIR2\app.exe", but bioth are unrelated to "DIR3\app.exe". – MSalters Aug 15 '22 at 09:49
  • The system cannot help you solve the problem to identify what is and what isn't *"your application"*. That's something you will have to implement yourself, by implementing a desktop-/system-wide registry where applications register and deregister themselves. This can be as simple as creating a named [event object](https://learn.microsoft.com/en-us/windows/win32/sync/event-objects) that the "server" application signals. – IInspectable Aug 15 '22 at 09:57
  • 2
    "*I need to send message to all currently running processes of my application*" - if the originator does not need a response from each target process, then I would just use `PostMessage()` with `HWND_BROADCAST` and a custom message registered with `RegisterWindowMessage()`. Only your app will know what to do with the message, it is OK that other apps will receive it, they will just ignore it. And if the originator also receives it, it can chose to ignore it, too. – Remy Lebeau Aug 15 '22 at 18:33
  • What's the difference between *Communication between all running processes of the same application* and *Communication between all running processes*? I mean Have you considered [Interprocess Communications](https://learn.microsoft.com/en-us/windows/win32/ipc/interprocess-communications)? After all, you're the code owner. – YangXiaoPo-MSFT Aug 16 '22 at 06:13
  • @remy-lebeau Thanks. I have used HWND_BROADCAST before, but I read somewhere that it's not recommended for inter-process communications. So I asked the question which way is the best in this case. I don't understand why peops are down-voting my question... – NoSkill Aug 16 '22 at 09:03
  • @NoSkill "*I read somewhere that [HWND_BROADCAST is] not recommended for inter-process communications*" - Not for full communication, no. But for just sending a notification to multiple processes, it works just fine. "*I don't understand why peops are down-voting my question*" - I didn't downvote, but it is probably due to a lack of research, lack of context, and asking for opinions. – Remy Lebeau Aug 16 '22 at 19:50

0 Answers0