How to Get Elevated Process Path in .NET
As you know, retrieving process path in .NET is quite straightforward. For example, if you have process instance someProcess
you can get the process path by accessing someProcess.MainModule.FileName
. However, for elevated processes, the previous code snippet throws Win32Exception
.
As you have probably guessed, the reason is related to the bugs I mentioned in my previous post. In this case too, the Process
class is using PROCESS_QUERY_INFORMATION
access right to open the desired process. The difference from the previous case is that in order to get the main module and process path, the code really needs PROCESS_QUERY_INFORMATION
so we cannot simply change it with PROCESS_QUERY_LIMITED_INFORMATION
.
Fortunately, Microsoft has introduced a new function for getting process path QueryFullProcessImageName which does work with PROCESS_QUERY_LIMITED_INFORMATION
access right. To call the function, we need to open the process ourselves and pass the handle.
private static string GetExecutablePath(Process Process)
{
//If running on Vista or later use the new function
if (Environment.OSVersion.Version.Major >= 6)
{
return GetExecutablePathAboveVista(Process.Id);
}
return Process.MainModule.FileName;
}
private static string GetExecutablePathAboveVista(int ProcessId)
{
var buffer = new StringBuilder(1024);
IntPtr hprocess = OpenProcess(ProcessAccessFlags.PROCESS_QUERY_LIMITED_INFORMATION,
false, ProcessId);
if (hprocess != IntPtr.Zero)
{
try
{
int size = buffer.Capacity;
if (QueryFullProcessImageName(hprocess, 0, buffer, out size))
{
return buffer.ToString();
}
}
finally
{
CloseHandle(hprocess);
}
}
throw new Win32Exception(Marshal.GetLastWin32Error());
}
[DllImport("kernel32.dll")]
private static extern bool QueryFullProcessImageName(IntPtr hprocess, int dwFlags,
StringBuilder lpExeName, out int size);
[DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess,
bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hHandle);
You can turn the above code snippet into an extension method too. If you want Microsoft to add a new property for easily getting the process path, vote for this suggestion at the connect website: Add Path property to Process class so that it works with elevated processes too.
发表评论
yXVSwF You made some decent points there. I looked on the net for more information about the issue and found most people will go along with your views on this website.
w4D8ex Wow! This could be one particular of the most beneficial blogs We ave ever arrive across on this subject. Basically Wonderful. I am also a specialist in this topic so I can understand your hard work.
You have brought up a very wonderful details , thanks for the post.
Some really select articles on this internet site , saved to favorites .
Wow! Thank you! I always wanted to write on my website something like that. Can I take a fragment of your post to my site?
aZHN4D Thanks, I have recently been searching for facts about this subject for ages and yours is the best I ave found so far.
bWJVEc Wow! Thank you! I permanently needed to write on my site something like that. Can I implement a part of your post to my blog?
dhtdnd You are my aspiration , I have few blogs and often run out from to post.
FAQnST thanks so much.It make me feel better. I can improve my E and have opportunities in my job
kwAe2L ppi claims ireland How can u add a flash slide show to your blogspot?
s7usQi It as not that I want to replicate your web-site, but I really like the layout. Could you tell me which theme are you using? Or was it especially designed?
Ap9rwD If you are free to watch funny videos online then I suggest you to pay a visit this site, it includes really so comic not only movies but also extra information.
YW1Gp2 This is one awesome post.Much thanks again. Fantastic.
iMQH89 Really appreciate you sharing this blog post.Much thanks again. Cool.
NQmyPt U never get what u expect u only get what u inspect
Magnificent site. A lot of helpful information here. I'аm sending it to several friends ans also sharing in delicious. And obviously, thanks for your effort!
bslCYQ This is a topic that as near to my heart Thank you! Exactly where are your contact details though?
cYyg7l Perfectly pent written content, Really enjoyed looking at.
YzxXR7
71IDWP Some really superb blog posts on this website , thankyou for contribution.
APuVDY Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is fantastic, as well as the content!
zF4p4s Really informative blog post.Thanks Again. Fantastic.
qh8xna Great, thanks for sharing this blog post. Keep writing.
xDFhz7 I think this is a real great blog post.Much thanks again. Keep writing.
wsCFl9 Real clean web site, appreciate it for this post.
10mD7v Thanks so much for the blog article. Much obliged.
ATDdnJ Great post.Really thank you! Will read on...
1ezgmy I think other web-site proprietors should take this web site as an model, very clean and magnificent user genial style and design, as well as the content. You're an expert in this topic!
RJpLiR F*ckin' awesome things here. I'm very glad to see your article. Thanks a lot and i am looking forward to contact you. Will you kindly drop me a mail?
bf9Jz0 You made some good points there. I looked on the internet for the subject and found most people will approve with your website.
rdOPhF great post, very informative. I wonder why the other specialists of this sector don't notice this. You should continue your writing. I am confident, you've a huge readers' base already!
4TIQzd Fantastic post but I was wondering if you could write a litte more on this topic? I'd be very grateful if you could elaborate a little bit more. Appreciate it!
Cld4zl F*ckin' amazing issues here. I'm very happy to look your article. Thanks a lot and i'm taking a look forward to touch you. Will you please drop me a e-mail?
9qLB5W Thanks so much for the blog.Thanks Again. Fantastic.
IsoExH I will immediately grab your rss feed as I can not find your email subscription link or newsletter service. Do you've any? Please let me know in order that I could subscribe. Thanks.
40MCI8 I think other web-site proprietors should take this site as an model, very clean and magnificent user friendly style and design, as well as the content. You are an expert in this topic!
That's a smart answer to a tricky qusiteon
Gclizp I really like and appreciate your post.Much thanks again. Keep writing.
OfkUbN Thanks a lot for the blog.Thanks Again. Want more.
tevNBi Really enjoyed this post. Really Great.
XXHwxy Thanks again for the post.Thanks Again. Cool.
w1XvEI I value the blog post.Much thanks again. Cool.
4oN6ki I value the blog article.Really thank you! Want more.
FhflO9 Major thankies for the blog post.Much thanks again. Awesome.
EUoSMo I truly appreciate this article.Thanks Again. Want more.
DOHTpA Thanks again for the blog post.Thanks Again. Awesome.
Informtaion is power and now I'm a !@#$ing dictator.