来自grhunter:<br><br>const <br>SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = <br>(Value: (0, 0, 0, 0, 0, 5)); <br>SECURITY_BUILTIN_DOMAIN_RID = $00000020; <br>DOMAIN_ALIAS_RID_ADMINS = $00000220; <br><br>function IsAdmin: Boolean; <br>var <br>hAccessToken: THandle; <br>ptgGroups: PTokenGroups; <br>dwInfoBufferSize: DWORD; <br>psidAdministrators: PSID; <br>x: Integer; <br>bSuccess: BOOL; <br>begin <br>Result := False; <br>bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True, <br>hAccessToken); <br>if not bSuccess then <br>begin <br>if GetLastError = ERROR_NO_TOKEN then <br>bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, <br>hAccessToken); <br>end; <br>if bSuccess then <br>begin <br>GetMem(ptgGroups, 1024); <br>bSuccess := GetTokenInformation(hAccessToken, TokenGroups, <br>ptgGroups, 1024, dwInfoBufferSize); <br>CloseHandle(hAccessToken); <br>if bSuccess then <br>begin <br>AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2, <br>SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, <br>0, 0, 0, 0, 0, 0, psidAdministrators); <br>{$R-} <br>for x := 0 to ptgGroups.GroupCount - 1 do <br>if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid) then <br>begin <br>Result := True; <br>Break; <br>end; <br>{$R+} <br>FreeSid(psidAdministrators); <br>end; <br>FreeMem(ptgGroups); <br>end; <br>end; <br><br>procedure TForm1.Button1Click(Sender: TObject); <br>begin <br>if isAdmin then <br>begin <br>ShowMessage('以系统管理员身份登录'); <br>end; <br>end.