AHK - WMI - Snippets
Re: AHK - WMI - Snippets
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).
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).
Re: AHK - WMI - Snippets
About WMI vs WinAPI
Disadvantage: Speed
Advantage: Wraps the native API / Get more data with less work
Disadvantage: Speed
Advantage: Wraps the native API / Get more data with less work
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
The WMI_HDD_HEALTH() example on Win 10, 64bit, AutoHotkey v1.1.26.1, doesn't seem to work (returns empty)
Re: AHK - WMI - Snippets
@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.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]
My Stuff: mousey; volume; ahkinfo; instantbirder; npp highlighter; Dynamic File Menu; tv - a treeview library;
Re: AHK - WMI - Snippets
I would prefer a winapi function.
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=71627 -> see GetAdaptersInfo()
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=71627 -> see GetAdaptersInfo()
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
@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?
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]
My Stuff: mousey; volume; ahkinfo; instantbirder; npp highlighter; Dynamic File Menu; tv - a treeview library;
Re: AHK - WMI - Snippets
Code: Select all
AdaptersInfo := GetAdaptersInfo()
MsgBox % AdaptersInfo[1].DhcpEnabled
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
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
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
My Stuff: mousey; volume; ahkinfo; instantbirder; npp highlighter; Dynamic File Menu; tv - a treeview library;
Re: AHK - WMI - Snippets
@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:
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.
(objItem.DHCPEnabled[0] ? "No" : "Yes")
Reading the MSDN docs:
Now, I don't have retgistry value at: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.
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.
My Stuff: mousey; volume; ahkinfo; instantbirder; npp highlighter; Dynamic File Menu; tv - a treeview library;
Re: AHK - WMI - Snippets
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
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.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile
Re: AHK - WMI - Snippets
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?
What did you change, by the way, so I know what to look for with other things? pointer size, no?
My Stuff: mousey; volume; ahkinfo; instantbirder; npp highlighter; Dynamic File Menu; tv - a treeview library;
Re: AHK - WMI - Snippets
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]
Thank you! Hope this helps someone else cuz it took me a while to figure this out :p
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)
-TL
Re: AHK - WMI - Snippets
Thank you @Tigerlily
[AHK] v2.0.18 | [WIN] 11 Pro (23H2) | [GitHub] Profile