Windows API 23 篇 WTSQuerySessionInformation

函数原型:
BOOL
WINAPI
WTSQuerySessionInformationW(
                                                      IN HANDLE hServer,
                                                      IN DWORD SessionId,
                                                      IN WTS_INFO_CLASS WTSInfoClass,
                                                      __deref_out_bcount(*pBytesReturned) LPWSTR * ppBuffer,
                                                      __out DWORD * pBytesReturned
                                                      );
说明:
hServer: WTS_CURRENT_SERVER_HANDLE
SessionId:会话ID,可以从WTSEnumerateProcesses或者WTSEnumerateSessions 或WTSGetActiveConsoleSessionId
中得到,
WTSInfoClass:指定要查找的信息,该结构是一个枚举类型,可以看下面定义:
typedef enum _WTS_INFO_CLASS {
  WTSInitialProgram,
  WTSApplicationName,
 WTSWorkingDirectory,
 WTSOEMId,
 WTSSessionId,
 WTSUserName,
 WTSWinStationName,
 WTSDomainName,
 WTSConnectState,
 WTSClientBuildNumber,
 WTSClientName,
  WTSClientDirectory,
 WTSClientProductId,
 WTSClientHardwareId,
WTSClientAddress,
WTSClientDisplay,
WTSClientProtocolType
 } WTS_INFO_CLASS;

ppBuffer:输出指针
pBytesReturned:返回信息的长度

DWORD dwSessionId = WTSGetActiveConsoleSessionId();
		PVOID pstr = NULL;
		DWORD dwLen = 0;
		WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE, dwSessionId, WTS_INFO_CLASS::WTSUserName, (LPWSTR*)&pstr, &dwLen);
	
		wstring strUserName = (PWCHAR)pstr;
原文地址:https://www.cnblogs.com/priarieNew/p/9755664.html