ms c++ 获取当前进程的pid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/298257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ms c++ get pid of current process
提问by Bogdan Gavril MSFT
Parts of my application are in C++ under windows. I need the process id for the current process. Any thoughts?
我的应用程序的一部分是在 Windows 下的 C++ 中。我需要当前进程的进程 ID。有什么想法吗?
回答by Greg Hewgill
The GetCurrentProcessId
function will do this.
该GetCurrentProcessId
函数将执行此操作。
回答by David A. Gray
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.
已经习惯于看到成堆的代码来完成看似简单的任务,我对GetCurrentProcessId
. 今天早些时候,我看到它在调试器中运行,当时我在一个DllMain
例程中跟踪一段新代码,该例程将进程 ID 与嵌入的 GUID 结合起来,为互斥锁创建本地唯一的名称。
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.
顺便说一句,另外两个幸运的简单 Windows API 函数是GetLastError
和SetLastError
; 事实上,两者都与这个非常相似。