我怎么样获取一个进程,并且把他关闭??

解决方案 »

  1.   

    使用Windows API FindWindow  PostMessage
      

  2.   

    Private Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.DLL" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long也可以用这两个函数枚举进程,再关闭
      

  3.   

    Private Const TH32CS_SNAPHEAPLIST = &H1
    Private Const TH32CS_SNAPPROCESS = &H2
    Private Const TH32CS_SNAPTHREAD = &H4
    Private Const TH32CS_SNAPMODULE = &H8
    Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
    Private Const TH32CS_INHERIT = &H80000000
    Private Const PROCESS_TERMINATE = &H1&Private Sub Command1_Click()
    Dim Process As PROCESSENTRY32
    Dim ProcSnap As Long
    Dim cntProcess As Long
    cntProcess = 0
    List1.Clear
    ProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    If ProcSnap Then
        Process.dwsize = 1060                                        ' 通常用法
        Process32First ProcSnap, Process
        Do Until Process32Next(ProcSnap, Process) < 1                ' 遍历所有进程直到返回值为False
            List1.AddItem Trim(Process.szExeFile)
            cntProcess = cntProcess + 1
        Loop
    End If
    ReDim ProcessID(cntProcess) As Long
    Dim i As Long
    i = 0
    Process32First ProcSnap, Process
    Do Until Process32Next(ProcSnap, Process) < 1                    ' 遍历所有进程直到返回值为False
        ProcessID(i) = Process.th32ProcessID
        i = i + 1
    Loop
    CloseHandle (ProcSnap)
    End SubPrivate Sub Command2_Click()
    Dim c As Integer
    If List1.ListIndex < 0 Then
        MsgBox "请选择进程!", vbOKOnly + vbInformation, "提示"
    Else
        Dim hProcess As Long
        hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID(List1.ListIndex))
        If hProcess Then TerminateProcess hProcess, 0
        c = List1.ListCount
        While List1.ListCount = c
            Command1_Click
        Wend
    End If
    End SubPrivate Sub Command3_Click()
    Unload Me
    End Sub
      

  4.   

    楼上的大哥,可以把函数声明和PROCESSENTRY32 贴出来吗?小弟比较菜!!
      

  5.   

    模块中的代码:Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Const WM_CLOSE = &H10
    Public Const GW_HWNDNEXT = 2
    Public Const GW_HWNDFIRST = 0
    Public WinText As String * 256
    Public Handler(200) As LongPublic Sub findrunningprogram()
        Dim RenHWND As Long
        Dim Count As Integer
        Dim RetValue As Long
        Dim WinTextLength As Long
          Count = 0
          Form1.List1.Clear
          RenHWND = GetWindow(Form1.hwnd, GW_HWNDFIRST)
          Do
           RetValue = GetWindowText(RenHWND, WinText, 256)
           If RetValue <> 0 Then
              WinTextLength = GetWindowTextLength(RenHWND)
              If Left$(WinText, WinTextLength) <> Form1.Caption And Left$(WinText, WinTextLength) <> App.Title Then
                Form1.List1.AddItem "[" & RenHWND & "]   " & WinText
                Handler(Count) = RenHWND
                Count = Count + 1
              End If
           End If
           RenHWND = GetWindow(RenHWND, GW_HWNDNEXT)
          Loop Until RenHWND = 0
          Form1.List1.ListIndex = 0
          Form1.Label2.Caption = "共(" & Count & "项)"
    End SubForm1中的代码:Private Sub Command1_Click()
        Dim RenValue As Long
        RetValue = SendMessage(Handler(Form1.List1.ListIndex), WM_CLOSE, 0, 0)
        If RetValue <> 0 Then
           MsgBox "清除不成功!"
           Call findrunningprogram
        Else
           MsgBox "清除成功!"
        End If
    End SubPrivate Sub Command2_Click()
        Call findrunningprogram
    End SubPrivate Sub Command3_Click()
    Unload Form1
    End
    End SubPrivate Sub Form_Load()
        Call findrunningprogram
    End Sub
    在Form1上放3个按钮,1个label2,1个list1
    运行,ok