2010-09-26

使用 tcom 取得 network adapter name list

proc getNetworkAdapter {} {

    package require tcom;

    if [catch {::tcom::ref getobject "winmgmts:root/CIMV2"} wmi] then {
     return -code error $wmi;
    }

    set wql {SELECT * FROM Win32_NetworkAdapter}


    if [catch {$wmi ExecQuery $wql} tmp] then {
        return -code error $tmp;
    }
    
    ::tcom::foreach instance $tmp {
        set propSet [$instance Properties_]
        set msgVal [[$propSet Item NetConnectionID] Value]
        
        if {[string equal $msgVal ::tcom::NULL] == 0} {                         
            lappend Result $msgVal
        }
    }     

    return $Result    
}

使用 tcom 透過 WMI 拿到 network adapter name 的 list。

參考資料:
Matthias Hoffmann - Tcl-Code-Snippets - tcom & wmi - Examples