PDA

View Full Version : Portable way to obtain process ID number on Windows and Linux



eliben
10th October 2011, 12:24
QProcess::pid returns Q_PID which is an OS-dependend typedef:



#if (!defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)) || defined(qdoc)
typedef qint64 Q_PID;
#else
QT_END_NAMESPACE
typedef struct _PROCESS_INFORMATION *Q_PID;
QT_BEGIN_NAMESPACE
#endif

Is there a built-in way in Qt to obtain just the process ID number of a process, without resorting to OS-specific code? I came up with this utility function, but I'm surprised I can't find it in Qt itself:



quint64 getProcessID(const QProcess* proc)
{
#ifdef Q_WS_WIN
struct _PROCESS_INFORMATION* procinfo = proc->pid();
return procinfo->dwProcessId;
#else // Linux
return proc->pid();
#endif // Q_WS_WIN
}