🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages Started by salamander1965 Thu, 26 Jun 2008 22:03
PostThreadMessage() vs. PostMessage(HWND_BROADCAST, ...)
#104
Author: salamander1965
Date: Thu, 26 Jun 2008 22:03
48 lines
2892 bytes
Should a message queue that receives a message from PostThreadMessage() also 
receive one from PostMessage() with HWND_BROADCAST as the first parameter 
(assuming the queue belongs to a primary thread of a process)?

I have situation in which a main app starts a background process using 
CreateProcess() and then sends a registered message to the background app to 
register (the background app just keeps a reference count of the number of 
main apps).  The main app uses the following during processing of its 
WM_CREATE message.

PostMessage(HWND_BROADCAST, uiAppRegisterMsgID, KBLOCKREGISTER, NULL);

The background app does not have any windows and does not receive user 
input, so there are no calls in the message loop to TranslateMessage() and 
DispatchMessage().  The message loop just watches for the registered message 
to arrive:

while (GetMessage(&msg, NULL, 0, 0)) {
  if (msg.message == uiAppRegisterMsgID) {
    MessageBox(NULL, "App Message retrieved from queue", "KBLockExe message 
loop", MB_OK);
    if (msg.wParam == 0) {
      if ((--nAppRefCount) <= 0) {
        lpfnUnsetHook(hKBHook);
        CloseHandle(hReadyEvent);
        CloseHandle(hKBLockExeMux);
        FreeLibrary(hDllInst);
        ExitProcess(0);
      }
    }
    else if (msg.wParam == 1) {
      nAppRefCount++;
      MessageBox(NULL, "Application registered", "Registration", MB_OK);
    }
  }
}

The message loop never sees the registered message, though.  If the call to 
PostMessage() is replaced by PostThreadMessage() using the threadID of the 
background app, the message loop above responds as it should.

The problem is I cannot guarantee that the main app will always have the 
threadID it needs to send the message, so broadcasting using PostMessage() 
would be preferable.

Any suggestions as to why my approach is not working would be appreciated.

Thanks -- Steve 
Re: PostThreadMessage() vs. PostMessage(HWND_BROADCAST, ...)
#105
Author: Remy Lebeau (TeamB)
Date: Fri, 27 Jun 2008 05:33
24 lines
1720 bytes
"salamander1965" <salamander1@verizon.net> wrote in message 
news:48641223@newsgroups.borland.com...

> Should a message queue that receives a message from PostThreadMessage()
> also receive one from PostMessage() with HWND_BROADCAST as
> the first parameter (assuming the queue belongs to a primary thread of a 
> process)?

Only if the thread hosts a top-level window.  Otherwise, no.

> The message loop never sees the registered message, though.

Because it has no window to receive broadcasts with.

> The problem is I cannot guarantee that the main app will always have the 
> threadID it needs to send the message, so broadcasting using PostMessage() 
> would be preferable.

Then you have to give the background app a window.  You can't receive 
broadcasts without one.


Gambit 
Re: PostThreadMessage() vs. PostMessage(HWND_BROADCAST, ...)
#110
Author: salamander1965
Date: Fri, 27 Jun 2008 16:12
6 lines
1267 bytes
Thanks for the clearing up the difference between the two Post...() 
functions.  I don't mind being wrong and having to do things 
differently, but it's good to know *why* what I'm doing is wrong so I 
don't do it again.

-- Steve
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads