gpt4 book ai didi

delphi - 正在寻找 Delphi 7 代码来检测程序是否以管理员权限启动?

转载 作者:行者123 更新时间:2023-12-03 14:42:22 29 4
gpt4 key购买 nike

我正在寻找工作(显然)Delphi 7代码,以便我可以检查我的程序是否以管理员权限启动

提前致谢

[--- 重要更新 ---]

查看到目前为止答案中的代码,我意识到我的问题可能不太清楚,或者至少不完整:

  • 我想知道我的 Delphi 7 程序是否在设置“以管理员身份运行”复选框的情况下启动

  • 换句话说:我想知道我的Delphi 7 程序是否可以在 c:\Program Files... 文件夹中创建/更新文件.

仅检查您是否具有管理员权限还不够。

最佳答案

Windows API(使用)具有辅助函数 ( IsUserAnAdmin ) 来判断您是否以管理权限运行。

OS              Account Type   UAC           IsUserAdmin
============== ============= ============ ===========
Windows XP Standard n/a False
Windows XP Administrator n/a True
Windows Vista Standard Disabled False
Windows Vista Administrator Disabled True
Windows Vista Standard Not Elevated False
Windows Vista Administrator Not Elevated False
Windows Vista Standard Elevated True
Windows Vista Administrator Elevated True

Shell32 包装函数已弃用;这很好,因为它只是 a wrapper around other code, which you can still call你自己:

function IsUserAdmin: Boolean;
var
b: BOOL;
AdministratorsGroup: PSID;
begin
{
This function returns true if you are currently running with admin privileges.
In Vista and later, if you are non-elevated, this function will return false
(you are not running with administrative privileges).
If you *are* running elevated, then IsUserAdmin will return true, as you are
running with admin privileges.

Windows provides this similar function in Shell32.IsUserAnAdmin.
But the function is deprecated, and this code is lifted
from the docs for CheckTokenMembership:
http://msdn.microsoft.com/en-us/library/aa376389.aspx
}

{
Routine Description: This routine returns TRUE if the callers
process is a member of the Administrators local group. Caller is NOT
expected to be impersonating anyone and is expected to be able to
open its own process and process token.
Arguments: None.
Return Value:
TRUE - Caller has Administrators local group.
FALSE - Caller does not have Administrators local group.
}
b := AllocateAndInitializeSid(
SECURITY_NT_AUTHORITY,
2, //2 sub-authorities
SECURITY_BUILTIN_DOMAIN_RID, //sub-authority 0
DOMAIN_ALIAS_RID_ADMINS, //sub-authority 1
0, 0, 0, 0, 0, 0, //sub-authorities 2-7 not passed
AdministratorsGroup);
if (b) then
begin
if not CheckTokenMembership(0, AdministratorsGroup, b) then
b := False;
FreeSid(AdministratorsGroup);
end;

Result := b;
end;

换句话说:这个函数给了你你想要的答案:用户能否更新Program Files。

您需要厌倦检查您是否是管理员组成员的代码。您可以成为管理员组的成员,但没有任何管理权限。您还可以拥有管理权限,但不属于管理员组。

关于delphi - 正在寻找 Delphi 7 代码来检测程序是否以管理员权限启动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6261865/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com