我需要大致杀死一个应用程序,以便我可以在我的数据库中获得该应用程序的幻像订户(这不能通过关闭应用程序来生成)。手动,如果我们从任务管理器中终止应用程序,幻像用户将会存在。现在我需要在VB 6代码中自动执行它。帮帮我!谢谢。使用VB 6从任务管理器结束进程代码
3
A
回答
5
有两种方法: 1.如果WM_CLOSE有一个窗口(隐藏/可见),则发送到目标应用程序。任务管理器的“结束任务”使用此方法。大多数应用程序处理WM_CLOSE并优雅地终止。 2.使用TerminateProcess APi强行杀死 - 任务管理器的“End Process”使用此方法。这个API有力地杀死了这个进程。
一个例子可以在这里找到: http://www.vb-helper.com/howto_terminate_process.html
3
呼叫ShellExecute与TASKKILL命令
TASKKILL [/ S系统[/ U的用户名[/ P [口令]]]] { [/ FI过滤器] [/ PID processid |/IM imagename]} [/ T] [/ F]
说明: 此工具用于通过进程id(PID)或图像 名称终止任务。
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
1
卡尔·彼得森的出色的VB6代码归档具有同时使用WM_CLOSE和了TerminateProcess高品质sample code and full explanations。不接受替代品!
你可能会在很多代码中看到的一个缺陷是,发送WM_CLOSE到单个的窗口句柄是不够的 - 大多数应用程序包含大量的窗口。在卡尔代码中实现的答案:查找属于此应用程序的所有顶级窗口并将消息发送给每个窗口。
1
Shell "taskkill.exe /f /t /im processname.exe"
本力(/f
)与processname.exe的图像名称(/im
)的过程中terminatation,并且这是由它(/t
)开始所有的子进程。您可能不需要所有这些开关。更多信息请参见该taskkill
命令的帮助(请在命令行中输入以下):
taskkill/?
3
使用VB6.0 TASKKILL
Private Sub Command1_Click()
Shell "taskkill.exe /f /t /im Application.exe"
End Sub
相关问题
- 1. 结束任务管理器
- 2. 如何通过VBA代码杀死任务管理器进程?
- 3. 从VB调用受管DLL的问题6代码
- 4. 使用任务栏,进程管理器,AdPlus等获得转储
- 5. 停留在任务管理器后,代码清理
- 6. 从任务管理器中删除进程
- 7. C#结束任务没有结束进程
- 8. 任务管理器上显示的许多“excel”进程 - 进程
- 9. 为什么JavaFX应用程序任务管理器进程在关闭时没有结束
- 10. 从任务管理器中禁用Exe
- 11. 任务结束后的处理结果
- 12. 使用WPF托管Winform的任务管理器中的杀死进程
- 13. 理由Android的进程得到当任务管理器显示
- 14. 运行代码的子进程结束处理
- 15. 任务管理器:CPU使用历史
- 16. c# - 如何从应用程序中删除(32位) - 在任务管理器中结束
- 17. 如何在C#中的任务管理器中隐藏进程?
- 18. 任务管理器如何读取进程
- 19. 进程管理器
- 20. 如何防止从任务管理器关闭应用程序?
- 21. 当应用程序从任务管理器
- 22. 应用程序不会从任务管理器
- 23. PHP源代码管理服务器
- 24. VBA代码监视任务管理器中显示的窗口过程
- 25. 任务管理器像使用Java Swing的应用程序
- 26. 阻止进程在任务管理器中关闭
- 27. 隐藏进程任务管理器的接口
- 28. 任务管理器中的进程提交大小C#
- 29. 当程序执行结束时导致VB进程不能结束的形式
- 30. wscript.exe和任务管理器
谢谢你,它的工作原理! – oliverwood 2009-09-09 05:50:49