Ah. Thanks.
query session just runs
qwinsta - see if running that in your script instead works. (And does running
qwinsta on its own in the command prompt on your Windows 7 64bit Enterprise edition machine actually work?) You also might want to consider using
SetWorkingDir at the beginning at your script - LogFile contains a relative path to the log file and without SetWorkingDir, the directory where the file is written is at the whim of the working directory the parent process of your script set. (Or you could make LogFile's path an absolute one.)
But, anyway, since I don't really know what's up and as I've already written similar code last week, here's one WinAPI way of doing what qwinsta does (it won't run on anything lower than Windows 7 and if qwinsta doesn't work, I doubt this will but it's gotta be more reliable than parsing command-line output. Tested on my Windows 10 64-bit laptop that's not joined to a domain with AutoHotkey 64-bit and 32-bit):
Code: Select all
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
if ((wtsapi32 := DllCall("LoadLibrary", "Str", "wtsapi32.dll", "Ptr"))) {
if (DllCall("wtsapi32\WTSEnumerateSessionsEx", "Ptr", WTS_CURRENT_SERVER_HANDLE := 0, "UInt*", 1, "UInt", 0, "Ptr*", pSessionInfo, "UInt*", wtsSessionCount)) {
WTS_CONNECTSTATE_CLASS := {0: "WTSActive", 1: "WTSConnected", 2: "WTSConnectQuery", 3: "WTSShadow", 4: "WTSDisconnected", 5: "WTSIdle", 6: "WTSListen", 7: "WTSReset", 8: "WTSDown", 9: "WTSInit"}
cbWTS_SESSION_INFO_1 := A_PtrSize == 8 ? 56 : 32
Loop % wtsSessionCount {
currSessOffset := cbWTS_SESSION_INFO_1 * (A_Index - 1)
ExecEnvId := NumGet(pSessionInfo+0, currSessOffset, "UInt")
currSessOffset += 4
State := NumGet(pSessionInfo+0, currSessOffset, "UInt")
currSessOffset += 4
SessionId := NumGet(pSessionInfo+0, currSessOffset, "UInt")
currSessOffset += A_PtrSize
SessionName := StrGet(NumGet(pSessionInfo+0, currSessOffset, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0")
currSessOffset += A_PtrSize
HostName := StrGet(NumGet(pSessionInfo+0, currSessOffset, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0")
currSessOffset += A_PtrSize
UserName := StrGet(NumGet(pSessionInfo+0, currSessOffset, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0")
currSessOffset += A_PtrSize
DomainName := StrGet(NumGet(pSessionInfo+0, currSessOffset, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0")
currSessOffset += A_PtrSize
FarmName := StrGet(NumGet(pSessionInfo+0, currSessOffset, "Ptr"),, A_IsUnicode ? "UTF-16" : "CP0")
MsgBox % "Username: " . UserName . "`r`n" . "State: " . WTS_CONNECTSTATE_CLASS[State] . " (raw state: " . State . ")"
;if (UserName = A_UserName && State != 0) {
; ...
;}
;if (State == 0) ...
}
DllCall("wtsapi32\WTSFreeMemoryEx", "UInt", WTSTypeSessionInfoLevel1 := 2, "Ptr", pSessionInfo, "UInt", wtsSessionCount)
}
DllCall("FreeLibrary", "Ptr", wtsapi32)
}