Create a simple Windows inspector

It takes only a handful of lines of code to create Windows inspector program, that is, an utility that lets you display the handle, the class name and the contents (Text or Caption) of the window under the mouse cursor.

To create such a program, create a small form, add a Label1 control and a Timer1 control, set the Timer’s Interval property to a small value, say 100 milliseconds, and then add this code to the form’s code module:

Private Type POINTAPI    x As Long    y As LongEnd TypePrivate Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPrivate Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, _    ByVal yPoint As Long) As LongPrivate Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As LongPrivate Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal _    hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As LongPrivate Sub Timer1_Timer()    Dim lpPoint As POINTAPI    Dim wndHandle As Long    Dim wndCaption As String    Dim wndClass As String    Dim length As Long        ' get the position of the mouse cursor    GetCursorPos lpPoint    ' get the handle of the window under the mouse cursor    wndHandle = WindowFromPoint(lpPoint.x, lpPoint.y)    ' get the caption/text of the window    wndCaption = Space$(256)    length = GetWindowText(wndHandle, wndCaption, Len(wndCaption))    wndCaption = Left$(wndCaption, length)    ' get the window's class    wndClass = Space$(256)    length = GetClassName(wndHandle, wndClass, Len(wndClass))    wndClass = Left$(wndClass, length)        ' display the result in the label    Label1 = "[" & Hex$(wndHandle) & "] " & wndClass & IIf(Len(wndCaption), _        " - """ & wndCaption & """", "")End Sub

Now run the program and pass the mouse cursor over the window you want to retrieve information about.

Note that you can use this technique to peek at password-protected TextBox controls, at least those displayed by VB applications. This way to “steal” passwords works in Windows 95, 98 and NT. Fortunately, this security issue that has been fixed in Windows 2000.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: