Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Retrieve CPU speed using WMI in C#

Posted on 2006-11-10
Medium Priority
Last Modified: 2007-12-19

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?

Question by:alanteigne
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
LVL 21

Expert Comment

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

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

Author Comment

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

Author Comment

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 **************
    Assembly Version:
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    Assembly Version:
    Win32 Version:
    CodeBase: file:///C:/Documents%20and%20Settings/Alan.Lanteigne/My%20Documents/VS%20Stuff/iDeskInfo/iDeskInfo/iDeskInfo/bin/Release/iDeskInfo.exe
    Assembly Version:
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/
    Assembly Version:
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/
    Assembly Version:
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/
    Assembly Version:
    Win32 Version: 2.0.50727.42 (RTM.050727-4200)
    CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Management/

************** 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 section.
The application must also be compiled with debugging

For example:

    < jitDebugging="true" />

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.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 21

Expert Comment

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?
LVL 21

Accepted Solution

mastoo earned 1800 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.

Author Comment

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?

Author Comment

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?

LVL 21

Expert Comment

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:


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.

Author Comment

ID: 17929570

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


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA:…

715 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