Jump to content

Strange COM object problem


Recommended Posts

Consider the following code:

dim $objWMIService
dim $objNetworkConfig
dim $objNetwork
dim $objCfg
dim $strComputer
dim $strAssemble

$objNetwork = ObjCreate("WScript.Network")
$strComputer = $objNetwork.ComputerName
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
if @error <> 0 then exit(1)
$objNetworkConfig = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfig")
if @error <> 0 then exit(1)
for $objCfg in $objNetworkConfig
    $strAssemble = ""
    $strAssemble = $strAssemble & "Index: " & $objCfg.Index & @CRLF
    $strAssemble = $strAssemble & "Settings ID: " & $objCfg.SettingID & @CRLF
    $strAssemble = $strAssemble & "IP Address: " & $objCfg.IPAddress & @CRLF
    $strAssemble = $strAssemble & "Subnet Mask: " & $objCfg.IPSubnet & @CRLF
    $strAssemble = $strAssemble & "Gateway: " & $objCfg.DefaultIPGateway & @CRLF
    msgbox(0, "Network Adapter Configuration", $strAssemble)
next
msgbox(0, "Debug", "Done")
exit(0)

The problem is that the for loop does not execute. I'm not sure why either. The "Done" msgbox does display, so the code is not exiting before the loop. The docs releate to one possible problem though. "If the expression is an Object collection with no elements, the loop will be skipped and the Variable will contain an empty string." If that is the case, then why is $objNetworkConfig empty? I get data on that com object using VBScript. Below is the SAME code with a similar COM object that works:

dim $objWMIService
dim $objNetworkAdapter
dim $objNetwork
dim $objNet
dim $strComputer
dim $strAssemble

$objNetwork = ObjCreate("WScript.Network")
$strComputer = $objNetwork.ComputerName
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")
if @error <> 0 then exit(1)
$objNetworkAdapter = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapter")
if @error <> 0 then exit(1)
for $objNet in $objNetworkAdapter
    $strAssemble = ""
    $strAssemble = $strAssemble & "Name: " & $objNet.Name & @CRLF
    $strAssemble = $strAssemble & "Description: " & $objNet.Description & @CRLF
    $strAssemble = $strAssemble & "Type: " & $objNet.AdapterType & @CRLF
    $strAssemble = $strAssemble & "Connection Name: " & $objNet.NetConnectionID & @CRLF
    $strAssemble = $strAssemble & "PnP Device ID: " & $objNet.PNPDeviceID & @CRLF
    $strAssemble = $strAssemble & "MAC Address: " & $objNet.MACAddress & @CRLF
    $strAssemble = $strAssemble & "Index: " & $objNet.Index & @CRLF
    msgbox(0, "Network Adapter Configuration", $strAssemble)
next
msgbox(0, "Debug", "Done")
exit(0)

Practically the same code. The only difference being variable names and the COM object that is being referenced. Any ideas as to what is causing this? Could this be a AutoIT bug of some sort?

VBScript code that works:

On Error Resume Next 
strComputer = "." 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration") 
For Each objItem in colItems 
    strAssemble = ""
    strAssemble = strAssemble & "ArpAlwaysSourceRoute: " & objItem.ArpAlwaysSourceRoute & vbNewLine
    strAssemble = strAssemble & "ArpUseEtherSNAP: " & objItem.ArpUseEtherSNAP & vbNewLine
    strAssemble = strAssemble & "Caption: " & objItem.Caption & vbNewLine
    strAssemble = strAssemble & "DatabasePath: " & objItem.DatabasePath & vbNewLine
    strAssemble = strAssemble & "DeadGWDetectEnabled: " & objItem.DeadGWDetectEnabled & vbNewLine
    strDefaultIPGateway = Join(objItem.DefaultIPGateway, ",")
    strAssemble = strAssemble & "DefaultIPGateway: " & strDefaultIPGateway & vbNewLine
    strAssemble = strAssemble & "DefaultTOS: " & objItem.DefaultTOS & vbNewLine
    strAssemble = strAssemble & "DefaultTTL: " & objItem.DefaultTTL & vbNewLine
    strAssemble = strAssemble & "Description: " & objItem.Description & vbNewLine
    strAssemble = strAssemble & "DHCPEnabled: " & objItem.DHCPEnabled & vbNewLine
    strAssemble = strAssemble & "DHCPLeaseExpires: " & objItem.DHCPLeaseExpires & vbNewLine
    strAssemble = strAssemble & "DHCPLeaseObtained: " & objItem.DHCPLeaseObtained & vbNewLine
    strAssemble = strAssemble & "DHCPServer: " & objItem.DHCPServer & vbNewLine
    strAssemble = strAssemble & "DNSDomain: " & objItem.DNSDomain & vbNewLine
    strDNSDomainSuffixSearchOrder = Join(objItem.DNSDomainSuffixSearchOrder, ",")
    strAssemble = strAssemble & "DNSDomainSuffixSearchOrder: " & strDNSDomainSuffixSearchOrder & vbNewLine
    strAssemble = strAssemble & "DNSEnabledForWINSResolution: " & objItem.DNSEnabledForWINSResolution & vbNewLine
    strAssemble = strAssemble & "DNSHostName: " & objItem.DNSHostName & vbNewLine
    strDNSServerSearchOrder = Join(objItem.DNSServerSearchOrder, ",")
    strAssemble = strAssemble & "DNSServerSearchOrder: " & strDNSServerSearchOrder & vbNewLine
    strAssemble = strAssemble & "DomainDNSRegistrationEnabled: " & objItem.DomainDNSRegistrationEnabled & vbNewLine
    strAssemble = strAssemble & "ForwardBufferMemory: " & objItem.ForwardBufferMemory & vbNewLine
    strAssemble = strAssemble & "FullDNSRegistrationEnabled: " & objItem.FullDNSRegistrationEnabled & vbNewLine
    strGatewayCostMetric = Join(objItem.GatewayCostMetric, ",")
    strAssemble = strAssemble & "GatewayCostMetric: " & strGatewayCostMetric & vbNewLine
    strAssemble = strAssemble & "IGMPLevel: " & objItem.IGMPLevel & vbNewLine
    strAssemble = strAssemble & "Index: " & objItem.Index & vbNewLine
    strIPAddress = Join(objItem.IPAddress, ",")
    strAssemble = strAssemble & "IPAddress: " & strIPAddress & vbNewLine
    strAssemble = strAssemble & "IPConnectionMetric: " & objItem.IPConnectionMetric & vbNewLine
    strAssemble = strAssemble & "IPEnabled: " & objItem.IPEnabled & vbNewLine
    strAssemble = strAssemble & "IPFilterSecurityEnabled: " & objItem.IPFilterSecurityEnabled & vbNewLine
    strAssemble = strAssemble & "IPPortSecurityEnabled: " & objItem.IPPortSecurityEnabled & vbNewLine
    strIPSecPermitIPProtocols = Join(objItem.IPSecPermitIPProtocols, ",")
    strAssemble = strAssemble & "IPSecPermitIPProtocols: " & strIPSecPermitIPProtocols & vbNewLine
    strIPSecPermitTCPPorts = Join(objItem.IPSecPermitTCPPorts, ",")
    strAssemble = strAssemble & "IPSecPermitTCPPorts: " & strIPSecPermitTCPPorts & vbNewLine
    strIPSecPermitUDPPorts = Join(objItem.IPSecPermitUDPPorts, ",")
    strAssemble = strAssemble & "IPSecPermitUDPPorts: " & strIPSecPermitUDPPorts & vbNewLine
    strIPSubnet = Join(objItem.IPSubnet, ",")
    strAssemble = strAssemble & "IPSubnet: " & strIPSubnet & vbNewLine
    strAssemble = strAssemble & "IPUseZeroBroadcast: " & objItem.IPUseZeroBroadcast & vbNewLine
    strAssemble = strAssemble & "IPXAddress: " & objItem.IPXAddress & vbNewLine
    strAssemble = strAssemble & "IPXEnabled: " & objItem.IPXEnabled & vbNewLine
    strIPXFrameType = Join(objItem.IPXFrameType, ",")
    strAssemble = strAssemble & "IPXFrameType: " & strIPXFrameType & vbNewLine
    strAssemble = strAssemble & "IPXMediaType: " & objItem.IPXMediaType & vbNewLine
    strIPXNetworkNumber = Join(objItem.IPXNetworkNumber, ",")
    strAssemble = strAssemble & "IPXNetworkNumber: " & strIPXNetworkNumber & vbNewLine
    strAssemble = strAssemble & "IPXVirtualNetNumber: " & objItem.IPXVirtualNetNumber & vbNewLine
    strAssemble = strAssemble & "KeepAliveInterval: " & objItem.KeepAliveInterval & vbNewLine
    strAssemble = strAssemble & "KeepAliveTime: " & objItem.KeepAliveTime & vbNewLine
    strAssemble = strAssemble & "MACAddress: " & objItem.MACAddress & vbNewLine
    strAssemble = strAssemble & "MTU: " & objItem.MTU & vbNewLine
    strAssemble = strAssemble & "NumForwardPackets: " & objItem.NumForwardPackets & vbNewLine
    strAssemble = strAssemble & "PMTUBHDetectEnabled: " & objItem.PMTUBHDetectEnabled & vbNewLine
    strAssemble = strAssemble & "PMTUDiscoveryEnabled: " & objItem.PMTUDiscoveryEnabled & vbNewLine
    strAssemble = strAssemble & "ServiceName: " & objItem.ServiceName & vbNewLine
    strAssemble = strAssemble & "SettingID: " & objItem.SettingID & vbNewLine
    strAssemble = strAssemble & "TcpipNetbiosOptions: " & objItem.TcpipNetbiosOptions & vbNewLine
    strAssemble = strAssemble & "TcpMaxConnectRetransmissions: " & objItem.TcpMaxConnectRetransmissions & vbNewLine
    strAssemble = strAssemble & "TcpMaxDataRetransmissions: " & objItem.TcpMaxDataRetransmissions & vbNewLine
    strAssemble = strAssemble & "TcpNumConnections: " & objItem.TcpNumConnections & vbNewLine
    strAssemble = strAssemble & "TcpUseRFC1122UrgentPointer: " & objItem.TcpUseRFC1122UrgentPointer & vbNewLine
    strAssemble = strAssemble & "TcpWindowSize: " & objItem.TcpWindowSize & vbNewLine
    strAssemble = strAssemble & "WINSEnableLMHostsLookup: " & objItem.WINSEnableLMHostsLookup & vbNewLine
    strAssemble = strAssemble & "WINSHostLookupFile: " & objItem.WINSHostLookupFile & vbNewLine
    strAssemble = strAssemble & "WINSPrimaryServer: " & objItem.WINSPrimaryServer & vbNewLine
    strAssemble = strAssemble & "WINSScopeID: " & objItem.WINSScopeID & vbNewLine
    strAssemble = strAssemble & "WINSSecondaryServer: " & objItem.WINSSecondaryServer & vbNewLine
    Wscript.Echo strAssemble
Next

The Maelstorm

Link to comment
Share on other sites

What is the value of $objNetworkAdapter.length prior to the For loop?

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

What is the value of $objNetworkAdapter.length prior to the For loop?

Dale

Now that is a very good question. I'll go look at that right now.

[EDIT]

I think you mean $objNetworkConfig.length as that's the one where the problem is. I insert code to try and see the value of this property, and I get the following error:

The code now looks like this:

dim $objWMIService
dim $objNetworkConfig
dim $objNetwork
dim $objCfg
dim $strComputer
dim $strAssemble
dim $a

$objNetwork = ObjCreate("WScript.Network")
$strComputer = $objNetwork.ComputerName
$objWMIService = ObjGet("winmgmts:\\" & "." & "\root\cimv2")
if @error <> 0 then exit(1)
$objNetworkConfig = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfig")
if @error <> 0 then exit(1)
$a = $objNetworkConfig.length
msgbox(0, "Debug", "Object Length: " & $a)
for $objCfg in $objNetworkConfig
    $strAssemble = ""
    $strAssemble = $strAssemble & "Index: "         & $objCfg.Index & @CRLF
    $strAssemble = $strAssemble & "Settings ID: "   & $objCfg.SettingID & @CRLF
    $strAssemble = $strAssemble & "IP Address: "    & $objCfg.IPAddress & @CRLF
    $strAssemble = $strAssemble & "Subnet Mask: "   & $objCfg.IPSubnet & @CRLF
    $strAssemble = $strAssemble & "Gateway: "       & $objCfg.DefaultIPGateway & @CRLF
    msgbox(0, "Network Adapter Configuration", $strAssemble)
next
msgbox(0, "Debug", "Done")
exit(0)
Edited by Maelstorm

The Maelstorm

Link to comment
Share on other sites

I don't have an answer (I've not played with WMI mich), but I have some information for you...

The error you are getting is on the line with your For statement

wbemErrInvalidClass 2147749904 0x80041010

The specified class is not valid.

To make this easy, I used the error handing routine I have in IE.au3:

#include <IE.au3>
_IEErrorHandlerRegister()

And got this:

--> COM Error Encountered in tmp-asdfoo.au3

----> $IEComErrorScriptline = 24

----> $IEComErrorNumberHex = 80041010

----> $IEComErrorNumber = -2147217392

----> $IEComErrorWinDescription =

----> $IEComErrorDescription = 0

----> $IEComErrorSource = 0

----> $IEComErrorHelpFile = 0

----> $IEComErrorHelpContext = 0

----> $IEComErrorLastDllError = 0

Then looking in the WMI SDK I found the error text.

This is Odd for two reasons: 1) ConsoleWrite(ObjName($objNetworkConfig) & @CR) displays ISWbemObjectSet and 2) without a COM error handler I'd expect the script to abort when that error was encountered, but it didn't.

I think this is worth trying to replicate in VBScript to see is the behavior is different and also to try in a prior beta version of AutoIt to see if the error handling is the same (some changes were made in .126).

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

I don't have an answer (I've not played with WMI mich), but I have some information for you...

The error you are getting is on the line with your For statement

wbemErrInvalidClass 2147749904 0x80041010

The specified class is not valid.

To make this easy, I used the error handing routine I have in IE.au3:

This is Odd for two reasons: 1) ConsoleWrite(ObjName($objNetworkConfig) & @CR) displays ISWbemObjectSet and 2) without a COM error handler I'd expect the script to abort when that error was encountered, but it didn't.

I think this is worth trying to replicate in VBScript to see is the behavior is different and also to try in a prior beta version of AutoIt to see if the error handling is the same (some changes were made in .126).

Dale

That makes no sense. Two objects, both dealing with networking, one has a problem and the other doesn't. Look at my first post. I included VBScript code and that code does work. I get the data from the COM object. It failed on 3.1.1.122 beta as well. This is looking more and more like some kind of weird bug.

The Maelstorm

Link to comment
Share on other sites

That makes no sense. Two objects, both dealing with networking, one has a problem and the other doesn't. Look at my first post. I included VBScript code and that code does work. I get the data from the COM object. It failed on 3.1.1.122 beta as well. This is looking more and more like some kind of weird bug.

Can you cut your VBScript example down to a minimum? In order to investigate further it would be best to have a short, working VBScript example and a parallel, short AutoIt example that has trouble. In this way we can focus specifically on the problem at hand.

thanks,

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

This is not a COM Error at all, but a simple typo in the WMI query:

The incorrect line is:

$objNetworkConfig = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfig")

This should be:

$objNetworkConfig = $objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")

Regards,

-Sven

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...