Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2559
  • Last Modified:

Retrieve CPU speed using WMI in C#

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
alanteigne
Asked:
alanteigne
  • 5
  • 4
1 Solution
 
mastooCommented:
You're specifying the scope wrong.  For the local computer, use this:

mgmtScope = new ManagementScope(@"root\CIMV2");
0
 
alanteigneAuthor Commented:
Thanks for the response.  I just tried that, but still got the same error....
0
 
alanteigneAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mastooCommented:
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
 
mastooCommented:
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
 
alanteigneAuthor Commented:
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
 
alanteigneAuthor Commented:
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
 
mastooCommented:
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
 
alanteigneAuthor Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now