想写个VB小程序,它的功能是用来打开另几个独立的程序(EXE文件),如果关掉这个VB小程序的话,由它所打开的程序都要自行关闭。能提供编程方向或提供例的必给分先谢谢 ^_^

解决方案 »

  1.   

    假设按command1按钮就启动A.EXE、B.EXE、C.EXE,在程序退出时关闭它们。首先在一个模块里作以下声明:
    Const SYNCHRONIZE = &H100000
         public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
         Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
         Public Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    dim pida as long
    dim pidb as long
    dim pidc as long
    dim phnda as long
    dim phndb as long
    dim phndc as longprivate sub command1_click()
    pIda = Shell("a.exe", vbNormalFocus) ' Shell 传回a.exe的 Process Id
    pHnda = OpenProcess(SYNCHRONIZE, 0, pIda) ' 取得a.exe的 Process HandlepIdb = Shell("b.exe", vbNormalFocus) ' Shell 传回b.exe的 Process Id
    pHndb = OpenProcess(SYNCHRONIZE, 0, pIdb) ' 取得b.exe的 Process Handle
         
    pIdc = Shell("c.exe", vbNormalFocus) ' Shell 传回c.exe的 Process Id
    pHndc = OpenProcess(SYNCHRONIZE, 0, pIdc) ' 取得c.exe的 Process Handle
              
    end sub
    private form1_unload()
    Call TerminateProcess( pHnda, 0 ) ' TerminateProcess 所传入的是 Process Handle
    Call CloseHandle( pHnda )Call TerminateProcess( pHndb, 0 ) ' TerminateProcess 所传入的是 Process Handle
    Call CloseHandle( pHndb )Call TerminateProcess( pHndc, 0 ) ' TerminateProcess 所传入的是 Process Handle
    Call CloseHandle( pHndc )end
    end sub
       
    此方法适用于98,在nt、2000下无效。   
      

  2.   

    打开用shell函数,关闭的话查找这几个程序的进程然后KILL。
      

  3.   

    SORRY,是我表达不清,我的意思是如时刻意用"windows任务管理器"中止这个程序时,它都能实现同时关闭其它经由它所打开的程序。
      

  4.   

    试过了,可以的,你用ctrl+alt+del叫出任务管理器,把你的vb程序终止,那么a.exe,b.exe,c.exe都会一起结束的,另外,上面的代码不小心写错了,
    dim pida as long
    dim pidb as long
    dim pidc as long
    dim phnda as long
    dim phndb as long
    dim phndc as long
    应该改为:
    public pida as long
    public pidb as long
    public pidc as long
    public phnda as long
    public phndb as long
    public phndc as long98+vb6下通过.
      

  5.   

    Option ExplicitPublic Const GW_HWNDNEXT = 2Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long' Return the window handle for an instance handle.
    Function InstanceToWnd(ByVal target_pid As Long) As Long
        Dim test_hwnd As Long
        Dim test_pid As Long
        Dim test_thread_id As Long    ' Get the first window handle.
        test_hwnd = FindWindow(ByVal 0&, ByVal 0&)    ' Loop until we find the target or we run out
        ' of windows.
        Do While test_hwnd <> 0
            ' See if this window has a parent. If not,
            ' it is a top-level window.
            If GetParent(test_hwnd) = 0 Then
                ' This is a top-level window. See if
                ' it has the target instance handle.
                test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)            If test_pid = target_pid Then
                    ' This is the target.
                    InstanceToWnd = test_hwnd
                    Exit Do
                End If
            End If        ' Examine the next window.
            test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
        Loop
    End FunctionPrivate Sub Command1_Click()
        Dim pid As Long
        Dim buf As String
        Dim buf_len As Long
        Dim styles As Long    pid = Shell("notepad.exe", vbNormalFocus)
        If pid = 0 Then
            MsgBox "Error starting program"
            Exit Sub
        End If    Notepad_Hwnd& = InstanceToWnd(pid)    Notepad_OldParent& = SetParent(Notepad_Hwnd&, Me.hwnd)End Sub
      

  6.   

    我觉得楼上的可行性更强,因为nt,2k,xp下用任务管理器结束的程序就是用terminateprocess结束的,根本轮不上结束其他程序,对于一楼的回复我现在试试。
      

  7.   

    to JennyVenus() 
    已经声明在先,我的方法在nt、2000下是无效的。
      

  8.   

    用API:ShellExecute()打开你要运行的程序,它返回窗口的hwnd,再用SendMessage()发送WM_Close给它就可以了.
      

  9.   

    to lsftestNT不行也没所谓,但2K/XP下都有需要实现这项功能的。有其他办法吗?
      

  10.   

    用sendmessage
    发送消息就可以了