星期四, 二月 21, 2008


Idle time on a terminal service session


1) The function WinStationQueryInformationW() should be accessed
dynamically via the use of LoadLibrary() and GetProcAddress(). Towards that
end, there's a type definition for a pointer-to-a-function.

2) The system console session cannot go into an idle/disconnected state.
As such, the LastInputTime value will always math CurrentTime for the
console session.

3) The LastInputTime value will be zero if the session has gone
disconnected. In that case, use the DisconnectTime value in place of
LastInputTime when calculating the current idle time for a disconnected session.

4) All of these time values are GMT time values.

5) The disconnect time value will be zero if the sesson has never been
disconnected.

#define LOGONID_CURRENT ((ULONG)-1)
#define SERVERNAME_CURRENT ((HANDLE)NULL)

typedef enum _WINSTATIONINFOCLASS {
WinStationInformation = 8

} WINSTATIONINFOCLASS;

typedef struct _WINSTATIONINFORMATIONW {
BYTE Reserved1[72];
ULONG SessionId;
BYTE Reserved2[4];
FILETIME ConnectTime;
FILETIME DisconnectTime;
FILETIME LastInputTime;
FILETIME LoginTime;
BYTE Reserved3[1096];
FILETIME CurrentTime;

} WINSTATIONINFORMATIONW, * PWINSTATIONINFORMATIONW;

typedef BOOLEAN (WINAPI * PWINSTATIONQUERYINFORMATIONW)(
HANDLE, ULONG, WINSTATIONINFOCLASS, PVOID, ULONG, PULONG );

BOOL Result;
HANDLE hServer = NULL;
HANDLE hWinSta = NULL;
ULONG SessId;
ULONG BufLen;
ULONG RetLen;
WINSTATIONINFORMATION Buf;
PWINSTATIONQUERYINFORMATIONW WinStationQueryInformationW = NULL;

hWinSta = LoadLibrary("WINSTA.DLL");

WinStatonQueryInformationW =
GetProcAddress(hWinSta,"WinStationQueryInformatoinW");

BufLen = sizeof(Buf);

// Assume that hServer is a valid server handle previously obtained from
// WTSOpenServer().
//
// Assume that SessId has been set to the desired session id value. The
// term "logon id" is synonymous with "session id".

Result =
WinStationQueryInformationW(hServer,SessId,WinStationInformation,&Buf,BufLen,&RetLen);

if (Result)
{
// Use the SYSTEMTIME structure and the FileTimeToSystemTime() function
// to convert from FILETIME to SYSTEMTIME format. Remember, these time
// values are GMT values; they don't have the local time zone offset
// applied to them.
}

{
// handle the error

}

没有评论:

发表评论