|
发表于 2020-11-4 16:00:01
|
显示全部楼层
OpenProcess()的第一个叁数,指的是所取得的hProcess所具有的能力,像PROCESS_QUERY_INFORMATION 便是让GetExitCode()可取得hProcess所指的process之状态,而PROCESS_TERMINATE,便是让TerminateProcess(hProcess..)的指令能够生效,也就是说,不同叁数设定,使hProcess所具有的权限、能力有所不同。
所以,你只要使OpenProcess()的第一个叁数为PROCESS_TERMINATE就可以了关闭了,当然,针对于TerminateProcess(hProcess, 3838)你最好将第一个参数设为PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION,下面是修改后的代码:
Option Explicit
Const PROCESS_QUERY_INFORMATION = &H400
Const PROCESS_TERMINATE = &H1
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Sub main()
Dim ProcessId As Long
Dim hProcess As Long
ProcessId = Shell("notepad.exe", 1) '此处利用了 Shell当函数使用时返回的任务标识
hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, ProcessId)
Call TerminateProcess(hProcess, 3838)
End Sub
|
|