Click here to Skip to main content
15,881,852 members
Articles / All Topics

How to Break into Debugger When Specific Windows Message is Sent or Posted

Rate me:
Please Sign up or sign in to vote.
4.64/5 (6 votes)
15 May 2015Apache 7.2K   6  
How to break into debugger when specific Windows message is sent or posted


We have recently been fighting a weird problem in our .NET application that was caused by a duplicate WM_KEYDOWN message. It is relatively easy to figure out who receives the message (Spy++ helps a lot), but how to find out who sends it?

If you know (or suspect) the process that sends your message, here’s how to debug it in Visual Studio 2013:

  1. Run the program you want to debug.
  2. Attach debugger, make sure native debugger is selected.
  3. Break into debugger, if it’s not there already.
  4. Open window Debug->Windows->Modules, load symbols for user32.dll
  5. Open BreakPoints window.
  6. Add new break point at each of the functions below. IMPORTANT: Set language to C++ (does not seem to work otherwise).
  7. Functions: _PostMessageA@16, _PostMessageW@16, _SendMessageA@16, SendMessageW@16.
  8. For each breakpoint, set the following condition: *(int*)(esp+8) == 0x100
  9. Resume the app and enjoy.

Explanation of the condition:

ESP is the stack pointer
*ESP is the return address
*(ESP+4) is the first parameter, hWnd
*(ESP+8) is the second parameter, uMsg

So, our condition basically amounts to uMsg == WM_KEYDOWN.
If you want to break on sending a WM_KEYDOWN message to a specific window handle, you can set the condition like so:

*(int*)(esp+4) == 0xmyhandlehere && *(int*)(esp+8) == 0x100
This article was originally posted at http://www.ikriv.com/blog?p=1631

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0


Written By
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.

Please visit my web site: www.ikriv.com.

Comments and Discussions

 
-- There are no messages in this forum --