本文整理自网络,侵删。
//==============================type 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_CONNECTSTATE_CLASS = ( WTSActive, WTSConnected, WTSConnectQuery, WTSShadow, WTSDisconnected, WTSIdle, WTSListen, WTSReset, WTSDown, WTSInit );
PWTS_SESSION_INFO = ^WTS_SESSION_INFO; WTS_SESSION_INFO = record SessionId: DWORD; pWinStationName: LPTSTR; State: WTS_CONNECTSTATE_CLASS; end;const WTS_CURRENT_SERVER_HANDLE: THANDLE = 0;
{ Get Logged in USer }function WTSEnumerateSessions(hServer: THandle; Reserved: DWORD; Version: DWORD; var ppSessionInfo: PWTS_SESSION_INFO; var pCount: DWORD): BOOL; stdcall; external 'Wtsapi32.dll' name {$IFDEF UNICODE}'WTSEnumerateSessionsW'{$ELSE}'WTSEnumerateSessionsA'{$ENDIF};function WTSQuerySessionInformation(hServer: THandle; SessionId: DWORD; WTSInfoClass: WTS_INFO_CLASS; var ppBuffer: LPTSTR; var pBytesReturned: DWORD): BOOL; stdcall; external 'Wtsapi32.dll' name {$IFDEF UNICODE}'WTSQuerySessionInformationW'{$ELSE}'WTSQuerySessionInformationA'{$ENDIF};procedure WTSFreeMemory(pMemory: Pointer); stdcall; external 'Wtsapi32.dll';
//==============================
//==============================function GetLoggedInUser() : string;var Sessions, Session: PWTS_SESSION_INFO; NumSessions, I, NumBytes: DWORD; UserName: LPTSTR;begin if not WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, Sessions, NumSessions) then RaiseLastOSError; try if NumSessions > 0 then begin Session := Sessions; for I := 0 to NumSessions-1 do begin if Session.State = WTSActive then begin if WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, Session.SessionId, WTSUserName, UserName, NumBytes) then begin try Result := UserName; finally WTSFreeMemory(UserName); end; end; end; Inc(Session); end; end; finally WTSFreeMemory(Sessions); end;end;//==============================
相关阅读 >>
Delphi 过滤网页代码 <script></script>
Delphi 10.4.1 edgebrowser 模拟操作网页方法
Delphi win10系统通知 notificationcenter1 基本用法
更多相关阅读请进入《Delphi》频道 >>