function TDupFind_Main.IsAdmin: Boolean;
const
{ operating system (OS)constants }
cOsUnknown = -1;
cOsWin95 = 0;
cOsWin98 = 1;
cOsWin98SE = 2;
cOsWinME = 3;
cOsWinNT = 4;
cOsWin2000 = 5;
cOsXP = 6;
cOsWin2003 = 7;
cOsWinNew = 8;
function GetOperatingSystem: Integer;
var
osVerInfo: TOSVersionInfo;
majorVer, minorVer: Integer;
begin
Result := cOsUnknown;
{ set operating system type flag }
osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
if GetVersionEx(osVerInfo)
then
begin
majorVer := osVerInfo.dwMajorVersion;
minorVer := osVerInfo.dwMinorVersion;
case osVerInfo.dwPlatformId
of
VER_PLATFORM_WIN32_NT:
{ Windows NT/2000 }
begin
if majorVer <= 4
then
Result := cOsWinNT
else if (majorVer = 5)
and (minorVer = 0)
then
Result := cOsWin2000
else if (majorVer = 5)
and (minorVer = 1)
then
Result := cOsXP
else if (majorVer = 5)
and (minorVer = 2)
then
Result := cOsWin2003
else if (majorVer = 5)
and (minorVer >= 3)
then
Result := cOsWinNew
else
Result := cOsUnknown;
end;
VER_PLATFORM_WIN32_WINDOWS:
{ Windows 9x/ME }
begin
if (majorVer = 4)
and (minorVer = 0)
then
Result := cOsWin95
else if (majorVer = 4)
and (minorVer = 10)
then
begin
if osVerInfo.szCSDVersion[1] = '
A'
then
Result := cOsWin98SE
else
Result := cOsWin98;
end
else if (majorVer = 4)
and (minorVer = 90)
then
Result := cOsWinME
else
Result := cOsUnknown;
end;
else
Result := cOsUnknown;
end;
end
else
Result := cOsUnknown;
end;
const
SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority =
(Value: (0, 0, 0, 0, 0, 5));
SECURITY_BUILTIN_DOMAIN_RID = $00000020;
DOMAIN_ALIAS_RID_ADMINS = $00000220;
var
hAccessToken: THandle;
ptgGroups: PTokenGroups;
dwInfoBufferSize: DWORD;
psidAdministrators: PSID;
x: Integer;
bSuccess: BOOL;
TmpOS: integer;
begin
if GetOperatingSystem < 4
then
Result := True
else
begin
Result := False;
bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True,
hAccessToken);
if not bSuccess
then
begin
if GetLastError = ERROR_NO_TOKEN
then
bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY,
hAccessToken);
end;
if bSuccess
then
begin
GetMem(ptgGroups, 1024);
bSuccess := GetTokenInformation(hAccessToken, TokenGroups,
ptgGroups, 1024, dwInfoBufferSize);
CloseHandle(hAccessToken);
if bSuccess
then
begin
AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, psidAdministrators);
{$R-}
for x := 0
to ptgGroups.GroupCount - 1
do
if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid)
then
begin
Result := True;
Break;
end;
{$R+}
FreeSid(psidAdministrators);
end;
FreeMem(ptgGroups);
end;
end;
end;