CodeS-SourceS
Rechercher un code, un tuto, une réponse

Savoir si un usager a access à un fichier ou un dossier

Soyez le premier à donner votre avis sur cette source.

Snippet vu 4 880 fois - Téléchargée 28 fois

Contenu du snippet

reçoi un nom de fichier et un mask et permet de savoir si on a acces à ce dossier

j'ai pris une partie du code ici, masi j'ai du modifier beaucoup pour avoir se résultat. par contre, ça risque d,en aider plusieurs

exemple : If GetAccess(fichier, GENERIC_EXECUTE Or GENERIC_READ) Then

Source / Exemple :


Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Declare Function LookupAccountName Lib "advapi32.dll" Alias "LookupAccountNameA" (lpSystemName As String, ByVal lpAccountName As String, sid As Any, cbSid As Long, ByVal ReferencedDomainName As String, cbReferencedDomainName As Long, peUse As Long) As Long
Declare Function GetFileSecurity Lib "advapi32.dll" Alias "GetFileSecurityA" (ByVal lpFileName As String, ByVal RequestedInformation As Long, pSecurityDescriptor As Byte, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
Declare Function GetFileSecurityN Lib "advapi32.dll" Alias "GetFileSecurityA" (ByVal lpFileName As String, ByVal RequestedInformation As Long, ByVal pSecurityDescriptor As Long, ByVal nLength As Long, lpnLengthNeeded As Long) As Long

' The file/security API call constants.
' Refer to the MSDN for more information on how/what these constants
' are used for.
Public Const DACL_SECURITY_INFORMATION = &H4

Public Const GENERIC_READ = &H80000000
Public Const GENERIC_EXECUTE = &H20000000

'savoir si on a acces au fichier ou dossier
Public Function GetAccess(ByVal sFileName As String, lMask As Long) As Boolean

    Dim strUserName As String
    Dim lResult As Long            ' Result of various API calls.
    
    Dim bUserSid(255) As Byte      ' This will contain your SID.
    
    Dim lUserSID As Long           ' Used to hold the SID of the
                                  ' current user.
    Dim lSIDType As Long              ' The type of SID info we are
                                     ' getting back.
    Dim lFileSDSize As Long           ' Size of the File SD.
    Dim lSizeNeeded As Long           ' Size needed for SD for file.
                                     
    
    Dim sDomainName As String * 255   ' Domain the user belongs to.
    Dim lDomainNameLength As Long     ' Length of domain name needed.
    
    Dim bSDBuf() As Byte           ' Buffer that holds the security
                                  ' descriptor for this file.
   
    GetAccess = False
    
    Dim temp As Integer

    If InStr(Mid(sFileName, 2), "*") <> 0 Then
        sFileName = Mid(sFileName, InStr(Mid(sFileName, 2), "*") + 2)
    End If
    'Create a buffer
    strUserName = String(100, Chr$(0))
    'Get the username
    GetUserName strUserName, 100
    'strip the rest of the buffer
    strUserName = Left$(strUserName, InStr(strUserName, Chr$(0)) - 1)
' Get the SID of the user. (Refer to the MSDN for more information on SIDs
   ' and their function/purpose in the operating system.) Get the SID of this
   ' user by using the LookupAccountName API. In order to use the SID
   ' of the current user account, call the LookupAccountName API
   ' twice. The first time is to get the required sizes of the SID
   ' and the DomainName string. The second call is to actually get
   ' the desired information.

   lResult = LookupAccountName(vbNullString, strUserName, _
      bUserSid(0), 255, sDomainName, lDomainNameLength, _
      lSIDType)

   ' Now set the sDomainName string buffer to its proper size before
   ' calling the API again.
   sDomainName = Space(lDomainNameLength)

   ' Call the LookupAccountName again to get the actual SID for user.
   lResult = LookupAccountName(vbNullString, strUserName, _
      bUserSid(0), 255, sDomainName, lDomainNameLength, _
      lSIDType)

   ' Return value of zero means the call to LookupAccountName failed;
   ' test for this before you continue.
     If (lResult = 0) Then
        MsgBox "Error: Unable to Lookup the Current User Account: " _
           & sUserName
        Exit Function
     End If

   ' You now have the SID for the user who is logged on.
   ' The SID is of interest since it will get the security descriptor
   ' for the file that the user is interested in.
   ' The GetFileSecurity API will retrieve the Security Descriptor
   ' for the file. However, you must call this API twice: once to get
   ' the proper size for the Security Descriptor and once to get the
   ' actual Security Descriptor information.

   lResult = GetFileSecurityN(sFileName, DACL_SECURITY_INFORMATION, _
      0, 0, lSizeNeeded)

   ' Redimension the Security Descriptor buffer to the proper size.
   ReDim bSDBuf(lSizeNeeded)

   ' Now get the actual Security Descriptor for the file.
   lResult = GetFileSecurity(sFileName, DACL_SECURITY_INFORMATION, _
      bSDBuf(0), lSizeNeeded, lSizeNeeded)

   ' A return code of zero means the call failed; test for this
   ' before continuing.
   If (lResult <> 0) Then
      GetAccess = True
   End If
End Function

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.