Private Const MAX_PATH = 260
Private Type PROCESSENTRY32
dwSize As Long
'类型长度
cntUsage As Long
'使用
th32ProcessID As Long
'进程ID
th32DefaultHeapID As Long
'
th32ModuleID As Long
'进程线ID
cntThreads As Long
'线程
th32ParentProcessID As Long '父进程
pcPriClassBase As Long
'优先级
dwFlags As Long
szExeFile As String * MAX_PATH
'可执行程序名称
End Type
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal hObject As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Function FindProc(fileName As String) As Boolean
Dim proc As PROCESSENTRY32
Dim exeName As String
Dim snap As Long
Dim theloop As Long
Dim flag As Boolean
snap = CreateToolhelpSnapshot(&HF, 0)
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '取进程链首进程,进程信息存在snap中
Do While theloop <> 0
exeName = Left(proc.szExeFile, InStr(proc.szExeFile, Chr(0)) - 1) '去掉末尾字符
If LCase(exeName) = fileName Then
theloop = OpenProcess(1, True, proc.th32ProcessID) '取得进程句柄
flag = True
Exit Do
End If
theloop = ProcessNext(snap, proc) '取进程链下一进程
Loop
CloseHandle snap
FindProc = flag
End Function
上面是查找进程的代码
If FindProc("QQ.exe") = False Then
关机操作
End If