Solved

Exception thrown when accessing Idle Process in .Net 2.0

Posted on 2007-11-26
8
1,078 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
  • 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 62

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
 
LVL 62

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 250 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now