Enumerate Windows & ProcessIDs

M. Gallant 11/19/2001

The following java-moniker example shows how to retrieve an entire window enumeration based on the win32 API function EnumWindows(). The function returns a string containing the entire list of all top-level windows with the title text and the ProcessID of the launcher application. The helper java class contains one method,
obj.getAllTitleProcID()
where obj is an instance of the class WinTitleProcID. The simple vbs script below demonstrates instantiation and calling the Java moniker object and associated method. To use, ensure the two compiled Java class files are in your classpath (typically the current directory or C:\winnt\java\classes). The source file is shown below. It can be compiled with the MS jvc compiler as:
  jvc /x- WinTitleProcID.java
or the source file and 2 compiled classes are available as a zip file here.

This class can be used together with WMI Win32_Process class to distinguish scripts running under cscript or wscript by resolving the ProcessID returned with WinTitleProcID and that returned with the Win32_Process.ProcessID property. Note that WIn32_Process does not directly provide the associated window title. Note that the information returned by the WinTitleProcID Java class is a subset of that provided by the Win2000 and WinNT Support Tools native utility "Tlist.exe".

[See also a Signed Java Applet implementation]

wintitleproc.vbs

'**************************************************************************** ' File: wintitleproc.vbs (WSH for VBscript) ' Author: (c) M. Gallant 11/19/2001 ' ' ' Uses Java moniker class "WinTitleProcID" which must be in classpath. '***************************************************************************** Option Explicit Dim oJwintitleproc, windata set oJwintitleproc = GetObject("java:WinTitleProcID") 'Instantiate Java object via moniker. windata = oJwintitleproc.getAllTitleProcID() WScript.echo windata

Typical output of script


WinTitleProcID.java (Java class)

/* class WinTitleProcID helper java-moniker class for vbs, js, wsh scripts. Enumerates all top-level windows with win32 API, and returns window-text and ProcessID that generated window. Uses inner-class for J/Direct callback class/function. M. Gallant 11/19/2001 */ public class WinTitleProcID { private StringBuffer strb = new StringBuffer() ; class NewEnumProc extends com.ms.dll.Callback { // inner class callback public boolean callback(int hwnd, int lparam) { int [] processID = new int[1] ; StringBuffer text = new StringBuffer(50); GetWindowText(hwnd, text, text.capacity()+1); int threadprocessid = GetWindowThreadProcessId(hwnd, processID) ; if (text.length() != 0) { strb.append("Text: " + text + " ProcID: " + processID[0] + "\r\n") ; } return true; // return TRUE to continue enumeration. } } // end inner class public NewEnumProc gen() { // get reference to instance of callback class return new NewEnumProc() ; } public String getAllTitleProcID() { NewEnumProc mycallback = new NewEnumProc() ; boolean results = false; results = EnumWindows(mycallback, 1) ; if( !results) return " Problem enumerating windows!" ; else return strb.toString() ; } public static void main(String args[]) { // test method WinTitleProcID newenum = new WinTitleProcID() ; System.out.println(newenum.getAllTitleProcID()) ; } /** @dll.import("USER32") */ private static native int GetWindowText(int hwnd, StringBuffer text, int cch); /** @dll.import("USER32") */ private static native int GetWindowThreadProcessId(int hwnd, int[] lpdwProcessId) ; /** @dll.import("USER32") */ private static native boolean EnumWindows(NewEnumProc myproc, int param); }