FillWindowsTree – Fill a treeview with the windows hierarchy

FillWindowsTree – Fill a treeview with the windows hierarchy

Private 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 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal _    hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _    lParam As Any) As LongPrivate Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, _    ByVal wCmd As Long) As LongPrivate Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As LongPrivate Declare Function GetDesktopWindow Lib "user32" () As Long'API constantsConst WM_GETTEXT = &HDConst WM_GETTEXTLENGTH = &HEConst GWL_HINSTANCE = (-6)Const GWL_HWNDPARENT = (-8)Const GWL_STYLE = (-16)Const GWL_EXSTYLE = (-20)Const GW_CHILD = 5Const GW_HWNDFIRST = 0Const GW_HWNDLAST = 1Const GW_HWNDNEXT = 2Const GW_HWNDPREV = 3Const GW_MAX = 5Const GW_OWNER = 4' Fill a treeview with the complete windows hierarchy' The text of each node will be in the format: handle caption class_name' The tag of each node is the window's caption, useful if you want' to perform a simple search through the windows loaded'' The first parameter is the TreeView to fill, the second is the handle' of the window to start with (or zero for the desktop window)'' Example: fill the treeview control with ALL the windows'   starting with the desktop window'   FillWindowsTree TreeView1Function FillWindowsTree(tvw As TreeView, Optional ByVal hwnd As Long) As Long    Dim nNode As Node    Dim lChild As Long    Dim sText As String        On Error Resume Next    ' provide a default for hWnd argument    If hWnd = 0 Then         tvw.Nodes.Clear        hWnd = GetDesktopWindow()    End If        ' build the node's text: hwnd "caption" class    sText = CStr(hwnd) & " """ & GetWindowCaption(hwnd) & """ " & _        GetWindowClass(hwnd)    ' look if the treeview has a node for the parent window    Set nNode = tvw.Nodes("H" & GetParent(hwnd))    ' if not, add a root node    If nNode Is Nothing Then        Set nNode = tvw.Nodes.Add(, , "H" & CStr(hwnd), sText)    Else        ' if so, add this node as child of that node        Set nNode = tvw.Nodes.Add(nNode, tvwChild, "H" & CStr(hwnd), sText)    End If    nNode.Tag = GetWindowCaption(hwnd)        ' find the first child window    lChild = GetWindow(hwnd, GW_CHILD)    Do While lChild <> 0        ' call recursively this function        FillWindowsTree tvw, lChild        ' until there are no more child windows        lChild = GetWindow(lChild, GW_HWNDNEXT)    LoopEnd Function' return the caption of a windowFunction GetWindowCaption(ByVal hwnd As Long) As String    Dim length As Long    Dim text As String        length = SendMessage(hwnd, WM_GETTEXTLENGTH, 0, ByVal 0&)    text = Space$(length + 1)    SendMessage hwnd, WM_GETTEXT, length + 1, ByVal text    GetWindowCaption = Left$(text, length)End Function' return the class name of the specified windowFunction GetWindowClass(ByVal hwnd As Long) As String    Dim sClass As String    sClass = Space$(256)    GetClassName hwnd, sClass, 255    GetWindowClass = Left$(sClass, InStr(sClass, vbNullChar) - 1)End Function

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as