程序就是没有办法通过,急煞我也!(200分)

  • 主题发起人 主题发起人 ilovedelphi3
  • 开始时间 开始时间
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。
 
From MSDN:

AllocateAndInitializeSid
The AllocateAndInitializeSid function allocates and initializes a security identifier
(SID) with up to eight subauthorities.

BOOL AllocateAndInitializeSid(
PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
// pointer to identifier authority
BYTE nSubAuthorityCount, // count of subauthorities
DWORD dwSubAuthority0, // subauthority 0
DWORD dwSubAuthority1, // subauthority 1
DWORD dwSubAuthority2, // subauthority 2
DWORD dwSubAuthority3, // subauthority 3
DWORD dwSubAuthority4, // subauthority 4
DWORD dwSubAuthority5, // subauthority 5
DWORD dwSubAuthority6, // subauthority 6
DWORD dwSubAuthority7, // subauthority 7
PSID *pSid // pointer to pointer to SID
);

Parameters
pIdentifierAuthority
Pointer to an SID_IDENTIFIER_AUTHORITY structure, giving the top-level identifier
authority value to set in the SID.
nSubAuthorityCount
Specifies the number of subauthorities to place in the SID. This parameter also
identifies how many of the subauthority parameters have meaningful values. This
parameter must contain a value from 1 through 8.
For example, a value of 3 indicates that the subauthority values specified by the
dwSubAuthority0, dwSubAuthority1, and dwSubAuthority2 parameters have meaningful
values and to ignore the remainder.

...

QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Unsupported.
~~~~~~~~~~~~~~~~~~~~~ WIN95下DELPHI5 当然不行了(肯定不能运行,编译应该能通过)

还有:
Identifier authority Value
SECURITY_NULL_SID_AUTHORITY 0
SECURITY_WORLD_SID_AUTHORITY 1
SECURITY_LOCAL_SID_AUTHORITY 2
SECURITY_CREATOR_SID_AUTHORITY 3
SECURITY_NT_AUTHORITY 5
我找了半天,没有在Delphi5的pas文件中搜索到这些常量的定义,看来只能自己动手定义了。
(SECURITY_BUILTIN_DOMAIN_RID和DOMAIN_ALIAS_RID_ADMINS也一样)
 
照下面声明一下缺少的几个常(变量)就行了:
var
SECURITY_NT_AUTHORITY: SID_IDENTIFIER_AUTHORITY;
SECURITY_BUILTIN_DOMAIN_RID: Cardinal = $00000020;
DOMAIN_ALIAS_RID_ADMINS: Cardinal = $00000220;
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
SECURITY_NT_AUTHORITY.Value[5] := 5;
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;
Win95下编译可以通过,但只能在 WinNT/Win2K 下运行.
 
接受答案了.
 
接受答案了.
 
后退
顶部