3 // NT 4 doesn't have FlashWindowEx.
4 typedef BOOL (WINAPI
*t_FlashWindowEx
)(FLASHWINFO
*);
5 t_FlashWindowEx p_FlashWindowEx
;
6 #define FlashWindowEx p_FlashWindowEx
9 #include "detours.h" // see http://research.microsoft.com/sn/detours/
11 DETOUR_TRAMPOLINE(BOOL WINAPI
Real_SetForegroundWindow(HWND hWnd
), SetForegroundWindow
);
14 // IAT patching hook method. See http://www.naughter.com/hookimportfunction.html
15 // compile with cl /LD focuskiller.cpp HookImportFunction.cpp user32.lib
16 #include "HookImportFunction.h"
18 typedef BOOL (WINAPI
*t_SetForegroundWindow
)(HWND
);
19 t_SetForegroundWindow Real_SetForegroundWindow
;
24 BOOL WINAPI
Mine_SetForegroundWindow(HWND hWnd
)
27 HWND fg
= GetForegroundWindow();
28 HWND owner
= GetWindow(hWnd
, GW_OWNER
);
29 GetWindowThreadProcessId(fg
, &pid
);
33 wsprintf(buf
, "SetForegroundWindow(%x): owner = %x, %d <-> %d", hWnd
, owner
, pid
, mypid
);
34 OutputDebugString(buf
);
38 // a) another process' window is in the foreground
39 // b) the window to be put in front is a top-level window (should avoid putting one IDEA project in front of another one)
40 if (mypid
!= pid
|| owner
== NULL
) {
41 if (FlashWindowEx
!= NULL
) {
43 fw
.cbSize
= sizeof(fw
);
48 fw
.dwFlags
= FLASHW_TRAY
| FLASHW_TIMERNOFG
;
51 FlashWindow(hWnd
, TRUE
);
53 return TRUE
; // fake success
56 return Real_SetForegroundWindow(hWnd
);
59 void HookFunctions(HMODULE hModule
)
63 OutputDebugString("Using Detours hook...");
66 DetourFunctionWithTrampoline((PBYTE
)Real_SetForegroundWindow
,
67 (PBYTE
)Mine_SetForegroundWindow
);
70 OutputDebugString("Using IAT patching hook...");
74 hook
.szFunc
= "SetForegroundWindow";
75 hook
.pProc
= (PROC
)Mine_SetForegroundWindow
;
77 // hooking LoadLibrary and waiting until awt.dll is being loaded by java would be more correct but this works too
78 HMODULE awtModule
= LoadLibrary("awt.dll");
80 BOOL b
= HookImportFunctionsByName(awtModule
, "user32.dll", 1, &hook
, (PROC
*)&Real_SetForegroundWindow
, NULL
);
83 wsprintf(buf
, "Hooking SetForegroundWindow failed [0x%x]", GetLastError());
84 OutputDebugString(buf
);
89 OutputDebugString("Functions hooked");
93 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpReserved
)
95 if (fdwReason
== DLL_PROCESS_ATTACH
) {
98 wsprintf(buf
, "DLL Attached");
99 OutputDebugString(buf
);
102 mypid
= GetCurrentProcessId();
103 p_FlashWindowEx
= (t_FlashWindowEx
)GetProcAddress(GetModuleHandle("user32.dll"), "FlashWindowEx");
105 DisableThreadLibraryCalls((HMODULE
)hinstDLL
);
107 HookFunctions((HMODULE
)hinstDLL
);