无法从 GetProcessId(.. hWnd) (pInvoke) 中提取 processID

发布于 2024-08-01 18:56:22 字数 605 浏览 7 评论 0原文

我使用以下方法

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

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

谁能看看我是否做错了什么?

注意:该进程是从暴露给框架的自动化对象生成的,并且仅提供 HWND 属性。 也许还有另一种方法来获取 processID,因为我编写的代码首先负责运行它?

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

自动化应用程序.应用程序 extApp = 新 AutomationApplication.Application(); extApp.Run(); ...

im using the following method

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

to try and get the processId for a running process and the only information I have is the HWND. My problem is that it is always returning error code 6 which is ERROR_INVALID_HANDLE. I thought i might change the parameter to be of type int but that also didnt work. Im not able to enumerate the running processes because there may be more than 1 instance running at any one time.

Can anyone see if i am doing anything wrong?

NB: The process is spawned from an automation object exposed to the framework and only provides the HWND property. Perhaps there is another way to get the processID seeing as the code i wrote was responsible for running it in the first place?

My code looks something similar to this...

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

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

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

发布评论

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

评论(3

你另情深 2024-08-08 18:56:22

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 放入输出参数中。

GetProcessId gets the process ID when given a process handle, not a window handle. It's actually:

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

If you've got a window handle, then you want the GetWindowThreadProcessId function:

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

This returns the thread id, and puts the process id in the out-param.

我做我的改变 2024-08-08 18:56:22

什么是“AutomationApplication.Application”类? 那是你写的吗? .Run() 返回 PID 吗?

What is the 'AutomationApplication.Application' class? Did you write that? Does .Run() return a PID?

╰つ倒转 2024-08-08 18:56:22

请参阅 Pinvoke 上的示例,无需 WIN32 调用,因为您可以使用托管 API:

替代托管 API:
System.Diagnostics.Process 类包含许多模块、进程和线程方法。

例如:

using System.Diagnostics;
...
private void DumpModuleInfo(IntPtr hProcess)
{
    uint pid = GetProcessId(hProcess);
    foreach (Process proc in Process.GetProcesses())
    {
        if (proc.Id == pid)
        {
            foreach (ProcessModule pm in proc.Modules)
            {
                Console.WriteLine("{0:X8} {1:X8} {2:X8} {3}", (int)pm.BaseAddress,
                (int)pm.EntryPointAddress, (int)pm.BaseAddress + pm.ModuleMemorySize, pm.ModuleName);
            }
        }
    }
}

See an example on Pinvoke, no need for the WIN32 call, as you can use a managed API :

Alternative Managed API:
System.Diagnostics.Process class contains many module, process, and thread methods.

For example:

using System.Diagnostics;
...
private void DumpModuleInfo(IntPtr hProcess)
{
    uint pid = GetProcessId(hProcess);
    foreach (Process proc in Process.GetProcesses())
    {
        if (proc.Id == pid)
        {
            foreach (ProcessModule pm in proc.Modules)
            {
                Console.WriteLine("{0:X8} {1:X8} {2:X8} {3}", (int)pm.BaseAddress,
                (int)pm.EntryPointAddress, (int)pm.BaseAddress + pm.ModuleMemorySize, pm.ModuleName);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文