Mega Code Archive

 
Categories / Delphi / System
 

Get the Current User and Windows NT Domainname

Title: Get the Current User and Windows NT Domainname Question: How can I get the current Domain and Username Answer: Found in the Win32.hlp type PTokenUser = ^TTokenUser; _TOKEN_USER = record User: TSIDAndAttributes; end; TTokenUser = _TOKEN_USER; procedure getCurrentUserAndDomain(var User, Domain: String); var hProcess, hAccessToken: THandle; InfoBuffer: array[0..1000] of Char; szAccountName, szDomainName: array [0..200] of Char; dwInfoBufferSize, dwAccountSize, dwDomainSize: DWORD; pUser: PTokenUser; snu: SID_NAME_USE; begin dwAccountSize:=200; dwDomainSize:=200; hProcess:=GetCurrentProcess; OpenProcessToken(hProcess,TOKEN_READ,hAccessToken); GetTokenInformation(hAccessToken,TokenUser,@InfoBuffer[0],1000, dwInfoBufferSize); pUser:=PTokenUser(@InfoBuffer[0]); LookupAccountSid(nil, pUser.User.Sid, szAccountName, dwAccountSize, szDomainName, dwDomainSize, snu); User:=szAccountName; Domain:=szDomainName; CloseHandle(hAccessToken); end;