1. 程式人生 > >Windowns API 第24篇 WTSEnumerateSessions 枚舉session信息

Windowns API 第24篇 WTSEnumerateSessions 枚舉session信息

服務 set rate urn 空間站 tsa cti brush current

函數原型:
BOOL WTSEnumerateSessions(  HANDLE hServer,
                                                      DWORD Reserved, 
                                                      DWORD Version,
                                                      PWTS_SESSION_INFO* ppSessionInfo, 
                                                      DWORD* pCount
                                                    );
作用:對當前系統的session信息進行枚舉。
參數說明:
hServer:指定要對終端斷服務枚舉的句柄,本機可以的話可以為WTS_CURRENT_SERVER_HANDLE, 或者NULL
Reserved:系統保留位,必須為0
Version:指定枚舉請求的版本,必須為1
ppSessionInfo:一個WTS_SESSION_INFO結構
可以看一下該結構的定義:
typedef struct _WTS_SESSION_INFO
{   
     DWORD SessionId;  
     LPTSTR pWinStationName;
     WTS_CONNECTSTATE_CLASS State;
 } WTS_SESSION_INFO, * PWTS_SESSION_INFO;

該結構中包含繪畫ID, Windows空間站名,session的狀態,此狀態為枚舉值。再次看下一結構
typedef enum _WTS_CONNECTSTATE_CLASS
{
    WTSActive,              // User logged on to WinStation
    WTSConnected,           // WinStation connected to client
    WTSConnectQuery,        // In the process of connecting to client
    WTSShadow,              // Shadowing another WinStation
    WTSDisconnected,        // WinStation logged on without client
    WTSIdle,                // Waiting for client to connect
    WTSListen,              // WinStation is listening for connection
    WTSReset,               // WinStation is being reset
    WTSDown,                // WinStation is down due to error
    WTSInit,                // WinStation in initialization
} WTS_CONNECTSTATE_CLASS;


pCount:返回Session的數量,為輸出參數

舉例說明:

   void main()

   {

        PWTS_SESSION_INFO psi;
	DWORD dwCount;

	BOOL bRet = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &psi, &dwCount);

	if (!bRet)
		return 0;
	wstring strName;
	for (unsigned int i = 0; i < dwCount; i ++)
	{
		printf("%s \t", psi[i].pWinStationName);
		printf("%d \t", psi[i].SessionId);
		printf("%d \n", psi[i].State);
	}
	WTSFreeMemory(psi);

    }

Windowns API 第24篇 WTSEnumerateSessions 枚舉session信息