gpt4 book ai didi

c# - 无法从 GetProcessId(.. hWnd) (pInvoke) 中提取进程 ID

转载 作者:行者123 更新时间:2023-11-30 19:08:13 28 4
gpt4 key购买 nike

我正在使用下面的方法

    [DllImport("kernel32.dll", SetLastError=true)]
static extern int GetProcessId(IntPtr hWnd);

尝试获取正在运行的进程的 processId,而我拥有的唯一信息是 HWND。我的问题是它总是返回错误代码 6,即 ERROR_INVALID_HANDLE。我想我可能会将参数更改为 int 类型,但这也没有用。我无法枚举正在运行的进程,因为任何时候可能有超过 1 个实例在运行。

谁能看出我做错了什么?

注意:该进程是从暴露给框架的自动化对象产生的,并且只提供 HWND 属性。也许还有另一种方法可以让进程 ID 看到,因为我编写的代码首先负责运行它?

我的代码看起来与此类似...

AutomationApplication.Application extApp = new AutomationApplication.Application(); extApp.Run(); ...

最佳答案

GetProcessId在给定进程句柄而不是窗口句柄时获取进程 ID。它实际上是:

[DllImport("kernel32", SetLastError = true)]
static extern int GetProcessId(IntPtr hProcess);

如果您有一个窗口句柄,那么您需要 GetWindowThreadProcessId功能:

[DllImport("user32")]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int processId);

这将返回线程 ID,并将进程 ID 放入输出参数中。

关于c# - 无法从 GetProcessId(.. hWnd) (pInvoke) 中提取进程 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1283564/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com