Solved

Retrieve CPU speed using WMI in C#

Posted on 2006-11-10
9
2,441 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

11 Experts available now in Live!

Get 1:1 Help Now