Translate SID to name

后端 未结 2 2081
终归单人心
终归单人心 2021-01-06 22:10

My Delphi 2010 application needs to add a Windows user to the local Administrators group. I got this part working using NetLocalGroupAddMembers.

Now the application

2条回答
  •  清酒与你
    2021-01-06 22:57

    You don't need LsaLookupSids, this is meant for lookup or an array of SID's. Lookup of a single SID is usually done using LookupAccountSid. Example:

    uses JwaWindows; // or JwaSddl, JwaWinBase;
        var
          Sid: PSID;
          peUse: DWORD;
          cchDomain: DWORD;
          cchName: DWORD;
          Name: array of Char;
          Domain: array of Char;
        begin
          Sid := nil;
          // First convert String SID to SID
          Win32Check(ConvertStringSidToSid(PChar('S-1-5-32-544'), Sid));
    
          cchName := 0;
          cchDomain := 0;
          // Get Length
          if (not LookupAccountSid(nil, Sid, nil, cchName, nil, cchDomain, peUse))
            and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
          begin
            SetLength(Name, cchName);
            SetLength(Domain, cchDomain);
            if LookupAccountSid(nil, Sid, @Name[0], cchName, @Domain[0], cchDomain, peUse) then
            begin
               // note: cast to PChar because LookupAccountSid returns zero terminated string
               ShowMessageFmt('%s\%s', [PChar(Domain), PChar(Name)]);
            end;
          end;
    
          if Assigned(Sid) then
            LocalFree(DWORD(Sid));
    

    or even easier using Jwscl:

    uses JwsclSid;
    
        var
          Sid: TJwSecurityId;
        begin
          Sid := TJwSecurityId.Create('S-1-5-32-544');
          try
            ShowMessage(Sid.GetAccountName);
          finally
            Sid.Free;
          end;
    

提交回复
热议问题