Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2005 Mar : SID Conversion Issues Win XP Pro
| Subject: | SID Conversion Issues Win XP Pro |
| Posted by: | "Daniel Hobert" (dani..@cyberspacehq.com) |
| Date: | Wed, 23 Mar 2005 11:16:19 |
Hi All,
I'm trying to convert the retrieved SID to a string so that I can access the
information regarding the location of the directory where cookies are stored
on a machine. The problem I'm having is that the code I'm currently using
sometimes returns an SID that does not exist in the registry. I'm not real
familiar with this type of operation so I've hit a bit of a wall trying to
figure out why it sometimes doesn't return the proper value (it's a very
rare occurance but one that pops up just often enough to warrant
investigation).
Is there any way to assure I'm getting the right value? Is there an
alternate method for converting this to a string that is more consistent?
How is it that it could be returning the wrong value?
Here is the code I have been using, as gleaned from
borland.public.delphi.winapi (which I can't seem to "see" from my newsgroup
program).
procedure TForm1.Button1Click(Sender: TObject);
var
RefDomain : array[0..64] of Char;
RefDomainSize : DWORD;
SNU : SID_NAME_USE;
SID : PSID;
SidSize : DWORD;
NameBuf : array[0..80] of Char;
SizeBuf : cardinal;
computerBuf : array[0..80] of Char;
begin
SizeBuf := Sizeof(NameBuf);
GetUserName(NameBuf, SizeBuf);
SizeBuf := Sizeof(computerBuf);
GetComputerName(computerBuf,SizeBuf);
SidSize:=0;
RefDomainSize:=64;
LookupAccountName(computerBuf, NameBuf, Sid, SidSize, RefDomain,
RefDomainSize,SNU);
if GetLastError <> ERROR_INSUFFICIENT_BUFFER then RaiseLastWin32Error;
GetMem(Sid,SidSize);
RefDomainSize:=64;
if not
LookupAccountName(computerBuf,NameBuf,Sid,SidSize,RefDomain,RefDomainSize,SN
U) then
RaiseLastWin32Error
else
label1.caption := SIDToStr(sid);
end;
function TForm1.SIDToStr(sid : PSID) : string;
var
psia : PSIDIdentifierAuthority;
dwSubAuthorities : DWORD;
dwSidRev : DWORD;
dwCounter : DWORD;
begin
dwSidRev :=1;// SID_REVISION;
if IsValidSid (sid) then
begin
psia := GetSidIdentifierAuthority (sid);
dwSubAuthorities := GetSidSubAuthorityCount(sid)^;
result := Format ('S-%u-', [dwSidRev]);
if (psia^.Value[0] <> 0) or (psia^.Value[1] <> 0) then
begin
result := result + format('0x%02x%02x%02x%02x%02x%02x',
[psia^.Value[0], psia^.Value[1], psia^.Value[2],
psia^.Value[3], psia^.Value[4], psia^.Value [5]]);
end else
begin
result := result + format ('%u',
[DWORD (psia^.Value [5]) +
DWORD (psia^.Value [4] shl 8) +
DWORD (psia^.Value [3] shl 16) +
DWORD (psia^.Value [2] shl 24)]);
end;
for dwCounter := 0 to dwSubAuthorities - 1 do
result := result + Format ('-%u', [GetSidSubAuthority
(sid,dwCounter)^])
end else
raise Exception.Create ('Invalid SID');
end;
Cheers and TIA,
Daniel