怎么样用API来关闭一个进程?谢谢~

解决方案 »

  1.   

    什么意思??有窗体吗?如有,可以用findwindow(程序名)找到,关闭就行了
      

  2.   

    Private  Declare  Function  GetWindowThreadProcessId  Lib  "user32"  (ByVal  hwnd  As  Long,  lpdwProcessId  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  TerminateProcess  Lib  "kernel32"  (ByVal  hProcess  As  Long,  ByVal  uExitCode  As  Long)  As  Long  
    Private  Const  PROCESS_TERMINATE  =  &H1  
     
    ''用于结束外部进程,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  
      

  3.   

    可以通过调用Windows API函数FindWindow和PostMessage的办法,找出相应 
    的软件句柄,并通知Windows 98关闭该软件。以下是一个关闭Windows 98附件 
    中的小软件“计算器”的例子。 
      具体步骤如下: 
    1)      在VB中新建一个标准EXE工程; 
    2)      画出一个按钮Command1; 
    3)      在窗体Form1中定义Windows API的声明; 
            Const WM_CLOSE = &H10           
            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, lParam As Any) As Long           
    4)      在Command1的Click事件中编写以下代码: 
            Private Sub Command1_Click() 
              Dim winHwnd As Long 
              Dim RetVal As Long 
              winHwnd = FindWindow(vbNullString, "计算器") 
              Debug.Print winHwnd 
              If winHwnd <> 0 Then 
                 RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&) 
                 If RetVal = 0 Then 
                    MsgBox "关闭计算器出错!" 
                 End If 
               Else 
                 MsgBox "计算器没有运行。" 
               End If 
            End Sub