Version 22 of TWAPI and WMI

Updated 2007-01-23 09:40:52

Some examples of retrieving data from WMI through TWAPI COM support. See Handling WMI events using TWAPI for examples related to setting up and trapping WMI events.

Note that these examples are for illustrative purposes. Where possible, it is much more efficient to use TWAPI's commands that directly layer on top of the Win32 API instead of going through the COM/WMI layers.

All examples assume you have initialized with

  package require twapi 1.0 ;# Note you need the 1.0 release
  set wmi [twapi::_wmi]

and remember to do a

  $wmi -destroy

at the end


Installed Applications

Show applications that are installed on the system (warning: may take a while to run)

  $wmi -with {
    {ExecQuery "select * from Win32_Product"}
  } -iterate app {
    puts "[$app Name] [$app Version]"
  }

Show installed Microsoft applications (warning: may take a while to run)

  $wmi -with {
    {ExecQuery "select * from Win32_Product where Vendor='Microsoft Corporation'"}
  } -iterate app {
    puts "[$app Name] [$app Version]"
  }

Show installed OS hotfixes

  $wmi -with {
    {ExecQuery "select * from Win32_QuickFixEngineering"}
  } -iterate app {
    puts "[$app HotFixID] [$app Description] installed by [$app InstalledBy]"
  }

BIOS

Print BIOS information

  $wmi -with {
    {ExecQuery "select * from Win32_BIOS"}
  } -iterate bios { 
    puts [$bios GetObjectText_]
  }

Print a specific BIOS field

  $wmi -with {
    {ExecQuery "select * from Win32_BIOS"}
  } -iterate bios {
    puts [$bios BiosVersion]
  }

Windows services

Print list of Windows services that are set to autostart.

  $wmi -with {
    {ExecQuery "select * from Win32_Service where StartMode='Auto'"}
  } -iterate svc {
    puts [$svc DisplayName]
  }

Network adapters

Print list of network adapters in the system.

  $wmi -with {
    {ExecQuery "select * from Win32_NetworkAdapter"}
  } -iterate net {
    puts "[$net DeviceID]:[$net Description]:[$net NetConnectionStatus]"
  }

Motherboard

Print motherboard manufacturer

  $wmi -with {
    {ExecQuery "select * from Win32_BaseBoard"}
  } -iterate mb {
    puts "[$mb Manufacturer] [$mb Model] [$mb Name] SN# [$mb SerialNumber]"
  }

MHo: Here is something for which I don't have an explanation yet. The same thing happens using tcom. Using w2k.:

First, loading a tclsh (8.4.14):

 % expr 1.5*3.9
 5.85
 % package require twapi
 1.0
 % expr 1.5*3.9
 5.85
 % set wmi [twapi::_wmi]
 ::twapi::com_1
 % expr 1.5*3.9
 syntax error in expression "1.5*3.9": extra tokens at end of expression
 %

male - 2007-01-22:

First, some COM objects are specialized on the language the OS is working with. Second, some COM objects sets, without need, the locale of the application they are working in. Third, try after loading the WMI:

 % expr 1,5*3,9

I work, even being Germany, with an English OS, so I can't prove my thoughts!

But my experiences with the "Microsoft Services for UNIX" (NFS for Windows) showed me, that some Microsoft products are not developed with the right amount of sensitivity!

In our case the problem occurred, because NFS for Windows changed the locale of the application the DLLs are loaded in (e.g. because a file selection dialog needed the NFS environment) to a German one.

After a change of the locale, the numeric format might not use the period as floating point divider, but e.g. a comma! So the expression parser won't except the period anymore, but the comma in double values.

The tcl version 8.5 will eliminate that problem, because it won't use the system versions of strtoi, strtol, strtod, etc., which are locale dependent.

MHo: at this moment, I can't do further tests with w2k, because I'm sitting on my XP at home, where the problem does not exist (which indicates that you are right). Oh Microsoft...?

 > tclsh
 % expr 1.5*3.9
 5.85
 % package require twapi
 1.0
 % expr 1.5*3.9
 5.85
 % set wmi [twapi::_wmi]
 ::twapi::com_1
 % expr 1.5*3.9
 syntax error in expression "1.5*3.9": extra tokens at end of expression
 % expr 1,5*3,9
 syntax error in expression "1,5*3,9": extra tokens at end of expression
 % expr 1*3
 3

Using the comma as a separator doesn't help...So, after using TCOM or TWAPI to access WMI, usage of floating point numbers isn't possible anymore... do I really have to wait for Tcl 8.5 to solve this b i g problem???

male - 2007-01-23 (1):

What will happen if you "create" a double?

 % expr {double(1) / 3}

What string representation it will have?

APN Using , as a separator won't help because expr still parses the whole expression using "." as a separator and then passes individual numbers to str* and friends. Unfortunately, there is a mismatch between the expr parser and str* locale-based functions that I can't see how to solve (other than the Tcl 8.5 way). I ran into this with pdh.dll as well (performance counter) and the only way to fix it was to call setlocale(LC_ALL, "C") after every PDH call! I just find it incredible that a DLL would go about changing locales when it should really be using what the application sets.

male - 2007-01-23 (2):

Hhm - in our case (the NFS for Windows problem), the expr parser suddenly excepted doubles with "," as separator!

And we did the same like you APN - after each file selection the locale was changed back to "C" and everything worked.

I created a little locale tcl API, so that we were able to do this from tcl.

But ... I don't know, how a change back to the "normal" locale would influence the WMI?


Category Windows | Category System Administration