判断当前用户是否是超级管理员
function IsAdmin: Boolean;
var
hAccessToken: THandle;
ptgGroups: PTokenGroups;
dwInfoBufferSize: DWORD;
psidAdministrators: PSID;
x: Integer;
bSuccess: BOOL;
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;
procedure TForm1.Button1Click(Sender: TObject);
begin
if isAdmin then
begin
ShowMessage('Logged in as Administrator');
end;
---------------------------------------
判断指定用户是否是超级管理员
Author: Vasko Mitanov <vaskom@mt.net.mk>
Date: 2006年1月29日 4:34
From: borland.public.delphi.nativeapi.win32
type
PLocalGroupInfo0 = ^TLocalGroupInfo0;
_LOCALGROUP_USERS_INFO_0 = record
lgrui0_name: LPWSTR;
end;
TLocalGroupInfo0 = _LOCALGROUP_USERS_INFO_0;
GROUP_INFO_0 = _LOCALGROUP_USERS_INFO_0;
.
.
.
function NetUserGetLocalGroups(
servername:LPCWSTR;
username:LPCWSTR;
level:DWORD;
flags:DWORD;
var bufptr:Pointer;
prefmaxlen:DWORD;
var entriesread:DWORD;
var totalentries:DWORD):DWORD; stdcall; external 'netapi32.dll' name
'NetUserGetLocalGroups';
function NetApiBufferFree(
buffer: Pointer):DWORD; stdcall; external 'netapi32.dll' name
'NetApiBufferFree';
.
.
.
procedure GetGroupsByUserAndMachine(
MachineName: WideString;
UserName: WideString;
var Groups: TStrings);
var
lui0 : Pointer;
curlui0 : Pointer;
i : Integer;
ResStatus : DWORD;
EntriesRead: DWORD;
EntriesTotal: DWORD;
begin
if (Groups = nil) then Exit;
Groups.Clear;
if (UserName = '') then Exit;
lui0 := nil;
curlui0 := nil;
ResStatus := NetUserGetLocalGroups(PWideChar(MachineName),
PWideChar(UserName),
0, 1, lui0, MAX_PREFERRED_LENGTH, EntriesRead, EntriesTotal);
if ((ResStatus = NERR_SUCCESS) and (EntriesRead > 0)) then
begin
curlui0 := lui0;
for i := 0 to EntriesRead - 1 do
begin
Groups.Add(PLocalGroupInfo0(lui0)^.lgrui0_name);
Inc(Integer(lui0), sizeof(Pointer));
end;
end;
NetApiBufferFree(curlui0);
end;
function IsUserAdmin(UserName:string; MachineName:string):boolean;
var
Groups:TStrings;
i:integer;
begin
Groups := TStringList.Create;
GetGroupsByUserAndMachine(MachineName, UserName, Groups);
Result := False;
for i:= 0 to Groups.Count - 1 do
begin
if (LowerCase(Groups[i]) = 'administrators') then
begin
Result := True;
break;
end;
end;
Groups.Free;
end;
Usage:
procedure TForm1.Button1Click(Sender: TObject);
begin
if IsUserAdmin('Vasko Mitanov', '\\TITAN') then
Edit1.Text := 'Admin'
else
Edit1.Text := 'Normal User';
end;
If the user is located on local machine then you can
simply modify line:
ResStatus := NetUserGetLocalGroups(PWideChar(MachineName),
PWideChar(UserName),
0, 1, lui0, MAX_PREFERRED_LENGTH, EntriesRead, EntriesTotal);
with:
ResStatus := NetUserGetLocalGroups(nil PWideChar(UserName),
0, 1, lui0, MAX_PREFERRED_LENGTH, EntriesRead, EntriesTotal);