比如有个a.exe 工程名称和标题是 "ccc"我想在b.exe里关闭a.exe应该怎么做?

解决方案 »

  1.   

    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 LongPrivate Const WM_CLOSE = &H10Private Sub cmdCloseApp_Click()
    Dim CloseIt As Long
    CloseIt = FindWindow(vbNullString, "ccc")
    PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)
    End Sub 
      

  2.   

    2楼的代码测试没问题但是我在a.exe里有这样的代码Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = 0 Then
        If MsgBox("您确定吗", vbOKCancel) Then
         Unload Me
        Else
         Cancel = True
        End If
    End If
    End Sub这样PostMessage 后出来的是对话框,我不希望他出现对话框,希望他直接关闭 有什么办法吗?
      

  3.   

    Option ExplicitPrivate Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
    ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Const WM_CLOSE = &H10Private Sub Command1_Click()
        Dim lngTmp As Long
        lngTmp = FindWindow(vbNullString, "P1")
        Call SendMessage(lngTmp, WM_CLOSE, 0, 0)
    End Sub
      

  4.   

    用sendmessage , 上面P1改成ccc
      

  5.   

    一样啊 
    现在是可以关闭了 但是有另外一个问题
    但是我在a.exe里有这样的代码 Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) 
    If UnloadMode = 0 Then 
        If MsgBox("您确定吗", vbOKCancel) Then 
        Unload Me 
        Else 
        Cancel = True 
        End If 
    End If 
    End Sub 这样PostMessage 后出来的是对话框,我不希望他出现对话框,希望他直接关闭 有什么办法吗?
      

  6.   

    用shell("pskill a.exe");
    其中pskill.exe是一个工具软件
      

  7.   

    我重复测试过了 会出现对话提示的但是我在a.exe里有这样的代码 Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) 
    If UnloadMode = 0 Then 
        If MsgBox("您确定吗", vbOKCancel) Then 
        Unload Me 
        Else 
        Cancel = True 
        End If 
    End If 
    End Sub 
    你可能不明白我的意思  我是希望直接关掉 不要有这个提示
      

  8.   

    king06 你创个a.exe Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    If UnloadMode = 0 Then
        If MsgBox("您确定吗", vbOKCancel) Then
         Unload Me
        Else
         Cancel = True
        End If
    End If
    End Sub就这点代码测试下就知道了
      

  9.   

    我不是说了用WM_QUIT吗?把PostMessage里的WM_CLOSE换成WM_QUIT