C# 通过窗口句柄查找进程ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18184654/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:29:47  来源:igfitidea点击:

Find process id by window's handle

c#

提问by Amit Shadadi

i have a problem with getting a specific PID of a process, the problem with this process is that it's a hidden process, it's not showing on task manager / powershell, completely hidden.

我在获取进程的特定 PID 时遇到问题,此进程的问题在于它是一个隐藏进程,它没有显示在任务管理器/powershell 上,完全隐藏。

what i have do far is the main window handle of this process, the question is, how can i get the pid of it.

我所做的是这个进程的主窗口句柄,问题是,我怎样才能得到它的pid。

what i'm trying to do is to read the memory of this process and edit it, but can't do so without the PID i guess (since i need to get it's base address in memory).

我想要做的是读取这个进程的内存并对其进行编辑,但是如果没有 PID 我猜是不能这样做的(因为我需要在内存中获取它的基地址)。

So, if anyone has any workaround or something for me, it will be great.

所以,如果有人有任何解决方法或对我有用的东西,那就太好了。

P.S: this process does not show in Process.GetProcesses().

PS:这个过程没有显示在Process.GetProcesses()中。

ty!

泰!

采纳答案by Lloyd

You can use the following Windows API:

您可以使用以下 Windows API:

[DllImport("user32.dll", SetLastError=true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);

You pass in the HWND and use the out parameter to return the PID.

您传入 HWND 并使用 out 参数返回 PID。

You can read more on this function here on MSDN.

您可以在 MSDN上阅读有关此功能的更多信息。

回答by bash.d

You will need to use P/invoke with the Windows API.

您将需要将 P/invoke 与 Windows API 一起使用。

Declare a function in your class like

在类中声明一个函数,例如

 [DllImport("User32.dll")]
 static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

and then call it in your class.

然后在你的课堂上调用它。

See PInvoke.

请参阅PInvoke