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

Connaitre le SID du compte de l'utilisateur Windows qui exécute votre application [Code Source WD]

Discussion dans 'Windev' créé par Pascal, Mar 11, 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 connaitre le SID du compte de l'utilisateur Windows qui exécute votre application avec la fonction "LookupAccountName" 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).
       ERROR_NONE_MAPPED = 1332 // No mapping between account names and security IDs was done (winerror.h).
    FIN
     
    Les fonctions à déclarer :
    Code (Text):

    // Résumé : The GetUserName function retrieves the name of the user associated with the current thread.
    // Syntaxe :
    //[ <Résultat> = ] GetUserName ()
    //
    // Paramètres :
    //   Aucun
    // Valeur de retour :
    //    chaîne : //    A string to receive the user's logon name.
    //
    // Exemple :
    // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724432(v=vs.85).aspx
    //
    PROCEDURE GetUserName()

    LOCAL
       nResult est un entier = 0
       bufName est un Buffer = ""
       nSize est un entier = 0
       sName est une chaîne = ""
    nResult = API("advapi32.dll", "GetUserNameA", &bufName, &nSize)

    SI ((nResult = 0) ET (GetLastError() = ERROR_INSUFFICIENT_BUFFER)) ALORS
       SI (nSize > 0) ALORS bufName = Répète(Caract(0),nSize)
       nResult = API("advapi32.dll", "GetUserNameA", &bufName, &nSize)
    FIN

    SI (nResult = 0) ALORS sName = "" SINON sName = ChaîneRécupère(&bufName,crAdresseASCIIZ)

    RENVOYER (sName)

    // 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)

    // Résumé : The LookupAccountName function accepts the name of a system and an account as input. It retrieves a security identifier (SID) for the account and the name of the domain on which the account was found.
    // Syntaxe :
    //[ <Résultat> = ] LookupAccountName (<sSystemName> est chaîne, <sAccountName> est chaîne [, <sReferencedDomainName> est chaîne [, <nPeUse> est entier]])
    //
    // Paramètres :
    //   sSystemName (chaîne) : A string that specifies the name of the system. This string can be the name of a remote computer. If this string is NULL, the account name translation begins on the local system.
    //   sAccountName (chaîne) : A string that specifies the account name.
    //   sReferencedDomainName (chaîne - valeur par défaut="") : A string that receives the name of the domain or the computer name where the account name is found and when the function returns.
    //   nPeUse (entier - valeur par défaut=0) : A variable to a SID_NAME_USE enumerated type that indicates the type of the account when the function returns.
    // Valeur de retour :
    //    buffer : //    A buffer that receives the SID structure that corresponds to the account name pointed to by the sAccountName parameter.
    //
    // Exemple :
    // https://msdn.microsoft.com/en-us/library/windows/desktop/aa379159(v=vs.85).aspx
    //
    PROCEDURE LookupAccountName(LOCAL sSystemName est une chaîne, LOCAL sAccountName est une chaîne, sReferencedDomainName est une chaîne = "", nPeUse est un entier = 0)

    LOCAL
       nResult est un entier = 0
       nCchReferencedDomainName est un entier = 0
       bufSid est un Buffer = ""
       nCbSid est un entier = 0
               
    nResult = API("advapi32.dll", "LookupAccountNameA", &sSystemName, &sAccountName, &bufSid, &nCbSid, &sReferencedDomainName, &nCchReferencedDomainName, &nPeUse)

    SI ((nResult = 0) ET (GetLastError() = ERROR_INSUFFICIENT_BUFFER)) ALORS
       SI (nCbSid > 0) ALORS bufSid = Répète(" ",nCbSid)
       SI (nCchReferencedDomainName > 0) ALORS sReferencedDomainName = Répète(" ",nCchReferencedDomainName)
       nResult = API("advapi32.dll", "LookupAccountNameA", &sSystemName, &sAccountName, &bufSid, &nCbSid, &sReferencedDomainName, &nCchReferencedDomainName, &nPeUse)
    FIN

    SI (nResult = 0) ALORS
       bufSid = ""
       sReferencedDomainName = ""
       nPeUse = 0
    FIN

    RENVOYER (bufSid)

    // Résumé : The ConvertSidToStringSid function converts a security identifier (SID) to a string format suitable for display, storage, or transmission.
    // Syntaxe :
    //[ <Résultat> = ] ConvertSidToStringSid (<nSid> est entier)
    //
    // Paramètres :
    //   nSid (entier) : A memory address to the SID structure to be converted.
    // Valeur de retour :
    //    chaîne : //    A SID string.
    //
    // Exemple :
    // https://msdn.microsoft.com/en-us/library/windows/desktop/aa376399(v=vs.85).aspx
    //
    PROCEDURE ConvertSidToStringSid(LOCAL nSid est un entier)

    LOCAL
       nResult est un entier = 0
       sStringSid est une chaîne = ""
       nStringSid est un entier = 0
       
    nResult = API("advapi32.dll", "ConvertSidToStringSidA", nSid, &nStringSid)

    SI (nResult = 1) ALORS
       sStringSid = ChaîneRécupère(nStringSid,crAdresseASCIIZ)
       LocalFree(nStringSid)
    FIN

    SI (nResult = 0) ALORS sStringSid = ""

    RENVOYER (sStringSid)

    // Résumé : The LocalFree function frees the specified local memory object and invalidates its handle.
    // Syntaxe :
    //[ <Résultat> = ] LocalFree (<nhMem> est entier)
    //
    // Paramètres :
    //   nhMem (entier) : A handle to the local memory object.
    // Valeur de retour :
    //    entier : // If the function succeeds, the return value is NULL.
    //
    // Exemple :
    // https://msdn.microsoft.com/en-us/library/windows/desktop/aa366730(v=vs.85).aspx
    //
    PROCEDURE LocalFree(nhMem est un entier)

    LOCAL
       nErreur est un entier
       
    // Appel pour avoir les informations.
    nErreur = API("Kernel32.dll","LocalFree",nhMem)

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

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

    LOCAL
       bufSID est un Buffer = ""
       sDomainName est une chaîne = ""
       nSID_Type est un entier = 0
       nError est un entier = 0
       sName est une chaîne = ""
       
    bufSID = LookupAccountName(Null,GetUserName(),sDomainName,nSID_Type)

    SI (bufSID = "") ALORS
       nError = GetLastError()
       Trace("LookupAccountName failed with error : " + NumériqueVersChaîne(nError))
       SI (nError = ERROR_NONE_MAPPED) ALORS Trace("No mapping between account names and security IDs was done.")
    SINON
       Trace("The security identifier (SID) for the account is : " + ConvertSidToStringSid(&bufSID))
       Trace("The name of the domain or the computer name is : " + sDomainName)
       SELON nSID_Type
           CAS 1: Trace("The security identifier (SID) type : A user SID.")
           CAS 2: Trace("The security identifier (SID) type : A group SID.")
           CAS 3: Trace("The security identifier (SID) type : A domain SID.")
           CAS 4: Trace("The security identifier (SID) type : An alias SID.")
           CAS 5: Trace("The security identifier (SID) type : A SID for a well-known group.")
           CAS 6: Trace("The security identifier (SID) type : A SID for a deleted account.")
           CAS 7: Trace("The security identifier (SID) type : A SID that is not valid.")
           CAS 8: Trace("The security identifier (SID) type : A SID of unknown type.")
           CAS 9: Trace("The security identifier (SID) type : A SID for a computer.")
           CAS 10: Trace("The security identifier (SID) type : A mandatory integrity label SID.")  
       FIN
    FIN
     
    La DLL à décharger :
    Code (Text):

    SI (gnMy_DLL) ALORS DéchargeDLL(gnMy_DLL)
     
    Voila c'est fini. Maintenant vous pouvez connaitre le SID du compte de l'utilisateur Windows qui exécute votre application mais aussi le type de SID et le nom de l'ordinateur ou le nom du domaine de l'ordinateur.

    Code source et exécutable 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 :

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

     
  2. michel

    michel Well-Known Member
    MEMBRE WX

    Inscrit:
    Jan 1, 2018
    Messages:
    174
    J'aime reçus:
    598
    Merci Pascal..
     
    Pascal apprécie ceci.
  3. cyberripper

    cyberripper Active Member
    MEMBRE WX DUMP TEAM

    Inscrit:
    Jan 26, 2018
    Messages:
    76
    J'aime reçus:
    176
    Merci

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

    pour ton partage.

    @+ Cyber
     
    Pascal apprécie ceci.
  4. warjoen

    warjoen Active Member
    MEMBRE WX

    Inscrit:
    Jan 1, 2018
    Messages:
    155
    J'aime reçus:
    120
    Thanks for your sharing...
     
    Pascal apprécie ceci.
  5. PoloLeFou

    PoloLeFou Member

    Inscrit:
    Fev 6, 2018
    Messages:
    17
    J'aime reçus:
    27
    Merci, information toujours utile!
     
    Pascal apprécie ceci.

Partager cette page

Chargement...