5

我需要大致杀死一个应用程序,以便我可以在我的数据库中获得该应用程序的幻像订阅者(这不能通过关闭应用程序来产生)。手动,如果我们从任务管理器中终止应用程序,幻影订阅者将存在。现在我需要在 VB 6 代码中自动完成。帮助!谢谢。

4

8 回答 8

7

有两种方法:

  1. 如果它有一个窗口(隐藏/可见),则发送WM_CLOSE到目标应用程序。任务管理器的“结束任务”使用这种方法。大多数应用程序处理 WM_CLOSE 并正常终止。

  2. 使用TerminateProcessAPI 强行杀死——任务管理器的“结束进程”使用这种方法。此 API 强制终止该进程。

可以在这里找到一个例子:

VB Helper: HowTo: 立即终止进程

于 2009-09-04T11:26:06.843 回答
5

使用vb6.0 TaskKill

Private Sub Command1_Click()
Shell "taskkill.exe /f /t /im Application.exe"
End Sub
于 2016-05-17T04:46:18.460 回答
3
Shell "taskkill.exe /f /t /im processname.exe"

这会强制 ( /f) 终止带有/improcessname.exe 的映像名称 () 的进程,以及由它启动的任何子进程 ( /t)。您可能不需要所有这些开关。有关更多信息,请参阅taskkill命令帮助(在命令行中键入以下内容):

taskkill/?
于 2009-09-04T15:26:22.170 回答
3

使用 TaskKill 命令调用ShellExecute

TASKKILL [/S 系统 [/U 用户名 [/P [密码]]]] { [/FI 过滤器] [/PID processid | /IM 图像名] } [/T] [/F]

说明:此工具用于通过进程 ID (PID) 或映像名称终止任务。

于 2009-09-04T11:27:51.737 回答
1
Option Explicit

Private Declare Function IsWindow Lib "user32" (ByVal hWnd 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
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


Private Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Target As String

'---------------------------------------------------------------------------------------
' Creation Date :   24/10/2005 09:03
' Created By    :   Jason Bruwer
' Purpose         :   Returns the windows handle of a window if you know the name
'                    :   E.g.
'                           Microsoft Word
'                           Microsoft Excel
'                           Microsoft PowerPoint
'                           Adobe Reader
' Updated By    :   [Initials] - [Date] - [Changes]
'---------------------------------------------------------------------------------------
Public Function GetWindowsHandle(WindowName As String, hWindow As Long) As Boolean

    On Error GoTo Errors

    ' Get the target's window handle.
    hWindow = FindWindow(vbNullString, WindowName)

    If hWindow = 0 Then GoTo Cheers

    GetWindowsHandle = True

Cheers:
    Exit Function
Errors:
    frmMain.LogErrorAcrossUsingRBT ("GetWindowsHandle")
    GoTo Cheers
End Function


'---------------------------------------------------------------------------------------
' Creation Date :   24/10/2005 09:03
' Created By    :   Jason Bruwer
' Purpose        :   Enumerates all the currently open windows and searches for an application
'                        with the specified name.
' Updated By    :   [Initials] - [Date] - [Changes]
'---------------------------------------------------------------------------------------
Public Function TerminateTask(app_name As String) As Boolean

On Error GoTo Errors

Target = UCase(app_name)
EnumWindows AddressOf EnumCallback, 0

TerminateTask = True

Cheers:
Exit Function
Errors:
frmMain.LogErrorAcrossUsingRBT ("TerminateTask")
GoTo Cheers
End Function


'---------------------------------------------------------------------------------------
' Creation Date :   24/10/2005 09:04
' Created By    :   Jason Bruwer
' Purpose         :  Checks to see if this is the window we are looking for and then trys
'                        to kill the application
' Updated By    :   [Initials] - [Date] - [Changes]
'---------------------------------------------------------------------------------------
Public Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long

' Get the window's title.
length = GetWindowText(app_hWnd, buf, Len(buf))
title = Left$(buf, length)

'If title <> "" Then Debug.Print title

' See if this is the target window.
If InStr(UCase(title), Target) <> 0 Then
    ' Kill the window.
    If Not KillProcess(app_hWnd) Then Exit Function
End If

' Continue searching.
EnumCallback = 1

End Function


'---------------------------------------------------------------------------------------
' Creation Date :   24/10/2005 09:06
' Created By    :   Jason Bruwer
' Purpose         :  Trys to kill an application by using its windows handle
' Updated By    :   [Initials] - [Date] - [Changes]
'---------------------------------------------------------------------------------------
Public Function KillProcess(hWindow As Long) As Boolean
Dim RetrunValue As Long
Dim ProcessValue As Long
Dim ProcessValueID As Long
Dim ThreadID As Long

    On Error GoTo Errors

    If (IsWindow(hWindow) <> 0) Then
      ThreadID = GetWindowThreadProcessId(hWindow, ProcessValueID)

      If (ProcessValueID <> 0) Then
        App.LogEvent "Warning...killing orphan process..."

        ProcessValue = OpenProcess(PROCESS_ALL_ACCESS, CLng(0), ProcessValueID)
        RetrunValue = TerminateProcess(ProcessValue, CLng(0))
        CloseHandle ProcessValueID
      End If

    End If

    KillProcess = True

Cheers:
    Exit Function
Errors:
    frmMain.LogErrorAcrossUsingRBT ("KillProcess")
    GoTo Cheers
End Function
于 2009-09-04T11:36:37.087 回答
1

Karl Peterson 出色的 VB6 代码存档包含高质量的示例代码和使用 WM_CLOSE 和 TerminateProcess 的完整解释。不接受替代品!

您可能在大量代码中看到的一个缺陷是,将 WM_CLOSE 发送到您拥有的单个窗口句柄是不够的 - 大多数应用程序包含许多窗口。Karl 的代码中实现的答案是:找到属于该应用程序的所有顶级窗口并将消息发送给每个窗口。

于 2009-09-04T12:22:45.557 回答
0

这是我在 vb6 中按名称杀死进程的代码

这个对我有用

 Private Sub TerminateProcess(ProcessName As String)
        Dim Process As Object
        For Each Process In GetObject("winmgmts:").ExecQuery("Select Name from Win32_Process Where Name = '" & ProcessName & "'")
            Process.Terminate
        Next
End Sub
于 2019-07-16T04:30:51.897 回答
0

通过VB6内部方式做到这一点......

Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Public Sub KillProcess(ByVal processName As String)
       Set oWMI = GetObject("winmgmts:")
       Set oServices = oWMI.InstancesOf("win32_process")
       For Each oService In oServices
           servicename = LCase(Trim(CStr(oService.Name) & ""))
           If InStr(1, servicename, LCase(processName), vbTextCompare) > 0 Then
              oService.Terminate
           End If
       Next
End Sub

...并按如下方式使用它:

killProcess("notepad.exe")

第二种选择: [通过SHELL外部方式... ]

Shell "taskkill.exe /f /t /im notepad.exe"
于 2020-05-04T15:45:12.833 回答