delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
495 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Converting a SID to a stringGo to Bryan Ashby's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 5.x (or higher)
Category:
Security
Skill Level:
Scoring:
Last Update:
10/03/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet SID ConvertSidToStringSid Security ADVAPI32 NT 2000 XP
Times Scored:
8
Visits:
4525
Uploader: Bryan Ashby
Company: ContentWatch
Reference: N/A
 
Question/Problem/Abstract:
I've seen quite a few people ask how to convert a Security Identifier (SID) into a human-readable string. Since I was not able to find much help on the subject on newsgroups/etc., I decided to post some example code..
Answer:



Here are some wrappers and helper functions to make your life easier:

...

const
  SID_REVISION  = 1;

  FILENAME_ADVAPI32     = 'ADVAPI32.DLL';

  PROC_CONVERTSIDTOSTRINGSIDA   = 'ConvertSidToStringSidA';


type
  TConvertSidToStringSidA = function (Sid: PSID;
    var StringSid: LPSTR): BOOL; stdcall;

function WinGetSidStr (Sid : PSid) : string;
var
  SidToStr      : TConvertSidToStringSidA;
  h             : LongWord;
  Buf           : array [0..MAX_PATH - 1] of char;
  p             : PAnsiChar;
begin
  h := LoadLibrary (FILENAME_ADVAPI32);
  if h <> 0 then
  try
    @SidToStr := GetProcAddress (h, PROC_CONVERTSIDTOSTRINGSIDA);
    if @SidToStr <> nil then
    begin
      FillChar (Buf, SizeOf(Buf), 0);

      if SidToStr (Sid, p) then
        Result := '[' + string(p) + ']';

      LocalFree (LongWord(p));
    end;
  finally
    FreeLibrary (h);
  end;
end;

function GetSidStr (Sid : PSid) : string;
var
  Psia          : PSIDIdentifierAuthority;
  SubAuthCount  : LongWord;
  i             : LongWord;
begin
  if IsValidSid (Sid) then
  begin
    //
    // Win 2000+ contains ConvertSidToStringSidA() in advapi32.dll so we just
    // use it if we can
    //
    if (Win32Platform = VER_PLATFORM_WIN32_NT) and
      (Win32MajorVersion >= 5) then
      Result := WinGetSidStr (Sid)
    else
    begin
      Psia := GetSidIdentifierAuthority (Sid);
      SubAuthCount := GetSidSubAuthorityCount (Sid)^;
      Result := Format('[S-%u-', [SID_REVISION]);
      if ((Psia.Value[0] <> 0) or (Psia.Value[1] <> 0)) then
        Result := Result + Format ('%.2x%.2x%.2x%.2x%.2x%.2x', [Psia.Value[0],
          Psia.Value[1], Psia.Value[2], Psia.Value[3], Psia.Value[4],
          Psia.Value[5]])
      else
        Result := Result + Format ('%u', [LongWord(Psia.Value[5]) +
          LongWord(Psia.Value[4] shl 8) + LongWord(Psia.Value[3] shl 16) +
          LongWord(Psia.Value[2] shl 24)]);

      for i := 0 to SubAuthCount - 1 do
        Result := Result + Format ('-%u', [GetSidSubAuthority(Sid, i)^]);

      Result := Result + ']';
    end;
end;
end;

function GetLocalUserSidStr (const UserName : string) : string;
var
  RefDomain     : array [0..MAX_PATH - 1] of char;      // enough
  RefDomainSize : LongWord;
  Snu           : SID_NAME_USE;
  Sid           : PSid;
  SidSize       : LongWord;
begin
  SidSize := 0;
  RefDomainSize := SizeOf(RefDomain);
  Sid := nil;
  FillChar (RefDomain, SizeOf(RefDomain), 0);
  LookupAccountName (nil, PChar(UserName), Sid, SidSize, RefDomain,
  RefDomainSize, Snu);
  Sid := AllocMem (SidSize);
  try
    RefDomainSize := SizeOf(RefDomain);
    if LookupAccountName (nil, PChar(UserName), Sid, SidSize, RefDomain,
      RefDomainSize, Snu) then
      Result := GetSidStr (Sid);
  finally
    FreeMem (Sid, SidSize);
  end;
end;

...

To get a local users SID string simply call GetLocalUserSidStr('SomeUserName');






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
S. Carter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)