'大家试试下面的代码看看(为了测试方便请只开一个notepad.exe进程): Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As LongPrivate Const PROCESS_TERMINATE As Long = (&H1)
Private Const MAX_PATH As Integer = 260Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE As Long = &H8Private Type PROCESSENTRY32
dwSize As Long
cntusage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End TypePrivate Sub Command1_Click()
Dim ProNum As Long, PidData() As Long, hProcess As Long, i As Long
ProNum = FindPIDByName("notepad.exe", PidData()) '查找记事本的句柄,为了测试方便请只开一个notepad.exe进程
If ProNum = 0 Then Exit Sub
For i = 0 To ProNum - 1
hProcess = OpenProcess(PROCESS_TERMINATE, False, PidData(i)) '获取进程句柄
Print hProcess '打印句柄
Next
End SubPrivate Function FindPIDByName(ProName As String, PidData() As Long) As Long '返回符合条件的进程ID,保存到数组PidData里
Dim hProSnap As Long, hProID As Long, i As Long
Dim ProInfo As PROCESSENTRY32
ProName = LCase$(ProName)
ProInfo.dwSize = Len(ProInfo)
hProSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
hProID = Process32First(hProSnap, ProInfo)
While hProID <> 0
If LCase$(Left(ProInfo.szExeFile, InStr(1, ProInfo.szExeFile, vbNullChar) - 1)) = ProName Then
ReDim Preserve PidData(i)
PidData(i) = ProInfo.th32ProcessID
i = i + 1
End If
hProID = Process32Next(hProSnap, ProInfo)
Wend
FindPIDByName = i
CloseHandle hProSnap
End Function
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As LongPrivate Const PROCESS_TERMINATE As Long = (&H1)
Private Const MAX_PATH As Integer = 260Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE As Long = &H8Private Type PROCESSENTRY32
dwSize As Long
cntusage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End TypePrivate Sub Command1_Click()
Dim ProNum As Long, PidData() As Long, hProcess As Long, i As Long
ProNum = FindPIDByName("notepad.exe", PidData()) '查找记事本的句柄,为了测试方便请只开一个notepad.exe进程
If ProNum = 0 Then Exit Sub
For i = 0 To ProNum - 1
hProcess = OpenProcess(PROCESS_TERMINATE, False, PidData(i)) '获取进程句柄
Print hProcess '打印句柄
Next
End SubPrivate Function FindPIDByName(ProName As String, PidData() As Long) As Long '返回符合条件的进程ID,保存到数组PidData里
Dim hProSnap As Long, hProID As Long, i As Long
Dim ProInfo As PROCESSENTRY32
ProName = LCase$(ProName)
ProInfo.dwSize = Len(ProInfo)
hProSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
hProID = Process32First(hProSnap, ProInfo)
While hProID <> 0
If LCase$(Left(ProInfo.szExeFile, InStr(1, ProInfo.szExeFile, vbNullChar) - 1)) = ProName Then
ReDim Preserve PidData(i)
PidData(i) = ProInfo.th32ProcessID
i = i + 1
End If
hProID = Process32Next(hProSnap, ProInfo)
Wend
FindPIDByName = i
CloseHandle hProSnap
End Function
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwprocessid As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_SETTEXT = &HC
Dim HwndVal&, ChildHwnd&
Private Sub Command1_Click()
HwndVal = FindWindow(vbNullString, "计算器")
If HwndVal = 0 Then MsgBox "计算器没运行": Exit Sub
Print "计算器的句柄是: " & CStr(HwndVal) & Space(3) & "进程号是: " & CStr(ProcIDFromWnd(HwndVal))
SendMessage HwndVal, WM_SETTEXT, 0, ByVal "CBM666 的计算器"
ChildHwnd = FindWindowEx(HwndVal, 0, "Static", vbNullString)
If ChildHwnd <> 0 Then SendMessage ChildHwnd, WM_SETTEXT, 0, ByVal "123456789"
End SubFunction ProcIDFromWnd(ByVal hwnd As Long) As Long
GetWindowThreadProcessId hwnd, idProc
ProcIDFromWnd = idProc
End Function