Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear all,

I am still new to vb.net and want to get the handler and classname of window, textbox or command button when mouse is over there. I tried below code got the handler to textbox1 and nothing gets to textbox2 as classname. Below program suddenly stops working.
help me to get the class name using GetClassname API function or some other way

What I have tried:

Imports System.Runtime.InteropServices
Imports System.String
Imports System.Text



Public Class Form1

Public Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hwnd As Int32, ByVal lpClassName As String, ByVal nMaxCount As Int32) As Int32

Public Declare Function WindowFromPoint Lib "user32.dll" (ByVal xPoint As Int32, ByVal yPoint As Int32) As Int32

Public Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Int32) As Int32



Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

SetForegroundWindow(MyBase.Handle)
Dim ParentHandler As IntPtr = WindowFromPoint(MousePosition.X, MousePosition.Y)
TextBox1.Text = ParentHandler.ToString



Dim sClassName As New StringBuilder("", 256)
GetClassName(ParentHandler, sClassName.ToString, 256)
TextBox2.Text = sClassName.ToString()

End Sub
Posted
Updated 26-Jun-22 21:24pm

You are constructing a StringBuilder to hold the result, but then you don't use it to talk to GetClassName - you convert that to a string first and pass that:
VB
Dim sClassName As New StringBuilder("", 256)
GetClassName(ParentHandler, sClassName.ToString, 256)
Since you haven't added any data to the StringBuilder, the ToString call will return an empty string, so will have a length of zero - and since strings are immutable in .NET anyway GetClassName can't change it even if you did pass actual data ...
Pass the StringBuilder directly instead:
VB
Dim sClassName As New StringBuilder("", 256)
GetClassName(ParentHandler, sClassName, 256)
 
Share this answer
 
Comments
M.T.Danoj 24-May-19 2:45am    
Dear OriginalGriff and all

when I try below code, sClassname (within the the GetClassname API function) is underlined with red color gets error meesage
as "Value of type stringBuilder cannot be convert to string"

Help & Please, explain more


Dim sClassName As New StringBuilder("", 256)
GetClassName(ParentHandler, sClassName, 256)
OriginalGriff 24-May-19 2:59am    
What part do you not understand?
You can't pass a string to GetClassName, it expects a pointer to a string so it can fill it. That is a StringBuilder, because strings are immutable - they can't be changed once created.

Now, where do you think your error is?
OriginalGriff's first answer worked for me but I had to change my integer-type arguments as Integer.

Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Integer, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer) As Integer

Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer


Public Sub GetWinInfo(hParent As Integer)
        Dim hwnd As Integer
        Dim lngRet As Integer
        Dim strText As New System.Text.StringBuilder(Chr(0), 100)
        hwnd = FindWindowEx(hParent, 0&, vbNullString, vbNullString)
        While hwnd <> 0
            lngRet = GetClassName(hwnd, strText, 100)
            Debug.Print(strText.ToString)
            hwnd = FindWindowEx(hParent, hwnd, vbNullString, vbNullString)
        End While
End Sub
 
Share this answer
 
Comments
Richard Deeming 27-Jun-22 10:07am    
Completely irrelevant. Integer in VB.NET is an alias for the System.Int32 type.

Your change will make absolutely no difference - unless you have done something silly, like declaring your own Int32 class within your application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900