没事做,写个详细代码给你吧
#include<windowsh>
#include<tlhelp32h>//声明快照函数的头文件
int main(int argc,char argv[])
{
PROCESSENTRY32 pe32;
//在使用这个结构之前,先设置它的大小
pe32dwSize=sizeof(pe32);
//给系统内的所有进程拍一个快照
HANDLE hProcessSnap=::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
//遍历进程快照,轮流显示每个进程的信息
BOOL bMore=::Process32First(hProcessSnap,&pe32);
while(bMore)
{
if(strcmp("abcexe",pe32szExeFile)==0)//如果找到进程名为abcexe
{
HANDLE hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pe32th32ProcessID);//获取句柄
/这里已经打开那个进程的句柄了/ }
bMore=::Process32Next(hProcessSnap,&pe32);//寻找下一个
}
return 0;
}
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 EnumProcesses Lib "PSAPIDLL" (lpidProcess As Long, ByVal cb As Long, cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameEx Lib "PSAPIDLL" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "PSAPIDLL" (ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, lpcbNeeded As Long) As Long
Const PROCESS_QUERY_INFORMATION = &H400
Const PROCESS_VM_READ = &H10
Const MAX_PATH As Long = 260
Private Sub Command1_Click()
Dim szProcessName As String
Dim aProcesses() As Long
Dim hMod(0 To 1023) As Long
Dim hProcess As Long
Dim lLen As Long
Dim cbNeeded As Long
Dim cProcesses As Long
Dim lProcessID As Long
Dim K
Dim i As Long
ReDim aProcesses(0 To 1023) As Long
K = List1ListIndex
List1Clear
If (EnumProcesses(aProcesses(0), 1024 4, cbNeeded) <> 0) Then
cProcesses = cbNeeded / 4
For i = 0 To cProcesses - 1
lProcessID = aProcesses(i)
lLen = MAX_PATH
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lProcessID)
If (lProcessID = 0) Then
List1AddItem "System Idle Process"
ElseIf lProcessID < 5 Then
List1AddItem "System"
Else
If (hProcess <> 0) Then
If (EnumProcessModules(hProcess, hMod(0), 1024 4, cbNeeded)) Then
szProcessName = String$(lLen, 0)
LSet szProcessName = "unknown"
GetModuleFileNameEx hProcess, hMod(0), szProcessName, lLen
List1AddItem szProcessName
End If
End If
End If
Next i
If K < 2 Or K > List1ListCount - 1 Then K = List1ListCount - 1
List1ListIndex = K
Else
MsgBox "Failed to get process list", vbCritical
End If
End Sub
安装第三方库
pip install psutil使用如下,假设进程号为3213
import psutilproc = psutilProcess(pid=3213)
print procename()
tasklist似乎显示不了服务的captionname,这次换wmic吧:
@echo off
color 2
:S
tasklist/svc
set /p I=映像名:
for /f "tokens=2 skip=1 delims== usebackq" %%a in (`wmic service where "pathname like '%%\\%I%'" get caption /value`) do @(
echo %i%服务的显示名称为 %%a
pause>nul
)
goto S
版本 2
子程序 取进程名, 文本型, 公开, 通进进程ID取得该进程文件名
参数 进程ID, 整数型
局部变量 快照句柄, 整数型
局部变量 模块信息, 模块信息型
局部变量 文件名, 文本型
快照句柄 = _创建快照 (8, 进程ID)
如果真 (快照句柄 = -1)
返回 (“”)
如果真结束
模块信息size = 1024
_第一个模块 (快照句柄, 模块信息)
文件名 = 到文本 (模块信息模块文件名)
_关闭对象 (快照句柄)
返回 (文件名)
以上就是关于C++ 如何获取指定 进程名 的 进程ID 如:获取进程列表中 360tray.exe 的进程ID···全部的内容,包括:C++ 如何获取指定 进程名 的 进程ID 如:获取进程列表中 360tray.exe 的进程ID···、怎样用VB获取当前系统进程名 要求:、python中怎么根据进程号获取进程名等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)