永发信息网

VB 怎样通过进程名获取PID

答案:3  悬赏:0  手机版
解决时间 2021-01-31 01:55
  • 提问者网友:动次大次蹦擦擦
  • 2021-01-30 09:13
小弟刚学API,望前辈给代码时照顾到我,越简单越好!
最佳答案
  • 五星知识达人网友:举杯邀酒敬孤独
  • 2021-01-30 10:43
窗体上添加一个 text 一个按钮 只需在text中输入进程名单击一下按钮 就可以
只需要三个api
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private 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 * 1024
End Type

Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000

Dim pid As Long
Dim pname As String
Dim a As String
Private Sub Command1_Click()
a = Trim(LCase(Text1))
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
Dim flag As Boolean
Dim mName As String
Dim i As Integer

l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
End If
If (Process32First(l, my)) Then '遍历第一个进程
Do
i = InStr(1, my.szExeFile, Chr(0)) '返回chr(0)在各个进程中出现的位置
mName = LCase(Left(my.szExeFile, i - 1)) '返回小写的(返回i-1的前n个字符,即正确的名称)
If mName = a Then

pid = my.th32ProcessID

Text1 = Text1 & "的pid是 " & "---" & pid

End If
Loop Until (Process32Next(l, my) < 1)
End If
End Sub
全部回答
  • 1楼网友:白昼之月
  • 2021-01-30 13:05
窗体上添加一个 text 一个按钮command 只需在text中输入进程名 explorer.exe 单击一下按钮 就可以 获取该进程的pid 显示在text1上,要用到三个api: private declare function createtoolhelp32snapshot lib "kernel32" (byval dwflags as long, byval th32processid as long) as long private declare function process32first lib "kernel32" (byval hsnapshot as long, lppe as processentry32) as long private declare function process32next lib "kernel32" (byval hsnapshot as long, lppe as processentry32) as long private 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 * 1024 end type const th32cs_snapheaplist = &h1 const th32cs_snapprocess = &h2 const th32cs_snapthread = &h4 const th32cs_snapmodule = &h8 const th32cs_snapall = (th32cs_snapheaplist or th32cs_snapprocess or th32cs_snapthread or th32cs_snapmodule) const th32cs_inherit = &h80000000 dim pid as long dim pname as string dim a as string private sub command1_click() a = trim(lcase(text1)) dim my as processentry32 dim l as long dim l1 as long dim flag as boolean dim mname as string dim i as integer l = createtoolhelp32snapshot(th32cs_snapprocess, 0) if l then my.dwsize = 1060 end if if (process32first(l, my)) then '遍历第一个进程 do i = instr(1, my.szexefile, chr(0)) '返回chr(0)在各个进程中出现的位置 mname = lcase(left(my.szexefile, i - 1)) '返回小写的(返回i-1的前n个字符,即正确的名称) if mname = a then pid = my.th32processid text1 = text1 & "的pid是 " & "---" & pid end if loop until (process32next(l, my) < 1) end if end sub
  • 2楼网友:迷人又混蛋
  • 2021-01-30 12:00
'我来啦,API的,不用WMI,因为WMI的有的电脑不能用 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 TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long) Private Const TH32CS_SNAPPROCESS = &H2& Private 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 * 260 End Type Const PROCESS_TERMINATE = 1 Function GetPsPid(sProcess As String) As Long sProcess = LCase$(sProcess) Dim lSnapShot As Long, getp0 As String Dim lNextProcess As Long Dim tPE As PROCESSENTRY32 lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) If lSnapShot <> -1 Then tPE.dwSize = Len(tPE) lNextProcess = Process32First(lSnapShot, tPE) Do While lNextProcess getp0 = Left(tPE.szExeFile, InStr(tPE.szExeFile, Chr(0)) - 1) If sProcess = LCase$(getp0) Then GetPsPid = tPE.th32ProcessID Exit Function End If lNextProcess = Process32Next(lSnapShot, tPE) Loop CloseHandle (lSnapShot) End If End Function Private Sub Form_Load() Dim a As String a = "explorer.exe" '进程的名字 MsgBox GetPsPid(a) End Sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯