博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
判断当前进程是否以管理员权限启动的
阅读量:6820 次
发布时间:2019-06-26

本文共 3452 字,大约阅读时间需要 11 分钟。

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;

 

转载于:https://www.cnblogs.com/jxgxy/p/3335044.html

你可能感兴趣的文章
IT兄弟连 JavaWeb教程 Servlet会话跟踪 Cookie技术原理
查看>>
js算法: 图的两种表示方法以及广度优先算法
查看>>
CSS定位问题(3):相对定位,绝对定位
查看>>
如何给网站加入优雅的实时反爬虫策略
查看>>
手动配置无线网卡
查看>>
OSChina 周四乱弹 ——黑丝短裙java程序员同事
查看>>
设置iptables之后不能正常访问ftp解决方法
查看>>
maven使用国内镜像
查看>>
移动端rem布局
查看>>
改变状态栏的颜色
查看>>
UIImagePickerController说明
查看>>
01.C语言入门
查看>>
Spring-Batch中MapJobExplorerFactoryBean的配置方式
查看>>
jsp与iframe跨域访问的一个方法
查看>>
ViewPager + Fragment 取消预加载
查看>>
BigDecimal 02 - 注意事项
查看>>
用js玩桌球游戏
查看>>
maven下运行jetty报错
查看>>
android 配置framework 使应用首选安装在SD卡
查看>>
h5 点击表单 顶部fixed 菜单栏 上移
查看>>