delphi判断当前用户是否为管理员


本文整理自网络,侵删。

 Unit Unit1;


Interface

Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

Type
TForm1 = Class(TForm)
Procedure FormCreate(Sender: TObject);
Private
{ Private declarations }
Public
{ Public declarations }
End;

Var
Form1: TForm1;

Implementation

{$R *.dfm}

Function IsAdmin: Boolean;
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;
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.FormCreate(Sender: TObject);
Begin
If IsAdmin Then
ShowMessage('IsAdmin');
End;

End.

相关阅读 >>

Delphi getcomputername() getusername() 获取本机当前用户名

Delphi读取webbrowse中的图片显示在image中

Delphi webbrowser 加载html 将html代码转换成网页

processid, process handle, window handle 之间的互相转换

Delphi 枚举消息钩子的代码

Delphi过程函数传递参数的八种方式

Delphi 主窗体最小化时不显示在任务栏

Delphi 中 16 进制转换为 10 进制

Delphi函数指针,用于加载dll

Delphi 查询进程名进程id/进程路径 父进程/子进程

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...