?
Solved

Exception thrown when accessing Idle Process in .Net 2.0

Posted on 2007-11-26
8
Medium Priority
?
1,089 Views
Last Modified: 2013-12-16
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
Comment
Question by:Sephrial
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20354421
The 2.0 framework is tighter when it comes to security.  Where is this application running from?  Network share?  Web site?

Bob
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 20354498
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
 

Author Comment

by:Sephrial
ID: 20354904
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
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 
LVL 63

Expert Comment

by:Fernando Soto
ID: 20355811
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20357680
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
 

Author Comment

by:Sephrial
ID: 20358150
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
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 20359298
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
 

Author Comment

by:Sephrial
ID: 20360576
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question