I
ilovedelphi3
Unregistered / Unconfirmed
GUEST, unregistred user!
//判断登陆用户是否为administrator:
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;
我是WIN95下DELPHI5。