ms c++ get pid of current process

后端 未结 3 1171
孤独总比滥情好
孤独总比滥情好 2020-12-11 00:00

Parts of my application are in C++ under windows. I need the process id for the current process. Any thoughts?

相关标签:
3条回答
  • 2020-12-11 00:21

    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.

    0 讨论(0)
  • 2020-12-11 00:25

    You can use getpid() or _getpid() , which are defined in <process.h> library.

    https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/getpid?view=vs-2019

    0 讨论(0)
  • 2020-12-11 00:27

    The GetCurrentProcessId function will do this.

    0 讨论(0)
提交回复
热议问题