AHK - WMI - Snippets

Post your working scripts, libraries and tools
Drugoy
Posts: 48
Joined: 11 Jun 2016, 07:37
GitHub: Drugoy

Re: AHK - WMI - Snippets

17 Oct 2016, 07:40

What works faster? Executing WMI queries to get data or do direct WinAPI calls?
I suppose executing WMI queries should be a slower approach (although, I honestly don't know that), but I wonder how much slower (because I could choose WMI-query approach as it takes up less lines of code, lol).
User avatar
jNizM
Posts: 2613
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: AHK - WMI - Snippets

18 Oct 2016, 01:06

About WMI vs WinAPI
Disadvantage: Speed
Advantage: Wraps the native API / Get more data with less work
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
Guest

Re: AHK - WMI - Snippets

16 Aug 2017, 05:13

The WMI_HDD_HEALTH() example on Win 10, 64bit, AutoHotkey v1.1.26.1, doesn't seem to work (returns empty) :?:
User avatar
BGM
Posts: 488
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: AHK - WMI - Snippets

14 Jan 2020, 22:25

jNizM wrote:
01 Oct 2013, 05:05


IPAddress()
{
for objItem in ComObjGet("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
{
if (objItem.IPAddress[0] = A_IPAddress1)
{
return, % { 1 : objItem.Description[0], 2 : objItem.IPAddress[0], 3 : objItem.IPSubnet[0], 4 : objItem.DefaultIPGateway[0]
, 5 : ((objItem.DNSServerSearchOrder[1] = "") ? objItem.DNSServerSearchOrder[0] : objItem.DNSServerSearchOrder[0] ", " objItem.DNSServerSearchOrder[1])
, 6 : objItem.MACAddress, 7 : (objItem.DHCPEnabled[0] ? "No" : "Yes") }
}
}
}
[/code]
@jNizM When I run this command it reports that "no" DHCP is not enabled. But when I check ipconfig, it clearly says autoconfiguration is enabled and shows me the lease. I confirm it is the correct adapter. Looking here.
User avatar
jNizM
Posts: 2613
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: AHK - WMI - Snippets

15 Jan 2020, 03:03

I would prefer a winapi function.
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=71627 -> see GetAdaptersInfo()
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
BGM
Posts: 488
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: AHK - WMI - Snippets

15 Jan 2020, 15:19

@jNizM Can you help me out a little? Say I want to access a setting, such as DHCPenabled on the active NIC. I know the function returns values for all NICS (it sports 3 when I run it on my computer - my network, teamviewer and vmware). Let's say I just want to fetch values from the primary NIC.

I'm not sure I'm accessing the arrays properly - I always get an empgy messagebox. What do I do?

Code: Select all

AdaptersInfo := system.GetAdaptersInfo()
msgbox % AdaptersInfo[1][5]
User avatar
jNizM
Posts: 2613
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: AHK - WMI - Snippets

16 Jan 2020, 04:00

Code: Select all

AdaptersInfo := GetAdaptersInfo()
MsgBox % AdaptersInfo[1].DhcpEnabled
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
BGM
Posts: 488
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: AHK - WMI - Snippets

16 Jan 2020, 09:44

doh! Okay, now I feel like I don't get enough sleep. Thanks, jNizM.

Alright, so with that, I can fetch most of the information.

However, PrimaryWinsServer returns a blank, and SecondaryWinsserver returns 255.255.255. In my ipconfig report for that adapter I see both WINS Servers. HaveWins returns this value: 3487026

Also, when I try LeaseObtained, it says, "1970-01-01" for the date.
and LeaseExpires gives me, "1994-07-24" for the date.
I have no idea where it's getting those dates - very strange. (Windows 7)

And DhcpServer returns me "1.1" - that's all. It should be 192.168.1.13.
Gatewaylist returns simply, "254.0"

For these values, I confirm the Adapter description matches what I'm reading in ipconfig.
Maybe something else is the matter?
Here is my setup:
AutoHotkey 1.1.30.03 Unicode 32-bit on Windows 7 Pro x64
User avatar
BGM
Posts: 488
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: AHK - WMI - Snippets

16 Jan 2020, 10:59

@jNizM The WMI function works waaay better than the API function. Most of the values for the APi function are incorrect or incomplete. The only one of the WMI function above that doesn't work is the DHCPEnabled.
(objItem.DHCPEnabled[0] ? "No" : "Yes")

Reading the MSDN docs:
DHCPEnabled
Data type: boolean
Access type: Read-only
Qualifiers: MappingStrings ("Win32Registry|SYSTEM\\CurrentControlSet\\Services|EnableDHCP")
If TRUE, the dynamic host configuration protocol (DHCP) server automatically assigns an IP address to the computer system when establishing a network connection.
Now, I don't have retgistry value at:
My Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services
Maybe that's why it's returning 0? Maybe that only works if the value is statically set in the computer's local network config? My machine is pulling information from the DHCP Server broadcast.
User avatar
jNizM
Posts: 2613
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: AHK - WMI - Snippets

17 Jan 2020, 02:33

Yes.. found my mistake.
Since I use AHK in U64 for years, I did not tested my functions for U32.


edit: should be fixed now for 32-bit
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
User avatar
BGM
Posts: 488
Joined: 20 Nov 2013, 20:56
GitHub: bgmCoder
Contact:

Re: AHK - WMI - Snippets

17 Jan 2020, 15:25

Ahhhh! Much better! Thanks, jNizM!
What did you change, by the way, so I know what to look for with other things? pointer size, no?

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Qriist, toralf and 31 guests