Unable to extract processID from GetProcessId(.. hWnd) (pInvoke)

孤街醉人 提交于 2019-12-13 15:25:12

问题


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(); ...


回答1:


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




回答2:


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.




回答3:


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);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/1283564/unable-to-extract-processid-from-getprocessid-hwnd-pinvoke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!