我用SHELLEXECUTE启动一个程序,如果SHELLEXECUTE执行成功的话,能否利用它的返回值来关闭由它启动的那个程序?如果可以的话,怎么实现呢?多谢指教!

解决方案 »

  1.   

    返回HInstance,好象是不能利用它来做关闭动作的。
      

  2.   

    ShellExecuteReturn Values
    Returns a value greater than 32 if successful, or an error value that is less than or equal to 32 otherwiseThe return value is cast as an HINSTANCE for backward compatibility with 16-bit Microsoft® Windows® applications. It is not a true HINSTANCE, however. The only thing that can be done with the returned HINSTANCE is to cast it to an integer and compare it with the value 32 or one of the error codes below.意思是:执行成功的话返回大于32的整数.返回hinstance是为了向16位的 windows兼容,但它并不是真的hinstance,返回值只用于与32做比较用Shell 返回pid就可以操作了
      

  3.   

    Option Explicit
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As LongPublic Sub KillProcess(ByVal pid As Long)
    'Kill Process by pid
         On Error GoTo ErrHandler
        Dim mhandle As Long, ExitCode As Long
        If pid > 0 Then
            mhandle = OpenProcess(&H1F0FFF, 0, pid)
            ExitCode = GetExitCodeProcess(mhandle, 0)
            If ExitCode <> 0 Then
               TerminateProcess mhandle, ExitCode
            End If
        End If
        Exit Sub
    ErrHandler:
    End Sub
    Private Sub Command1_Click()
    Dim pid As Long
        pid = 0
        pid = Shell("notepad.exe", vbNormalFocus)
       If pid <> 0 Then
           Call KillProcess(pid)
        End If
    End Sub
      

  4.   

    刚写的例子,希望给你点参考~~~~~~~~~~~
    Option ExplicitPrivate Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Const SW_SHOWNOACTIVATE = 4Private Sub Command1_Click()
        
        ShellExecute vbNull, "open", "d:\1.txt", vbNullString, vbNullString, SW_SHOWNOACTIVATE
    End SubPrivate Sub Command2_Click()
        Dim hwnd As Long
        Dim pID As Long
        Dim pHandle As Long, exitCode As Long
        
        On Error GoTo errMsg
        hwnd = FindWindow(vbNullString, "1.txt - 记事本")
        GetWindowThreadProcessId hwnd, pID
        pHandle = OpenProcess(&H1F0FFF, False, pID)
        exitCode = GetExitCodeProcess(pHandle, 0&)
        If exitCode <> 0 Then
            TerminateProcess pHandle, exitCode
        End If
    errMsg:
        
    End Sub
      

  5.   

    hwnd = FindWindow(vbNullString, "1.txt - 记事本")
    再直接发WM_CLOSE就OK了。