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
’检查文件的访问权限
Public 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 'Rights not supported. Returning -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 'Adding Impersonation Token for thread
OpenThreadToken GetCurrentThread(), TOKEN_QUERY, 0, hToken 'Opening of Token of current thread
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
责任编辑:小草