Version 2 of Matthias Hoffmann - Tcl-Code-Snippets - tcom & wmi - Examples

Updated 2004-06-04 09:36:51

readPopUps: reading all the network-popup messages which habe been received on a machine out of the eventlog using tcom and wmi:

 package require tcom
 proc readPopUps {{cb ""}} {
      set res {}
      if [catch {::tcom::ref getobject "winmgmts:root/CIMV2"} wmi] then {
         return -code error $wmi; # minimal errorhandling
      set wql {select * from Win32_NTLogEvent where LogFile='System' and \
                                                    EventType='3'    and \
                                                    SourceName='Application Popup'}
      if [catch {$wmi ExecQuery $wql} tmp] then {
         return -code error $tmp; # minimal errorhandling
      # enumerating all 'records'
      ::tcom::foreach instance $tmp {
                      set propSet [$instance Properties_]
                      # only the property (object) 'Message' is of interest here
                      set msgVal [[$propSet Item Message] Value]
                      if {[string equal $cb ""]} {
                         lappend res $msgVal
                      } else {
                         uplevel [list $cb $msgVal]
      return $res

A view tests/demos:

returning entries as a list, print them one by one in a loop afterwords:

 foreach rec [readPopUps] {
         puts $rec;

retrieving the entries via a directly coded callback:

 readPopUps {puts}

retrieving the entries via a callback proc:

 proc callBack {args} {
      puts $args

 readPopUps callBack

More technical infos can be found at .