Einzelnen Beitrag anzeigen

berens

Registriert seit: 3. Sep 2004
431 Beiträge
 
Delphi 2010 Professional
 
#6

AW: Mitgliedschaft in Windows-Benutzergruppe feststellen

  Alt 6. Aug 2015, 16:40
Also das Listet mir wesentlich mehr Gruppen auf als das andere Verfahren, aber meine tatsächliche Gruppe "Vertrieb" fehlt hier irgendwie? Das angemeldete Konto stimmt auf jeden Fall, auch die anderen Gruppen passen generell.

(Mit dem anderen Verfahren wurde "Vertrieb" auch korrekt angezeigt.)




Delphi-Quellcode:
function ConvertSidToStringSidW(SID: PSID; var StringSid: LPWSTR): Boolean; stdcall;
    external 'advapi32.dllname 'ConvertSidToStringSidW';

implementation

{$R *.dfm}

function ConvertStringSidToSid(StringSid: PWideChar; var Sid: PSID): Boolean; stdcall; external 'advapi32.dllname
'ConvertStringSidToSidW';


function StrSIDToName(const StrSID: Widestring; var Name: string; var SIDType: DWORD): Boolean;
var
  SID : PSID;
  Buffer : PansiChar;
  NameLen, TempLen : Cardinal;
  err : Boolean;
begin
  SID := nil;
  err := ConvertStringSIDToSID(PWideChar(StrSID), SID);
  if err then
  begin
    NameLen := 0;
    TempLen := 0;
    LookupAccountSidA(nil, SID, nil, NameLen, nil, TempLen, SIDType);
    GetMem(Buffer, NameLen);
    try
      err := LookupAccountSidA(nil, SID, Buffer, NameLen, nil, TempLen, SIDType);
      if err then
        SetString(Name, Buffer, Namelen);
    finally
      FreeMem(Buffer);
    end;
  end;
  if Assigned(SID) then
    LocalFree(Cardinal(SID));
  result := err;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
 adsi: TADSI;
 ui: TADSIUserInfo;

  TokenHandle, ReturnLength, int_size: Cardinal;
  TokenInformation : PTokenGroups;
  aTokenUser: PSIDAndAttributes;
  i, j: integer;
  t: string;
  w: PAnsiChar;
  c1, c2, c3: Cardinal;
begin
// adsi := TADSI.Create(Self);
// adsi.GetUser(adsi.CurrentDomain, adsi.CurrentUserName, ui);
// ShowMessage(ui.Groups);


  if OpenProcessToken(GetCurrentProcess(), TOKEN_READ, TokenHandle) then
  begin
    GetTokenInformation(TokenHandle, TOKENUSER, nil, 0, ReturnLength);
    int_size:=ReturnLength;
    aTokenUser:=AllocMem(int_size);
    if GetTokenInformation(TokenHandle, TOKENUSER, aTokenUser, int_size, ReturnLength) then
    begin

// hier die USER Sid
      ConvertSidToStringSidA(aTokenUser.Sid, w);
      ListBox1.Items.Add('!UserSID: ' + w);
      StrSIDToName(w, t, c1);
      ListBox2.Items.Add('!UserSID: ' + t);
    end;
    FreeMem(aTokenUser);

    GetTokenInformation(TokenHandle, TokenGroups, nil, 0, ReturnLength);
    int_size:=ReturnLength;
    TokenInformation:=AllocMem(int_size);
    if GetTokenInformation(TokenHandle, TokenGroups, TokenInformation, int_size, ReturnLength) then
      for i:=0 to TokenInformation.GroupCount-1 do
        with TokenInformation.Groups[i] do
        begin
        ConvertSidToStringSidA(TokenInformation.Groups[i].Sid, w);

           ListBox1.Items.Add(w);
            StrSIDToName(w, t, c1);
           ListBox2.Items.Add(t);
// hier gibt es die SID der Gruppe
        end;
    FreeMem(TokenInformation);
    CloseHandle(TokenHandle);
  end;

  ListBox1.Sorted := False;
  ListBox1.Sorted := True;
  ListBox2.Sorted := False;
  ListBox2.Sorted := True;

end;
  Mit Zitat antworten Zitat