Solved

Retrieve CPU speed using WMI in C#

Posted on 2006-11-10
9
2,506 Views
Last Modified: 2007-12-19
Hello,

I'm trying to write a simple class that will return several parameters of the system it's running on.  I've created the beginnings of the class using documentation on WMI I've found, but it's not working.  Here is my class so far:

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Management.Instrumentation;

namespace iDeskInfo
{
    class iDeskInfo
    {

        private String strCPU;
        private ManagementScope mgmtScope;
        private ObjectQuery objQuery;
        private ManagementObjectSearcher objSearcher;
        private ManagementObjectCollection objCollection;
                 

        public iDeskInfo()
        {
       
        }


        public String getCPU() {
            mgmtScope = new ManagementScope(@"\\root\CIMV2");

            objQuery = new System.Management.ObjectQuery("SELECT * FROM Win32_Processor");

            objSearcher = new System.Management.ManagementObjectSearcher(mgmtScope, objQuery);

            objCollection = objSearcher.Get();

            foreach (System.Management.ManagementObject objItem in objCollection)
            {
                strCPU = objItem["CurrentClockSpeed"].ToString();
            }

            return strCPU;
        }



    }
}


I have a simple form that creates and instance of the class, and when you click a button, tries to return strCPU.  If I get rid of the WMI code and simple hard-code strCPU, it returns fine.  If I try to use the WMI code, I get an error:

"TypeInitializationException was unhandled"
"The type initializer for 'System.Management.ManagementPath' threw an exception."

on the line that reads "mgmtScope = new ManagementScope(@"\\root\CIMV2");"

Can anyone help me out to get this working?

Thanks...
0
Comment
Question by:alanteigne
[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
  • 5
  • 4
9 Comments
 
LVL 21

Expert Comment

by:mastoo
ID: 17917394
You're specifying the scope wrong.  For the local computer, use this:

mgmtScope = new ManagementScope(@"root\CIMV2");
0
 

Author Comment

by:alanteigne
ID: 17917448
Thanks for the response.  I just tried that, but still got the same error....
0
 

Author Comment

by:alanteigne
ID: 17917610
Here's the output of the error when i try to run the compiled exe:

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.TypeInitializationException: The type initializer for 'System.Management.ManagementPath' threw an exception. ---> System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {CF4CC405-E2C5-4DDD-B3CE-5E7582D8C9FA} failed due to the following error: 80040154.
   at System.Management.MTAHelper.CreateInMTA(Type type)
   at System.Management.ManagementPath.CreateWbemPath(String path)
   at System.Management.ManagementPath..ctor(String path)
   at System.Management.ManagementPath..cctor()
   --- End of inner exception stack trace ---
   at System.Management.ManagementPath..ctor(String path)
   at System.Management.ManagementScope..ctor(String path)
   at iDeskInfo.iDeskInfo.getCPU() in c:\documents and settings\alan.lanteigne\my documents\VS Stuff\iDeskInfo\iDeskInfo\iDeskInfo\iDeskInfo.cs:line 24
   at iDeskInfo.Form1.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\Alan.Lanteigne\My Documents\VS Stuff\iDeskInfo\iDeskInfo\iDeskInfo\Form1.cs:line 28
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
iDeskInfo
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/Documents%20and%20Settings/Alan.Lanteigne/My%20Documents/VS%20Stuff/iDeskInfo/iDeskInfo/iDeskInfo/bin/Release/iDeskInfo.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Management
    Assembly Version: 2.0.0.0
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Management/2.0.0.0__b03f5f7f11d50a3a/System.Management.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.


0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 21

Expert Comment

by:mastoo
ID: 17917841
Interesting.  You could try fully qualifying the scope with the local computer, but I wonder if this is a permissions problem.  Do you run this as a local admin, and have you tested that leaf under WMI CIM Studio?
0
 
LVL 21

Accepted Solution

by:
mastoo earned 450 total points
ID: 17917902
Ah, just saw your last post.  That's a "class not registered" error and the GUID is for WbemDefaultPathParser.  You might check regedit for that GUID to see that it shows up under HKCR\CLSID\{...} and possibly try your exe on a different computer.
0
 

Author Comment

by:alanteigne
ID: 17917907
Actually, about 30 seconds ago I put the EXE on a 2003 machine, and it ran just fine.  

how do i fix a "class not registered" issue?
0
 

Author Comment

by:alanteigne
ID: 17918118
Well, I did a registry search for that class id, and got nada.  Do you know why that could be?  I'm assuming that's something that should have been included with the .net framework 2.0....

Any suggestions on what I should do now?

Thanks,
Alan
0
 
LVL 21

Expert Comment

by:mastoo
ID: 17918378
That's a com thing, not a .net thing.  I'm just not sure if it is supposed to come with windows or not.  I at times download the wmi sdk from ms and I'm sure it installs and registers a bunch of com wmi stuff.  If you do this google:

wmi site:microsoft.com

there a couple of hits on "wmi administrative tools" and "download details: windows management instrumentation (wmi) core" that might be good candidates.  I'm guessing one of those downloads installs the full suite of the latest wmi stuff, and hopefully that entails registering the com dll that you appear to be missing.
0
 

Author Comment

by:alanteigne
ID: 17929570
Hey-

I took care of the problem Friday somehow by doing the following:

1.  Removing all of the the .NET framework installations on my machine
2.  rebooting
3.  running the WMI diagnostic utility from MS
4.  Performing the actions suggested by the utility (deleting some old classes)
5.  Rebooting
6.  Installing .NET 2.0

After that, it worked!  I don't know which step took care of it, but it appears to have taken care of the problem.  I'll award the points for pointing me toward the class error and not a coding error....

Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

724 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