Vb6 使用VB 6代码从任务管理器结束进程

Vb6 使用VB 6代码从任务管理器结束进程,vb6,taskmanager,kill-process,Vb6,Taskmanager,Kill Process,我需要大致杀死一个应用程序,以便在我的数据库中获得该应用程序的虚拟订户(这不能通过关闭应用程序来产生)。手动地,如果我们从任务管理器中终止应用程序,则虚拟订户将存在。现在我需要在VB6代码中自动完成它。救命啊!谢谢。有两种方法: 如果目标应用程序有窗口(隐藏/可见),则向其发送WM_CLOSE。任务管理器的“结束任务”使用此方法。大多数应用程序处理WM_关闭并优雅地终止 使用TerminateProcessAPI强制终止-任务管理器的“结束进程”使用此方法。此API强制终止进程 可以在此处找到一

我需要大致杀死一个应用程序,以便在我的数据库中获得该应用程序的虚拟订户(这不能通过关闭应用程序来产生)。手动地,如果我们从任务管理器中终止应用程序,则虚拟订户将存在。现在我需要在VB6代码中自动完成它。救命啊!谢谢。

有两种方法:

  • 如果目标应用程序有窗口(隐藏/可见),则向其发送
    WM_CLOSE
    。任务管理器的“结束任务”使用此方法。大多数应用程序处理WM_关闭并优雅地终止

  • 使用
    TerminateProcess
    API强制终止-任务管理器的“结束进程”使用此方法。此API强制终止进程

  • 可以在此处找到一个示例:

    使用TaskKill命令调用

    TASKKILL[/S系统[/U用户名[/p [密码]]] {[/FI filter][/PID processid |/IM imagename]}[/T][/F]

    说明: 此工具用于按进程id(PID)或映像终止任务 名字

    选项显式
    私有声明函数IsWindow Lib“user32”(ByVal hWnd作为Long)作为Long
    私有声明函数OpenProcess Lib“kernel32”(ByVal dwDesiredAccess为Long,ByVal bInheritHandle为Long,ByVal dwProcessId为Long)为Long
    私有声明函数TerminateProcess Lib“kernel32”(ByVal hProcess As Long,ByVal uExitCode As Long)作为Long
    私有声明函数CloseHandle Lib“kernel32”(ByVal hObject As Long)为Long
    私有声明函数GetWindowThreadProcessId Lib“user32”(ByVal hWnd为Long,lpdwProcessId为Long)为Long
    公共声明函数findwindowlib“user32”别名“FindWindowA”(ByVal lpClassName作为字符串,ByVal lpWindowName作为字符串),长度为
    公共声明函数SendMessage Lib“user32”别名“SendMessage a”(ByVal hWnd为Long,ByVal wMsg为Long,ByVal wParam为Long,lParam为Any)为Long
    公共声明函数EnumWindows库“user32”(ByVal lpEnumFunc为Long,ByVal lParam为Long)为Long
    公共声明函数GetWindowText Lib“user32”别名“GetWindowTextA”(ByVal hWnd为长,ByVal lpString为字符串,ByVal cch为长)为长
    私有常量进程\u所有\u访问=&H1F0FFF
    作为字符串的私有目标
    '---------------------------------------------------------------------------------------
    '创建日期:24/10/2005 09:03
    '创建人:Jason Bruwer
    '用途:如果知道窗口的名称,则返回窗口的窗口句柄
    ’:例如。
    “微软Word
    “微软Excel
    'Microsoft PowerPoint
    “AdobeReader
    '更新人:[姓名首字母]-[日期]-[更改]
    '---------------------------------------------------------------------------------------
    公共函数GetWindowsHandle(WindowName为字符串,hWindow为长)为布尔值
    关于错误转到错误
    '获取目标的窗口句柄。
    hWindow=FindWindow(vbNullString,WindowName)
    如果hWindow=0,则转到欢呼
    GetWindowsHandle=True
    干杯:
    退出功能
    错误:
    frmMain.logErrorAcrossingRBT(“GetWindowsHandle”)
    后藤干杯
    端函数
    '---------------------------------------------------------------------------------------
    '创建日期:24/10/2005 09:03
    '创建人:Jason Bruwer
    '目的:枚举当前打开的所有窗口并搜索应用程序
    '使用指定的名称。
    '更新人:[姓名首字母]-[日期]-[更改]
    '---------------------------------------------------------------------------------------
    公共函数TerminateTask(应用程序名称为字符串)为布尔值
    关于错误转到错误
    目标=UCase(应用程序名称)
    EnumCallback的EnumWindows地址,0
    TerminateTask=True
    干杯:
    退出功能
    错误:
    frmMain.LOGERRORSASSINGRBT(“终止任务”)
    后藤干杯
    端函数
    '---------------------------------------------------------------------------------------
    '创建日期:24/10/2005 09:04
    '创建人:Jason Bruwer
    '目的:检查这是否是我们正在寻找的窗口,然后尝试
    '终止应用程序
    '更新人:[姓名首字母]-[日期]-[更改]
    '---------------------------------------------------------------------------------------
    公共函数EnumCallback(ByVal app_hWnd为Long,ByVal param为Long)为Long
    Dim buf作为字符串*256
    将标题设置为字符串
    暗长
    '获取窗口的标题。
    长度=GetWindowText(应用程序宽度、宽度、长度(宽度))
    标题=左$(基本单位,长度)
    “如果标题为“”,则调试。打印标题
    '查看这是否是目标窗口。
    如果InStr(UCase(title),Target)为0,则
    “关上窗户。
    如果没有关闭进程(app_hWnd),则退出函数
    如果结束
    '继续搜索。
    EnumCallback=1
    端函数
    '---------------------------------------------------------------------------------------
    '创建日期:24/10/2005 09:06
    '创建人:Jason Bruwer
    '目的:尝试使用windows句柄终止应用程序
    '更新人:[姓名首字母]-[日期]-[更改]
    '---------------------------------------------------------------------------------------
    公共函数KillProcess(hWindow长度)为布尔值
    Dim RetrunValue尽可能长
    将进程值设置为Long
    Dim ProcessValueID尽可能长
    暗淡无光
    关于错误转到错误
    如果(IsWindow(hWindow)0),则
    ThreadID=GetWindowThreadProcessId(hWindow,ProcessValueID)
    如果是(ProcessValueID 0),则
    App.LogEvent“警告…正在终止孤立进程…”
    ProcessValue=OpenProcess(PROCESS\u ALL\u访问,CLng(0),ProcessValueID)
    RetrunValue=TerminateProcess(ProcessValue,CLng(0))
    CloseHandle ProcessValueID
    如果结束
    如果结束
    KillProcess=True
    干杯:
    退出功能
    错误:
    frmMain.logErrorAcrossingRBT(“KillProcess”)
    后藤干杯
    端函数
    
    卡尔·彼得森的e
    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
    
    Shell "taskkill.exe /f /t /im processname.exe"
    
    taskkill/?
    
    Private Sub Command1_Click()
    Shell "taskkill.exe /f /t /im Application.exe"
    End Sub
    
    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 "taskkill.exe /f /t /im notepad.exe"