• 判断当前进程是否以管理员权限启动的


    uses
     shlobj;
     type
     WELL_KNOWN_SID_TYPE = (
     WinNullSid,
     WinWorldSid,
     WinLocalSid,
     WinCreatorOwnerSid,
     WinCreatorGroupSid,
     WinCreatorOwnerServerSid,
     WinCreatorGroupServerSid,
     WinNtAuthoritySid,
     WinDialupSid,
     WinNetworkSid,
     WinBatchSid,
     WinInteractiveSid,
     WinServiceSid,
     WinAnonymousSid,
     WinProxySid,
     WinEnterpriseControllersSid,
     WinSelfSid,
     WinAuthenticatedUserSid,
     WinRestrictedCodeSid,
     WinTerminalServerSid,
     WinRemoteLogonIdSid,
     WinLogonIdsSid,
     WinLocalSystemSid,
     WinLocalServiceSid,
     WinNetworkServiceSid,
     WinBuiltinDomainSid,
     WinBuiltinAdministratorsSid,
     WinBuiltinUsersSid,
     WinBuiltinGuestsSid,
     WinBuiltinPowerUsersSid,
     WinBuiltinAccountOperatorsSid,
     WinBuiltinSystemOperatorsSid,
     WinBuiltinPrintOperatorsSid,
     WinBuiltinBackupOperatorsSid,
     WinBuiltinReplicatorSid,
     WinBuiltinPreWindows2000CompatibleAccessSid,
     WinBuiltinRemoteDesktopUsersSid,
     WinBuiltinNetworkConfigurationOperatorsSid,
     WinAccountAdministratorSid,
     WinAccountGuestSid,
     WinAccountKrbtgtSid,
     WinAccountDomainAdminsSid,
     WinAccountDomainUsersSid,
     WinAccountDomainGuestsSid,
     WinAccountComputersSid,
     WinAccountControllersSid,
     WinAccountCertAdminsSid,
     WinAccountSchemaAdminsSid,
     WinAccountEnterpriseAdminsSid,
     WinAccountPolicyAdminsSid,
     WinAccountRasAndIasServersSid,
     WinNTLMAuthenticationSid,
     WinDigestAuthenticationSid,
     WinSChannelAuthenticationSid,
     WinThisOrganizationSid,
     WinOtherOrganizationSid,
     WinBuiltinIncomingForestTrustBuildersSid,
     WinBuiltinPerfMonitoringUsersSid,
     WinBuiltinPerfLoggingUsersSid,
     WinBuiltinAuthorizationAccessSid,
     WinBuiltinTerminalServerLicenseServersSid);
     {$EXTERNALSYM WELL_KNOWN_SID_TYPE}
     TWellKnownSidType = WELL_KNOWN_SID_TYPE;
     
    function CreateWellKnownSid(WellKnownSidType: WELL_KNOWN_SID_TYPE; DomainSid: PSID;
     pSid: PSID; var cbSid: DWORD): BOOL; stdcall;external advapi32 name 'CreateWellKnownSid';
     function CheckTokenMembership(TokenHandle: THANDLE; SidToCheck: PSID; var IsMember: BOOL): BOOL; stdcall; external advapi32 name 'CheckTokenMembership';
     Function GetProcessElevation(var pElevationType: TTokenElevationType;
     var pIsAdmin: LongBool): Boolean;
     const
     SECURITY_MAX_SID_SIZE = 68;
     VAR
     hToken: THandle;
     r: Cardinal;
     adminSID: array [0 .. SECURITY_MAX_SID_SIZE - 1] of byte;
     sidSize: Cardinal;
     e: Integer;
     hUnfilteredToken: THandle;
     Begin
     Result := False;
     sidSize := SizeOf(adminSID);
     if OpenProcessToken(GetCurrentProcess, TOKEN_QUERY, hToken) and
     Windows.GetTokenInformation(hToken, Windows.TokenElevationType,
     @pElevationType, SizeOf(pElevationType), r) and
     CreateWellKnownSid(WinBuiltinAdministratorsSid, nil, PSid(@adminSID),
     sidSize) then
     begin
     case pElevationType of
     TokenElevationTypeLimited:
     begin
     if GetTokenInformation(hToken, TokenLinkedToken, @hUnfilteredToken,
     SizeOf(hUnfilteredToken), r) and
     (CheckTokenMembership(hUnfilteredToken, PSid(@adminSID),
     pIsAdmin)) then
     begin
     Result := true;
     CloseHandle(hUnfilteredToken);
     end;
     end;
     
    else
     begin
     pIsAdmin := IsUserAnAdmin();
     Result := true;
     end;
     end;
     end
     else
     begin
     e := GetLastError();
     if e = 0 then
     begin
     GetTickCount;
     end;
     end;
     CloseHandle(hToken);
     
    End;
     
    调用示例:
     
    procedure TForm4.Button1Click(Sender: TObject);
     var
     pIsAdmin: LongBool;
     pElevationType: TTokenElevationType;
     begin
     if GetProcessElevation(pElevationType, pIsAdmin) then
     begin
     case pElevationType of
     TokenElevationTypeLimited:
     ShowMessage('这是一个受限用户');
     TokenElevationTypeFull:
     ShowMessage('这是一个拥有管理员权限的用户');
     TokenElevationTypeDefault:
     ShowMessage('这是一个默认的用户');
     end;
     end;
     end;
  • 相关阅读:
    【转】Android Web Server
    【转】Android Http Server
    一步一步教你150行代码实现简书滑动返回效果
    Android代码内存优化建议 OnTrimMemory
    Android应用Activity、Dialog、PopWindow、Toast窗口添加机制及源码分析
    Android 快速实现 ViewPager 滑动页卡切换(可用作整个 app上导航)
    阴影效果 ShadowLayout 布局实现(让控件实现立体效果)
    ViewDragHelper让你处理View拖动时,代码减半!
    Android淘宝电影日期选项卡的实现-tab 栏居中滚动
    Android开源项目分类汇总
  • 原文地址:https://www.cnblogs.com/jxgxy/p/3335044.html
Copyright © 2020-2023  润新知