1

I have an application (native C++, Windows), that cannot be run simultaneously on one machine. The behavior that I want to implement is this: on the attempt to run second instance of the application the first one stops running.

To do so I want to use WinApi function BroadcastSystemMessage() something like an example below.

When the application start it sends:

BroadcastSystemMessage(BSF_POSTMESSAGE, &dwRecepients, 0x666, 0, 0);

But, when I run my application in debug mode it doesn't hit

case 0x666:
    int iClose = 0 + 1;
break;

when I start another instance. The other messages are nandled correctly (WM_KEYDOWN, WM_ACTIVATE and others).

What I'm I doing wrong?

Kirill Daybov
  • 480
  • 3
  • 19
  • 1
    This is called Single instance application, there a a lot of samples how to do this, usually with named mutex. For example: http://support.microsoft.com/kb/243953 – Alex F Sep 10 '13 at 07:11
  • Ok, but most of those examples sipmply doesn't allow to run another instance. In my case — I need to kill the old one. There is a possibility that first one is stuck in running processes, so, it is preferable killing the old one rather than not allowing new one to start. – Kirill Daybov Sep 10 '13 at 07:49

2 Answers2

0

The solution began to work after I changed the type of the message to WM_APPCOMMAND + 10. Anyway, it didn't help, because BroadcastSystemMessage() doesn't broadcast messages to tabs in a browser, which is my case. Also, I couldn't find the range of message types that are allowed to be sent with BroadcastSystemMessage().

Kirill Daybov
  • 480
  • 3
  • 19
0

In order to broadcast a custom message you need to create an id for it with the RegisterWindowMessage function, for example:

UINT msg666 = RegisterWindowMessage(L"custom_devil_message");

and use it both in the sending and receiving code:

// sending code
BroadcastSystemMessage(BSF_POSTMESSAGE, &dwRecepients, msg666, 0, 0);

// receiving code
case msg666:
    int iClose = 0 + 1;
break;

Remember that messages do not work for console applications.

Zac
  • 4,510
  • 3
  • 36
  • 44