我要做一个程序, 需要用过进程名称查找PID, 就像tasklist一样的效果但是网上查了很长时间, VB好像没有专门的函数, 也没有人写过这种代码.郁闷那位高手可以帮助下?

解决方案 »

  1.   

    EnumProcesses + GetModuleFileNameExA即可完成
      

  2.   

    【CBM666 枚举运行中的EXE路径与PID】2007年07月21日 星期六 14:26'添加 List1Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Const GW_HWNDNEXT = 2
    '***************************************************************
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Const MAXLEN = 255
    '*****************************************************************************
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Const SW_SHOWNORMAL = 1
    '***************************************************************
    Dim aa$, pidhwnd&, jj%
    Dim objWMIService, objProcess, colProcess
    Dim strComputer, strList
    Private Sub Form_Load()
         List1.Move 0, 0, 10000, 4000
         Me.Width = List1.Width + 120: Me.Height = List1.Height + 405
         Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2
         Call showalljc
    End SubPrivate Sub showalljc()
         strComputer = "."
         Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
         Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process")
         List1.Clear
         For Each objProcess In colProcess
            strList = objProcess.Name
            pidhwnd = InstanceToWnd(objProcess.ProcessID)
            aa = IIf(getclassnm(pidhwnd) <> "", "类名:" & getclassnm(pidhwnd), "")
            List1.AddItem strList & Space(2) & objProcess.ProcessID & Space(2) & Trim(Str(pidhwnd)) & Space(2) & objProcess.ExecutablePath & Space(2) & GetCaptionFromHwnd(pidhwnd) & Space(2) & aa
         Next
         Set objWMIService = Nothing
         Set colProcess = Nothing
    End SubFunction InstanceToWnd(ByVal target_pid As Long) As Long
         Dim test_hwnd&, test_pid&, test_thread_id&
         test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
         Do While test_hwnd <> 0
            If GetParent(test_hwnd) = 0 Then
               test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
               If test_pid = target_pid Then
                  InstanceToWnd = test_hwnd
                  Exit Do
               End If
            End If
            test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
         Loop
    End FunctionPrivate Function GetCaptionFromHwnd(hwnd As Long) As String
         'Check1 Option1 Combo1 Text1 Command1 Drive1 Data1 这些控件可以被检测到标题
         Dim strBuffer$, intCount%
         strBuffer = String$(MAXLEN - 1, 0)
         intCount = GetWindowText(hwnd, strBuffer, MAXLEN)
         If intCount > 0 Then
            jj = InStr(strBuffer, Chr(0))
            GetCaptionFromHwnd = Mid(strBuffer, 1, jj - 1)
         End If
    End FunctionFunction getclassnm(WinWnd As Long) As String
         Dim Ret$, RetVal&, lpClassName$
         'ShowWindow WinWnd, SW_SHOWNORMAL
         lpClassName = Space(256)
         RetVal = GetClassName(WinWnd, lpClassName, 256)
         'MsgBox "Class名称" & Left$(lpClassName, RetVal)
         getclassnm = Left$(lpClassName, RetVal)
    End Function