VB写关闭进程程序(使用API函数)
比如说我要关闭本机里正在运行的QQ.EXE进程要怎么做抄袭别人的无视给个简单例说明谢谢代码短一点直接API函数定义和TerminateProcess函数使用说明,例子上个...
比如说我要关闭本机里正在运行的QQ.EXE进程要怎么做
抄袭别人的无视 给个简单例说明 谢谢
代码短一点 直接API函数定义和TerminateProcess函数使用说明,例子上个就OK了 展开
抄袭别人的无视 给个简单例说明 谢谢
代码短一点 直接API函数定义和TerminateProcess函数使用说明,例子上个就OK了 展开
展开全部
'VB 终止指定进程
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 * 1024
End Type
Private Type MODULEENTRY32
dwSize As Long
th32ModuleID As Long
th32ProcessID As Long
GlblcntUsage As Long
ProccntUsage As Long
modBaseAddr As Byte
modBaseSize As Long
hModule As Long
szModule As String * 256
szExePath As String * 1024
End Type
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Sub KillExe(EXEName As String)
Dim my As PROCESSENTRY32
Dim hProcessHandle As Long
Dim success As Long
Dim l As Long
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
If (Process32First(l, my)) Then
Do
If UCase$(Right$(Left$(my.szExeFile, InStr(1, my.szExeFile, Chr$(0)) - 1), Len(EXEName))) = UCase$(EXEName) Then
hProcessHandle = OpenProcess(&H1F0FFF, True, my.th32ProcessID)
If hProcessHandle <> 0 Then success = TerminateProcess(hProcessHandle, ByVal 0&)
If success = 1 Then CloseHandle (hProcessHandle)
End If
Loop Until (Process32Next(l, my) < 1)
End If
CloseHandle l
End If
End Sub
Private Sub Command1_Click()
Dim a As String
a = "c:\windows\notepad.exe" '以记事本为例
KillExe a
End Sub
可以是完整文件名 比如 "c:\windows\notepad.exe"
也可以不完整 比如 "notepad.exe"
不区分大小写
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 * 1024
End Type
Private Type MODULEENTRY32
dwSize As Long
th32ModuleID As Long
th32ProcessID As Long
GlblcntUsage As Long
ProccntUsage As Long
modBaseAddr As Byte
modBaseSize As Long
hModule As Long
szModule As String * 256
szExePath As String * 1024
End Type
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Sub KillExe(EXEName As String)
Dim my As PROCESSENTRY32
Dim hProcessHandle As Long
Dim success As Long
Dim l As Long
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
If (Process32First(l, my)) Then
Do
If UCase$(Right$(Left$(my.szExeFile, InStr(1, my.szExeFile, Chr$(0)) - 1), Len(EXEName))) = UCase$(EXEName) Then
hProcessHandle = OpenProcess(&H1F0FFF, True, my.th32ProcessID)
If hProcessHandle <> 0 Then success = TerminateProcess(hProcessHandle, ByVal 0&)
If success = 1 Then CloseHandle (hProcessHandle)
End If
Loop Until (Process32Next(l, my) < 1)
End If
CloseHandle l
End If
End Sub
Private Sub Command1_Click()
Dim a As String
a = "c:\windows\notepad.exe" '以记事本为例
KillExe a
End Sub
可以是完整文件名 比如 "c:\windows\notepad.exe"
也可以不完整 比如 "notepad.exe"
不区分大小写
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询