永发信息网

VB关于处理进程的问题

答案:2  悬赏:0  手机版
解决时间 2021-08-01 12:33
  • 提问者网友:战皆罪
  • 2021-08-01 04:25

用Shell函数打开一个新程序,获得的那个taskID是不是就是进程的PID?

如果是。用什么API可以kill掉那个进程?

最佳答案
  • 五星知识达人网友:野味小生
  • 2021-08-01 05:44

得到的是PID


可以用API干掉,但是太麻烦了


只要用任务管理器就行了,代码如下


Private Sub Form_Load()
Dim S As String
Dim PID As Long
S = "notepad.exe"
PID = Shell(S, vbNormalFocus)
MsgBox "已经打开了记事本,你看到了吗? 单击确定关闭记事本"
Shell "cmd /c taskkill /F /PID " & PID, vbHide
End
End Sub

全部回答
  • 1楼网友:执傲
  • 2021-08-01 06:04

那我就补充一个API的吧,反正差不多

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 Private Const PROCESS_TERMINATE = &H1 ''用于结束外部进程,hCloseWnd 是要结束的程序的主窗口的 HWND Public Function TernamiteProcessByPID(ByVal PID As Long) As Boolean Dim hProcessID As Long Dim hProcess As Long On Error GoTo PROC_EXIT hProcess = OpenProcess(PROCESS_TERMINATE, False, PID) If hProcess = 0 Then GoTo PROC_EXIT If TerminateProcess(hProcess, 0&) = 0 Then GoTo PROC_EXIT TernamiteProcessByPID = True PROC_EXIT: If Err.Number <> 0 Then Debug.Print Err.Description Err.Clear End If End Function

Private Sub Form_Load() Dim S As String Dim PID As Long S = "notepad.exe" PID = Shell(S, vbNormalFocus) MsgBox "已经打开了记事本,你看到了吗? 单击确定关闭记事本" Dim T As Boolean T = TernamiteProcessByPID(PID) If T Then MsgBox "关闭成功" Else MsgBox "关闭失败" End Sub

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯