Parts of my application are in C++ under windows. I need the process id for the current process. Any thoughts?
-
4I first read it as "get rid of current process" :) – Otherside Nov 18 '08 at 10:52
-
@Otherside made me look in vocabulary – SerG Feb 05 '15 at 11:45
3 Answers
The GetCurrentProcessId
function will do this.

- 951,095
- 183
- 1,149
- 1,285
-
There is also `_getpid()` - http://msdn.microsoft.com/en-us/library/t2y34y40%28v=vs.80%29.aspx – user93353 Nov 15 '12 at 14:13
-
1`_getpid()` just calls `GetCurrentProcessId()`. There is only one process id, and the lowest level function that returns it is `GetCurrentProcessId()`. – Greg Hewgill Nov 15 '12 at 18:37
Having grown accustomed to seeing yards and yards of code to accomplish seemingly straightforward tasks, I was pleasantly surprised at the directness of GetCurrentProcessId
. Earlier today, I watched it run in a debugger, when I was following a new bit of code in a DllMain
routine that combines the process ID with an embedded GUID to create a locally unique name for a mutex.
Following is the entire routine, all three machine instructions.
mov eax,fs:[00000018]
mov eax,dword ptr [eax+20h]
ret
BTW, two other blessedly simple Windows API functions are GetLastError
and SetLastError
; indeed, both are quite similar to this one.

- 1,039
- 12
- 19
-
I forgot to mention in that "short, sweet" group is GetProcessHeap. – David A. Gray Jun 09 '15 at 07:10
You can use getpid()
or _getpid()
, which are defined in <process.h>
library.
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getpid?view=vs-2019

- 375
- 3
- 7
-
As Greg Hewgill already mentioned under the accepted answer, `_getpid()` just calls `GetCurrentProcessId()`, which is the lowest level function that returns the process id. – fcdt Sep 06 '20 at 18:40
-
1Actually before I write this answer, I tried `GetCurrentProcessId()` but I couldn't run it, because I had not included `
` library, which was not mentioned in the previous answer. After some tries I got it. – Muhammed_G Sep 07 '20 at 19:36