模块中声明:
Public Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
窗体代码:
Private Sub Command1_Click()
' TerminateProcess -1, 1125 'also can
ExitProcess 1234
End Sub
测试程序:
模块声明:
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, uExitCode As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hProcess As Long) As Long
窗体代码:
Const PROCESS_QUERY_INFORMATION = &H400
Const PROCESS_TERMINATE = &H1
Dim ret As Long
Dim h As Long
Dim pid As Long
Private Sub Command1_Click()
If h Then
GetExitCodeProcess h, ret
MsgBox ret
Else
MsgBox "fail"
End If
End Sub
Private Sub Form_Load()
pid = 1448
h = OpenProcess(PROCESS_QUERY_INFORMATION + PROCESS_TERMINATE, 0, pid)
End Sub
Private Sub Form_Unload(Cancel As Integer)
If h Then CloseHandle h
End Sub
用api函数:GetExitCodeProcess
声明:
Declare Function GetExitCodeProcess Lib "kernel32" Alias "GetExitCodeProcess" (ByVal hProcess As Long, lpExitCode As Long) As Long
返回值:
Long,非零表示成功,零表示失败。会设置GetLastError
参数:
hProcess ------- Long,想获取退出代码的一个进程的句柄
lpExitCode ----- Long,用于装载进程退出代码的一个长整数变量。如进程尚未中止,则设为常数STILL_ACTIVE
例程:
Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Sub Form_Load()
ExitProcess GetExitCodeProcess(GetCurrentProcess, 0)
End Sub