Introduction
This article demonstrates how to use WMI (Windows Management Instrumentation) in C#, to retrieve several types of information about the processor, such as the CPU clock speed, Voltage, Manufacturer, and other properties. Included in the first .zip is the executable polls your system for all the implemented properties. The bench-marker looks like this:

Background
It seems that WMI is an unknown concept to many beginners and maybe it intimidates the somewhat more advanced ones. On the MSDN forums there are several questions on how to get CPU/Harddrive information. In this article I will demonstrate how to get a handful of CPU related properties, beginning on what hopefully will become a series of WMI articles/wrappers in the near future.
Using the Code
To use the wrapper, download the .cs file and place it in your application's solution folder: WMI_ProcessorInformationWrapper.zip - 901 B.
Next add a using reference to the namespace:
using WMI_ProcessorInformation;
the last step is to call one of the static methods like this:
WMI_Processor_Information.GetCpuManufacturer() ;
That's really all that there is to using the wrapper, so now lets take a tiny peek at some of what goes on behind the scenes. Most of the methods look like this one:
public static string GetCpuManufacturer()
{
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_Processor");
foreach (ManagementObject queryObj in searcher.Get())
{
return queryObj["Manufacturer"].ToString();
}
}
catch (Exception e)
{
return null;
}
return null;
}
This article was not meant to teach WMI but rather to present a conveinient way of getting CPU information through the wrapper. To learn more about WMI check out some the great article here on codeproject: CodeProject Search for WMI
Future Wrappers
WMI is very powerfull and can be used to get the properties of many different system components such as:
| Harddrive (total space, space remaining) |
| Battery (on a laptop) |
| Screensavers (executable path, timeout ) |
| Wallpaper (image display mode, path to image) |
| hardware refridgeration!(fan RPM,etc), |
| User Account (User name, User's time zone) |
Do you have any specific WMI wrapper you would like to see for C#? If so just drop a comment at this article and I'll try to get it written!
P.S. As this is my first article please cut me at least a little slack, but I am NOT opposed to constructive critisism and we'll have an article version at 2.5 before you know it.
History
V 0.9 ~Initial release.
| You must Sign In to use this message board. |
|
|
 |
|
 |
Hi  i had a problem when i was trying to get software features with ProSysLib (Win32_SoftwareFeature).i'm using c# and when i execute the program it returns an exception.
that's the code :
PSLSystem psl = new PSLSystem(); PSLTable pslT = psl.Tools.WMI.GetData(psl.Tools.WMI.DefaultNamespace, "select * from Win32_SoftwareFeature"); for (int i = 0; i < pslT.nRows; i++) {
for (int j = 0; j < pslT.nCols; j++) { Console.WriteLine("{0}\t{1}",pslT.GetColName(j).ToString(),pslT.GetValue(i,j).ToString()); } Console.WriteLine("*****************************************************************"); } Console.WriteLine("wajdi"); Console.Read(); and that's the exception:
Une exception non gérée du type 'System.Runtime.InteropServices.COMException' s'est produite dans ConsoleApplication19.exe
Informations supplémentaires : WMI FAILED. A WMI-specific error occurred.
i hope you can help me. Thank you.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
I'm not sure on this. The error message doesn't say much, and what it does say, is in french Maybe you could enclose the statement in a try/catch and then copy and paste here the full error message if there is one. that would be more enlightening.
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _______________________________________________________________________________________ My programs never have bugs, they just develop random features. _______________________________________________________________________________________ Computers are so strange, you just yawn good and proper and they've got something new out! R. A.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
First of all,i just want to thank you for answering me.
this is the exception :
System.Runtime.InteropServices.COMException
and this is the exception message :
WMI FAILED. A WMI-specific error occurred.
i also get the same exception when i try to get computer software using Win32_Product.
thank you...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
 | Question?  User of Users Group | 13:30 23 May '08 |
|
 |
The article could use a bit of expansion for better votes I guess, I didn't vote on it, but hey I am not interested in that 
I really wonder how they pulled it off in WMI when one of the problems was obtaining the correct number of cores, physical or logical whatever it was processors via the Win32 API.
And they only fixed it on x64 and Vista..
Any news on that? aka will this really work aka does anyone know if that brilliant innovation in API testing has been patched up ?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
"The article could use a bit of expansion for better votes I guess" It probally could but as I said in the article, i'm not trying to teach you to use WMI but just simplify the task of getting Processor info. As to your question, I did not have any problems with the data returned. I got the correct amount of Physical and Logical cores. (I am running Windows Vista Ultimate)
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _______________________________________________________________________________________ My programs never have bugs, they just develop random features.
modified on Saturday, May 24, 2008 9:49 AM
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|