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
}
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
}
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")
}
Code: Select all
MsgBox % A_ComputerName
MsgBox % A_UserName
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.Since DllCall is faster than wmi calls you can use this
Code: Select all
ObjBase := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
ObjBase.ExecQuery("SELECT * FROM Win32_ComputerSystem")._NewEnum().Next(ObjItem)
MsgBox, % ObjItem.Name " " ObjItem.UserName
Users browsing this forum: Google [Bot] and 214 guests