2009-09-04 30 views
3

私はデータベース内のそのアプリケーションのファンタム加入者を得ることができるようにアプリケーションを大雑把に殺す必要があります(これはアプリケーションを閉じることによっては生成できません)。手動で、タスクマネージャからアプリケーションを終了すると、ファントム加入者が存在します。今私はVB 6のコードでそれを自動的に行う必要があります。助けて!ありがとう。VB6コードを使用したタスクマネージャからの終了プロセス

答えて

5

2つの方法があります。 1. WM_CLOSEにウィンドウがある場合は非表示/非表示のWM_CLOSEを送信します。タスクマネージャの「タスクの終了」は、このメソッドを使用します。ほとんどのアプリケーションはWM_CLOSEを処理し、正常終了します。 2. TerminateProcess APiを使用して強制的に強制終了 - タスクマネージャの「プロセスの終了」では、この方法が使用されます。このAPIは強制的にプロセスを終了させます。

例は、ここで見つけることができる: http://www.vb-helper.com/howto_terminate_process.html

+0

それは動作しますが、ありがとうございます! – oliverwood

3

TaskKillコマンドでコールShellExecute

TASKKILL [/ Sシステム[/ Uユーザー名[/ P [パスワード]]]] { [/ FIフィルタ] [/ PIDのprocessid |このツールは、プロセスID(PID)またはイメージ の名前でタスクを終了するために使用されます。 VB6のコードの

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

カール・ピーターソンの優れたアーカイブには、WM_CLOSEとのTerminateProcessの両方を使用して、高品質sample code and full explanationsを持っています。代用品を受け入れないでください!

WM_CLOSEをに送信すると、たくさんのコードが表示される可能性があります。単一のウィンドウハンドルでは不十分です。ほとんどのアプリケーションは、多数のウィンドウで構成されています。カールのコードで実行される答え:このアプリケーションに属するすべてのトップレベルのウィンドウを見つけて、それぞれにメッセージを送信します。

1
Shell "taskkill.exe /f /t /im processname.exe" 

この力(/f)processname.exeのイメージ名(/im)、およびそれによって開始されたすべての子プロセス(/t)とプロセスのterminatation。これらのスイッチはすべて必要ではないかもしれません。 (コマンドラインで次のように入力します)詳細については、taskkillコマンドのヘルプを参照してください:

taskkill/? 
3

使用VB6.0 TaskKill

Private Sub Command1_Click() 
Shell "taskkill.exe /f /t /im Application.exe" 
End Sub 
関連する問題