PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
WMI (Windows Management Instrumentation) - Get Hardware Info-VBForums
Results 1 to 9 of 9

Thread: WMI (Windows Management Instrumentation) - Get Hardware Info

  1. #1

    Thread Starter
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    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:
    1. 'query for "Win32_Processor" class under WMI
    2.         Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_Processor")
    3.         'searcher is what runs the query, set the Query object as a parameter
    4.         Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery)
    5.         'calls the .Get method of the searcher, in order to get the object info
    6.         'This should loop for all processors if you have multiple ones
    7.         For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
    8.             'displays all properties for object in Mof format (there are other formats)
    9.             MessageBox.Show(Mgmt.GetText(Management.TextFormat.Mof))
    10.         Next
    11.         'this is an example of only displaying the particular properties you want
    12.         For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
    13.             MessageBox.Show(Mgmt("Manufacturer").ToString)
    14.             MessageBox.Show(Mgmt("Name").ToString)
    15.             MessageBox.Show(Mgmt("MaxClockSpeed").ToString)
    16.             MessageBox.Show(Mgmt("ProcessorID").ToString)
    17.             MessageBox.Show(Mgmt("ProcessorType").ToString)
    18.             MessageBox.Show(Mgmt("Revision").ToString)
    19.         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.

  2. #2

    Thread Starter
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    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:
    1. 'at top of form
    2.         Imports WMI.WMIClasses
    3.  
    4.         'uses Win32_CDROMDrive WMI class
    5.         Dim MyDrive As New Win32.CDROMDrive
    6.         'gets all instances of the class, loops through
    7.         For Each Drive As Win32.CDROMDrive In MyDrive.GetInstances()
    8.             MessageBox.Show(Drive.Caption)
    9.             MessageBox.Show(Drive.Status)
    10.         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.

  3. #3
    Lively Member
    Join Date
    Nov 2002
    Posts
    73

    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.
    Troy

  4. #4

    Thread Starter
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    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:
    1. 'they imported the System.Management namespace
    2. Dim opt As New ConnectionOptions()
    3. opt.Username = UserName
    4. opt.Password = SecurelyStoredPassword
    5. 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.

  5. #5

    Thread Starter
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    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:
    1. '***NOTE - once again, you need a reference to "System.Management" in order to use
    2.         Dim MyobjectQuery As New System.Management.ObjectQuery("select * from Win32_NetworkAdapterConfiguration")
    3.         Dim MySearcher As New System.Management.ManagementObjectSearcher(MyobjectQuery)
    4.         For Each Mgmt As System.Management.ManagementObject In MySearcher.Get()
    5.             'try/catch block needed because not all adapters will return these properties
    6.             Try
    7.                 MessageBox.Show(Mgmt("Caption").ToString)
    8.                 MessageBox.Show(Mgmt("MACAddress").ToString)
    9.                 'IP address property seems to be an array, so we have to cast it into one
    10.                 Dim MyArray As System.Array = DirectCast(Mgmt("IPAddress"), System.Array)
    11.                 For Each str As String In MyArray
    12.                     MessageBox.Show(str)
    13.                 Next
    14.             Catch
    15.             End Try
    16.         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.

  6. #6
    Fanatic Member uniquegodwin's Avatar
    Join Date
    Jul 2005
    Location
    Chennai,India
    Posts
    694

    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!

  7. #7
    Member
    Join Date
    Jul 2008
    Location
    Czech Republic
    Posts
    55

    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.
    Code Hard, Go Pro!

  8. #8
    New Member
    Join Date
    Jul 2009
    Posts
    3

    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

  9. #9
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    524

    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
  •  



Featured


Click Here to Expand Forum to Full Width