|
The first parameter of OpenProcess() refers to the capabilities of the obtained hProcess. For example, PROCESS_QUERY_INFORMATION allows GetExitCode() to obtain the status of the process pointed to by hProcess, and PROCESS_TERMINATE is to let TerminateProcess(hProcess. .) command can take effect, that is, different parameter settings make hProcess have different permissions and capabilities.
So, you only need to set the first parameter of OpenProcess() to PROCESS_TERMINATE to close it. Of course, for TerminateProcess(hProcess, 3838) you'd better set the first parameter to PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, the following is the modified The code:
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)'Here is the task ID returned by Shell when the function is used
hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, ProcessId)
Call TerminateProcess(hProcess, 3838)
End Sub |
|