遍历exe文件进程
0 Reply , Posted in 程序代码 on 2017 28, 2017
////////////////////////////////////form 中添加一个list /////////
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Const GW_HWNDNEXT = 2
'***************************************************************
Private
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA"
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As
Long
Const MAXLEN = 255
'*****************************************************************************
Private
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal
hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As
Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
'***************************************************************
Dim aa$, pidhwnd&, jj%
Dim objWMIService, objProcess, colProcess
Dim strComputer, strList
Private Sub Form_Load()
List1.Move 0, 0, 10000, 4000
Me.Width = List1.Width + 120: Me.Height = List1.Height + 405
Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
Call showalljc
End Sub
Private Sub showalljc()
strComputer = "."
Set
objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process")
List1.Clear
For Each objProcess In colProcess
strList = objProcess.Name
pidhwnd = InstanceToWnd(objProcess.ProcessID)
aa = IIf(getclassnm(pidhwnd) <> "", "类名:" & getclassnm(pidhwnd), "")
List1.AddItem strList & Space(2) & objProcess.ProcessID &
Space(2) & Trim(Str(pidhwnd)) & Space(2) &
objProcess.ExecutablePath & Space(2) &
GetCaptionFromHwnd(pidhwnd) & Space(2) & aa
Next
Set objWMIService = Nothing
Set colProcess = Nothing
End Sub
Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd&, test_pid&, test_thread_id&
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
If GetParent(test_hwnd) = 0 Then
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Private Function GetCaptionFromHwnd(hwnd As Long) As String
'Check1 Option1 Combo1 Text1 Command1 Drive1 Data1 这些控件可以被检测到标题
Dim strBuffer$, intCount%
strBuffer = String$(MAXLEN - 1, 0)
intCount = GetWindowText(hwnd, strBuffer, MAXLEN)
If intCount > 0 Then
jj = InStr(strBuffer, Chr(0))
GetCaptionFromHwnd = Mid(strBuffer, 1, jj - 1)
End If
End Function
Function getclassnm(WinWnd As Long) As String
Dim Ret$, RetVal&, lpClassName$
'ShowWindow WinWnd, SW_SHOWNORMAL
lpClassName = Space(256)
RetVal = GetClassName(WinWnd, lpClassName, 256)
'MsgBox "Class名称" & Left$(lpClassName, RetVal)
getclassnm = Left$(lpClassName, RetVal)
End Function