Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Enumerate Top Windows

 [1/1] from: ptretter::charter::net at: 10-Jun-2003 10:44


Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable Clear DayThis is a script I wrote with some useful Windows handling routines. This requires /PRO /Command or /SDK for the library component and a win32 platform. The script prints out Top window handles and ANY window that is hung or not responding. However, their seems to be a glitch with the SendMessageTimeout function as it seems to interrupt the gathering of the other windows - not sure if that is a bug or not. If the code looks crappy its because I did a cut and paste and lost some of the formatting. --- snip ------ REBOL [ Title: "TOPWINDOW lister" Author: "Paul Tretter" Date: 10-June-2003 ] viswindows: [] ; container for Visible Titled windows notviswindows: [] ; container for Invisible Titled windows viswindowsnt: [] ; container for Visible NON-Titled windows plainparents: [] ; container for Non Visible Non Titled windows hungwindows: [] ; container for hung windows including child windows fetch: 0 WM_NULL: to-integer #{0000} WM_DESTROY: to-integer #{0002} WM_CLOSE: to-integer #{0010} GW_CHILD: 5 GW_HWNDNEXT: 2 GW_OWNER: 4 SMTO_ABORTIFHUNG: to-integer #{0002} SMTO_BLOCK: to-integer #{0001} SMTO_NORMAL: to-integer #{0000} SMTO_NOTIMEOUTIFNOTHUNG: to-integer #{0008} winlib: load/library %user32.dll sendmessagetimeout: make routine! [ "Sends the specified message to one of more windows." hWnd [integer!] "[in] Handle to the window whose window procedure will receive the message. " Msg [integer!] "[in] Specifies the message to be sent." wParam [integer!] "[in] Specifies additional message-specific information." lParam [integer!] "[in] Specifies additional message-specific information." fuFlags [integer!] "[in] Specifies how to send the message. " uTimeout [integer!] "[in] Specifies the duration, in milliseconds, of the time-out period." lpdwResult [integer!] "[in] Receives the result of the message processing. " return: [integer!] "If the function succeeds, the return value is nonzero otherwise zero." ] winlib "SendMessageTimeoutA" iswindowvisible: make routine! [ "The IsWindowVisible function retrieves the visibility state of the specified window." hwnd [integer!] "[in] Handle to the window to test" return: [integer!] {return value will <> 0 if its parent window, its parent's parent windows, anad so forth have the WS_VISIBLE sytle. Otherwise its 0} ] winlib "IsWindowVisible" getlasterror: make routine! [return: [long]] winlib2: load/library %kernel32.dll "GetLastError" getwindowtext: make routine! [ {The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer.} hWnd [integer!] "[in] Handle to the window or control containing the text." lpString [string!] "[out] Pointer to the buffer that will receive the text." nMaxCount [integer!] "[in] Specifies the maximum number of characters to copy to the buffer, including the NULL character." return: [integer!] {If the function succeeds, the return value is the length, in characters, of the copied string, not including the terminating NULL character.} ] winlib "GetWindowTextA" str: make string! "" ; needed to hold windows title and passed to the getwindowtext routine. loop 200 [append str "^@"] iswindow: make routine! [ "The IsWindow function determines whether the specified window handle identifies an existing window." hWnd [integer!] "[in] Handle to the window to test." return: [integer!] "If the window handle identifies an existing window, the return value is nonzero otherwise zero." ] winlib "IsWindow" sendmessage: make routine! [ "The SendMessage function sends the specified message to a window or windows." hWnd [integer!] "[in] Handle to the window whose window procedure will receive the message." Msg [integer!] "[in] Specifies the message to be sent." wParam [integer!] "[in] Specifies additional message-specific information." lParam [integer!] "[in] Specifies additional message-specific information." return: [integer!] "The return value specifies the result of the message processing; it depends on the message sent." ] winlib "SendMessageA" getparent: make routine! [ "The GetParent function retrieves a handle to the specified window's parent or owner." hWnd [integer!] "[in] Handle to the window whose parent window handle is to be retrieved." return: [integer!] {If the window is a child window, the return value is a handle to the parent window. If the window is a top-level window, the return value is a handle to the owner window. If the window is a top-level unowned window or if the function fails, the return value is NULL.} ] winlib "GetParent" pid: make struct! [proccessid [integer!]] none ; struct needed for GetWindowThreadProcessId getwindowthreadprocessid: make routine! [ {The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window.} hWnd [integer!] "[in] Handle to the window." lPdwProcessId [struct! [processid [integer!]]] "[out] Pointer to a variable that receives the process identifier." return: [integer!] "The return value is the identifier of the thread that created the window." ] winlib "GetWindowThreadProcessId" destroywindow: make routine! [ "The DestroyWindow function destroys the specified window." hWnd [integer!] "[in] Handle to the window to be destroyed." return: [integer!] "If the function succeeds, the return value is nonzero.otherwise its zero" ] winlib "DestroyWindow" getdesktopwindow: make routine! [ "The GetDesktopWindow function returns a handle to the desktop window." return: [integer!] "The return value is a handle to the desktop window" ] winlib "GetDesktopWindow" getwindow: make routine! [ {The GetWindow function retrieves a handle to a window that has the specified relationship (Z-Order or owner) to the specified window.} hwnd [integer!] "[in] Handle to a window." typ [integer!] "[in] Specifies the relationship between the specified window and the window whose handle is to be retrieved." return: [integer!] {If the function succeeds, the return value is a window handle. If no window exists with the specified relationship to the specified window, the return value is NULL.} ] winlib "GetWindow" ret: getdesktopwindow ; Retrieves the handle to the top most window in the Z-Order which is the Desktop window getwindow-ret: getwindow ret GW_CHILD ; returns the first child windows of the topmost window - the desktop. loop 1000 [ getwindow-next-ret: getwindow getwindow-ret gw_hwndnext sendmessagetimeout-ret: sendmessageTimeout getwindow-next-ret WM_NULL 0 0 SMTO_ABORTIFHUNG and SMTO_BLOCK 1000 fetch getparent-ret: getparent getwindow-next-ret getlasterror-ret: getlasterror getwindow-ret: getwindow-next-ret getwindowthreadprocessid getwindow-next-ret pid iswindowvisible-ret: iswindowvisible getwindow-next-ret getwindowtext-ret: getwindowtext getwindow-next-ret str 200 if all [getparent-ret = 0 getlasterror-ret = 2 iswindowvisible-ret > 0 getwindowtext-ret > 0][append viswindows rejoin ["Window: " getwindow-next-ret " with Proccess-ID of: " pid/proccessid with Title: trim/with str "@" newline ]] if all [getparent-ret = 0 getlasterror-ret = 2 iswindowvisible-ret > 0 getwindowtext-ret = 0][append viswindowsnt rejoin ["Window: " getwindow-next-ret " with Process-ID of: " pid/proccessid newline]] if all [getparent-ret = 0 getlasterror-ret = 2 iswindowvisible-ret = 0 getwindowtext-ret > 0][append notviswindows rejoin ["Window: " getwindow-next-ret " with Process-ID of: " pid/proccessid " with Title: " trim/with str "@" newline ]] if all [getparent-ret = 0 getlasterror-ret = 2 iswindowvisible-ret = 0 getwindowtext-ret = 0][append plainparents rejoin ["Window: " getwindow-next-ret " with Process-ID of: " pid/proccessid newline]] if all [sendmessagetimeout-ret = 0 getwindow-next-ret > 0][append hungwindows rejoin ["Window: " getwindow-next-ret " is hung! with Process-ID of: " pid/proccessid newline]] str: make string! "" loop 200 [append str "^@"] getlasterror-ret: 0 if getwindow-next-ret = 0 [break] ] print "*************************************************" print "********** TOPWINDOWS BY PAUL TRETTER ***********" print "*************************************************" print newline print ["DESKTOP WINDOW HANDLE = " ret newline] print [" ******* TOPLEVEL WINDOWS *******" newline] if not empty? viswindows [ print ["VISIBLE TITLED WINDOWS:" newline viswindows] ] if not empty? viswindowsnt[ print ["VISBILE UNTITLED WINDOWS:" newline viswindowsnt] ] if not empty? notviswindows [ print ["NOT VISIBLE TITLED WINDOWS:" newline notviswindows] ] if not empty? plainparents [ print ["NOT VISIBLE UNTITLED WINDOWS:" newline plainparents] ] either not empty? hungwindows [ print ["HUNG WINDOWS: " newline hungwindows] ][ print ["HUNG WINDOWS: " newline "NO HUNG WINDOWS DETECTED!" newline] ] free winlib free winlib2 halt --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.483 / Virus Database: 279 - Release Date: 5/19/2003 -- Binary/unsupported file stripped by Listar -- -- Type: image/jpeg -- File: Clear Day Bkgrd.JPG