1

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

Community
  • 1
  • 1
Ron
  • 3,975
  • 17
  • 80
  • 130
  • Start by changing a setting so your program runs as a 64-bit process. Project + Properties, Build tab, Platform target = AnyCPU (untick Prefer 32-bit if you see it). A process otherwise can prevent you from messing with it so this is no slamdunk. – Hans Passant Dec 14 '13 at 14:10
  • @HansPassant Platform target was on AnyCPU (its the default). I tried all the options (x86, x64, AnyCPU) but I still get the same Access Denied on some apps. Anyway I cant see how changing the Plaform target should solve it – Ron Dec 14 '13 at 15:00
  • 1
    You are running elevated? And in the same session as the target process? – David Heffernan Dec 14 '13 at 15:40
  • Ok, Apperantly the elevated did work. I used the wrong ProcessAccessFlags and thats why it didnt work. – Ron Dec 14 '13 at 15:47
  • @Ron I am facing same issue . Could you please explain in details what was the fix of this? – Muktesh Kumar Aug 28 '17 at 03:12

0 Answers0