Solved

Retrieve CPU speed using WMI in C#

Posted on 2006-11-10
9
2,456 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
A short film showing how OnPage and Connectwise integration works.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

943 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

15 Experts available now in Live!

Get 1:1 Help Now