'代码出处:http://download.csdn.net/download/lujianjian/3650563
'参考网址:http://zhidao.baidu.com/link?url=iUSz-d9dZ20gDUjy1pkI-fSqhvbjSTtFkvyIoVTvh1F55VWoH8Kv5Vk6pe7CVq3zX3FS0JGRBb8kmFlIjfgD0SKqbch2UVtyyZWkjt_IWTa
'参考网址:http://baike.baidu.com/view/589425.htm
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 Const TH32CS_SNAPPROCESS = &H2
Private Type PROCESSENTRY32
dwsize As Long
cntUseage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
swFlags As Long
szExeFile As String * 1024
End Type
'---------------------------------------------进程相关
Private Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Const TH32CS_SNAPTHREAD = &H4
Private Type THREADENTRY32
dwsize As Long
cntusage As Long
th32threadID As Long
th32OwnerProcessID As Long
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type
'---------------------------------------------线程相关
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
'---------------------------------------------进程线程快照相关
Private Declare Function OpenThread Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwThreadId As Long) As Long
'---------------------------------------------获得线程对像
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
'---------------------------------------------关闭一个内核对象(其中包括文件、文件映射、进程、线程、安全和同步对象等)
Private Declare Function SuspendThread Lib "kernel32" (ByVal hThread As Long) As Long '暂停线程
Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long '恢复线程
Private Const THREAD_SUSPEND_RESUME = (&H2)
'---------------------------------------------暂停恢复线程相关
Private Sub Form_Load() '获取系统进程列表
Dim MySnapHandle As Long
Dim ProcessInfo As PROCESSENTRY32
MySnapHandle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) '获得系统进程快照句柄
ProcessInfo.dwsize = Len(ProcessInfo)
If Process32First(MySnapHandle, ProcessInfo) <> 0 Then
Do
List1.AddItem "进程pid:" & ProcessInfo.th32ProcessID & Chr(9) & "进程名:" & ProcessInfo.szExeFile
Loop While Process32Next(MySnapHandle, ProcessInfo) <> 0
End If
CloseHandle MySnapHandle '关闭系统进程快照句柄
End Sub
Private Sub Command1_Click() '暂停该进程所有线程
Dim a As Long
Dim b As THREADENTRY32
a = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0) '获得系统线程快照句柄
b.dwsize = Len(b)
If Thread32First(a, b) <> 0 Then
Do
If b.th32OwnerProcessID = Split(Split(List1.List(List1.ListIndex), "进程pid:")(1), Chr(9))(0) Then
'Split(Split(List1.List(List1.ListIndex), "进程pid:")(1), Chr(9))(0)为该进程某个线程的线程句柄
Dim lngHandle As Long
lngHandle = OpenThread(THREAD_SUSPEND_RESUME, 0, b.th32threadID) '获得某个线程对像
Call SuspendThread(lngHandle) '暂停线程
End If
Loop Until Thread32Next(a, b) = 0 '直到最后一个线程才退出
End If
CloseHandle a '关闭系统线程快照句柄
MsgBox "线程已暂停", vbOKOnly, Me.Caption
End Sub
Private Sub Command2_Click() '恢复该进程所有线程
Dim a As Long
Dim b As THREADENTRY32
a = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0) '获得线程快照句柄
b.dwsize = Len(b)
If Thread32First(a, b) <> 0 Then
Do
If b.th32OwnerProcessID = Split(Split(List1.List(List1.ListIndex), "进程pid:")(1), Chr(9))(0) Then
'Split(Split(List1.List(List1.ListIndex), "进程pid:")(1), Chr(9))(0)为该进程某个线程的线程句柄
Dim lngHandle As Long
lngHandle = OpenThread(THREAD_SUSPEND_RESUME, 0, b.th32threadID) '获得某个线程对像
Call ResumeThread(lngHandle) '恢复线程
End If
Loop Until Thread32Next(a, b) = 0 '直到最后一个线程才退出
End If
CloseHandle a '关闭系统线程快照句柄
MsgBox "线程已恢复", vbOKOnly, Me.Caption
End Sub