请问如何枚举系统中所有的进程名?

解决方案 »

  1.   

    真正的枚举进程的方法(VB)   http://www.hackerxfiles.net/archiver/?tid-81053.html
                             http://www.80diy.com/home/20051108/10/4378494.htmlVB 枚举隐藏进程     http://blog.csdn.net/chenhui530/archive/2007/10/09/1817052.aspx
      

  2.   

    chenjl1031第一个网址的可是可以,但在得到到进程名后面有一些像倒7字的字符,用Trim()函数去不掉,好象不是空格。
      

  3.   

    '*************************************************************************
    '**模 块 名:ModFindProcess
    '**说    明:进程相关操作
    '**创 建 人:马大哈 http://www.m5home.com/
    '**日    期:2006年3月18日
    '**日    期:2007年1月23日
    '**描    述:改进了结束进程的条件,可以根据PID来结束
    '**版    本:V1.3
    '*************************************************************************
    Option ExplicitPublic Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Public Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Public Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPublic Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
    Public Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
    Public Const TH32CS_SNAPPROCESS As Long = 2&
    Public Const PROCESS_TERMINATE = 1Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szexeFile As String * 260
    End TypePrivate Type MyProcess
        ExeName As String
        PID As Long
    End TypePublic Function CloseProcess(Optional ByVal ProName As String, Optional ByVal PID As Long) As Integer
        '传入进程名或PID,结束相应进程
        Dim tPID As Long
        Dim tPHwnd As Long
        Dim ProArr() As String, PIDArr() As Long
        Dim I As Long
        
        Call ListProcess(ProArr, PIDArr)
        For I = 1 To UBound(ProArr)
            If PIDArr(I) = PID Or ProArr(I) = ProName Then      '配对进程ID或进程名
                Exit For
            End If
        Next I
        
        If I > UBound(PIDArr) Then Exit Function
        tPID = PIDArr(I)
        
        tPHwnd = OpenProcess(PROCESS_TERMINATE, False, tPID)
        Debug.Print tPHwnd
        If tPHwnd Then
            CloseProcess = TerminateProcess(tPHwnd, 0)
        End If
    End FunctionPublic Function FindProcess(ByVal ProName As String, Optional ByRef PID As Long) As Boolean
        '传入进程名,如果进程存在,在PID里返回进程ID,函数返回True,否则返回Flase
        'ProName: 指定进程名
        'PID: 如果进程名存在,返回其PID
        '返回值: 进程名存在返回TRUE,否则返回FALSE
        Dim ProArr() As String, PIDArr() As Long
        Dim I As Long
        
        Call ListProcess(ProArr, PIDArr)
        For I = 1 To UBound(ProArr)
            If ProArr(I) = ProName Then
                PID = PIDArr(I)
                FindProcess = True
                Exit For
            End If
        Next I
    End FunctionPublic Function ListProcess(ByRef ProExeName() As String, ByRef ProPid() As Long)
        '列出进程以及相应PID
        'ProExeName(): 进程名
        'ProPid(): 相应的PID
        Dim MyProcess As PROCESSENTRY32
        Dim mySnapshot As Long
        Dim ProData() As MyProcess
        Dim I As Long
        
        ReDim ProData(0)
        
        MyProcess.dwSize = Len(MyProcess)
        mySnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        ProcessFirst mySnapshot, MyProcess
        
        ReDim Preserve ProData(UBound(ProData) + 1)
        
        ProData(UBound(ProData)).ExeName = Left(MyProcess.szexeFile, InStr(MyProcess.szexeFile, Chr(0)) - 1)
        ProData(UBound(ProData)).PID = MyProcess.th32ProcessID
        
        'Debug.Print ProData(UBound(ProData)).ExeName
        
        MyProcess.szexeFile = ""
        
        While ProcessNext(mySnapshot, MyProcess)
            ReDim Preserve ProData(UBound(ProData) + 1)
            
            ProData(UBound(ProData)).ExeName = Left(MyProcess.szexeFile, InStr(MyProcess.szexeFile, Chr(0)) - 1)
            ProData(UBound(ProData)).PID = MyProcess.th32ProcessID
            
        '    Debug.Print ProData(UBound(ProData)).ExeName
            
            MyProcess.szexeFile = ""
        Wend
        
        ReDim ProExeName(UBound(ProData))
        ReDim ProPid(UBound(ProData))
        
        For I = 1 To UBound(ProData)
            With ProData(I)
                ProExeName(I) = .ExeName
                ProPid(I) = .PID
            End With
        Next I
    End Function
      

  4.   

    Dim sArrProName() as string ,lArrPID() as longcall ListProcess(sArrProName,lArrPID)'sArrProName()里面就是所有的进程
    'lArrPID()里面就是对应的PID.
      

  5.   

    遇到用Trim()函数去不掉后面多余东西的时候,有一个最笨的办法,就是一个一个地取出来,比如:dim str as string,s as string
    dim Newstr as string       
    dim i as integer
    for i=1 to len(str)
         s=mid(str,i,1)
        if S<>"" then Newstr=Newstr & s
    next  以前,我用API函数GetWindowsDir得到Windows目录时出现过这样的情况,得到的目录是和你设置的缓冲区一样长(比如255),其实多余的都是空格,用Trim()函数就是去不掉。
    进程名后面那个不是倒7字的字符,是字符串结束符。
      

  6.   

    trim 修剪字符串前后的空格,而不会剪掉chr(0)
      

  7.   

    GetWindowsDir之类的API的返回值请注意一下.它返回的就是"复制到缓冲区里内容的长度".所以直接拿来再用LEFT或MID函数取一下就OK.所有字符串操作API,一般都有这种返回值的,用于标记复制了多少内容.我发的那个代码是用的找NULL的方式...大量使用时效率应该不如直接利用API返回值的好.