c++ 女士 获取当前进程的pid

发布于 2024-07-09 05:26:22 字数 64 浏览 4 评论 0原文

我的应用程序的一部分是在 Windows 下用 C++ 编写的。 我需要当前进程的进程 ID。 有什么想法吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀子花开つ 2024-07-16 05:26:22

GetCurrentProcessId 函数会这样做。

The GetCurrentProcessId function will do this.

狼性发作 2024-07-16 05:26:22

我已经习惯了用大量的代码来完成看似简单的任务,因此我对 GetCurrentProcessId 的直接性感到惊喜。 今天早些时候,我观察了它在调试器中的运行情况,当时我正在跟踪 DllMain 例程中的一段新代码,该例程将进程 ID 与嵌入式 GUID 结合起来,为互斥体创建本地唯一的名称。

以下是整个例程,所有三个机器指令。

mov         eax,fs:[00000018]
mov         eax,dword ptr [eax+20h]
ret

顺便说一句,另外两个简单的 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 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.

好听的两个字的网名 2024-07-16 05:26:22

您可以使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文