关闭进程的两种方法_WZH AND SEA_百度空间

方法一:

Option Explicit

'声明API

Private 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 * 260

End Type

Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long

Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long

Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long

Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long

Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)

Private Const TH32CS_SNAPPROCESS = &H2&

'关闭指定名称的进程

Private Sub KillProcess(sProcess As String)

Dim lSnapShot As Long

Dim lNextProcess As Long

Dim tPE As PROCESSENTRY32

lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)

If lSnapShot <> -1 Then

tPE.dwSize = Len(tPE)

lNextProcess = Process32First(lSnapShot, tPE)

Do While lNextProcess

If LCase$(sProcess) = LCase$(Left(tPE.szExeFile, InStr(1, tPE.szExeFile, Chr(0)) - 1)) Then

Dim lProcess As Long

Dim lExitCode As Long

lProcess = OpenProcess(1, False, tPE.th32ProcessID)

TerminateProcess lProcess, lExitCode

CloseHandle lProcess

End If

lNextProcess = Process32Next(lSnapShot, tPE)

Loop

CloseHandle (lSnapShot)

End If

End Sub

Call KillProcess("要关闭的文件名.exe") ' 注意,文件名必须与进程表一致

--------------------------------------------------------------------------------

方法二:

--------------------------------------------------------------------------------

用shell 执行 taskkill /f /im 进程名

例如Shell "taskkill /f /im 要关闭的文件名.exe", vbNormalFocus

notepad.exe这个是记事本

--------------------------------------------------------------------------------

'API声明

Public Const TH32CS_SNAPPROCESS As Long = 2&

Public 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 * 260&

End Type

Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long

Public Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long

Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, lProcessID As Long) As Long

Public Declare Function Process32First Lib "kernel32" (ByVal hsnapshot As Long, uprocess As PROCESSENTRY32) As Long

Public Declare Function Process32Next Lib "kernel32" (ByVal hsnapshot As Long, uprocess As PROCESSENTRY32) As Long

Public Sub KillProcess(strProcessName As String)

Dim uprocess As PROCESSENTRY32

Dim hsnapshot As Long

Dim hresult As Long

Dim sName As String

Dim hProID As Long

Dim i As Long

uprocess.dwSize = Len(uprocess)

hsnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)

If hsnapshot Then

hresult = Process32First(hsnapshot, uprocess)

Do While hresult

i = InStr(1, uprocess.szExeFile, Chr(0))

sName = LCase$(Left$(uprocess.szExeFile, i - 1))

If sName = strProcessName Then

hProID = OpenProcess(1&, -1&, uprocess.th32ProcessID)

If hProID Then

TerminateProcess hProID, 0&

End If

End If

hresult = Process32Next(hsnapshot, uprocess)

Loop

End If

End Sub



郑重声明:资讯 【关闭进程的两种方法_WZH AND SEA_百度空间】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——