delphi 检查当前用户权限  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 检查当前用户权限


delphi 检查当前用户权限

www.3464.com

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.
推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.042416095733643 seconds