Get Executable Path of Window by Handle - Access D

2019-06-06 17:18发布

I am trying to get Executable Path of Window by Handle.
I am using the following code to achieve:

[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr handle, out uint processId);

public string GetFilePath(IntPtr hwnd)
{
    try
    {
        uint pid = 0;
        GetWindowThreadProcessId(hwnd, out pid);
        Process proc = Process.GetProcessById((int)pid); //Gets the process by ID.
        return proc.MainModule.FileName.ToString();   //Returns the path.
    }
    catch (Exception ex)
    {
        return ex.ToString();
    }
}

and it works fine, except for some applications (such as TeamSpeak 3 64bit [if it matters]).

How can I overcome the Access Denied problem programmaticly?

I tried to use the following aswell but same problem:
Get a executable path from a window handle?
Access denied while getting process path
How to Get Elevated Process Path in .NET

0条回答
登录 后发表回答