我在自己的程序里调用了其他程序,但一段时间后又要关闭掉,我先用findwindow
再postmessage,但调用的程序标题会发生变化,这种方法不好。我又用openprocess后再terminateprocess,但好象实际上关闭不了这个进程,是我权限不够吗,还是程序有问题?请高手指教!!

解决方案 »

  1.   

    Public Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
    Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    Public Declare Function GetCurrentProcess Lib "kernel32" () As Long
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long'ExitProcess GetExitCodeProcess(GetCurrentProcess, 0 )
    '关闭EXE文件      PROID=进程句柄
    Public Function CloseExe(ProID As Long)
         Dim Rc As Long
         '中断一个进程
          Call TerminateProcess(ProID, 0)
         '关闭该进程
          Call CloseHandle(ProID)
    End Function
      

  2.   

    //我又用openprocess后再terminateprocess,但好象实际上关闭不了这个进程,是我权限不够吗,还是程序有问题?请高手指教!!用openprocess获得进程句柄时注意一下就可以了:
    Option Explicit
    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
    Const PROCESS_QUERY_INFORMATION = &H400
    Const PROCESS_TERMINATE = &H1
    Dim hProcess As LongPrivate Sub Command1_Click()
        Dim pid As Long
        pid = Shell("notepad", 1)
        hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, 0, pid) 'PROCESS_TERMINATE为必须
        
    End SubPrivate Sub Command2_Click()
        TerminateProcess hProcess, 0
        CloseHandle hProcess
    End Sub
      

  3.   

    //我在自己的程序里调用了其他程序,但一段时间后又要关闭掉,我先用findwindow
    再postmessage,但调用的程序标题会发生变化,这种方法不好既然是自己调用的,可以根据pid直接获得窗口句柄,不需要用窗口标题:
    Option Explicit
    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 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 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
    Const GW_HWNDNEXT = 2
    Const WM_CLOSE = &H10
    Dim mhWnd 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 Command1_Click()
        Dim Pid As Long
        Pid = Shell("notepad", 1)
        mhWnd = InstanceToWnd(Pid)
    End SubPrivate Sub Command2_Click()
        PostMessage mhWnd, WM_CLOSE, 0&, ByVal 0&
    End Sub
      

  4.   

    同意楼上的。
    http://expert.csdn.net/Expert/topic/2820/2821000.xml?temp=.2655451