1. Ce site utilise des cookies. En continuant à utiliser ce site, vous acceptez l'utilisation des cookies. En savoir plus.
  2. Bonjour tout le monde ! Veillez consulter la Politique de forum pour comprendre nos règles, Merci a vous !
    Rejeter la notice

Savoir si votre application est exécuter en tant qu'administrateur [Code Source WD]

Discussion dans 'Windev' créé par Pascal, Fev 18, 2018.

  1. Pascal

    Pascal Active Member
    MEMBRE WX

    Inscrit:
    Fev 11, 2018
    Messages:
    82
    J'aime reçus:
    126
    Bonjour tout le monde,

    Si cela vous intéresse ou peut aider quelqu'un d'autre, vous trouverez ci-dessous un code qui permet de savoir si votre application est exécutée en tant qu'administrateur avec la fonction "CheckTokenMembership" de l'API "advapi32.dll".

    [​IMG]
    Les constantes à déclarer :
    Code (Text):

    CONSTANT
       ERROR_INSUFFICIENT_BUFFER = 122 // The data area passed to a system call is too small (winerror.h).
       WinBuiltinAdministratorsSid = 26 // Indicates a SID that matches the administrator group (Winnt.h).
    FIN
     
    Les fonctions à déclarer :
    Code (Text):

    // Résumé : The CreateWellKnownSid function creates a SID for predefined aliases.
    // Syntaxe :
    //[ <Résultat> = ] CreateWellKnownSid (<nWellKnownSidType> est entier [, <nDomainSid> est entier])
    //
    // Paramètres :
    //   nWellKnownSidType (entier) : Member of the WELL_KNOWN_SID_TYPE enumeration that specifies what the SID will identify.
    //   nDomainSid (entier - valeur par défaut=0) : A memory address to a SID that identifies the domain to use when creating the SID. Pass NULL to use the local computer.
    // Valeur de retour :
    //    buffer : //    The return value is a buffer that receives the initialized SID structure.
    //
    // Exemple :
    // https://msdn.microsoft.com/fr-fr/library/windows/desktop/aa446585(v=vs.85).aspx
    //
    PROCEDURE CreateWellKnownSid(LOCAL nWellKnownSidType est un entier, LOCAL nDomainSid est un entier = 0)

    LOCAL
       nResult est un entier = 0
       nCbSid est un entier = 0
       bufSID est un Buffer = ""

    nResult = API("advapi32.dll", "CreateWellKnownSid", nWellKnownSidType, nDomainSid, &bufSID, &nCbSid)

    SI ((nResult = 0) ET (GetLastError() = ERROR_INSUFFICIENT_BUFFER)) ALORS
       SI (nCbSid > 0) bufSID = Répète(" ",nCbSid)
       nResult = API("advapi32.dll", "CreateWellKnownSid", nWellKnownSidType, nDomainSid, &bufSID, &nCbSid)
    FIN

    SI (nResult = 0) ALORS bufSID = ""

    RENVOYER (bufSID)

    // Résumé : The CheckTokenMembership function determines whether a specified security identifier (SID) is enabled in an access token.
    // Syntaxe :
    //[ <Résultat> = ] CheckTokenMembership (<nTokenHandle> est entier, <nSidToCheck> est entier)
    //
    // Paramètres :
    //   nTokenHandle (entier) : A handle to an access token. If nTokenHandle is NULL, CheckTokenMembership uses the impersonation token of the calling thread.
    //   nSidToCheck (entier) : A memory address to a SID structure.
    // Valeur de retour :
    //    entier : //    If the SID is present and has the SE_GROUP_ENABLED attribute, the return value is nonzero otherwise the return value is zero. If the function fails, the return value is -1.
    //
    // Exemple :
    // https://msdn.microsoft.com/fr-fr/library/windows/desktop/aa376389(v=vs.85).aspx
    //
    PROCEDURE CheckTokenMembership(LOCAL nTokenHandle est un entier, LOCAL nSidToCheck est un entier)

    LOCAL
       nResult est un entier = 0
       nIsMember est un entier = 0
     
    nResult = API("advapi32.dll", "CheckTokenMembership", nTokenHandle, nSidToCheck, &nIsMember)

    SI (nResult = 0) ALORS
       nResult = -1
       RENVOYER(nResult)  
    FIN

    RENVOYER(nIsMember)

    // Résumé : The GetLastError function retrieves the calling thread's last-error code value. The last-error code is maintained on a per-thread basis. Multiple threads do not overwrite each other's last-error code.
    // Syntaxe :
    //[ <Résultat> = ] GetLastError ()
    //
    // Paramètres :
    //   Aucun
    // Valeur de retour :
    //    entier : // The return value is the calling thread's last-error code.
    //
    // Exemple :
    // http://msdn.microsoft.com/en-us/library/windows/desktop/ms679360(v=vs.85).aspx
    //
    PROCEDURE GetLastError()

    LOCAL
       nError est un entier
     
    // Appel pour avoir les informations.
    nError = API("Kernel32.dll","GetLastError")

    RENVOYER(nError)
     
    La DLL à charger :
    Code (Text):

    gnMy_DLL est un entier = 0
    gnMy_DLL = ChargeDLL("advapi32.dll")
     
    Le code pour utiliser la fonction "CheckTokenMembership" (pour l'exemple mettre ce code dans un bouton) :
    Code (Text):

    LOCAL
       nResult est un entier = 0
       bufSID est un Buffer = CreateWellKnownSid(WinBuiltinAdministratorsSid) // SID = S1-5-32-544
     
    SI (bufSID <> "") ALORS
       nResult = CheckTokenMembership(Null,&bufSID)
       SI (nResult = -1) ALORS
           Trace("CheckTokenMembership failed with error : " + NumériqueVersChaîne(GetLastError()))
           Trace(ErreurInfo())
       SINON
           SI (nResult = 1) ALORS
               Trace("The caller's process is a member of the Administrators local group.")
           SINON
               Trace("The caller's process is not a member of the Administrators local group.")  
           FIN
       FIN
    SINON
       Trace("CreateWellKnownSid failed with error : " + NumériqueVersChaîne(GetLastError()))
       Trace(ErreurInfo())  
    FIN
     
    La DLL à décharger :
    Code (Text):

    SI (gnMy_DLL) ALORS DéchargeDLL(gnMy_DLL)
     
    Voila c'est fini. Maintenant vous pouvez détecter si votre application est exécuter en tant qu'administrateur.

    Code source et executable au format windev 15 :

    Bonjour visiteur, Merci de vous Inscrire ou de vous connectez pour voir les liens!



    Bon dev à vous tous !!!!

    Cordialement,
    Pascal

    PS : Si l'UAC est déactivé la fonction renvoie toujours "The caller's process is a member of the Administrators local group."
    Pour connaitre l'état de l'UAC avec windev :

    Bonjour visiteur, Merci de vous Inscrire ou de vous connectez pour voir les liens!

     
    #1 Pascal, Fev 18, 2018
    Dernière édition: Fev 18, 2018
  2. charlie

    charlie Active Member
    MEMBRE WX

    Inscrit:
    Jan 1, 2018
    Messages:
    201
    J'aime reçus:
    190
    Intéressant pour le débug à distance,merci.
     
    Pascal apprécie ceci.
  3. Fakirato

    Fakirato New Member
    WXG24 MEMBRE WX WXG 23 WXG 22 WXG 21

    Inscrit:
    Déc 30, 2017
    Messages:
    302
    J'aime reçus:
    816
    Merci, très pratique pour les app qui nécessitent des autorisations au fichier et dossier système !
     
    Pascal apprécie ceci.

Partager cette page

Chargement...