Windows NT/2000/XP has API function AccessCheck, which in fact checks access rights to every operating system object, which supports access rights. This function is called implicitly by system every time user accesses such object. To call AccessCheck function explicitly it is necessary to carry out a whole series of operations with data structures responsible for OS security and call some other functions.
To demonstrate this example I created a sample folder C:\Public. I modified permission for "Everyone".
To implement Quick Copy/paste Demo perform the following steps
- Create a standard exe project - Place one command button and textbox on form1 - Add module - Run project.. Enter File or Directory path in text1 - Press Command1 to check Read/Write/Execute/All permission
Code for Form1.frm |
Click here to copy the following block | Dim AccessRead As Boolean, AccessWrite As Boolean, AccessExecute As Boolean, AccessAll As Boolean
Private Function CheckFileAccess(Filename As String, ByVal DesiredAccess As Long) As Long Dim r As Long, SecDesc() As Byte, SDSize As Long, hToken As Long Dim PrivSet As PRIVILEGE_SET, GenMap As GENERIC_MAPPING Dim Volume As String, FSFlags As Long
If Not IsNT() Then CheckFileAccess = -1 Exit Function End If
If Left$(Filename, 2) = "\\" Then r = InStr(3, Filename, "\") If r = 0 Then Volume = Filename & "\" Else Volume = Left$(Filename, r) End If ElseIf Mid$(Filename, 2, 2) = ":\" Then Volume = Left$(Filename, 3) End If
GetVolumeInformation Volume, vbNullString, 0, ByVal 0&, _ ByVal 0&, FSFlags, vbNullString, 0
If (FSFlags And FS_PERSISTENT_ACLS) = 0 Then CheckFileAccess = -1 Exit Function End If
GetFileSecurity Filename, OWNER_SECURITY_INFORMATION _ Or GROUP_SECURITY_INFORMATION _ Or DACL_SECURITY_INFORMATION, 0, 0, SDSize
If Err.LastDllError <> 122 Then CheckFileAccess = -1 Exit Function End If
If SDSize = 0 Then Exit Function ReDim SecDesc(1 To SDSize) If GetFileSecurity(Filename, OWNER_SECURITY_INFORMATION _ Or GROUP_SECURITY_INFORMATION _ Or DACL_SECURITY_INFORMATION, _ SecDesc(1), SDSize, SDSize) = 0 Then Exit Function End If
ImpersonateSelf SecurityImpersonation
OpenThreadToken GetCurrentThread(), TOKEN_QUERY, 0, hToken
If hToken <> 0 Then GenMap.GenericRead = FILE_GENERIC_READ GenMap.GenericWrite = FILE_GENERIC_WRITE GenMap.GenericExecute = FILE_GENERIC_EXECUTE GenMap.GenericAll = FILE_ALL_ACCESS MapGenericMask DesiredAccess, GenMap AccessCheck SecDesc(1), hToken, DesiredAccess, GenMap, _ PrivSet, Len(PrivSet), CheckFileAccess, r CloseHandle hToken End If
RevertToSelf
End Function
Private Function IsNT() As Boolean Dim OSVer As OSVERSIONINFO OSVer.dwOSVersionInfoSize = Len(OSVer) GetVersionEx OSVer IsNT = (OSVer.dwPlatformId = VER_PLATFORM_WIN32_NT) End Function
Private Sub Command1_Click()
AccessRead = CheckFileAccess(Text1, FILE_GENERIC_READ) = FILE_GENERIC_READ AccessWrite = CheckFileAccess(Text1, FILE_GENERIC_WRITE) = FILE_GENERIC_WRITE AccessExecute = CheckFileAccess(Text1, FILE_GENERIC_EXECUTE) = FILE_GENERIC_EXECUTE AccessAll = CheckFileAccess(Text1, FILE_ALL_ACCESS) = FILE_ALL_ACCESS
s = "ALL permission is " & AccessAll & vbCrLf s = s & "READ permission is " & AccessRead & vbCrLf s = s & "WRITE permission is " & AccessWrite & vbCrLf s = s & "EXECUTE permission is " & AccessExecute & vbCrLf
MsgBox s End Sub |
Click here to copy the following block | Option Explicit
Public Const MAXIMUM_ALLOWED As Long = &H2000000 Public Const DELETE As Long = &H10000 Public Const READ_CONTROL As Long = &H20000 Public Const WRITE_DAC As Long = &H40000 Public Const WRITE_OWNER As Long = &H80000 Public Const SYNCHRONIZE As Long = &H100000
Public Const STANDARD_RIGHTS_READ As Long = READ_CONTROL Public Const STANDARD_RIGHTS_WRITE As Long = READ_CONTROL Public Const STANDARD_RIGHTS_EXECUTE As Long = READ_CONTROL Public Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Public Const FILE_READ_DATA As Long = &H1 Public Const FILE_LIST_DIRECTORY As Long = &H1 Public Const FILE_ADD_FILE As Long = &H2 Public Const FILE_WRITE_DATA As Long = &H2 Public Const FILE_CREATE_PIPE_INSTANCE As Long = &H4 Public Const FILE_ADD_SUBDIRECTORY As Long = &H4 Public Const FILE_APPEND_DATA As Long = &H4 Public Const FILE_READ_EA As Long = &H8 Public Const FILE_READ_PROPERTIES As Long = FILE_READ_EA Public Const FILE_WRITE_EA As Long = &H10 Public Const FILE_WRITE_PROPERTIES As Long = FILE_WRITE_EA Public Const FILE_EXECUTE As Long = &H20 Public Const FILE_TRAVERSE As Long = &H20 Public Const FILE_DELETE_CHILD As Long = &H40 Public Const FILE_READ_ATTRIBUTES As Long = &H80 Public Const FILE_WRITE_ATTRIBUTES As Long = &H100
Public Const FILE_GENERIC_READ As Long = (STANDARD_RIGHTS_READ _ Or FILE_READ_DATA Or FILE_READ_ATTRIBUTES _ Or FILE_READ_EA Or SYNCHRONIZE)
Public Const FILE_GENERIC_WRITE As Long = (STANDARD_RIGHTS_WRITE _ Or FILE_WRITE_DATA Or FILE_WRITE_ATTRIBUTES _ Or FILE_WRITE_EA Or FILE_APPEND_DATA Or SYNCHRONIZE)
Public Const FILE_GENERIC_EXECUTE As Long = (STANDARD_RIGHTS_EXECUTE _ Or FILE_READ_ATTRIBUTES Or FILE_EXECUTE Or SYNCHRONIZE)
Public Const FILE_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED _ Or SYNCHRONIZE Or &H1FF&)
Public Const GENERIC_READ As Long = &H80000000 Public Const GENERIC_WRITE As Long = &H40000000 Public Const GENERIC_EXECUTE As Long = &H20000000 Public Const GENERIC_ALL As Long = &H10000000
Public Const OWNER_SECURITY_INFORMATION As Long = &H1 Public Const GROUP_SECURITY_INFORMATION As Long = &H2 Public Const DACL_SECURITY_INFORMATION As Long = &H4 Public Const TOKEN_QUERY As Long = 8 Public Const SecurityImpersonation As Integer = 3 Public Const ANYSIZE_ARRAY = 1
Public Type GENERIC_MAPPING GenericRead As Long GenericWrite As Long GenericExecute As Long GenericAll As Long End Type
Public Type LUID LowPart As Long HighPart As Long End Type
Public Type LUID_AND_ATTRIBUTES pLuid As LUID Attributes As Long End Type
Public Type PRIVILEGE_SET PrivilegeCount As Long Control As Long Privilege(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES End Type
Public 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
Public Declare Function AccessCheck Lib "advapi32.dll" _ (pSecurityDescriptor As Byte, ByVal ClientToken As Long, _ ByVal DesiredAccess As Long, GenericMapping As GENERIC_MAPPING, _ PrivilegeSet As PRIVILEGE_SET, PrivilegeSetLength As Long, _ GrantedAccess As Long, Status As Long) As Long
Public Declare Function ImpersonateSelf Lib "advapi32.dll" _ (ByVal ImpersonationLevel As Integer) As Long
Public Declare Function RevertToSelf Lib "advapi32.dll" () As Long
Public Declare Sub MapGenericMask Lib "advapi32.dll" (AccessMask As Long, _ GenericMapping As GENERIC_MAPPING)
Public Declare Function OpenThreadToken Lib "advapi32.dll" _ (ByVal ThreadHandle As Long, ByVal DesiredAccess As Long, _ ByVal OpenAsSelf As Long, TokenHandle As Long) As Long
Public Declare Function GetCurrentThread Lib "kernel32" () As Long
Public Declare Function CloseHandle Lib "kernel32" _ (ByVal hObject As Long) As Long
Public Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 End Type
Public Const VER_PLATFORM_WIN32_NT As Long = 2
Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _ (lpVersionInformation As OSVERSIONINFO) As Long
Public Const FS_PERSISTENT_ACLS As Long = &H8
Public Declare Function GetVolumeInformation Lib "kernel32" _ Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, _ ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, _ lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, _ lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, _ ByVal nFileSystemNameSize As Long) As Long |
|