Atlanta Custom Software Development 

   Search        Code/Page

User Login



Forgot the Password?
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
» Regular Expr Tester
» Free Tools

Create a simple Windows inspector
[ All Languages » VB »  Windows]

Total Hit ( 3627)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article



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:

Click here to copy the following block
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.

Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with, SQL Server and other MS technologies. He is, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )

Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.