如何判断当前用户是否有管理员权限(50分)

  • 主题发起人 主题发起人 太阳火
  • 开始时间 开始时间

太阳火

Unregistered / Unconfirmed
GUEST, unregistred user!
如题。当然是指NT/2000/XP系统啦
 
用api得到说属于的组

-----
http://www.8421.org
 
具体怎么样?
 
http://jcl.sourceforge.net/
下载jcl 里面有例子
 
可不可以给个具体的url
谢谢
 
给分把,测试过了
unit Unit1;
interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,StdCtrls;
const
SECURITY_NT_AUTHORITY: TSidIdentifierAuthority = (Value: (0, 0, 0, 0, 0, 5));
SECURITY_BUILTIN_DOMAIN_RID = ($00000020);
DOMAIN_ALIAS_RID_ADMINS = ($00000220);
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
function IsAdministrator: Boolean;
private
{ Private declarations }
public
{ Public declarations }

end;

var
Form1: TForm1;

implementation

{$R *.dfm}
function TForm1.IsAdministrator: Boolean;
var
psidAdmin: Pointer;
Token: THandle;
Count: DWORD;
TokenInfo: PTokenGroups;
HaveToken: Boolean;
I: Integer;
begin
Result := False;
psidAdmin := nil;
TokenInfo := nil;
HaveToken := False;
try
HaveToken := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, Token);
if (not HaveToken) and (GetLastError = ERROR_NO_TOKEN) then
HaveToken := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, Token);
if HaveToken then
begin
Win32Check(AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0,
psidAdmin));
if GetTokenInformation(Token, TokenGroups, nil, 0, Count) or
(GetLastError <> ERROR_INSUFFICIENT_BUFFER) then
RaiseLastOSError;
TokenInfo := PTokenGroups(AllocMem(Count));
Win32Check(GetTokenInformation(Token, TokenGroups, TokenInfo, Count, Count));
for I := 0 to TokenInfo^.GroupCount - 1 do
begin
{$R-} // Groups is an array [0..0] of TSIDAndAttributes, ignore ERangeError
Result := EqualSid(psidAdmin, TokenInfo^.Groups.Sid);
{$R+}
if Result then Break;
end;
end;
finally
if TokenInfo <> nil then FreeMem(TokenInfo);
if HaveToken then CloseHandle(Token);
if psidAdmin <> nil then FreeSid(psidAdmin);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if IsAdministrator then showmessage(‘是超级用户‘);
end;

end.

 
接受答案了.
 
后退
顶部