How to use GetObject("winmgmts:\\" & StrComputer & "\root\cimv2")

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • active

    How to use GetObject("winmgmts:\\" & StrComputer & "\root\cimv2")

    This works with Strict Off

    But not with Strict On

    Sometimes I can figure out what is needed by running it and using QuickWatch
    to see the type required but

    GetObject("winm gmts:\\" & StrComputer & "\root\cimv 2")

    returns a value System._ComObje ct of Type Object

    I don't know what to make of that.

    Do you know how to fix this code?

    thanks in advance



    Dim StrComputer As String = "."

    Dim ObjWMIService As Object = GetObject("winm gmts:\\" & StrComputer &
    "\root\cimv 2")

    Dim ColItems As Object = ObjWMIService.E xecQuery("Selec t * from
    Win32_Processor ")

    For Each objItem As Object In ColItems

    Console.WriteLi ne("L2 Cache Size: " & objItem.L2Cache Size)

    Console.WriteLi ne("L2 Cache Speed: " & objItem.L2Cache Speed)

    Console.WriteLi ne("Current Voltage: " & objItem.Current Voltage / 10.0)

    Console.WriteLi ne("Maximum Clock Speed: " & objItem.current ClockSpeed)

    Next


  • Lloyd Sheen

    #2
    Re: How to use GetObject(&quot ;winmgmts:\\&qu ot; & StrComputer & "\root\cim v2")

    What you want to do is add a reference to Microsoft WMI Scripting V1.2
    Library. It is added as a COM component thru the Add References.

    You can then step thru without Strict On and get the object names you need.


    Lloyd Sheen

    " active" <activeNOSPAM @a-znet.comwrote in message
    news:e1Jel1pWHH A.3652@TK2MSFTN GP04.phx.gbl...
    This works with Strict Off
    >
    But not with Strict On
    >
    Sometimes I can figure out what is needed by running it and using
    QuickWatch to see the type required but
    >
    GetObject("winm gmts:\\" & StrComputer & "\root\cimv 2")
    >
    returns a value System._ComObje ct of Type Object
    >
    I don't know what to make of that.
    >
    Do you know how to fix this code?
    >
    thanks in advance
    >
    >
    >
    Dim StrComputer As String = "."
    >
    Dim ObjWMIService As Object = GetObject("winm gmts:\\" & StrComputer &
    "\root\cimv 2")
    >
    Dim ColItems As Object = ObjWMIService.E xecQuery("Selec t * from
    Win32_Processor ")
    >
    For Each objItem As Object In ColItems
    >
    Console.WriteLi ne("L2 Cache Size: " & objItem.L2Cache Size)
    >
    Console.WriteLi ne("L2 Cache Speed: " & objItem.L2Cache Speed)
    >
    Console.WriteLi ne("Current Voltage: " & objItem.Current Voltage / 10.0)
    >
    Console.WriteLi ne("Maximum Clock Speed: " & objItem.current ClockSpeed)
    >
    Next
    >
    >

    Comment

    • Lloyd Sheen

      #3
      Re: How to use GetObject(&quot ;winmgmts:\\&qu ot; &amp; StrComputer &amp; &quot;\root\cim v2&quot;)


      " active" <activeNOSPAM @a-znet.comwrote in message
      news:e1Jel1pWHH A.3652@TK2MSFTN GP04.phx.gbl...
      This works with Strict Off
      >
      But not with Strict On
      >
      Sometimes I can figure out what is needed by running it and using
      QuickWatch to see the type required but
      >
      GetObject("winm gmts:\\" & StrComputer & "\root\cimv 2")
      >
      returns a value System._ComObje ct of Type Object
      >
      I don't know what to make of that.
      >
      Do you know how to fix this code?
      >
      thanks in advance
      >
      >
      >
      Dim StrComputer As String = "."
      >
      Dim ObjWMIService As Object = GetObject("winm gmts:\\" & StrComputer &
      "\root\cimv 2")
      >
      Dim ColItems As Object = ObjWMIService.E xecQuery("Selec t * from
      Win32_Processor ")
      >
      For Each objItem As Object In ColItems
      >
      Console.WriteLi ne("L2 Cache Size: " & objItem.L2Cache Size)
      >
      Console.WriteLi ne("L2 Cache Speed: " & objItem.L2Cache Speed)
      >
      Console.WriteLi ne("Current Voltage: " & objItem.Current Voltage / 10.0)
      >
      Console.WriteLi ne("Maximum Clock Speed: " & objItem.current ClockSpeed)
      >
      Next
      >
      >
      Ok what you need to do is add a COM reference to WbemScripting (Microsoft
      WMI Scripting V1.2 Library) then your code would like something like:::

      The string which is produced (Dim t As String = objItem.GetObje ctText_(0))
      gives a string like the following:

      "
      instance of Win32_Processor
      {
      AddressWidth = 32;
      Architecture = 9;
      Availability = 3;
      Caption = "x64 Family 15 Model 4 Stepping 7";
      CpuStatus = 1;
      CreationClassNa me = "Win32_Processo r";
      CurrentClockSpe ed = 2666;
      CurrentVoltage = 30;
      DataWidth = 64;
      Description = "x64 Family 15 Model 4 Stepping 7";
      DeviceID = "CPU0";
      ExtClock = 133;
      Family = 2;
      L2CacheSize = 1024;
      L2CacheSpeed = 2666;
      L3CacheSize = 0;
      L3CacheSpeed = 0;
      Level = 15;
      LoadPercentage = 13;
      Manufacturer = "GenuineInt el";
      MaxClockSpeed = 2666;
      Name = "Intel(R) Pentium(R) D CPU 2.66GHz";
      NumberOfCores = 2;
      NumberOfLogical Processors = 2;
      PowerManagement Supported = FALSE;
      ProcessorId = "BFEBFBFF00000F 47";
      ProcessorType = 3;
      Revision = 1031;
      Role = "CPU";
      SocketDesignati on = "";
      Status = "OK";
      StatusInfo = 3;
      Stepping = "7";
      SystemCreationC lassName = "Win32_Computer System";
      SystemName = "DUALCORE";
      UpgradeMethod = 1;
      Version = "Model 4, Stepping 7";
      };
      "


      Option Strict On
      Imports WbemScripting
      Imports System.Collecti ons

      Module Module1


      Sub Main()
      Dim StrComputer As String = "."

      Dim ObjWMIService As SWbemServicesEx =
      DirectCast(GetO bject("winmgmts :\\" & StrComputer & "\root\cimv 2"),
      SWbemServicesEx )

      Dim ColItems As SWbemObjectSet = ObjWMIService.E xecQuery("Selec t *
      from Win32_Processor ")

      Dim en As IEnumerator = ColItems.GetEnu merator

      Dim s As String
      en.MoveNext()
      While en.Current IsNot Nothing
      Dim objItem As SWbemObjectEx
      objItem = DirectCast(en.C urrent, SWbemObjectEx)
      Dim t As String = objItem.GetObje ctText_(0)
      'Console.WriteL ine("L2 Cache Size: " & objItem.L2Cache Size)

      'Console.WriteL ine("L2 Cache Speed: " & objItem.L2Cache Speed)

      'Console.WriteL ine("Current Voltage: " & objItem.Current Voltage
      / 10.0)

      'Console.WriteL ine("Maximum Clock Speed: " &
      objItem.current ClockSpeed)
      en.MoveNext()
      End While
      End Sub

      End Module

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: How to use GetObject(&quot ;winmgmts:\\&qu ot; &amp; StrComputer &amp; &quot;\root\cim v2&quot;)

        "Lloyd Sheen" <a@b.cschrieb :
        What you want to do is add a reference to Microsoft WMI Scripting V1.2
        Library. It is added as a COM component thru the Add References.
        Why not use 'System.Managem ent' ("System.Manage ment.dll"), which provides a
        managed wrapper around WMI?

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

        Comment

        • active

          #5
          Re: How to use GetObject(&quot ;winmgmts:\\&qu ot; &amp; StrComputer &amp; &quot;\root\cim v2&quot;)

          For Each objItem As SWbemObjectEx In ColItems

          Console.WriteLi ne("L2 Cache Size: " & objItem.L2Cache Size)

          I still get the Object Strict disallows late binding for objItem

          It must be possible to do this because if I set Option Strict Off

          it works Ok



          Thanks



          "Lloyd Sheen" <a@b.cwrote in message
          news:890DFC7B-E9C6-4574-B027-D8E1A9CFCCB0@mi crosoft.com...
          What you want to do is add a reference to Microsoft WMI Scripting V1.2
          Library. It is added as a COM component thru the Add References.
          >
          You can then step thru without Strict On and get the object names you
          need.
          >
          >
          Lloyd Sheen
          >

          Comment

          • active

            #6
            Re: How to use GetObject(&quot ;winmgmts:\\&qu ot; &amp; StrComputer &amp; &quot;\root\cim v2&quot;)

            I tried to find L2 cache size in the docs about ManagementClass but there is
            so much text. Probably if I had more familiarity I could find it. But so far
            it eludes me.

            Thanks

            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
            news:uzJK6TrWHH A.4216@TK2MSFTN GP02.phx.gbl...
            "Lloyd Sheen" <a@b.cschrieb :
            >What you want to do is add a reference to Microsoft WMI Scripting V1.2
            >Library. It is added as a COM component thru the Add References.
            >
            Why not use 'System.Managem ent' ("System.Manage ment.dll"), which provides
            a managed wrapper around WMI?
            >
            --
            M S Herfried K. Wagner
            M V P <URL:http://dotnet.mvps.org/>
            V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

            Comment

            • active

              #7
              Re: How to use GetObject(&quot ;winmgmts:\\&qu ot; &amp; StrComputer &amp; &quot;\root\cim v2&quot;)

              Why is it objItem.GetObje ctText_(0) is not late binding
              but objItem.L2Cache Size is???
              Dim t As String = objItem.GetObje ctText_(0)
              'Console.WriteL ine("L2 Cache Size: " & objItem.L2Cache Size)
              Thanks for the help


              Comment

              Working...