Access denied while getting process path

前端 未结 2 980
南笙
南笙 2020-12-01 22:14

I am trying to get process path by pid but I\'m getting Win32Exception (access id denied).

The code looks like this:

string path = Process.GetProcessBy         


        
2条回答
  •  渐次进展
    2020-12-01 22:50

    Finally I managed to solve it. As it turned out there is new function in Vista and above for getting process path and new process access (PROCESS_QUERY_LIMITED_INFORMATION):

    QueryFullProcessImageName

    Here is the code that works from non-elevated process:

        private static string GetExecutablePathAboveVista(UIntPtr dwProcessId)
        {
            StringBuilder buffer = new StringBuilder(1024);
            IntPtr hprocess = OpenProcess(ProcessAccessFlags.PROCESS_QUERY_LIMITED_INFORMATION, false, dwProcessId);
            if (hprocess != IntPtr.Zero)
            {
                try
                {
                    int size = buffer.Capacity;
                    if (QueryFullProcessImageName(hprocess, 0, buff, out size))
                    {
                        return buffer.ToString();
                    }
                }
                finally
                {
                    CloseHandle(hprocess);
                }
            }
            return string.Empty;
        }
    

提交回复
热议问题