c++中怎样根据进程名得到进程pid

c++中怎样根据进程名得到进程pid,第1张

HANDLE hSnapShot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0 );

PROCESSENTRY32 procentry=sizeof(PROCESSENTRY32);

BOOL bFlag=Process32First( hSnapShot, &procentry ) ;

while( bFlag )

{

if(stricmp(procentryszExeFile, "你的程序名 ")==0)

processid=procentryth32ProcessID; //找到

bFlag=Process32Next(hSnapShot,&procentry);

}

注意一个程序可能有几个实例同时运行,所以可能同时找到几个ID。

在Android中取得当前进程

说明:上面代码关键的两个地方解释,也就是取得当前进程名

androidosProcessmyPid()) //获取PID,目前获取自己的也只有该API

ActivityManagerRunningAppProcessInfo类

说明: 封装了正在运行的进程信息

常用字段:

int pid 进程ID

int uid 进程所在的用户ID

String processName 进程名,默认是包名或者由Android:process=””属性指定

String [ ] pkgList 运行在该进程下的所有应用程序包名

通过linux命令来获取进程ID范例代码:

Process psProcess = RuntimegetRuntime()exec("sh");

DataOutputStream out = new DataOutputStream( psProcessgetOutputStream() );

InputStream is = psProcessgetInputStream();

outwriteBytes( "ps | grep '***loader' | cut -c 10-14\n" );

outwriteBytes( "ps\n" );

outflush();

try {

    psProcesswaitFor();

     } catch (InterruptedException e) {

                 // TODO Auto-generated catch block

                 eprintStackTrace();}

  

if(isread() != 0)

{

   byte firstByte = (byte)isread();  

   int available = isavailable();

   byte[] characters = new byte[available + 1];

   characters[0] = firstByte;

   isread( characters, 1, available );

   String re = new String( characters );

窗体上添加一个 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

mydwSize = 1060

End If

If (Process32First(l, my)) Then '遍历第一个进程

Do

i = InStr(1, myszExeFile, Chr(0)) '返回chr(0)在各个进程中出现的位置

mName = LCase(Left(myszExeFile, i - 1)) '返回小写的(返回i-1的前n个字符,即正确的名称)

If mName = a Then

pid = myth32ProcessID

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

End If

Loop Until (Process32Next(l, my) < 1)

End If

End Sub

我现在有一个窗口句柄(HWND),那如何获取该窗口的进程ID呢

需要什么函数,GetCurrentProcessId()是获取当前的进程ID,但是我想通过那个窗口的句柄获取那个窗口的进程,怎么弄

handle=getwindowthreadprocessid(hwnd)

就用GetWindowThreadProcessId,这个函数是获取指定窗口的线程ID和进程ID

C/C++ code

HWND hWnd; // 窗口的句柄DWORD dwPID;DWORD dwThreadID = ::GetWindowThreadProcessId(hWnd, &dwPID);

执行成功后,dwPID便是获取到的进程ID

358 icePub_getProcessInfo

l 函数原型:

int WINAPI icePub_getProcessInfo(char strInfoList ,int maxLen)

输入:maxLen strInfoList最大长度

输出:strInfoList 进程基本信息,name|pid|path,一行一条信息

VB sample 代码:

Private Declare Function icePub_getProcessInfo Lib "icePubDlldll" (ByVal strInfoList As String, ByVal maxLen As Integer) As Integer

Dim a2 As Integer

DIm buff As String

buff=space(102430+1)

a2=icePub_getProcessInfo(buff,102430)

MsgBox buff

>

以上就是关于c++中怎样根据进程名得到进程pid全部的内容,包括:c++中怎样根据进程名得到进程pid、如何获取当前应用app所在的进程id和进程名称、Android怎么获取进程Id等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9593537.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存