REBOL [ title: "EnumWindows" author: "Brett Handley" purpose: "Experiment with callback in REBOL." date: 17-mar-2005 comment: { Uses callback feature of which only 16 callbacks can be created within REBOL. - See http://www.rebol.net/article/0141.html EnumWindows is more reliable than calling GetWindow in a loop. This script does not check for dll errors. Use at your own risk. } ] use [user32-dll result error win32-true-value win32-false-value] [ win32-true-value: 1 win32-false-value: 0 attempt [ ; Load library user32-dll: load/library %user32.dll win32-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.} ] user32-dll "GetWindowTextA" window-text?: func [ hWnd [integer!] "Handle to the window." /length max-length [integer!] "Maximum length of text to return." /local buffer ][ max-length: abs any [max-length 200] buffer: head insert/dup make string! max-length to char! 0 max-length win32-GetWindowText hWnd buffer max-length buffer ] win32-EnumWindows: make routine! [ "Enumerates all top-level windows on the screen." lpEnumFunc [callback! [int int return: [int]]] "[in] Pointer to an application-defined callback function." lParam [integer!] "[in] Specifies an application-defined value to be passed to the callback function." return: [integer!] "If the function succeeds, the return value is nonzero otherwise zero." ] user32-dll "EnumWindows" hwnd-list: copy [] SampleEnumWindowsProc: func [ {Application-defined callback function used with the EnumWindows or EnumDesktopWindows.} hWnd {[in] Handle to a top-level window.} lParam {[in] Specifies the application-defined value given in EnumWindows or EnumDesktopWindows.} ] [ append hwnd-list hwnd win32-true-value ] result: win32-EnumWindows :SampleEnumWindowsProc 0 print ["win32-EnumWindows returned with result" mold result] foreach hwnd hwnd-list [ print [head insert/dup copy {} "-" 40 newline to-hex hwnd "|" window-text? hwnd] ] ] attempt [ ; Free library if user32-dll [free user32-dll] print "Library freed." ] ]