寻找Delphi 7代码以检测程序是否以管理员权限启动? [英] Looking for Delphi 7 code to detect if a program is started with administrator rights?

查看:210
本文介绍了寻找Delphi 7代码以检测程序是否以管理员权限启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for working (obviously) Delphi 7 code so I can check whether my program is started with administrator rights.

预先感谢

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

到目前为止,已经回顾了答案中的代码,我意识到我的问题可能不太清楚,或者至少还不完整:

Having reviewed the code in the answers so far, I realise that my question maybe is not so clear, or at least is not complete:

  • 我想知道我的Delphi 7程序是否以以管理员身份运行"复选框设置为启动.

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

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

Just checking if you have administrator rights is not enough for this.

推荐答案

Windows API(用于)具有辅助功能(

The Windows API (used) to have a helper function (IsUserAnAdmin) to tell if you are running with administrative privileges.

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包装器函数;很好,因为它只是围绕其他代码的包装,您仍然可以调用自己:

The Shell32 wrapper function is deprecated; which is fine because it was just a wrapper around other code, which you can still call yourself:

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;

换句话说:此功能为您提供所需的答案:用户可以更新程序文件.

In other words: This function gives you the answer you want: Can the user update Program Files.

您需要对检查您是否是管理员组成员的代码感到厌倦.您可以是管理员组的成员,但没有任何管理权限.您还可以具有管理特权,但不能成为管理员组的成员.

You need to be weary of code that check if you're a member of the Administrator's group. You can be part of the Administrator's group, but not have any administrative privileges. You can also have administrative privileges, but not be part of the Administrator's group.

这篇关于寻找Delphi 7代码以检测程序是否以管理员权限启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆