public static LockState GetSessionLockState(uint sessionId)
{
IntPtr ppBuffer;
uint pBytesReturned;
var result = WTSQuerySessionInformation(
WtsCurrentServer,
sessionId,
WTS_INFO_CLASS.WTSSessionInfoEx,
out ppBuffer,
out pBytesReturned
);
if (result == False)
return LockState.Unknown;
var sessionInfoEx = Marshal.PtrToStructure<WTSINFOEX>(ppBuffer);
if (sessionInfoEx.Level != 1)
return LockState.Unknown;
var lockState = sessionInfoEx.Data.WTSInfoExLevel1.SessionFlags;
WTSFreeMemoryEx(WTS_TYPE_CLASS.WTSTypeSessionInfoLevel1, ppBuffer, pBytesReturned);
if (IsWin7)
{
/* Ref: https://msdn.microsoft.com/en-us/library/windows/desktop/ee621019(v=vs.85).aspx
* Windows Server 2008 R2 and Windows 7: Due to a code defect, the usage of the WTS_SESSIONSTATE_LOCK
* and WTS_SESSIONSTATE_UNLOCK flags is reversed. That is, WTS_SESSIONSTATE_LOCK indicates that the
* session is unlocked, and WTS_SESSIONSTATE_UNLOCK indicates the session is locked.
* */
switch (lockState)
{
case WtsSessionstateLock:
return LockState.Unlocked;
case WtsSessionstateUnlock:
return LockState.Locked;
default:
return LockState.Unknown;
}
}
switch (lockState)
{
case WtsSessionstateLock:
return LockState.Locked;
case WtsSessionstateUnlock:
return LockState.Unlocked;
default:
return LockState.Unknown;
}
}