Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.StartTime Access Denied

My code needs to determine how long a particular process has been running. But it continues to fail with an access denied error message on the Process.StartTime request. This is a process running with a User's credentials (ie, not a high-privilege process). There's clearly a security setting or a policy setting, or something that I need to twiddle with to fix this, as I can't believe the StartTime property is in the Framework just so that it can fail 100% of the time.

A Google search indicated that I could resolve this by adding the user whose credentials the querying code is running under to the "Performance Log Users" group. However, no such user group exists on this machine.

like image 891
TheSmurf Avatar asked Aug 26 '08 17:08

TheSmurf


3 Answers

I've read something similar to what you said in the past, Lars. Unfortunately, I'm somewhat restricted with what I can do with the machine in question (in other words, I can't go creating user groups willy-nilly: it's a server, not just some random PC).

Thanks for the answers, Will and Lars. Unfortunately, they didn't solve my problem.

Ultimate solution to this is to use WMI:

using System.Management;
String queryString = "select CreationDate from Win32_Process where ProcessId='" + ProcessId + "'";
SelectQuery query = new SelectQuery(queryString);

ManagementScope scope = new System.Management.ManagementScope(@"\\.\root\CIMV2");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection processes = searcher.Get();

    //... snip ... logic to figure out which of the processes in the collection is the right one goes here

DateTime startTime = ManagementDateTimeConverter.ToDateTime(processes[0]["CreationDate"].ToString());
TimeSpan uptime = DateTime.Now.Subtract(startTime);

Parts of this were scraped from Code Project:

http://www.codeproject.com/KB/system/win32processusingwmi.aspx

And "Hey, Scripting Guy!":

http://www.microsoft.com/technet/scriptcenter/resources/qanda/jul05/hey0720.mspx

like image 94
TheSmurf Avatar answered Oct 17 '22 12:10

TheSmurf


Process of .Net 1.1 uses the Performance Counters to get the information. Either they are disabled or the user does not have administrative rights. Making sure the Performance Counters are enabled and the user is an administrator should make your code work.

Actually the "Performance Counter Users Group" should enough. The group doesn't exist by default. So you should create it yourself.

Process of .Net 2.0 is not depended on the Performance Counters.

See http://weblogs.asp.net/nunitaddin/archive/2004/11/21/267559.aspx

like image 2
Lars Truijens Avatar answered Oct 17 '22 11:10

Lars Truijens


The underlying code needs to be able to call OpenProcess, for which you may require SeDebugPrivilege.

Is the process you're doing the StartTime request on running as a different user to your own process?

like image 1
Will Dean Avatar answered Oct 17 '22 10:10

Will Dean