Find handle of previous active window (win32)

Posted on 2009-07-04
Last Modified: 2013-12-04
I'm trying to write a small win32 application (in C or C++)  that sends pre-cooked keystrokes to another application. The user experience should be this:

The user works with application X (which can be anything), then uses the mouse to click somewhere in the window belonging to application Y (written by me), which is visible on the screen. Application Y uses SendInput to send keystrokes to application X.

The problem is: SendInput sends keystrokes to the foreground window. How can application Y determine which window was the foreground window immediately before it received the mouse click?

Back in 1997, someone asked a similar question here (, and the answer was to look at the wparam of the WM_SETFOCUS message. However,  the answer was later withdrawn (and in Vista that parameter appears to always be 0). One of the last comments on the question from 1997 says: "It seems there is no way to obtain handle of deactivated window."

Is that really true?
Question by:cto
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

JohnGaby earned 250 total points
ID: 24776987
You might try creating a window that does not take the focus in the first place.  That way you wouldn't need to determine where the focus originated  Check out the WS_EX_NOACTIVATE style:
LVL 86

Assisted Solution

jkr earned 250 total points
ID: 24776997
If we believe MSDN, it should be WM_SETFOCUS (, where it says: "wParam - Handle to the window that has lost the keyboard focus. This parameter can be NULL."

However, if that does not work for you, you might want to consider setting a global hook (see - "Win32 Hooks") that filters for WM_KILLFOCUS and sends the handle of the window that message was targetted at to your app. E.g.
LRESULT CALLBACK HookProc	(	int		nCode,	// hook code
                                WPARAM	wParam,	// removal flag 
                                LPARAM  lParam 	// address of structure with message 
    PMSG	pmsg	=	( PMSG)	lParam;
    if	(	0	>	nCode	||	PM_NOREMOVE	==	wParam)	
            return	(	CallNextHookEx	(	g_hhk,
    if	(WM_KILLFOCUS == pmsg->message)
            /* this one is for us, so grab the window handle here */
    return	(	CallNextHookEx	(	g_hhk,

Open in new window


Author Comment

ID: 24777462
Thank you, JohnGaby and jkr. Both solutions are useful, and I have split the points between you.


Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question