Help converting script to v2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
kiwichick
Posts: 169
Joined: 21 Jan 2014, 22:03

Help converting script to v2

24 May 2024, 19:33

This is a v1 script I have using a function I found online years ago. Can someone please help me convert this to v2? I've tried using the AHK-v2-script-converter but it throws an error and doesn't complete the conversion.

Code: Select all

Wifi = % GetLocalIPByAdaptor("Wifi")

GetLocalIPByAdaptor(adaptorName) {
    objWMIService := ComObjGet("winmgmts:{impersonationLevel = impersonate}!\\.\root\cimv2")
    colItems := objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = '" adaptorName "'")._NewEnum, colItems[objItem]
    colItems := objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE InterfaceIndex = '" objItem.InterfaceIndex "'")._NewEnum, colItems[objItem]
    Return objItem.IPAddress[0]
}

MsgBox %Wifi%
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Help converting script to v2

24 May 2024, 23:59

Code: Select all

#Requires AutoHotkey v2

MsgBox GetLocalIPByAdapter('Wifi') ; <— specify the adapter name you are interested in

GetLocalIPByAdapter(adapterName) {
    wmi := ComObjGet('winmgmts:')
    adapters := wmi.InstancesOf('Win32_NetworkAdapter where NetConnectionID = "' . adapterName . '"')
    if adapters.Count {
        adapter := adapters.ItemIndex(0)
        return wmi.InstancesOf(
            'Win32_NetworkAdapterConfiguration where InterfaceIndex = "' . adapter.InterfaceIndex . '"'
        ).ItemIndex(0).IPAddress[0]
    }
    return '"' . adapterName . '" not found'
}
It's faster and less resource intensive that way:

Code: Select all

#Requires AutoHotkey v2

MsgBox GetLocalIPByAdapter('Wifi') ; <— specify the adapter name you are interested in

GetLocalIPByAdapter(adapterName) {
    static ERROR_BUFFER_OVERFLOW := 111, NI_MAXHOST := 0x401, AF_INET := 2, NI_NUMERICHOST := 2
    DllCall('Ws2_32\WSAStartup', 'UShort', 0x0202, 'Ptr', buf := Buffer(512))
    res := DllCall('Iphlpapi\GetAdaptersAddresses', 'Int', AF_INET, 'UInt', 0, 'Ptr', 0, 'Ptr', 0, 'UIntP', &size := 0)
    if res != ERROR_BUFFER_OVERFLOW {
        DllCall('Ws2_32\WSACleanup')
        return 'The GetAdaptersAddresses() function failed with an error: ' . res
    }
    buf := Buffer(size, 0)
    DllCall('Iphlpapi\GetAdaptersAddresses', 'Int', AF_INET, 'UInt', 0, 'Ptr', 0, 'Ptr', buf, 'UIntP', &size)
    addr1 := buf.ptr ; IP_ADAPTER_ADDRESSES
    Loop {
        found := StrGet(NumGet(addr1 + 8 + A_PtrSize * 8, 'Ptr')) = adapterName
    } until found || !addr1 := NumGet(addr1 + 8, 'Ptr')
    if found {
        addr2 := NumGet(addr1 + 8 + A_PtrSize * 2, 'Ptr') ; IP_ADAPTER_UNICAST_ADDRESS
        SOCKET_ADDRESS := NumGet(addr2 + 8 + A_PtrSize, 'Ptr')
        len := NumGet(addr2 + 8 + A_PtrSize * 2, 'UInt')
        ipAddressBuf := Buffer(NI_MAXHOST, 0)
        res := DllCall('Ws2_32\getnameinfo', 'Ptr', SOCKET_ADDRESS, 'UInt', len,
                                             'Ptr', ipAddressBuf, 'UInt', NI_MAXHOST,
                                             'Ptr', 0, 'UInt', 0, 'Int', NI_NUMERICHOST)
        info := res == 0 ? StrGet(ipAddressBuf, 'CP0') : 'The getnameinfo() function failed with an error: ' . res
    }
    DllCall('Ws2_32\WSACleanup')
    return info ?? '"' . adapterName . '" not found'
}
kiwichick
Posts: 169
Joined: 21 Jan 2014, 22:03

Re: Help converting script to v2

25 May 2024, 04:33

teadrinker wrote:
24 May 2024, 23:59
Perfect! Thank you very much. There's no way I would have gotten there myself.
kiwichick
Posts: 169
Joined: 21 Jan 2014, 22:03

Re: Help converting script to v2

08 Jun 2024, 20:25

teadrinker wrote:
24 May 2024, 23:59

Code: Select all

#Requires AutoHotkey v2

MsgBox GetLocalIPByAdapter('Wifi') ; <— specify the adapter name you are interested in

GetLocalIPByAdapter(adapterName) {
    wmi := ComObjGet('winmgmts:')
    adapters := wmi.InstancesOf('Win32_NetworkAdapter where NetConnectionID = "' . adapterName . '"')
    if adapters.Count {
        adapter := adapters.ItemIndex(0)
        return wmi.InstancesOf(
            'Win32_NetworkAdapterConfiguration where InterfaceIndex = "' . adapter.InterfaceIndex . '"'
        ).ItemIndex(0).IPAddress[0]
    }
    return '"' . adapterName . '" not found'
}
I have chosen to use this script from you and it's great. I like that it includes a message for when adaptername is not found. However, I'd like to include some error code for any other issues around adaptername. For example, if adaptername is Ethernet but Ethernet isn't connected. Currently when I run the code for Ethernet, I receive an error message that says the function parameter expects a String but receives a ComValue. I thought I might be able to use something like this:

Code: Select all

if (type(adaptername) != 'String') {
return '"' . adapterName . '" not connected'}
I've tried it to get it to work in your code but I can't so I don't know if the code is correct or if it's even the right way to achieve what I want. Can you help, please?
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Help converting script to v2

09 Jun 2024, 10:38

kiwichick wrote: if adaptername is Ethernet but Ethernet isn't connected. Currently when I run the code for Ethernet, I receive an error message that says the function parameter expects a String but receives a ComValue.
Show me how you run the code.
kiwichick
Posts: 169
Joined: 21 Jan 2014, 22:03

Re: Help converting script to v2

09 Jun 2024, 18:06

teadrinker wrote:
09 Jun 2024, 10:38
Show me how you run the code.
Exactly as you've written it but using the word Ethernet instead of WiFi.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Help converting script to v2

10 Jun 2024, 04:02

Code: Select all

#Requires AutoHotkey v2

MsgBox GetLocalIPByAdapter('Ethernet') ; <— specify the adapter name you are interested in

GetLocalIPByAdapter(adapterName) {
    wmi := ComObjGet('winmgmts:')
    adapters := wmi.InstancesOf('Win32_NetworkAdapter where NetConnectionID = "' . adapterName . '"')
    if adapters.Count {
        adapter := adapters.ItemIndex(0)
        ipAddressArr := wmi.InstancesOf(
            'Win32_NetworkAdapterConfiguration where InterfaceIndex = "' . adapter.InterfaceIndex . '"'
        ).ItemIndex(0).IPAddress
        return ipAddressArr is ComObjArray ? ipAddressArr[0] : '"' . adapterName . '" disconnected'
    }
    return '"' . adapterName . '" not found'
}
kiwichick
Posts: 169
Joined: 21 Jan 2014, 22:03

Re: Help converting script to v2

11 Jun 2024, 16:26

teadrinker wrote:
10 Jun 2024, 04:02

Code: Select all

#Requires AutoHotkey v2

MsgBox GetLocalIPByAdapter('Ethernet') ; <— specify the adapter name you are interested in

GetLocalIPByAdapter(adapterName) {
    wmi := ComObjGet('winmgmts:')
    adapters := wmi.InstancesOf('Win32_NetworkAdapter where NetConnectionID = "' . adapterName . '"')
    if adapters.Count {
        adapter := adapters.ItemIndex(0)
        ipAddressArr := wmi.InstancesOf(
            'Win32_NetworkAdapterConfiguration where InterfaceIndex = "' . adapter.InterfaceIndex . '"'
        ).ItemIndex(0).IPAddress
        return ipAddressArr is ComObjArray ? ipAddressArr[0] : '"' . adapterName . '" disconnected'
    }
    return '"' . adapterName . '" not found'
}
Perfect! Thank you so much for your help.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Google [Bot] and 36 guests