Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Mar 4, 2000

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 Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, _
    ByVal yPoint As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal _
    hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private 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.

Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date