我有下一代码:
’api  申明
Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Declare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function GetCurrentProcessId Lib "kernel32" () As Long‘在vb程序调用,可得本进程信息,关闭当本窗体
GetCurrentProcess
GetCurrentProcessId
TerminateProcess(GetCurrentProcess, GetCurrentProcessId) As Long  现我要关闭指定的程序,怎做啊,怎样获得他的进程名,进程ID呢

解决方案 »

  1.   

    TerminateProcess关闭程序太暴力了,用SendMessage 发送WM_CLOSE 吧:
    Option ExplicitPrivate Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Private Const WM_CLOSE = &H10
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    Const GW_HWNDNEXT = 2
    Dim mWnd As Long
    Function InstanceToWnd(ByVal target_pid As Long) As Long
        Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
        'Find the first window
        test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
        Do While test_hwnd <> 0
            'Check if the window isn't a child
            If GetParent(test_hwnd) = 0 Then
                'Get the window's thread
                test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
                If test_pid = target_pid Then
                    InstanceToWnd = test_hwnd
                    Exit Do
                End If
            End If
            'retrieve the next window
            test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
        Loop
    End FunctionPrivate Sub Command1_Click()
        Dim Pid As Long
        'Lock the window update
        LockWindowUpdate GetDesktopWindow
        'Execute notepad.Exe
        Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
        If Pid = 0 Then MsgBox "Error starting the app"
        'retrieve the handle of the window
        mWnd = InstanceToWnd(Pid)
        
    End SubPrivate Sub Command2_Click()
        SendMessage mWnd, WM_CLOSE, 0&, ByVal 0&
    End Sub
      

  2.   

    hwnd=findwindow(vbnullstring,"目标进程Caption")
    GetWindowThreadProcessId hwnd
    openprocess id,???,0
    TerminateProcess it
      

  3.   

    大虾,FindWindow ,,,怎用啊,我指定,另一运行程序,进程与进程ID,,,,,期待更高手,谢谢
      

  4.   

    Private Declare Function WaitForSingleObject Lib "kernel32" _
             (ByVal hHandle As Long, _
             ByVal dwMilliseconds As Long) As Long      Private Declare Function FindWindow Lib "user32" _
             Alias "FindWindowA" _
             (ByVal lpClassName As String, _
             ByVal lpWindowName As String) As Long      Private Declare Function PostMessage Lib "user32" _
             Alias "PostMessageA" _
             (ByVal hwnd As Long, _
             ByVal wMsg As Long, _
             ByVal wParam As Long, _
             ByVal lParam As Long) As Long      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 GetWindowThreadProcessId Lib "user32" _
             (ByVal hwnd As Long, _
             lpdwProcessId As Long) As Long      'Constants that are used by the API
          Const WM_CLOSE = &H10
          Const INFINITE = &HFFFFFFFF
          Const SYNCHRONIZE = &H100000      Private Sub Form_Load()
             Command1.Caption = "Start the Calculator"
             Command2.Caption = "Close the Calculator"
          End Sub      Private Sub Command1_Click()
             Shell "calc.exe", vbNormalNoFocus
          End Sub      Private Sub Command2_Click()
             Dim hWindow As Long
             Dim hThread As Long
             Dim hProcess As Long
             Dim lProcessId As Long
             Dim lngResult As Long
             Dim lngReturnValue As Long         hWindow = FindWindow(vbNullString, "Calculator")
             hThread = GetWindowThreadProcessId(hWindow, lProcessId)
             hProcess = OpenProcess(SYNCHRONIZE, 0&, lProcessId)
             lngReturnValue = PostMessage(hWindow, WM_CLOSE, 0&, 0&)
             lngResult = WaitForSingleObject(hProcess, INFINITE)
             DoEvents
             hWindow = FindWindow(vbNullString, "计算器")
             If IsWindow(hWindow) = 1 Then
                MsgBox "Handle still exists."
             Else
                MsgBox "All Program Instances Closed."
             End If
          End Sub
      

  5.   

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
    Const GW_HWNDNEXT = 2
    Dim mWnd As Long
    Function InstanceToWnd(ByVal target_pid As Long) As Long
        Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
        'Find the first window
        test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
        Do While test_hwnd <> 0
            'Check if the window isn't a child
            If GetParent(test_hwnd) = 0 Then
                'Get the window's thread
                test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
                If test_pid = target_pid Then
                    InstanceToWnd = test_hwnd
                    Exit Do
                End If
            End If
            'retrieve the next window
            test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
        Loop
    End Function
    Private Sub Form_Load()    Dim Pid As Long
        'Lock the window update
        LockWindowUpdate GetDesktopWindow
        'Execute notepad.Exe
        Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
        If Pid = 0 Then MsgBox "Error starting the app"
        'retrieve the handle of the window
        mWnd = InstanceToWnd(Pid)
        'Set the notepad's parent
        SetParent mWnd, Me.hwnd
        'Put the focus on notepad
        Putfocus mWnd
        'Unlock windowupdate
        LockWindowUpdate False
    End Sub
    Private Sub Form_Unload(Cancel As Integer)
        'Unload notepad
        DestroyWindow mWnd
        'End this program
        TerminateProcess GetCurrentProcess, 0
    End Sub
      

  6.   

    Private Sub Command2_Click()
          hWindow = FindWindow(vbNullString, "运行程序的标题名")       
           Call TernamiteProcessByHWND(hWindow)
       end sub
    ''用于结束外部进程,hCloseWnd  是要结束的程序的主窗口的  HWND
    Public Function TernamiteProcessByHWND(ByVal hCloseWnd As Long) As Boolean
    Dim hProcessID     As Long
    Dim hProcess         As Long
    On Error GoTo PROC_EXIT
           If hCloseWnd = 0 Then GoTo PROC_EXIT
           If GetWindowThreadProcessId(hCloseWnd, hProcessID) = 0 Then GoTo PROC_EXIT
           hProcess = OpenProcess(PROCESS_TERMINATE, False, hProcessID)
           If hProcess = 0 Then GoTo PROC_EXIT
           If TerminateProcess(hProcess, 0&) = 0 Then GoTo PROC_EXIT
           TernamiteProcessByHWND = True
    PROC_EXIT:
           If Err.Number <> 0 Then
                   Debug.Print Err.Description
                   Err.Clear
           End If
    End Function