User Domain lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Weitentaaal · 2025-02-06 10:08 Hello, how should i check for the user's domain name. I tried using GetTokenInformation but can't quite figure it out. I would like to grant a User, in a certain Domain, permission to do specific things. thanks for any help, i hope i posted this in the right Forum Section. ---------------------------------------------------------------------- H₂SO₄ · 2025-02-06 10:19 You could use  GetEnvironmentVariable  to check the value of  USERDOMAIN  on Windows,  but note that (like any other environment variable) it can easily be changed/overwritten by the user. ---------------------------------------------------------------------- Weitentaaal · 2025-02-06 11:14 i asked the Network Admin and he told me that the User's are not able to Edit the Environment Variables. So i should be safe when using GetEnvironmentVariable? Its Windows only currently so that wont be a problem either. ---------------------------------------------------------------------- Zvoni · 2025-02-06 11:50 What about querying LDAP? https://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol Quotedn: cn=John Doe,dc=example,dc=com ---------------------------------------------------------------------- Remy Lebeau · 2025-02-06 23:20 Quote from: Weitentaaal on February 06, 2025, 11:08:24 amI tried using GetTokenInformation but can't quite figure it out. What did you try that is not working? Perhaps you can use GetTokenInformation() to get the user's SID, and then use LookupAccountSid() to get the domain where that SID is found. Quote from: Weitentaaal on February 06, 2025, 11:08:24 ami hope i posted this in the right Forum Section. Not really.  This question has nothing to do with network/web programming. The Programming > Operating Systems > Windows forum would probably have been more appropriate. ---------------------------------------------------------------------- Weitentaaal · 2025-02-07 07:21 i do get an error i can't figure out. Compiler saying: "Error: Variable identifier expected" when i try to use "LookupAccountSid". what am i doing wrong ? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetDomainName: string;var   TokenHandle: THandle;   TokenInformation: Pointer;   TokenInfoLength: DWORD;   TokenInfoClass: TTokenInformationClass;   TokenUser: TOKEN_USER;   DomainName: array[0..255] of Char;   AccountName: array[0..255] of Char;   SIDNameUse: SID_NAME_USE;begin   Result:= '';   try      // Open the process token      if not OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, TokenHandle) then begin         //RaiseLastOSError;       Exit;      end;      // Query the required token info length      TokenInfoLength:= 0;      GetTokenInformation(TokenHandle, TokenInfoClass, nil, 0, TokenInfoLength);      // Allocate memory for the token info structure      GetMem(TokenInformation, TokenInfoLength);      try         // Query the token information       if not GetTokenInformation(TokenHandle, TokenInfoClass, TokenInformation, TokenInfoLength, TokenInfoLength) then begin          //RaiseLastOSError;            Exit;         end;         // Retrieve SID          TokenUser:= PTokenUser(TokenInformation)^;       // Look up the account associated with the SID         if not LookupAccountSid(nil, TokenUser.User.Sid, AccountName, SizeOf(AccountName),            DomainName, SizeOf(DomainName), SIDNameUse) then  //<-- Variable Identifier expected (SIDNameUse)       begin            //RaiseLastOSError;            Exit;         end;         ShowMessage('Domain: ' + string(DomainName));         ShowMessage('Account: ' + string(AccountName));         Result:= String(DomainName)      finally         FreeMem(TokenInformation);       CloseHandle(TokenHandle);      end;   except      Result:= false;   end;end; ---------------------------------------------------------------------- H₂SO₄ · 2025-02-07 07:40 The names returned by LookupAccountSid can be of any length; the account & domain name buffers may be too small, in which case the function writes the required lengths into  cchName  and  cchReferencedDomainName. These parameters are pointers (the Windows unit declares them as either  LPDWORD  or  var DWORD), and as such they require an actual, addressable variable, which  SizeOf  doesn't provide. I'm more surprised that the compiler doesn't already complain at  SizeOf(AccountName),  but I guess there's no requirement for the compiler to check argument types from left to right. ---------------------------------------------------------------------- TRon · 2025-02-07 07:49 Quote from: Weitentaaal on February 07, 2025, 08:21:35 ami do get an error i can't figure out. Compiler saying: "Error: Variable identifier expected" when i try to use "LookupAccountSid". what am i doing wrong ? Too little info. take a pick Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function LookupAccountSid(lpSystemName:LPCWSTR; Sid:PSID; Name:LPWSTR; cbName:LPDWORD; ReferencedDomainName:LPWSTR;cbReferencedDomainName:LPDWORD; peUse:PSID_NAME_USE):WINBOOL; external 'advapi32' name 'LookupAccountSidW';function LookupAccountSid(lpSystemName: PChar; Sid: PSID; Name: PChar; var cbName: DWORD; ReferencedDomainName: PChar; var cbReferencedDomainName: DWORD; var peUse: SID_NAME_USE): BOOL;external 'advapi32' name 'LookupAccountSidA';function LookupAccountSid(lpSystemName:LPCSTR; Sid:PSID; Name:LPSTR; cbName:LPDWORD; ReferencedDomainName:LPSTR;cbReferencedDomainName:LPDWORD; peUse:PSID_NAME_USE):WINBOOL; external 'advapi32' name 'LookupAccountSidA'; function LookupAccountSid(lpSystemName: LPCTSTR; Sid: PSID; Name: LPTSTR; var cchName: DWORD; ReferencedDomainName: LPTSTR; var cchReferencedDomainName: DWORD;   var peUse: SID_NAME_USE): BOOL; stdcall;