# VBForums CodeBank > CodeBank - Visual Basic .NET >  WMI (Windows Management Instrumentation)  - Get Hardware Info

## gigemboy

This is an example of how easy it is to get various hardware information using WMI. The process includes declaring an object query, using a WMI class name that you want to retrieve info for, then executing the query to return the object information and properties. The ManagementObjectSearcher object has a simple .Get method that is used in order to retrieve the information for the particular WMI class.

You can retrieve WMI information in .NET by using methods and objects in the System.Management namespace. In order to try out the example code below, remember to add a reference to System.Management in your project.

Below is a simple example of getting processor information on the computer. Notice the "Win32_Processor" class. This is only one of several classes you can retrieve information for. There is an example to show all properties for the object, as well as an example of only getting the particular properties you wish.


VB Code:
'query for "Win32_Processor" class under WMI
        Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_Processor")
        'searcher is what runs the query, set the Query object as a parameter
        Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery)
        'calls the .Get method of the searcher, in order to get the object info 
        'This should loop for all processors if you have multiple ones
        For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
            'displays all properties for object in Mof format (there are other formats)
            MessageBox.Show(Mgmt.GetText(Management.TextFormat.Mof))
        Next
        'this is an example of only displaying the particular properties you want
        For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
            MessageBox.Show(Mgmt("Manufacturer").ToString)
            MessageBox.Show(Mgmt("Name").ToString)
            MessageBox.Show(Mgmt("MaxClockSpeed").ToString)
            MessageBox.Show(Mgmt("ProcessorID").ToString)
            MessageBox.Show(Mgmt("ProcessorType").ToString)
            MessageBox.Show(Mgmt("Revision").ToString)
        Next
To learn more about the TONS of classes and objects you can retrieve info for (as well as to find documentation of the different object properties), visit this link: http://msdn.microsoft.com/library/de...mi_classes.asp

----------


## gigemboy

Ran into a few problems with this method. Some of the properties had numerical values, and you don't really know what those numerical values mean until you look at the documentation. To remedy this requires you to build the enumeration values for the fields that require them, which quickly becomes a very tedious task when using several different classes.

I was searching around to see if I could find some sort of resource that had the enumeration values for every WMI class, and thats where I ran into a handly little tool called "Mgmtclassgen.exe", which actually ships in the tools for Visual Studio .NET 2003. It creates wrapper classes for whatever WMI class you need it for. So in doing this, I was able to create an ENTIRE class library of the Win32 Hardware and Operating System WMI classes, and I am posting it here  :Smilie: 

Below is the class library I created with 250+ wmi classes that you can use (Every class listed in the MSDN documentation for "Hardware" and "Operating System" for WMI), in one .DLL. The dll is about 3 megabytes uncompressed (710kb zipped), so it is not a particularly small file, but it ecapsulates everything inside of each class, including retrieving all instances, and exposing all the properties and methods so you can easily find them with intellisense inside of the .NET IDE... (as well as showing enumerated return values when accessing the properties in code)

To use the DLL, simply add a reference to it in your project. The complete namespace is WMI.WMIClasses.Win32, but you can Import "WMI.WMIClasses" in order to make it shorter. Below is a sample of using the library...

VB Code:
'at top of form
        Imports WMI.WMIClasses
         'uses Win32_CDROMDrive WMI class
        Dim MyDrive As New Win32.CDROMDrive
        'gets all instances of the class, loops through
        For Each Drive As Win32.CDROMDrive In MyDrive.GetInstances()
            MessageBox.Show(Drive.Caption)
            MessageBox.Show(Drive.Status)
        Next
Thats it  :Smilie:  Download the library by clicking the link below...

***Note - library was created with .NET 2003, for the 1.1 framework. It has not been tested with the 1.0 (2002) or the 2.0 framework (2005), so there is no guarantee that it will work for those.

http://www.redheadedlefty.com/vbforums/WMI.zip (710kb)

----------


## Trekkan

So how could I use this to connect to a remote machine?  I don't see anything that would allow for another machine connection to be made.

----------


## gigemboy

If using the WMI.dll, in intellisense, you will notice several different overloaded constructors when trying to declare a new class, one of which takes in a ManagementScope object, which you can pass into it in order to specify the remote machine to run the query on...

An Example from documentation on using a ManagementScope object...

VB Code:
'they imported the System.Management namespace
Dim opt As New ConnectionOptions()
opt.Username = UserName 
opt.Password = SecurelyStoredPassword
Dim s As New ManagementScope("\\MyServer\root\default", opt)
If trying to use the first example, there is a similar overloaded constructor for the ManagementObjectSearcher object that allows you to pass in a ManagementScope object in order to specify the remote machine...
(cant give actual test code since I just have one machine here  :Smilie:  )

----------


## gigemboy

Some properties of some of the objects can be arrays, so you will have to do something special in order to return the values. The below code is an example if getting the MAC Address and IP address of your network interfaces. The IP Address property seems to be an array, so you have to cast it into one:

VB Code:
'***NOTE - once again, you need a reference to "System.Management" in order to use
        Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_NetworkAdapterConfiguration")
        Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery)
        For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
            'try/catch block needed because not all adapters will return these properties
            Try
                MessageBox.Show(Mgmt("Caption").ToString)
                MessageBox.Show(Mgmt("MACAddress").ToString)
                'IP address property seems to be an array, so we have to cast it into one
                Dim MyArray As System.Array = DirectCast(Mgmt("IPAddress"), System.Array)
                For Each str As String In MyArray
                    MessageBox.Show(str)
                Next
            Catch
            End Try
        Next
The way you will notice if it is an array is if you try to use the .ToString method, you will get something like "System.String[]" as the return value, instead of the property value.

----------


## uniquegodwin

Hi man,
The research you put into this was really very useful for me...
Thanks a tonne bud

----------


## iMPR3SSiON

Hi! I am unable to download this file: http://www.redheadedlefty.com/vbforums/WMI.zip

Can you reupload this file? Thanks.

----------


## VisualBasicN00B

Hi,

Where can i get another copy of this file? The link seems to be dead.

Thanks,
VBN00B

----------


## diablo21

can someone give me full source of the program with updates

----------

