From c274f24adf15e98a9fbf65571984e7b0c30c40da Mon Sep 17 00:00:00 2001 From: ashok Date: Sat, 20 Sep 2014 12:30:12 +0000 Subject: Make findwindow more robust by ensuring it is a window belonging to the same process. --- win/tkWinTest.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/win/tkWinTest.c b/win/tkWinTest.c index 2c38d71..8a92f5a 100644 --- a/win/tkWinTest.c +++ b/win/tkWinTest.c @@ -439,6 +439,7 @@ TestfindwindowObjCmd( Tcl_DString titleString, classString; HWND hwnd = NULL; int r = TCL_OK; + DWORD myPid; Tcl_DStringInit(&classString); Tcl_DStringInit(&titleString); @@ -454,7 +455,28 @@ TestfindwindowObjCmd( } if (title[0] == 0) title = NULL; +#if 0 hwnd = FindWindow(class, title); +#else + /* We want find a window the belongs to us and not some other process */ + hwnd = NULL; + myPid = GetCurrentProcessId(); + while (1) { + DWORD pid, tid; + hwnd = FindWindowEx(NULL, hwnd, class, title); + if (hwnd == NULL) + break; + tid = GetWindowThreadProcessId(hwnd, &pid); + if (tid == 0) { + /* Window has gone */ + hwnd = NULL; + break; + } + if (pid == myPid) + break; /* Found it */ + } + +#endif if (hwnd == NULL) { Tcl_SetObjResult(interp, Tcl_NewStringObj("failed to find window: ", -1)); -- cgit v0.12