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

A

active

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("winmgmts:\\" & StrComputer & "\root\cimv2")

returns a value System._ComObject 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("winmgmts:\\" & StrComputer &
"\root\cimv2")

Dim ColItems As Object = ObjWMIService.ExecQuery("Select * from
Win32_Processor")

For Each objItem As Object In ColItems

Console.WriteLine("L2 Cache Size: " & objItem.L2CacheSize)

Console.WriteLine("L2 Cache Speed: " & objItem.L2CacheSpeed)

Console.WriteLine("Current Voltage: " & objItem.CurrentVoltage / 10.0)

Console.WriteLine("Maximum Clock Speed: " & objItem.currentClockSpeed)

Next
 
L

Lloyd Sheen

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
 
L

Lloyd Sheen

active said:
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("winmgmts:\\" & StrComputer & "\root\cimv2")

returns a value System._ComObject 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("winmgmts:\\" & StrComputer &
"\root\cimv2")

Dim ColItems As Object = ObjWMIService.ExecQuery("Select * from
Win32_Processor")

For Each objItem As Object In ColItems

Console.WriteLine("L2 Cache Size: " & objItem.L2CacheSize)

Console.WriteLine("L2 Cache Speed: " & objItem.L2CacheSpeed)

Console.WriteLine("Current Voltage: " & objItem.CurrentVoltage / 10.0)

Console.WriteLine("Maximum Clock Speed: " & objItem.currentClockSpeed)

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.GetObjectText_(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;
CreationClassName = "Win32_Processor";
CurrentClockSpeed = 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 = "GenuineIntel";
MaxClockSpeed = 2666;
Name = "Intel(R) Pentium(R) D CPU 2.66GHz";
NumberOfCores = 2;
NumberOfLogicalProcessors = 2;
PowerManagementSupported = FALSE;
ProcessorId = "BFEBFBFF00000F47";
ProcessorType = 3;
Revision = 1031;
Role = "CPU";
SocketDesignation = "";
Status = "OK";
StatusInfo = 3;
Stepping = "7";
SystemCreationClassName = "Win32_ComputerSystem";
SystemName = "DUALCORE";
UpgradeMethod = 1;
Version = "Model 4, Stepping 7";
};
"


Option Strict On
Imports WbemScripting
Imports System.Collections

Module Module1


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

Dim ObjWMIService As SWbemServicesEx =
DirectCast(GetObject("winmgmts:\\" & StrComputer & "\root\cimv2"),
SWbemServicesEx)

Dim ColItems As SWbemObjectSet = ObjWMIService.ExecQuery("Select *
from Win32_Processor")

Dim en As IEnumerator = ColItems.GetEnumerator

Dim s As String
en.MoveNext()
While en.Current IsNot Nothing
Dim objItem As SWbemObjectEx
objItem = DirectCast(en.Current, SWbemObjectEx)
Dim t As String = objItem.GetObjectText_(0)
'Console.WriteLine("L2 Cache Size: " & objItem.L2CacheSize)

'Console.WriteLine("L2 Cache Speed: " & objItem.L2CacheSpeed)

'Console.WriteLine("Current Voltage: " & objItem.CurrentVoltage
/ 10.0)

'Console.WriteLine("Maximum Clock Speed: " &
objItem.currentClockSpeed)
en.MoveNext()
End While
End Sub

End Module
 
H

Herfried K. Wagner [MVP]

Lloyd Sheen said:
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.Management' ("System.Management.dll"), which provides a
managed wrapper around WMI?
 
A

active

For Each objItem As SWbemObjectEx In ColItems

Console.WriteLine("L2 Cache Size: " & objItem.L2CacheSize)

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
 
A

active

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
 
A

active

Why is it objItem.GetObjectText_(0) is not late binding
but objItem.L2CacheSize is???
Dim t As String = objItem.GetObjectText_(0)
'Console.WriteLine("L2 Cache Size: " & objItem.L2CacheSize)

Thanks for the help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top