Advertisement

Reading win32 message loop from another thread

Started by January 31, 2000 08:15 AM
4 comments, last by Starfall 24 years, 10 months ago
Does anyone know if it is possible to read Windows messages being passed to a certain thread from another thread? I''m currently attempting to run two threads - the first initialises the application window and does most things, while the second I need to be continuously polling the first''s message loop. I''ve found no way to do it so far. Using PeekMessage and GetMessage and passing either the main window''s HWND or passing NULL instead, I get no messages. From what I can garner from the help file, this is due to the fact that a given thread can''t read another''s messages. But is there a way? Otherwise I may have to swap my threads around, which would be a hassle I''d prefer to avoid if at all possible. Any suggestions gratefully appreciated. Thanks! Starfall
It sure is.

Take a look at SetWindowLong(). You can use to to replace the WndProc() of any hWnd. The function replaces the old one, so I would do:

WNDPROC pOldProc = NULL;  // global variableWinMain() // of your program{  ...  pOldProc = SetWindowLong(target_hWnd, GWL_WNDPROC, myProc);  ...}// target_hWnd is the other program''s hWnd, you can find// this a variety of ways// callback describing your replacement myProcLRESULT CALLBACK myProc(  HWND hwnd,      // handle to window  UINT uMsg,      // message identifier  WPARAM wParam,  // first message parameter  LPARAM lParam   // second message parameter){  // parse the uMsg, and/or call the original wndproc  // to keep the original program running  if (uMsg == WM_QUIT) // or whatever it is to qui  {    // replace the old one so the program can close    // without any bombing out of memory    SetWindowLong(hWnd, GWL_WNDPROC, pOldProc)    return 0;  }    if (pOldProc != NULL)  {    return pOldProc(hwnd, uMsg, wParam, lParam);  }  else    return 0;}
Advertisement
Just a quick question. What would that benifit?
William Reiach - Human Extrodinaire

Marlene and Me


Thanks for the suggestion, but as Gromit points out, this doesn''t actually help me. Since the message loop is still being read in the first thread, and the messages being dispatched to the second, I don''t get to read the message loop any more often. In case I didn''t make it clear, the point of running the message loop in the second thread is so that I can poll it more frequently. Unless there was a way to make incoming messages call the WindowProc directly (is there?), being able to change it isn''t quite what I need. I guess I will just have to switch my threads over unless there is another way to do it...

Thanks

Starfall
Yo,

Take a look at the SetWindowsHookEx, I think that''s what you want. It can take the messages before them go to the destination window procedure, and you can even modify them.

Hope this helps,
Nicodemus.
Nicodemus.----"When everything goes well, something will go wrong." - Murphy
Thanks Nicodemus. I gave this a go but ran into some other problems along the way... so I decided the simplest thing to do might be to switch threads after all. It was actually a lot less hassle than I thought it might be.

Regards

Starfall

This topic is closed to new replies.

Advertisement