Created
June 4, 2014 23:52
-
-
Save auycro/f0f11750f0d49bf2a951 to your computer and use it in GitHub Desktop.
get HKLM.software.....{userSID}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uses | |
SysUtils, | |
Variants, | |
ActiveX, | |
ComObj; | |
function ConvertSidToStringSid(Sid: PSID; out StringSid: PChar): BOOL; stdcall; external 'ADVAPI32.DLL' name {$IFDEF UNICODE} 'ConvertSidToStringSidW'{$ELSE} 'ConvertSidToStringSidA'{$ENDIF}; | |
function SIDToString(ASID: PSID): string; | |
var | |
StringSid : PChar; | |
begin | |
ConvertSidToStringSid(ASID, StringSid); | |
Result := string(StringSid); | |
end; | |
function GetLocalComputerName: string; | |
var | |
nSize: DWORD; | |
begin | |
nSize := MAX_COMPUTERNAME_LENGTH + 1; | |
SetLength(Result, nSize); | |
if not GetComputerName(PChar(Result), nSize) then | |
Result := ''; | |
end; | |
function GetComputerSID:string; | |
var | |
Sid: PSID; | |
cbSid: DWORD; | |
cbReferencedDomainName : DWORD; | |
ReferencedDomainName: string; | |
peUse: SID_NAME_USE; | |
Success: BOOL; | |
lpSystemName : string; | |
lpAccountName: string; | |
begin | |
Sid:=nil; | |
try | |
lpSystemName:=''; | |
lpAccountName:=GetLocalComputerName; | |
cbSid := 0; | |
cbReferencedDomainName := 0; | |
// First call to LookupAccountName to get the buffer sizes. | |
Success := LookupAccountName(PChar(lpSystemName), PChar(lpAccountName), nil, cbSid, nil, cbReferencedDomainName, peUse); | |
if (not Success) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then | |
begin | |
SetLength(ReferencedDomainName, cbReferencedDomainName); | |
Sid := AllocMem(cbSid); | |
// Second call to LookupAccountName to get the SID. | |
Success := LookupAccountName(PChar(lpSystemName), PChar(lpAccountName), Sid, cbSid, PChar(ReferencedDomainName), cbReferencedDomainName, peUse); | |
if not Success then | |
begin | |
FreeMem(Sid); | |
Sid := nil; | |
RaiseLastOSError; | |
end | |
else | |
Result := SIDToString(Sid);//+' '+SIDToString(^cbSid); | |
end | |
else | |
RaiseLastOSError; | |
finally | |
if Assigned(Sid) then | |
FreeMem(Sid); | |
end; | |
end; | |
procedure Main(); | |
begin | |
Console.WriteLine(GetComputerSID); | |
end; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment