Link to home
Start Free TrialLog in
Avatar of Sephrial
Sephrial

asked on

Exception thrown when accessing Idle Process in .Net 2.0

I have a problem that I am unable to solve at the moment, likely due to some .NET 2.0 features that I do not understand.  I have the following code which used to work error free in a .NET 1.1 application.  After porting it to 2.0 (Visual Studio 2005) I'm getting a System.ComponentModel.Win32Exception with NativeErrorCode = 5 for processID = 0, processName = "Idle".  

I thougth it's likely a .NET Security problem, because the NativeErrorCode is "Access is denied".  I don't believe I am allowed to employ the technique called Impersonation because of the Security implications of encoding a username/password in a program.  Has anyone run across this problem before?  And if so do you recall the solution?  My gut tells me that this is very common.
Process[] procs = Process.GetProcesses();
            foreach (Process proc in procs)
            {
                try
                {
                    Console.WriteLine("PROC:" + proc.Id + ":" +
                     proc.MainModule.ModuleName);
                }
                catch (System.ComponentModel.Win32Exception)
                {
                    Console.WriteLine("EXCEPTION!!!! - " + proc.Id + ":" +
                         proc.ProcessName);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Other Exception:" + ex.Message);
                }
            }

Open in new window

Avatar of Bob Learned
Bob Learned
Flag of United States of America image

The 2.0 framework is tighter when it comes to security.  Where is this application running from?  Network share?  Web site?

Bob
Hi Sephrial;

From Microsoft Documentation:
http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.mainmodule(VS.71).aspx

A process module represents a .dll or .exe file that is loaded into a particular process. The MainModule property lets you view information about the executable used to start the process, including the module name, file name, and module memory details.

The Idle process "System Idle Process" has no associated dll or exe it represents the percentage of the CPU which is available for use. The same holds true for the process System but it represents the system itself. These will throw an exception that states that it can not enumerate the process which is correct. This happens in .Net 2003 and .Net 2005.

To get around the issue do something like this

        if (proc.ProcessName != "System" && proc.ProcessName != "Idle")
        {
            Console.WriteLine("PROC:" + proc.Id + ":" +
                proc.MainModule.ModuleName);
        }

Open in new window

Avatar of Sephrial
Sephrial

ASKER

Thanks for your prompt replies gentlemen.  I actually want to get the StartTime property of the Proc for my specific program Fernando, but many of the properties for the "Idle" Process report the said exception.  I thought the code sample I posted would properly illustrate my problem, but perhaps not.  I agree that the MainModule property perhaps would not make sense for the Idle process and was a bad example.  I want to be able to access the StartTime like I can access the ID and the ProcessName.  If I instead print out proc.StartTime I would have the same issue.  When I look at the proc object in Visual Studio debugger, it shows the Win32Exception for most of the properties, but not all.

Bob, I am running this application both locally and on a shared drive (added application to .Net 2.0 Runtime Safe Zone).  The problem is reported both when it is run locally and when it is run on the network share.

Thanks,

Sephrial
Hi Sephrial;

The process called "Idle" is not a process at all but part of the operating system. It is not a separate code file that runs on the system. Therefore you will see most of the properties do not have valid objects associated with them. If you look at the task manager you will note that all the task have a file extension of exe to them except for System and Idle.

Fernando
Sephrial,

What information do you get from reading the start time for the Idle process?  Are you looking to detect system idle time?

Bob
I'm trying to discern the last time the Windows box was rebooted.  The Idle process is always available and is standard across all windows platforms so it seemed like the best method to get the time the computer was last rebooted.  This code used to work on 1.1:


Process systemIdleProcess = Process.GetProcessById(0); // System Idle Process PID = 0
ticksStart = systemIdleProcess.StartTime.Ticks;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, Bob thanks!  It took me a long time to find a working code sample, so I posted the solution below.  This snippet calculates the time of the last Windows Start-up.   Do you know if this query will succeed for all 32 bit versions of Windows or is this only for modern versions of Windows?


using System.Management;
 
ManagementObjectSearcher query = new ManagementObjectSearcher(@"\\.\root\cimv2", "SELECT * FROM Win32_OperatingSystem");
DateTime bootup = DateTime.MinValue;
foreach (ManagementObject mo in query.Get())
{
     string DMTF = mo.Properties["LastBootUpTime"].Value.ToString();
     bootup = ManagementDateTimeConverter.ToDateTime(DMTF);
}
if (bootup > DateTime.MinValue)
     ticksStart = bootup.Ticks;  // Time of last bootup

Open in new window