Listing+other+windows+and+applications

//By Michael Hicks, June 2007, amended by Richard Russell, March 2011//

The Microsoft Windows API call 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, get their captions or text , find those which are visible , and where possible the file which opened them. 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:
code format="bb4w" 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 code


 * Note:** Owing to the use of 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 [|Microsoft support article] for further information.

This problem can be overcome by using the code below:

Windows 2000 or later:
code format="bb4w" 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 code