首页
社区
课程
招聘
[求助]判断当前用户是否具有管理员权限,可是该函数怎么总是返回FALSE呢(DELPHI)
发表于: 2008-1-6 10:22 6659

[求助]判断当前用户是否具有管理员权限,可是该函数怎么总是返回FALSE呢(DELPHI)

2008-1-6 10:22
6659
function IsAdmin: Boolean;
const
  SECURITY_BUILTIN_DOMAIN_RID=32;
  DOMAIN_ALIAS_RID_ADMINS =544;
var
  hAccessToken: THandle;
  ptgGroups: PTokenGroups;
  dwInfoBufferSize: DWORD;
  sid: PSID;
  x: Integer;
  bSuccess: BOOL;
  sia:TSIDIdentifierAuthority;
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(_SID_IDENTIFIER_AUTHORITY(sia), 2,
           SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,0, 0, 0, 0, 0, 0, sid) ;
      {$R-}
      for x := 0 to ptgGroups.GroupCount-1 do begin
        if EqualSid(sid, ptgGroups.Groups[x].Sid) then
        begin
          Result := True;
          Break;
        end;
      end;
      {$R+}
      FreeSid(sid);
    end;
    FreeMem(ptgGroups);
  end;
end;

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (2)
雪    币: 325
活跃值: (97)
能力值: ( LV13,RANK:530 )
在线值:
发帖
回帖
粉丝
2
IsUserAnAdmin Function

--------------------------------------------------------------------------------

Tests whether the current user is a member of the Administrator's group.

Syntax

BOOL IsUserAnAdmin(VOID);
Return Value

Returns TRUE if the user is a member of the Administrator's group; otherwise, FALSE.

Remarks

This function is a wrapper for CheckTokenMembership. It is recommended to call that function directly to determine Administrator group status rather than calling IsUserAnAdmin.

Note  This function is available through Windows Vista. It might be altered or unavailable in subsequent versions of Microsoft Windows.
Function Information

Minimum DLL Version shell32.dll version 5.0 or later
Custom Implementation No
Header shlobj.h
Import library shell32.lib
Minimum operating systems Windows 2000

See Also

CheckTokenMembership

F5 。
2008-1-6 19:16
0
雪    币: 212
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
谢谢楼上的,搞定了
2008-1-7 10:00
0
游客
登录 | 注册 方可回帖
返回
//