• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1105
  • Last Modified:

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

0
Sephrial
Asked:
Sephrial
  • 3
  • 3
  • 2
1 Solution
 
Bob LearnedCommented:
The 2.0 framework is tighter when it comes to security.  Where is this application running from?  Network share?  Web site?

Bob
0
 
Fernando SotoRetiredCommented:
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

0
 
SephrialAuthor Commented:
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
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Fernando SotoRetiredCommented:
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
0
 
Bob LearnedCommented:
Sephrial,

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

Bob
0
 
SephrialAuthor Commented:
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

0
 
Bob LearnedCommented:
Just because something from Microsoft used to work, doesn't mean that it will always work.

Another option is to use a WMI query for Win32_OperatingSystem.LastBootUpTime.  This query can be run on against either a local machine or a remote machine (with the right privileges).

Bob
0
 
SephrialAuthor Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now