PDA

View Full Version : Determine the Process ID



Jimmy2775
29th October 2007, 20:26
Here's a (hopefully) quick question for you: how can I determine the process ID of my Qt application. I'm using Qt 4.2 on WindowsXP. I don't see any way to do this using Qt, so I'd expect there's some Windows-specific way to do it.

Thanks

Jimmy

Jimmy2775
29th October 2007, 20:38
Hmmm.... I suppose the Win32 function "GetCurrentProcessId()" might help... duh....

ChristianEhrlicher
31st October 2007, 06:41
Why not use QProcess::pid () (http://doc.trolltech.com/4.3/qprocess.html#pid) ?

marcel
31st October 2007, 07:31
Why not use QProcess::pid () (http://doc.trolltech.com/4.3/qprocess.html#pid) ?
This applies only for running instances of QProcess.

tiho_d
2nd August 2011, 16:13
May be for your current process you can also use the static function: qint64 QCoreApplication::applicationPid ()

kosasker
7th October 2011, 09:16
(For Windows Only)I was need to, and find solution. You can get other informations that i wrote below, from tlhelp32.h
It prints info about running process, not services!
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682489(v=vs.85).aspx



typedef struct tagPROCESSENTRY32W {
DWORD dwSize;
DWORD cntUsage;
DWORD th32ProcessID;
DWORD th32DefaultHeapID;
DWORD th32ModuleID;
DWORD cntThreads;
DWORD th32ParentProcessID;
LONG pcPriClassBase;
DWORD dwFlags;
WCHAR szExeFile[MAX_PATH];
} PROCESSENTRY32W,*PPROCESSENTRY32W,*LPPROCESSENTRY3 2W;




//in header
#include <tlhelp32.h>
#include <stdio.h>


//in Qt source
void MainWindow::getProcList()
{
BOOL bResult;
PROCESSENTRY32 processInfo = {sizeof(PROCESSENTRY32)};
HANDLE hSnapShot;
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
bResult = Process32First(hSnapShot, &processInfo);
while(bResult) {
wprintf(L"Name: %s - SysProcId: %d \n", processInfo.szExeFile,processInfo.th32ProcessID);
bResult = Process32Next(hSnapShot, &processInfo);
}
CloseHandle(hSnapShot);
}



Best Regards.