Get LastInputTime by WinStationQueryInformationW returns with 7 min difference

10 pts.
Windows Protocols
Windows Terminal Server
Hi experts, I have a strangle problem with LastInputTime from WinStationInformationW function. The problem is that it sometimes return LastInputTime with 7 min difference from its actual time. For example: last input time was at 11.10 but it returns 11.03. It usually occurs when the person logs in for the first time at the terminal server. But it can also appear during active session. I don't think that the problem is in the code, because it appears occasionally. Thank you in advance. Here is the code:
WinStationInformation = 8
//Works only on Windows 2000/2003
private static extern int WinStationQueryInformationW(
IntPtr hServer,
uint SessionId,
uint WinStationInformation,
[Out] IntPtr Buf,
uint BufLen,
ref uint RetLen);

[DllImport("wtsapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr WTSOpenServer(string ServerName);

private static extern void WTSCloseServer(IntPtr hServer);

public struct WinstaInfo
public int SessionId;
public DateTime ConnectTime;
public DateTime DisconnectTime;
public DateTime LastInputTime;
public DateTime LoginTime;
public DateTime CurrentTime;

private static DateTime FileTimeToDateTime(System.Runtime.InteropServices.ComTypes.FILETIME ft)

long hFT = (((long)ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
return DateTime.FromFileTime(hFT);

public static WinstaInfo WTSQuerySessionInfo(string ServerName, uint SessionId)
uint RetLen = 0;
int ret;

System.IntPtr pwsInfo = Marshal.AllocHGlobal(Marshal.SizeOf(wsInfo));
WinstaInfo RetWsInfo = new WinstaInfo();
IntPtr hServer = WTSOpenServer(ServerName);
ret = WinStationQueryInformationW(
ref RetLen);

wsInfo = (WINSTATIONINFORMATIONW) Marshal.PtrToStructure(
RetWsInfo.ConnectTime = FileTimeToDateTime(wsInfo.ConnectTime);
RetWsInfo.CurrentTime = FileTimeToDateTime(wsInfo.CurrentTime);
RetWsInfo.DisconnectTime = FileTimeToDateTime(wsInfo.DisconnectTime);
RetWsInfo.LastInputTime = FileTimeToDateTime(wsInfo.LastInputTime);
RetWsInfo.LoginTime = FileTimeToDateTime(wsInfo.LoginTime);
RetWsInfo.SessionId = (int)wsInfo.SessionId;
}catch(Win32Exception w32ex)
return RetWsInfo;

Answer Wiki

Thanks. We'll let you know when a new response is added.

Yes, I have run into this too. I suspect that it is a bug in Windows, as I’ve seen this 7 minute difference appear in the built-in Windows Terminal Services Manager as well.

BTW, if you’d rather avoid P/Invokes, you can use Cassia which wraps the underlying API ugliness.

Discuss This Question:  

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.


Share this item with your network: