Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   API de Windows (https://www.clubdelphi.com/foros/forumdisplay.php?f=7)
-   -   ¿Cómo saber si se ejecuta programa en modo administrador? (https://www.clubdelphi.com/foros/showthread.php?t=90715)

cozina 18-08-2016 16:34:04

¿Cómo saber si se ejecuta programa en modo administrador?
 
Hola a todos, quisiera saber si hay alguna forma de conseguir saber si mi programa se está ejecutando en modo administrador o como usuario normal. ¿Es posible?
Teniendo en cuenta que puede ejecutarse en distintas versiones de windows.
Muchas gracias.

dec 18-08-2016 17:35:48

Hola,

Hace tiempo que ocupo el siguiente código, a ver si te sirve a ti también:

Código Delphi [-]

uses
  Windows;

const
 SECURITY_NT_AUTHORITY : TSIDIdentifierAuthority = ( Value : ( 0, 0, 0, 0, 0, 5 ) );
 SECURITY_BUILTIN_DOMAIN_RID = $00000020;
 DOMAIN_ALIAS_RID_ADMINS = $00000220;
 DOMAIN_ALIAS_RID_USERS = $00000221;
 DOMAIN_ALIAS_RID_GUESTS = $00000222;
 DOMAIN_ALIAS_RID_POWER_USERS = $00000223;

function CheckTokenMembership(TokenHandle: THandle; SidToCheck: PSID; var IsMember: BOOL): BOOL; stdcall; external advapi32;

// http://stackoverflow.com/questions/6...dministrator-r
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;

Ejemplo de uso:

Código Delphi [-]
if IsUserAdmin() then
begin
  // Whatever
end;

En el hilo de Stackoverflow donde lo encontré puedes acaso obtener más información.

cozina 18-08-2016 17:58:41

Hola, funciona perfectamente, muchísimas gracias. Ustedes sí que saben.

No me sale nada para poder darle una puntuación o un gracias virtual, seguramente no tengo permisos.

dec 18-08-2016 23:36:26

Hola,

Cita:

Empezado por cozina (Mensaje 507868)
Hola, funciona perfectamente, muchísimas gracias. Ustedes sí que saben.

No me sale nada para poder darle una puntuación o un gracias virtual, seguramente no tengo permisos.

Bueno. Vaya el mérito en este caso a quien respondió a la questión en StackOverflow: esta vez sí que me limité a copiar y pegar, prácticamente...


La franja horaria es GMT +2. Ahora son las 18:21:17.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi