enumerator.cls

来自「Windows超级黑客得到windows运行程序的信息,很经典的」· CLS 代码 · 共 955 行 · 第 1/3 页

CLS
955
字号
' Returns an enumeration of threads
'
Public Sub ReturnThreadEnum(ByRef Threads() As Long)

If (EnumsReady = False Or NewEnum = 1) And DoingSomething = False Then EnumAll

Dim i As Long
ReDim Threads(LBound(ThreadsEnum) To UBound(ThreadsEnum)) As Long

For i = LBound(ThreadsEnum) To UBound(ThreadsEnum)
  Threads(i) = ThreadsEnum(i).ID
Next i

End Sub

'
' Returns an enumeration of windows
'
Public Sub ReturnWindEnum(ByRef Winds() As Long)
'
' also remember to put the desktop window inside the enumeration
'

If (EnumsReady = False Or NewEnum = 1) And DoingSomething = False Then EnumAll

Dim i As Long
ReDim Winds(LBound(WindsEnum) To UBound(WindsEnum)) As Long

For i = LBound(WindsEnum) To UBound(WindsEnum)
  Winds(i) = WindsEnum(i)
Next i

End Sub

'
' Enumerates everything in a treeview control, according to the style)
'
Public Sub EnumInTreeView(ByRef TreeView As TreeView, Optional PicList As ImageList, Optional DesktopPic As String = "", Optional ProcessPic As String = "", Optional ThreadPic As String = "", Optional WindPic As String = "", Optional ByVal Style As DispEnum = 0)

Dim Mouser As Hourglass
Set Mouser = New Hourglass

If (EnumsReady = False Or NewEnum = 1) And DoingSomething = False Then EnumAll
TreeView.Visible = False

Select Case Style
  Case Is = DispEnum.Full
    TreeFull TreeView, PicList, DesktopPic, ProcessPic, ThreadPic, WindPic
End Select

TreeView.Visible = True

End Sub

'
' Returns if a window is valid or not
'
Public Function IsValidWindow(ByVal hWnd As Long) As Boolean
IsValidWindow = IsWindow(hWnd)
End Function

'
' Destroys all enumerations
'
Public Sub DestroyEnums()

mvarEnumsReady = False
ReDim ProcessEnum(0 To 0) As Long
ReDim ThreadsEnum(0 To 0) As ThreadIt
ReDim WindsEnum(0 To 0) As Long

End Sub

'
' Returns whether or not the enumerations are ready
'
Public Property Get EnumsReady() As Boolean
EnumsReady = mvarEnumsReady
End Property

'
' Enumerates everything
'
Public Sub EnumAll()

Dim Mouser As Hourglass
Set Mouser = New Hourglass

' Dimensioning
Dim i As Long
Dim k As Long
Dim Temp As Long

' First, we destroy the enumerations.
DestroyEnums

'
' Now to enumerate the windows.
'
' Send it off to the module, as I can't use the addressof operator insed a class module.

ReDim WindsEnum(0 To 0) As Long
modConst.EnumAllWinds WindsEnum

'
' The windows are now enumerated. Now to enumerate the processes.
'

Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
ReDim ProcessEnum(0 To 0) As Long

uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)

Do While rProcessFound
  ReDim Preserve ProcessEnum(0 To UBound(ProcessEnum) + 1) As Long
  ProcessEnum(UBound(ProcessEnum)) = uProcess.th32ProcessID
  rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop

Call CloseHandle(hSnapshot)

'
' The processes are now also enumerated.
'

'
' Thankyou to Konstantin Tretyakov (kt_ee@yahoo.com) for a lot of the code that went
' into all of these enumerations!!!
'

'  /--- Code used before I found enumeration APIs
' \/
'
'Dim Found As Boolean
'Dim CurrentThread As Long
'
'ReDim ThreadsEnum(0 To 0) As ThreadIt
'
'For i = 1 To UBound(WindsEnum)
'  Found = False
'  CurrentThread = WindThread(WindsEnum(i))
'  For k = 1 To UBound(ThreadsEnum)
'    If CurrentThread = ThreadsEnum(k).ID Then Found = True: Exit For
'  Next k
'  If Found = False Then
'    ReDim Preserve ThreadsEnum(0 To UBound(ThreadsEnum) + 1) As ThreadIt
'    ThreadsEnum(UBound(ThreadsEnum)).ID = CurrentThread
'    ThreadsEnum(UBound(ThreadsEnum)).Process = WindProcess(WindsEnum(i))
'  End If
'Next i

Dim lSnapShot As Long
Dim CanEnum As Long
Dim uThread  As THREADENTRY32
Dim ItemToAdd As String
ReDim ThreadsEnum(0 To 0) As ThreadIt

lSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPTHREAD, 0&)

If lSnapShot <> 0 Then
  uThread.lSize = Len(uThread)
  CanEnum = Thread32First(lSnapShot, uThread)
  Do While CanEnum
    ReDim Preserve ThreadsEnum(0 To UBound(ThreadsEnum) + 1) As ThreadIt
    ThreadsEnum(UBound(ThreadsEnum)).ID = uThread.lThreadID
    ThreadsEnum(UBound(ThreadsEnum)).Process = uThread.lOwnerProcessID
    CanEnum = Thread32Next(lSnapShot, uThread)
  Loop
  CloseHandle (lSnapShot)
End If

'
' Good Lord! I thought I'd never get through all of that! well, I did =>
'

mvarEnumsReady = True

End Sub

'
' Kills a task by it's process Id
'
Public Function KillProcess(ByVal ProcessID As Long) As Boolean

Const PROCESS_ALL_ACCESS = &H0
Dim exitCode As Long
Dim myProcess As Long

' Get the process
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)
If IsNull(myProcess) = True Or myProcess = 0 Then KillProcess = False: GoTo closit

' Get the Exit Code
If GetExitCodeProcess(ProcessID, exitCode) = False Then
  If GetLastError = True Then
    KillProcess = False
    GoTo closit
  End If
End If

' Kill the process
KillProcess = TerminateProcess(myProcess, exitCode)

closit:

' ... and close the handle
Call CloseHandle(myProcess)

End Function

Public Function NumOfWinds() As Long
NumOfWinds = UBound(WindsEnum)
End Function

Public Function NumOfProcesses() As Long
NumOfProcesses = UBound(ProcessEnum)
End Function

Public Function NumOfThreads() As Long
NumOfThreads = UBound(ThreadsEnum)
End Function

Public Function FixApi(ByVal APIString As String) As String
If InStr(APIString, Chr(0)) <> 0 Then FixApi = Left$(APIString, InStr(APIString, Chr(0)) - 1) Else FixApi = APIString
End Function

Private Sub TreeFull(ByRef Tree As TreeView, Optional PicList As ImageList, Optional DesktopPic As String, Optional ProcessPic As String, Optional ThreadPic As String, Optional WindPic As String)

'
' Whack all of the data into the treeview control, with nice formatting and junk.
'

Dim NodesWork As Node
Dim i As Long

On Error GoTo errhandler

' First of all, lets get rid of all of the nodes that are there already.
Tree.Nodes.Clear
' And do all of the property setting business...
Tree.ImageList = PicList

' Next, we place the desktop on the topmost node.
Set NodesWork = Tree.Nodes.Add(, , "_" & Format8(DesktopWind), Format8(DesktopWind) & " " & ClassName(DesktopWind) & " (Desktop Window) " & GetWindText(DesktopWind), DesktopPic)
NodesWork.Tag = DesktopWind

' Now go through and place in all of the processes
For i = 1 To UBound(ProcessEnum)
  Set NodesWork = Tree.Nodes.Add("_" & Format8(DesktopWind), tvwChild, "_" & Format8(ProcessEnum(i)), Format8(ProcessEnum(i)) & " """ & ProcessName(ProcessEnum(i)) & """", ProcessPic)
  NodesWork.Tag = ProcessEnum(i)
Next i

' Next goes in the threads...
For i = 1 To UBound(ThreadsEnum)
  Set NodesWork = Tree.Nodes.Add("_" & Format8(ThreadsEnum(i).Process), tvwChild, "_" & Format8(ThreadsEnum(i).ID), Format8(ThreadsEnum(i).ID) & IIf(ThreadHung(ThreadsEnum(i).ID), "  This thread is not responding.", ""), ThreadPic)
  NodesWork.Tag = ThreadsEnum(i).ID
Next i

' And guess what? The next item to do is the windows =P
' First of all, though, create a new array with a data type that stores
' all sorts of info; i.e., text, id, classname, and the would-be key of the parent node.
' Also check for a window being a child of the desktop. In such a case,
' put it in under it's thread, and add some text to the string to display.

Dim Winds2() As Winded
Dim k As Long
ReDim Winds2(0 To 0) As Winded
k = 0

For i = 1 To UBound(WindsEnum)
  If WindsEnum(i) <> DesktopWind Then
    ReDim Preserve Winds2(0 To UBound(Winds2) + 1) As Winded
    k = k + 1
    Winds2(k).hWnd = WindsEnum(k)
    Winds2(k).ClassName = ClassName(Winds2(k).hWnd)
    Winds2(k).Parent = ParentWind(Winds2(k).hWnd)
    Winds2(k).Text = GetWindText(Winds2(k).hWnd)
    Winds2(k).Thread = WindThread(Winds2(k).hWnd)
    Winds2(k).Process = WindProcess(Winds2(k).hWnd)
    Winds2(k).UseMe = True
    If Winds2(k).Parent = 0 Then
      Winds2(k).StringParentUse = "_" & Format8(Winds2(k).Thread)
    ElseIf Winds2(k).Parent = DesktopWind Then
      Winds2(k).StringParentUse = "_" & Format8(Winds2(k).Thread)
    Else
      Winds2(k).StringParentUse = "_" & Format8(Winds2(k).Parent)
    End If
    If Winds2(k).Parent = DesktopWind Then
      With Winds2(k)
        .DisplayText = Format8(Winds2(k).hWnd) & " " & .ClassName & " (This is a child of the desktop window) """ & .Text & """"
      End With
    Else
      With Winds2(k)
        .DisplayText = Format8(Winds2(k).hWnd) & " " & .ClassName & " """ & .Text & """"
      End With
    End If
  End If
Next i

Dim Changed As Boolean

Doadds:
Do
  Changed = False
  For i = 1 To UBound(Winds2)
    If Winds2(i).UseMe = True And ParentIsIn(Tree.Nodes, Winds2(i).StringParentUse) = True Then
      Set NodesWork = Tree.Nodes.Add(Winds2(i).StringParentUse, tvwChild, "_" & Format8(Winds2(i).hWnd), Winds2(i).DisplayText, WindPic)
      NodesWork.Tag = Winds2(i).hWnd
      Winds2(i).UseMe = False
      Changed = True
    End If
  Next i
Loop While Changed = True

' Now go through and look for broken windows.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?