Attribute VB_Name = "basCopyFilePerms" '------------------------------------------ ' basCopyFilePerms ' ' Allows you to copy NT file permissions from one file ' to another file. ' ' XferSecurity ' -stSrcFile ' The file from which permissions are copied ' -stDstFile ' The file which is to receive the permissions ' - ' True if the permission copy succeeded (or if on Win9x) ' ' (c) 1999-2001 Trigeminal Software, Inc. All Rights Reserved '------------------------------------------ Option Compare Binary Option Explicit 'POSSIBLE Win32 ERRORS to get: Private Const ERROR_PRIVILEGE_NOT_HELD As Long = 1314& ' A required privilege is not held by the client. Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122& ' The data area passed to a system call is too small. Private Const ERROR_FILE_NOT_FOUND As Long = 2& ' The system cannot find the file specified. Private Const ERROR_PATH_NOT_FOUND As Long = 3& ' The system cannot find the path specified. Private Const ERROR_INVALID_ACCESS As Long = 12& ' The access code is invalid. Private Const ERROR_INVALID_DRIVE As Long = 15& ' The system cannot find the drive specified. Private Const ERROR_INVALID_PARAMETER As Long = 87& ' The parameter is incorrect. Private Const ERROR_INVALID_NAME As Long = 123& ' The filename, directory name, or volume label syntax is incorrect. ' Information flags Private Const OWNER_SECURITY_INFORMATION As Long = &H1& Private Const GROUP_SECURITY_INFORMATION As Long = &H2& Private Const DACL_SECURITY_INFORMATION As Long = &H4& Private Const SACL_SECURITY_INFORMATION As Long = &H8& Private Declare Function SetFileSecurityW _ Lib "advapi32.dll" ( _ ByVal lpFileName As Long, _ ByVal SecurityInformation As Long, _ pSecurityDescriptor As Any _ ) As Long Private Declare Function GetFileSecurityW _ Lib "advapi32.dll" ( _ ByVal lpFileName As Long, _ ByVal RequestedInformation As Long, _ pSecurityDescriptor As Any, _ ByVal nLength As Long, _ lpnLengthNeeded As Long _ ) As Long Private Declare Function GetVersion Lib "kernel32" () As Long Public Function XferSecurity(stSrcFile As String, stDstFile As String) As Boolean Dim dwInfo As Long Dim pSD() As Byte Dim cb As Long Dim rc As Long ' This check SHOULD be (GetVersion < 0x80000000) ' but VB does not have unsigned integers, so we have ' to fake it. If GetVersion < &H80000000 Then ' Win9x, so just return TRUE. XferSecurity = True Exit Function End If ' Set the info we want to get for copying dwInfo = OWNER_SECURITY_INFORMATION Or _ GROUP_SECURITY_INFORMATION Or _ DACL_SECURITY_INFORMATION ' Get the size needed for the security descriptor rc = GetFileSecurityW(StrPtr(stSrcFile), dwInfo, ByVal 0&, 0, cb) If rc = 0 And Err.LastDllError = ERROR_INSUFFICIENT_BUFFER Then ' Confirmed that the problem was that we were not big enough. ' So fix the size of the buffer and call again with the info. ReDim pSD(0 To cb - 1) If GetFileSecurityW(StrPtr(stSrcFile), dwInfo, pSD(0), (cb), cb) <> 0 Then XferSecurity = (SetFileSecurityW(StrPtr(stDstFile), dwInfo, pSD(0)) <> 0) End If End If End Function