AHK - WMI - Snippets

Post your working scripts, libraries and tools for AHK v1.1 and older
Drugoy
Posts: 48
Joined: 11 Jun 2016, 07:37
Contact:

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: 3183
Joined: 30 Sep 2013, 01:33
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] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
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: 507
Joined: 20 Nov 2013, 20:56
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: 3183
Joined: 30 Sep 2013, 01:33
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] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
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: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

16 Jan 2020, 04:00

Code: Select all

AdaptersInfo := GetAdaptersInfo()
MsgBox % AdaptersInfo[1].DhcpEnabled
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
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: 507
Joined: 20 Nov 2013, 20:56
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: 3183
Joined: 30 Sep 2013, 01:33
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] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
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?
User avatar
Tigerlily
Posts: 377
Joined: 04 Oct 2018, 22:31

Re: AHK - WMI - Snippets

26 Jan 2021, 00:42

Hi @jNizM ,

Thank you for creating this thread! I recently was able to figure out how to get/set the backlight brightness on a laptop display monitor because of using your cleaned up examples (appeared to be the best I could find anywhere on the net).

I thought you would probably like to know this, since you created the Monitor Class - I am going to add this to the v2 Monitor Class when I have some free time!


NOTE: All the examples below are written for AutoHotkey V2 (alpha 122-f595abc2), to translate to v1 syntax, just add a % after each MsgBox

[LAPTOP MONITOR BACKLIGHT]

Code: Select all

; # NOTE: These examples will most likely ONLY work on a built-in LAPTOP DISPLAY MONITOR
; https://docs.microsoft.com/en-us/windows/win32/wmicoreprov/wmimonitorbrightness

; Get Current Backlight level
WmiMonitorBrightness := ComObjGet("winmgmts:\\.\root\wmi").ExecQuery("SELECT * FROM WmiMonitorBrightness")
for backlight in WmiMonitorBrightness
    MsgBox backlight.CurrentBrightness

; Get total backlight levels (e.g. 101)
for backlight in WmiMonitorBrightness
    MsgBox maxLevel := backlight.Levels

; Get array containing acceptable backlight levels (e.g. [0, 1, 2, 3....99, 100])
for backlight in WmiMonitorBrightness
    levelArray := backlight.Level

; Display array values separated by a comma
levelList := ""
for level in levelArray
    levelList .= level (A_Index = maxLevel ? "" : ",")
MsgBox levelList



; https://docs.microsoft.com/en-us/windows/win32/wmicoreprov/wmimonitorbrightnessmethods

; Set Backlight level 
WmiMonitorBrightnessMethods := ComObjGet("winmgmts:\\.\root\wmi").ExecQuery("SELECT * FROM WmiMonitorBrightnessMethods")
for backlight in WmiMonitorBrightnessMethods
    MsgBox backlight.WmiSetBrightness(5,7) ; Timeout in 5 seconds, Set backlight to level 7%

; Get Monitor Instance name
for monitor in WmiMonitorBrightnessMethods
    MsgBox monitor.InstanceName

; Set Ambient Light Sensor to OFF (the sensor that automatically adjusts backlight to match ambient lighting)
for backlight in WmiMonitorBrightnessMethods
    MsgBox backlight.WmiSetALSBrightnessState(false)
Thank you! Hope this helps someone else cuz it took me a while to figure this out :p
-TL
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: AHK - WMI - Snippets

27 Jan 2021, 08:30

Thank you @Tigerlily
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: JoeWinograd, TOTAL and 131 guests