|
-
Feb 15th, 2006, 06:12 PM
#1
WMI (Windows Management Instrumentation) - Get Hardware Info
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
Last edited by gigemboy; Feb 16th, 2006 at 04:22 PM.
-
Feb 20th, 2006, 06:04 PM
#2
Re: WMI (Windows Management Instrumentation) - Get Hardware Info
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 
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 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)
Last edited by gigemboy; Mar 31st, 2006 at 04:23 PM.
-
Mar 9th, 2006, 07:00 PM
#3
Lively Member
Re: WMI (Windows Management Instrumentation) - Get Hardware Info
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.
-
Mar 10th, 2006, 08:15 AM
#4
Re: WMI (Windows Management Instrumentation) - Get Hardware Info
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 )
Last edited by gigemboy; Mar 10th, 2006 at 08:19 AM.
-
Apr 7th, 2006, 11:51 AM
#5
Re: WMI (Windows Management Instrumentation) - Get Hardware Info
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.
-
Aug 29th, 2006, 11:36 AM
#6
Fanatic Member
Re: WMI (Windows Management Instrumentation) - Get Hardware Info
Hi man,
The research you put into this was really very useful for me...
Thanks a tonne bud
Godwin
Help someone else with what someone helped you! 
-
Feb 5th, 2009, 03:37 PM
#7
Member
Re: WMI (Windows Management Instrumentation) - Get Hardware Info
Hi! I am unable to download this file: http://www.redheadedlefty.com/vbforums/WMI.zip
Can you reupload this file? Thanks.
-
Jul 20th, 2009, 01:05 AM
#8
New Member
Re: WMI (Windows Management Instrumentation) - Get Hardware Info
Hi,
Where can i get another copy of this file? The link seems to be dead.
Thanks,
VBN00B
-
Jul 29th, 2009, 12:49 PM
#9
Fanatic Member
Re: WMI (Windows Management Instrumentation) - Get Hardware Info
can someone give me full source of the program with updates
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|