[VB6.0] 如何利用程序名称获取该程序的PID值
如题.我想利用我所知道的程序名称来获取该程序的PID值.如:我知道这个程序的名称是Explorer.exe我需要怎么才知道他的PID值,除了手动的查找外,还有没有其他的方...
如题. 我想利用我所知道的程序名称来获取该程序的PID值. 如: 我知道这个程序的名称是 Explorer.exe 我需要怎么才知道他的PID值,除了手动的查找外,还有没有其他的方法.
展开
2013-04-23
展开全部
在dos下可以这样 tasklist /FI "IMAGENAME eq explorer.exe" 基于此可以'给你个简单方便的程序?
'创建如下控件:
'Command1,Text1,Timer1.
'单击按钮,稍候片刻,在Text2中显示结果。
'===========================
Const p = "d:\a.txt"
Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 1000
Text1.Text = ""
Dim t As String
Shell "cmd /c echo. >""d:\a.txt""" '创建空文件
Shell "cmd /c" & "tasklist /FI ""IMAGENAME eq explorer.exe""" & ">" & p '读取任务PID
End SubPrivate Sub Form_Load()
Timer1.Enabled = FalseEnd SubPrivate Sub Timer1_Timer() '读取文件
Open p For Input As #1
Do While EOF(1) = False
Line Input #1, t
SS = SS + Trim(t)
Loop
Close #1
Text1.Text = "PID=" & Split(Split(SS, "explorer.exe")(1), "Console")(0) ‘提取字段
Timer1.Enabled = False
End Sub
'创建如下控件:
'Command1,Text1,Timer1.
'单击按钮,稍候片刻,在Text2中显示结果。
'===========================
Const p = "d:\a.txt"
Private Sub Command1_Click()
Timer1.Enabled = True
Timer1.Interval = 1000
Text1.Text = ""
Dim t As String
Shell "cmd /c echo. >""d:\a.txt""" '创建空文件
Shell "cmd /c" & "tasklist /FI ""IMAGENAME eq explorer.exe""" & ">" & p '读取任务PID
End SubPrivate Sub Form_Load()
Timer1.Enabled = FalseEnd SubPrivate Sub Timer1_Timer() '读取文件
Open p For Input As #1
Do While EOF(1) = False
Line Input #1, t
SS = SS + Trim(t)
Loop
Close #1
Text1.Text = "PID=" & Split(Split(SS, "explorer.exe")(1), "Console")(0) ‘提取字段
Timer1.Enabled = False
End Sub
2013-04-23
展开全部
VB做的话,代码比较凌乱、冗长,没有美感。。。不过还是给你贴出来吧。------------------
Option Explicit
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
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) 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
Dim lSnapShot As Long
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
If LCase$(sProcess) = LCase$(Left(tPE.szExeFile, InStr(1, tPE.szExeFile, Chr(0)) - 1)) Then
Dim lProcess As Long
Dim lExitCode As Long
GetPsPid = tPE.th32ProcessIDCloseHandle lProcess
End If
lNextProcess = Process32Next(lSnapShot, tPE)
Loop
CloseHandle (lSnapShot)
End If
End Function调用GetPsPid("进程名")在form1窗体是加入按钮控件和编辑框控件然后计入代码运行输入进程名然后就可以看到pid了。Private Sub Command1_Click()
Print GetPsPid(Text1.Text)
End Sub
Option Explicit
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
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) 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
Dim lSnapShot As Long
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
If LCase$(sProcess) = LCase$(Left(tPE.szExeFile, InStr(1, tPE.szExeFile, Chr(0)) - 1)) Then
Dim lProcess As Long
Dim lExitCode As Long
GetPsPid = tPE.th32ProcessIDCloseHandle lProcess
End If
lNextProcess = Process32Next(lSnapShot, tPE)
Loop
CloseHandle (lSnapShot)
End If
End Function调用GetPsPid("进程名")在form1窗体是加入按钮控件和编辑框控件然后计入代码运行输入进程名然后就可以看到pid了。Private Sub Command1_Click()
Print GetPsPid(Text1.Text)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询