Solved

Exception thrown when accessing Idle Process in .Net 2.0

Posted on 2007-11-26
8
1,075 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

13 Experts available now in Live!

Get 1:1 Help Now