Get LastInputTime by WinStationQueryInformationW returns with 7 min difference

Discussion in 'Windows' started by benya, Mar 30, 2009.

  1. benya

    benya New Member

    Joined:
    Feb 18, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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 apperars ocassionally.

    Thank you in advance.

    Here is the code:

    Code:
    private enum WINSTATIONINFOCLASS{
    WinStationInformation = 8
    }
    //Works only on Windows 2000/2003
    [DllImport("winsta.dll")]
    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);
    
    [DllImport("wtsapi32.dll")]
    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;
    
    WINSTATIONINFORMATIONW wsInfo = new WINSTATIONINFORMATIONW();
    System.IntPtr pwsInfo = Marshal.AllocHGlobal(Marshal.SizeOf(wsInfo));
    WinstaInfo RetWsInfo = new WinstaInfo();
    IntPtr hServer = WTSOpenServer(ServerName);
    try
    {
        ret = WinStationQueryInformationW(
        hServer,
        SessionId,
        (uint)WINSTATIONINFOCLASS.WinStationInformation,
        pwsInfo,
        (uint)Marshal.SizeOf(typeof(WINSTATIONINFORMATIONW)),
        ref RetLen);
    
    
    
    if(ret==1)
    {
        wsInfo = (WINSTATIONINFORMATIONW) Marshal.PtrToStructure(
            pwsInfo,
            typeof (WINSTATIONINFORMATIONW));
        WTSCloseServer(hServer);
        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)
    {
    }
    finally
    {
    Marshal.FreeHGlobal(pwsInfo);
    }
    return RetWsInfo;
    }
     
    Last edited by a moderator: Mar 31, 2009

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice