=====Listing other windows and applications===== //By Michael Hicks, June 2007, amended by Richard Russell, March 2011//\\ \\ The Microsoft Windows API call "EnumWindows" enables a user to find all the windows which are open. This function requires the use of a "callback" function, and to use a callback function it is necessary deploy the "CALLBACK.BBC" library supplied with //BBC BASIC for Windows//.\\ \\ In this short program we find all the open windows ("EnumWindows"), get their captions or text ("GetWindowText"), find those which are visible ("IsWindowVisible"), and where possible the file which opened them ("GetWindowModuleFileName"). The details of visible windows are printed. If the test for visibility is removed, numerous hidden windows can be seen. ===== Windows 95 or 98 only: ===== INSTALL @lib$+"CALLBACK" index% = 1 lParam% = 0 DIM hWnd%(1000) SYS FN_syscalls("EnumWindows"), FN_callback(FNenumfunc(),2), lParam% ret% = FN_sysresult nMaxCount% = 255 DIM lpString% nMaxCount%, lpszFileName% nMaxCount% PRINT "List of visible windows with Process ID and module name" PRINT "=======================================================" ' FOR i% = 1 TO index% $$lpszFileName% = "" $$lpString% = "" SYS "GetWindowText", hWnd%(i%), lpString%, nMaxCount% SYS "IsWindowVisible", hWnd%(i%) TO vis% SYS "GetWindowModuleFileName", hWnd%(i%), lpszFileName%, nMaxCount% SYS "GetWindowThreadProcessId", hWnd%(i%), ^pid% TO tpid% IF vis% AND $$lpString% <> "" THEN COLOUR 0: PRINT "* " $$lpString% COLOUR 1: PRINT " [pid=" + STR$(pid%) + "] ", $$lpszFileName% ENDIF NEXT END DEF FNenumfunc(hwnd%, lparam%) hWnd%(index%) = hwnd% index% += 1 = 1 **Note:** Owing to the use of "GetWindowModuleFileName" this code requires Windows 98 or earlier to work fully. In later versions of Windows the reported filename may not be correct if the window is not owned by the calling process; see this [[http://support.microsoft.com/?id=228469|Microsoft support article]] for further information.\\ \\ This problem can be overcome by using the code below: ===== Windows 2000 or later: ===== INSTALL @lib$+"CALLBACK" SYS "LoadLibrary", "PSAPI.DLL" TO psapi% SYS "GetProcAddress", psapi%, "GetModuleFileNameExA" TO `GetModuleFileNameEx` Index% = 1 lParam% = 0 DIM hWnd%(1000) SYS FN_syscalls("EnumWindows"), FN_callback(FNenumfunc(),2), lParam% ret% = FN_sysresult nMaxCount% = 255 DIM lpWindowText% nMaxCount%, lpszFileName% nMaxCount% PRINT "List of visible windows with Process ID and image name" PRINT "======================================================" ' FOR i% = 1 TO Index%-1 SYS "GetWindowText", hWnd%(i%), lpWindowText%, nMaxCount% SYS "IsWindowVisible", hWnd%(i%) TO vis% IF vis% AND $$lpWindowText% <> "" THEN SYS "GetWindowThreadProcessId", hWnd%(i%), ^pid% SYS "OpenProcess", &410, 0, pid% TO hprocess% SYS `GetModuleFileNameEx`, hprocess%, 0, lpszFileName%, nMaxCount% SYS "CloseHandle", hprocess% COLOUR 0: PRINT "* " $$lpWindowText% COLOUR 1: PRINT " [pid=" + STR$(pid%) + "] ", $$lpszFileName% ENDIF NEXT COLOUR 0 END DEF FNenumfunc(hwnd%, lparam%) hWnd%(Index%) = hwnd% Index% += 1 = 1