-2

My app1 PostMessage WM_LBUTTONDOWN and WM_LBUTTONUP to app2 (third-party) which is in different process.

How to make sure those message been handled by app2 in app1, the logic in app1 depends on the result of those messages after PostMessage.

Here's the pseudo code for app1

PostMessage(app2Handle, WM_LBUTTON_DOWN, 0, lParam);
PostMessage(app2Handle, WM_LBUTTON_UP, 0, lParam);
// How to ensure above messages has been handled by app2 here?
user1633272
  • 2,007
  • 5
  • 25
  • 48
  • Get the other app to notify you when it has processed the messages. Why aren't you using automation anyway? – David Heffernan Oct 18 '16 at 06:17
  • The UI Automation is not very reliable for non-active window. i.e. clicking a TreeView item requires the windows is active. That's why I switched to win32 API. – user1633272 Oct 18 '16 at 06:20
  • 1
    That's complete nonsense. UI Automation **is** reliable, regardless of whether the target window is active or not. Anyway, read [Replaying input is not the same as reprocessing it](https://blogs.msdn.microsoft.com/oldnewthing/20121206-00/?p=5903) and [You can’t simulate keyboard input with PostMessage](https://blogs.msdn.microsoft.com/oldnewthing/20050530-11/?p=35513/) to understand, why this cannot work. – IInspectable Oct 18 '16 at 08:39
  • OK, let's focus on the question. BTW, I have tried UI Automation, it has some limitations for some operations like clicking TreeView item, manipulating DateTimePicker, etc... I was using library White, even button clicking requires the target widow being on top. Anyway those are different questions. – user1633272 Oct 18 '16 at 08:51
  • If you have the source code of both App1 and App2, an alternative implementation could be register a custom message and define a simple communication protocol, eg App2 should send an acknowledgement to App1 when the message is processed is done, or an error or "unable to process" code etc. – Constantine Georgiou Oct 18 '16 at 09:30
  • app2 is third-party application. So it's impossible to change its behavior. – user1633272 Oct 18 '16 at 09:41
  • OK, so let's focus on the question. You cannot tell when the recipient processes the messages that you post it. If you wanted to know that you would send them rather than posting them. Of course, everything that you are doing here is contrary to the system design. Perhaps if you simply must do it this way, and must use `PostMessage`, then the answer is simple. You can't find out the information you require, and so you can give up on this task as impossible. – David Heffernan Oct 18 '16 at 10:00
  • *"I was using library White, even button clicking requires the target widow being on top"* - That's highly unlikely. UI Automation does not require the UI to be visible, to interact with its accessible tree. And since [TestStack.White](https://github.com/TestStack/White) is based on UI Automation, those very same rules apply. But then again, a project where the master branch fails to compile is of dubious quality. Maybe try using [UI Automation](https://msdn.microsoft.com/en-us/library/windows/desktop/ee684009.aspx) without any code in between? – IInspectable Oct 18 '16 at 19:05

1 Answers1

1

PostMessage() works asynchronously. It simply puts the message into the target window's message queue and then exits immediately. There is no notification when the message is processed. If you need to know that, you could try using a message hook from SetWindowsHookEx() to monitor the activity of the target window's message queue and/or window procedure. Or, maybe you could use SetWinEventHook() to receive events like EVENT_OBJECT_INVOKED, EVENT_OBJECT_SELECTION..., etc if the mouse messages are meant to cause such click/selection actions in the window.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770