我希望能由进程名获得该进程的PID,在网上查过了
像这个http://zhidao.baidu.com/question/58342775.html
刚开始用是可以的,但是生成EXE时
在这个位置:
If LCase$(sProcess) = LCase$(Left(tPE.szExeFile, InStr(1, tPE.szExeFile, Chr(0)) - 1)) Then
就是LEFT那里提示数组未定义,咋办呀
生成的EXE启动后提示下标溢出
ps:我用的是VB精简版,就是十几M的那种,有关系吗?谁能帮我解决这个问题呀??
万分感谢!!

解决方案 »

  1.   

    Option Explicit
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
    Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
    Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As LongPrivate Const PROCESS_TERMINATE As Long = (&H1)
    Private Const MAX_PATH As Integer = 260Private Const TH32CS_SNAPPROCESS = &H2
    Private Const TH32CS_SNAPTHREAD = &H4
    Private Const TH32CS_SNAPMODULE As Long = &H8Private Type 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 * MAX_PATH
    End TypePrivate Sub Command1_Click()
        Dim ProNum As Long, PidData() As Long, hProcess As Long, i As Long
        ProNum = FindPIDByName("notepad.exe", PidData()) '查找记事本的句柄,为了测试方便请只开一个notepad.exe进程
        If ProNum = 0 Then Exit Sub
        For i = 0 To ProNum - 1
            MsgBox PidData(i)
        Next
    End SubPrivate Function FindPIDByName(ProName As String, PidData() As Long) As Long '返回符合条件的进程ID,保存到数组PidData里
        Dim hProSnap As Long, hProID As Long, i As Long
        Dim ProInfo As PROCESSENTRY32
        ProName = LCase$(ProName)
        
        ProInfo.dwSize = Len(ProInfo)
        hProSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
        hProID = Process32First(hProSnap, ProInfo)
        While hProID <> 0
            If LCase$(Left(ProInfo.szExeFile, InStr(1, ProInfo.szExeFile, vbNullChar) - 1)) = ProName Then
                ReDim Preserve PidData(i)
                PidData(i) = ProInfo.th32ProcessID
                i = i + 1
            End If
            hProID = Process32Next(hProSnap, ProInfo)
        Wend
        FindPIDByName = i
        CloseHandle hProSnap
    End Functionhttp://topic.csdn.net/u/20080417/21/78fe7086-a61f-4fa9-ac12-c04386f44472.html