c++ 女士 获取当前进程的pid
我的应用程序的一部分是在 Windows 下用 C++ 编写的。 我需要当前进程的进程 ID。 有什么想法吗?
Parts of my application are in C++ under windows. I need the process id for the current process. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
GetCurrentProcessId
函数会这样做。The
GetCurrentProcessId
function will do this.我已经习惯了用大量的代码来完成看似简单的任务,因此我对
GetCurrentProcessId
的直接性感到惊喜。 今天早些时候,我观察了它在调试器中的运行情况,当时我正在跟踪DllMain
例程中的一段新代码,该例程将进程 ID 与嵌入式 GUID 结合起来,为互斥体创建本地唯一的名称。以下是整个例程,所有三个机器指令。
顺便说一句,另外两个简单的 Windows API 函数是 GetLastError 和 SetLastError; 事实上,两者都与此非常相似。
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 aDllMain
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.
BTW, two other blessedly simple Windows API functions are
GetLastError
andSetLastError
; indeed, both are quite similar to this one.您可以使用
getpid()
或_getpid()
,它们在
库中定义。https://learn。 microsoft.com/en-us/cpp/c-runtime-library/reference/getpid?view=vs-2019
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