Calling WMI object method?

Get help with using AutoHotkey and its commands and hotkeys
Bruttosozialprodukt
Posts: 457
Joined: 24 Jan 2014, 22:28

Calling WMI object method?

21 Apr 2016, 06:37

I am trying to call a method of a WMI object. Specifucally this one.
Microsoft links to this example code:

Code: Select all

On Error Resume Next 
 
strComputer = "." 
Set objWMIService = GetObject("winmgmts:" _ 
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
 
Set colNetCards = objWMIService.ExecQuery _ 
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True") 
 
For Each objNetCard in colNetCards 
    arrGateways = Array("192.168.1.100", "192.168.1.200") 
    objNetCard.SetGateways(arrGateways) 
Next
And this is what I tried in AutoHotkey:

Code: Select all

objWMIService := ComObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
colItems := objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
For objItem in colItems {
    MsgBox, % "Description:`t" objItem.Description[0] "`n"
            . "IPAddress:`t`t" objItem.IPAddress[0] "`n"
            . "IPSubnet:`t`t" objItem.IPSubnet[0] "`n"
            . "DefaultIPGateway:`t" objItem.DefaultIPGateway[0] "`n"
            . "DNS-Server:`t" objItem.DNSServerSearchOrder[0] "`n"
            . "MACAddress:`t" objItem.MACAddress "`n"
            . "DHCPEnabled:`t" objItem.DHCPEnabled[0] "`n" ; 0 = Yes | -1 = No
    If (objItem.DHCPEnabled[0] = 0) {
        oldGateway := objItem.DefaultIPGateway[0] ;Save old gateway
        objItem.SetGateways(["192.168.200.252"]) ;Set a new Gateway
        objItem.SetGateways([oldGateway]) ;Restore old Gateway
    }
}
But I get an error as soon as objItem.SetGateways() get's called.

Code: Select all

Error:  0x80041001 - 
Source:		SWbemObjectEx
Description:	Generic failure 
HelpFile:		(null)
HelpContext:	0

Specifically: SetGateways
User avatar
jNizM
Posts: 2414
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Calling WMI object method?

21 Apr 2016, 07:15

No wmi, but do the same:
netsh command
[AHK] 1.1.30.01 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
lifeweaver
Posts: 144
Joined: 10 May 2014, 05:57
GitHub: lifeweaver
Location: OH

Re: Calling WMI object method?

21 Apr 2016, 07:21

Hi Bruttosozialprodukt,

Do you need a ComObjArray()
Bruttosozialprodukt
Posts: 457
Joined: 24 Jan 2014, 22:28

Re: Calling WMI object method?

21 Apr 2016, 07:38

Hmm... I guess VT_BSTR is the type I'm looking for, as it is the only string type in the list.

Code: Select all

arrGateways := ComObjArray(VT_BSTR, 1)
arrGateways[0] := "192.168.200.252"
objItem.SetGateways(arrGateways)
But I still get the same error.

This is how microsoft describes the type:

Code: Select all

Item            Description
Length prefix   A four-byte integer that contains the number of bytes in the following data string. It appears immediately before the first character of the data string. This value does not include the terminating null character.
Data string     A string of Unicode characters. May contain multiple embedded null characters.
Terminator      Two null characters.
Do it have to do this manually?
Last edited by Bruttosozialprodukt on 21 Apr 2016, 07:39, edited 1 time in total.
User avatar
jNizM
Posts: 2414
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Calling WMI object method?

21 Apr 2016, 07:39

thx for the hint lifeweaver - this example works:

Code: Select all

for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = TRUE")
{
    MsgBox % "Description:`t"      objItem.Description[0]          "`n"
           . "IPAddress:`t`t"      objItem.IPAddress[0]            "`n"
           . "IPSubnet:`t`t"       objItem.IPSubnet[0]             "`n"
           . "DefaultIPGateway:`t" objItem.DefaultIPGateway[0]     "`n"
           . "DNS-Server:`t"       objItem.DNSServerSearchOrder[0] "`n"
           . "MACAddress:`t"       objItem.MACAddress[0]           "`n"
           . "DHCPEnabled:`t"      objItem.DHCPEnabled[0]          "`n"
    if (objItem.DHCPEnabled[0] = 0)
    {
        oldGateway := ComObjArray(0xC, 1), oldGateway[0] := objItem.DefaultIPGateway[0]
        newGateway := ComObjArray(0xC, 1), newGateway[0] := "192.168.1.254"
        objItem.SetGateways(newGateway)
        sleep 5000
        objItem.SetGateways(oldGateway)
    }
}
edit: renew code
[AHK] 1.1.30.01 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
Bruttosozialprodukt
Posts: 457
Joined: 24 Jan 2014, 22:28

Re: Calling WMI object method?

21 Apr 2016, 07:45

Ahh, thank you! That works. But 0xC is VT_VARIANT, right? Why do we need that type?
User avatar
jNizM
Posts: 2414
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Calling WMI object method?

21 Apr 2016, 07:47

dunno.. but in example 1 is also VT_VARIANT for strings ^^
[AHK] 1.1.30.01 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [GitHub] Profile
Donations are appreciated if I could help you
Bruttosozialprodukt
Posts: 457
Joined: 24 Jan 2014, 22:28

Re: Calling WMI object method?

21 Apr 2016, 07:51

Oh okay. Well, at least it works now. :)

Return to “Ask For Help”

Who is online

Users browsing this forum: FredOoo, sinkfaze and 153 guests