Private 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 Private Declare Function GetSecurityDescriptorOwner Lib "advapi32.dll" _ (pSecurityDescriptor As Any, pOwner As Long, lpbOwnerDefaulted As Long) As _ Long Private Declare Function LookupAccountSid Lib "advapi32.dll" Alias _ "LookupAccountSidA" (ByVal lpSystemName As String, ByVal Sid As Long, _ ByVal name As String, cbName As Long, ByVal ReferencedDomainName As String, _ cbReferencedDomainName As Long, peUse As Long) As Long Private Declare Function GetWindowsDirectory Lib "kernel32" Alias _ "GetWindowsDirectoryA" (ByVal lpBuffer As String, _ ByVal nSize As Long) As Long
Const OWNER_SECURITY_INFORMATION = &H1 Const ERROR_INSUFFICIENT_BUFFER = 122& Const MAX_PATH = 255
Function GetFileOwner(ByVal szfilename As String) As String Dim bSuccess As Long Dim sizeSD As Long Dim pOwner As Long Dim ownerName As String Dim domain_name As String Dim name_len As Long Dim domain_len As Long Dim sdBuf() As Byte Dim nLength As Long Dim deUse As Long bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION, 0, 0&, _ sizeSD) If (bSuccess = 0) And (Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER) Then _ Exit Function ReDim sdBuf(0 To sizeSD - 1) As Byte bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION, sdBuf(0), _ sizeSD, sizeSD) If bSuccess = 0 Then Exit Function bSuccess = GetSecurityDescriptorOwner(sdBuf(0), pOwner, 0&) If bSuccess = 0 Then Exit Function
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName, name_len, _ domain_name, domain_len, deUse) If (bSuccess = 0) And (Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER) Then _ Exit Function
ownerName = Space(name_len - 1) domain_name = Space(domain_len - 1)
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName, name_len, _ domain_name, domain_len, deUse) If bSuccess = 0 Then Exit Function GetFileOwner = ownerName End Function |