Pages

Tuesday, June 26, 2007

WMI

Windows Management Instrumentation provides API's for retrieving system data and doing many hardware operations.
WMI Creator - This creates the essential code for what ever WMI data you wanted to collect. There are SQL queries for WMI called as WQL.

Following is a sample code to parse the SMBIOS data, by which you can collect the information on memory that your motherboard has. Instead of getting data via Windows, you can collect it through this interface. Well, why such a round about way of collecting data?

Yup in certain application development that needs more information on the hardware data, which usually the windows OS does not explicitly provide.
If you take a look @ SMBIOS spec, you will know how vast the structures are...

sample code

const string m_WmiNamespaceInterested = "ROOT\\WMI";

const string m_WqlQueryInterested = "select * from MSSMBios_RawSMBiosTables";

const string m_PropertyInterested = "SMBiosData";

internal void GetRawSmbiosData(out byte [] rawSmbiosDataArray)
{
object rawSMBiosData = null;

//Connection credentials to the remote computer - not needed if the logged in account has access
ConnectionOptions oConn = new ConnectionOptions();
ManagementScope oMs = new ManagementScope(m_WmiNamespace, oConn);

//get Fixed disk stats
ObjectQuery oQuery = new ObjectQuery(m_WqlQuery);
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);

ManagementObjectCollection oReturnCollection = oSearcher.Get();

foreach (ManagementObject oReturn in oReturnCollection)
{
rawSMBiosData = oReturn[m_PropertyInOutput];
}

rawSmbiosDataArray = rawSMBiosData as byte[];

}

No comments: