WIn32API ターミナルサービス上の情報を取得する WTSQuerySessionInformation

ターミナルサービス上の指定した情報を取得するには、WTSQuerySessionInformation関数を用いる。


WTSQuerySessionInformation関数のプロトタイプは、以下の通り。

BOOL WTSQuerySessionInformation(
  HANDLE hServer, //ターミナルサーバーのハンドル
  DWORD SessionId, //セッションID
  WTS_INFO_CLASS WTSInfoClass, //取得したい情報の種類
  LPTSTR *ppBuffer, //取得した情報
  DWORD *pBytesReturned //取得した情報のサイズ
);



WTS_INFO_CLASS列挙体は、WtsApi32.h に以下のように定義されている

typedef enum _WTS_INFO_CLASS {
    WTSInitialProgram,
    WTSApplicationName,
    WTSWorkingDirectory,
    WTSOEMId,
    WTSSessionId,
    WTSUserName,
    WTSWinStationName,
    WTSDomainName,
    WTSConnectState,
    WTSClientBuildNumber,
    WTSClientName,
    WTSClientDirectory,
    WTSClientProductId,
    WTSClientHardwareId,
    WTSClientAddress,
    WTSClientDisplay,
    WTSClientProtocolType,
    WTSIdleTime,
    WTSLogonTime,
    WTSIncomingBytes,
    WTSOutgoingBytes,
    WTSIncomingFrames,
    WTSOutgoingFrames,
    WTSClientInfo,
    WTSSessionInfo,
    WTSSessionInfoEx,
    WTSConfigInfo,
    WTSValidationInfo,
    WTSSessionAddressV4,
    WTSIsRemoteSession
} WTS_INFO_CLASS;





  • WTSQuerySessionInformation関数を使用するためのコード

  • SessionIdに 1 、WTSInfoClassには WTSWinStationName を指定して、WTSQuerySessionInformation関数を呼び出す

    #include <windows.h>
    #include <stdio.h>
    #include <wtsapi32.h>
    
    #pragma comment(lib, "wtsapi32.lib")
    
    int main()
    {
        HANDLE hServer;
        DWORD dwSessionId;
        LPTSTR szBuffer;
        DWORD dwBufferSize;
    
    
        hServer = WTS_CURRENT_SERVER_HANDLE;
        dwSessionId = 1;
    
        WTSQuerySessionInformation(
          hServer,
          dwSessionId,
          WTSWinStationName,
          &szBuffer,
          &dwBufferSize
        );
    
        _tprintf(TEXT("%s\n"), szBuffer);
    
        WTSFreeMemory(szBuffer);
    
        WTSCloseServer(hServer);
    
        return 0;
    }



  • 実行結果
  • Console




  • 参考

  • http://msdn.microsoft.com/ja-jp/library/cc429625.aspx