How to directly address items returned from WMI by ComObjGet

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Randy31416
Posts: 58
Joined: 15 Jan 2014, 19:09

How to directly address items returned from WMI by ComObjGet

30 Apr 2015, 09:56

The following code works nicely on my machine.

Code: Select all

ObjBase := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
ObjList := ObjBase.ExecQuery("SELECT * FROM Win32_ComputerSystem")
For ObjItem in ObjList
{
  MsgBox, % ObjItem.Name " " ObjItem.UserName
}
What I would like is a way to remove the For loop and directly address the (single) item in the list returned by the ExecQuery -- replace the For loop with something like UserName := Syntax.UserName. Is there AutoHotkey syntax for this?
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: How to directly address items returned from WMI by ComOb

30 Apr 2015, 10:06

Since DllCall is faster than wmi calls you can use this

Code: Select all

MsgBox % ComputerName()
MsgBox % UserName()

UserName(name := 2) ; https://github.com/jNizM/AHK_DllCall_WinAPI/blob/master/src/System%20Information%20Functions/GetUserNameEx.ahk
{
    static size := VarSetCapacity(buf, 1023, 0) + 1
    if !(DllCall("secur32.dll\GetUserNameEx", "UInt", name, "Ptr", &buf, "UInt*", size))
        return DllCall("kernel32.dll\GetLastError")
    return StrGet(&buf, size, "UTF-16")
}

ComputerName() ; https://github.com/jNizM/AHK_DllCall_WinAPI/blob/master/src/System%20Information%20Functions/GetComputerName.ahk
{
    static size := 31 + 1 * (A_IsUnicode ? 2 : 1), init := VarSetCapacity(buf, size, 0)
    if !(DllCall("kernel32.dll\GetComputerName", "Ptr", &buf, "UInt*", size))
        return DllCall("kernel32.dll\GetLastError")
    return StrGet(&buf, size, "UTF-16")
}
or just use

Code: Select all

MsgBox % A_ComputerName
MsgBox % A_UserName
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Randy31416
Posts: 58
Joined: 15 Jan 2014, 19:09

Re: How to directly address items returned from WMI by ComOb

30 Apr 2015, 12:44

Since DllCall is faster than wmi calls you can use this
Regrettably, GetUserName, GetUserNameEx, and A_UserName all report the same, wrong for my purposes, name. They all report the name of the user who started the program that is executing and is making the call to GetUserName et al. What I need is the name of the currently logged on user, and the briefest code-wise method I've found is using WMI. I have written code loops that retrieve this using WTSEnumerateSessions and WTSQuerySessionInformation, but this is a tedious process and requires accurate bookkeeping to fetch data from the memory burrers these calls produce and to free the memory buffers that they allocate. Something simpler would be much better, especially moving it into AutoHotkey from Visual Basic 6. So a second question, a good answer to which would obviate the first, might be "how do i get the name of the logged on user". Extensive net searching has revealed to me no more succinct answer than the WMI code above. Hence my first question, which is a question about AutoHotkey syntax for objects.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: How to directly address items returned from WMI by ComOb

30 Apr 2015, 17:39

Apparently you have to use a for loop. The .Item() method from the documentation doesn't work.

However, in AutoHotkey you can just do what the for loop does:

Code: Select all

ObjBase := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
ObjBase.ExecQuery("SELECT * FROM Win32_ComputerSystem")._NewEnum().Next(ObjItem)
MsgBox, % ObjItem.Name " " ObjItem.UserName
I'm not really sure when the full winmgmts string is needed, but generally, ComObjGet("winmgmts:") will work.

Return to “Ask for Help (v1)”

Who is online

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