By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

API Help please - How do I find the owner of a file or folder using

P: n/a
Hello All

I am trying to find the owner of a file or folder on our network (Windows
2000 Server) using VB.Net and/or API. so I can search for Folders that don't
follow our company's specified folder structure and naming conventions and
then send a Net send message to those users telling them to rectify.
The information I want to get is when you select the file/folder and then:
Properties -> Security Tab -> Advanced Button -> Owner Tab -> Current Owner
of this Item.

I found the following code (see VBA CODE below) which works a treat in VBA
but would like to convert in into VB.Net.
I attempted to change for VB.Net (see VB.Net CODE below) but the first call
to GetFileSecurity() fails to assign the buffer size to sizeSD so it fails
later on
when calling GetSecurityDescriptorOwner() with NullReferenceException error.

Hope someone rises to the challenge.
Thanks in advance
John

'=========================== VBA CODE====================================
Option Compare Database
Option Explicit

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

' return the name of the file owner
'
' runs over Windows NT or 2000, and works only with files in NTFS partitions

Function GetFileOwner(ByVal szfilename As String) As String
Dim bSuccess As Long ' Status variable
Dim sizeSD As Long ' Buffer size to store Owner's SID
Dim pOwner As Long ' Pointer to the Owner's SID
Dim ownerName As String ' Name of the file owner
Dim domain_name As String ' Name of the first domain for the owner
Dim name_len As Long ' Required length for the owner name
Dim domain_len As Long ' Required length for the domain name
Dim sdBuf() As Byte ' Buffer for Security Descriptor
Dim nLength As Long ' Length of the Windows Directory
Dim deUse As Long ' Pointer to a SID_NAME_USE enumerated type
' indicating the type of the account

' Call GetFileSecurity the first time to obtain the size of the buffer
' required for the Security Descriptor.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION, 0,
0&, _
sizeSD)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER)
Then _
Exit Function

' Create a buffer of the required size and call GetFileSecurity again
ReDim sdBuf(0 To sizeSD - 1) As Byte
' Fill the buffer with the security descriptor of the object specified
by
' the
' filename parameter. The calling process must have the right to view
the
' specified
' aspects of the object's security status.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION,
sdBuf(0), _
sizeSD, sizeSD)
' exit if error
If bSuccess = 0 Then Exit Function

' Obtain the owner's SID from the Security Descriptor, exit if error
bSuccess = GetSecurityDescriptorOwner(sdBuf(0), pOwner, 0&)
If bSuccess = 0 Then Exit Function

' Retrieve the name of the account and the name of the first domain on
' which this SID is found. Passes in the Owner's SID obtained
previously.
' Call LookupAccountSid twice, the
' first time to obtain the required size of the owner and domain names.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName, name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER)
Then _
Exit Function

' Allocate the required space in the name and domain_name string
variables.
' Allocate 1 byte less to avoid the appended NULL character.
ownerName = Space(name_len - 1)
domain_name = Space(domain_len - 1)

' Call LookupAccountSid again to actually fill in the name of the owner
' and the first domain.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName, name_len, _
domain_name, domain_len, deUse)
If bSuccess = 0 Then Exit Function

' we've found a result
GetFileOwner = ownerName

End Function
'=========================== VB.Net
CODE====================================
Option Explicit On

Imports System.Runtime.InteropServices

Module Module2

Private Declare Function GetFileSecurity Lib "advapi32.dll" Alias _
"GetFileSecurityA" (ByVal lpFileName As String, ByVal
RequestedInformation _
As Long, ByVal pSecurityDescriptor As Byte, ByVal nLength As Long, _
ByVal lpnLengthNeeded As Long) As Long
Private Declare Function GetSecurityDescriptorOwner Lib "advapi32.dll" _
(<MarshalAsAttribute(UnmanagedType.AsAny)> ByVal pSecurityDescriptor
As Object, ByVal pOwner As Long, ByVal 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, ByVal cbName As Long, ByVal
ReferencedDomainName As String, _
ByVal cbReferencedDomainName As Long, ByVal 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 As Long = &H1
Const ERROR_INSUFFICIENT_BUFFER As Long = 122&
Const MAX_PATH = 255

' return the name of the file owner
'
' runs over Windows NT or 2000, and works only with files in NTFS
partitions

Function GetFileOwner(ByVal szfilename As String) As String
Dim bSuccess As Long ' Status variable
Dim sizeSD As Long ' Buffer size to store Owner's SID
Dim pOwner As Long ' Pointer to the Owner's SID
Dim ownerName As String ' Name of the file owner
Dim domain_name As String ' Name of the first domain for the owner
Dim name_len As Long ' Required length for the owner name
Dim domain_len As Long ' Required length for the domain name
Dim sdBuf() As Byte ' Buffer for Security Descriptor
Dim nLength As Long ' Length of the Windows Directory
Dim deUse As Long ' Pointer to a SID_NAME_USE enumerated
type
' indicating the type of the account

' Call GetFileSecurity the first time to obtain the size of the
buffer
' required for the Security Descriptor.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION,
0, 0&, _
sizeSD)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <>
ERROR_INSUFFICIENT_BUFFER) Then _
Exit Function

' Create a buffer of the required size and call GetFileSecurity
again
ReDim sdBuf(sizeSD)
' Fill the buffer with the security descriptor of the object
specified by
' the
' filename parameter. The calling process must have the right to
view the
' specified
' aspects of the object's security status.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION,
sdBuf(0), _
sizeSD, sizeSD)
' exit if error
If bSuccess = 0 Then Exit Function

' Obtain the owner's SID from the Security Descriptor, exit if error
bSuccess = GetSecurityDescriptorOwner(sdBuf(0), pOwner, 0&)
If bSuccess = 0 Then Exit Function

' Retrieve the name of the account and the name of the first domain
on
' which this SID is found. Passes in the Owner's SID obtained
previously.
' Call LookupAccountSid twice, the
' first time to obtain the required size of the owner and domain
names.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <>
ERROR_INSUFFICIENT_BUFFER) Then _
Exit Function

' Allocate the required space in the name and domain_name string
variables.
' Allocate 1 byte less to avoid the appended NULL character.
ownerName = Space(name_len - 1)
domain_name = Space(domain_len - 1)

' Call LookupAccountSid again to actually fill in the name of the
owner
' and the first domain.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
If bSuccess = 0 Then Exit Function

' we've found a result
GetFileOwner = ownerName

End Function

End Module
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi John

The first thing I note in your conversion is that all variables of type Long
should convert to variables of type Integer, as .NET redefines the length of
long and integer.

Others will possibly come up with other issues, but that may help you on
your way.

HTH

Charles
"John Regan" <johnr@*Remove*!This_BITlambertTAKEOUTengREMOVE_TH IS.com> wrote
in message news:Oc**************@TK2MSFTNGP11.phx.gbl...
Hello All

I am trying to find the owner of a file or folder on our network (Windows
2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified folder structure and naming conventions and
then send a Net send message to those users telling them to rectify.
The information I want to get is when you select the file/folder and then:
Properties -> Security Tab -> Advanced Button -> Owner Tab -> Current Owner of this Item.

I found the following code (see VBA CODE below) which works a treat in VBA
but would like to convert in into VB.Net.
I attempted to change for VB.Net (see VB.Net CODE below) but the first call to GetFileSecurity() fails to assign the buffer size to sizeSD so it fails
later on
when calling GetSecurityDescriptorOwner() with NullReferenceException error.
Hope someone rises to the challenge.
Thanks in advance
John

'=========================== VBA CODE====================================
Option Compare Database
Option Explicit

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

' return the name of the file owner
'
' runs over Windows NT or 2000, and works only with files in NTFS partitions
Function GetFileOwner(ByVal szfilename As String) As String
Dim bSuccess As Long ' Status variable
Dim sizeSD As Long ' Buffer size to store Owner's SID
Dim pOwner As Long ' Pointer to the Owner's SID
Dim ownerName As String ' Name of the file owner
Dim domain_name As String ' Name of the first domain for the owner
Dim name_len As Long ' Required length for the owner name
Dim domain_len As Long ' Required length for the domain name
Dim sdBuf() As Byte ' Buffer for Security Descriptor
Dim nLength As Long ' Length of the Windows Directory
Dim deUse As Long ' Pointer to a SID_NAME_USE enumerated type
' indicating the type of the account

' Call GetFileSecurity the first time to obtain the size of the buffer
' required for the Security Descriptor.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION, 0,
0&, _
sizeSD)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER)
Then _
Exit Function

' Create a buffer of the required size and call GetFileSecurity again
ReDim sdBuf(0 To sizeSD - 1) As Byte
' Fill the buffer with the security descriptor of the object specified
by
' the
' filename parameter. The calling process must have the right to view
the
' specified
' aspects of the object's security status.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION,
sdBuf(0), _
sizeSD, sizeSD)
' exit if error
If bSuccess = 0 Then Exit Function

' Obtain the owner's SID from the Security Descriptor, exit if error
bSuccess = GetSecurityDescriptorOwner(sdBuf(0), pOwner, 0&)
If bSuccess = 0 Then Exit Function

' Retrieve the name of the account and the name of the first domain on
' which this SID is found. Passes in the Owner's SID obtained
previously.
' Call LookupAccountSid twice, the
' first time to obtain the required size of the owner and domain names. bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName, name_len, _ domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER)
Then _
Exit Function

' Allocate the required space in the name and domain_name string
variables.
' Allocate 1 byte less to avoid the appended NULL character.
ownerName = Space(name_len - 1)
domain_name = Space(domain_len - 1)

' Call LookupAccountSid again to actually fill in the name of the owner ' and the first domain.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName, name_len, _ domain_name, domain_len, deUse)
If bSuccess = 0 Then Exit Function

' we've found a result
GetFileOwner = ownerName

End Function
'=========================== VB.Net
CODE====================================
Option Explicit On

Imports System.Runtime.InteropServices

Module Module2

Private Declare Function GetFileSecurity Lib "advapi32.dll" Alias _
"GetFileSecurityA" (ByVal lpFileName As String, ByVal
RequestedInformation _
As Long, ByVal pSecurityDescriptor As Byte, ByVal nLength As Long, _ ByVal lpnLengthNeeded As Long) As Long
Private Declare Function GetSecurityDescriptorOwner Lib "advapi32.dll" _ (<MarshalAsAttribute(UnmanagedType.AsAny)> ByVal pSecurityDescriptor As Object, ByVal pOwner As Long, ByVal 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, ByVal cbName As Long, ByVal
ReferencedDomainName As String, _
ByVal cbReferencedDomainName As Long, ByVal 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 As Long = &H1
Const ERROR_INSUFFICIENT_BUFFER As Long = 122&
Const MAX_PATH = 255

' return the name of the file owner
'
' runs over Windows NT or 2000, and works only with files in NTFS
partitions

Function GetFileOwner(ByVal szfilename As String) As String
Dim bSuccess As Long ' Status variable
Dim sizeSD As Long ' Buffer size to store Owner's SID
Dim pOwner As Long ' Pointer to the Owner's SID
Dim ownerName As String ' Name of the file owner
Dim domain_name As String ' Name of the first domain for the owner Dim name_len As Long ' Required length for the owner name
Dim domain_len As Long ' Required length for the domain name
Dim sdBuf() As Byte ' Buffer for Security Descriptor
Dim nLength As Long ' Length of the Windows Directory
Dim deUse As Long ' Pointer to a SID_NAME_USE enumerated
type
' indicating the type of the account

' Call GetFileSecurity the first time to obtain the size of the
buffer
' required for the Security Descriptor.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION,
0, 0&, _
sizeSD)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <>
ERROR_INSUFFICIENT_BUFFER) Then _
Exit Function

' Create a buffer of the required size and call GetFileSecurity
again
ReDim sdBuf(sizeSD)
' Fill the buffer with the security descriptor of the object
specified by
' the
' filename parameter. The calling process must have the right to
view the
' specified
' aspects of the object's security status.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION,
sdBuf(0), _
sizeSD, sizeSD)
' exit if error
If bSuccess = 0 Then Exit Function

' Obtain the owner's SID from the Security Descriptor, exit if error bSuccess = GetSecurityDescriptorOwner(sdBuf(0), pOwner, 0&)
If bSuccess = 0 Then Exit Function

' Retrieve the name of the account and the name of the first domain on
' which this SID is found. Passes in the Owner's SID obtained
previously.
' Call LookupAccountSid twice, the
' first time to obtain the required size of the owner and domain
names.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <>
ERROR_INSUFFICIENT_BUFFER) Then _
Exit Function

' Allocate the required space in the name and domain_name string
variables.
' Allocate 1 byte less to avoid the appended NULL character.
ownerName = Space(name_len - 1)
domain_name = Space(domain_len - 1)

' Call LookupAccountSid again to actually fill in the name of the
owner
' and the first domain.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
If bSuccess = 0 Then Exit Function

' we've found a result
GetFileOwner = ownerName

End Function

End Module

Nov 20 '05 #2

P: n/a
Hi Charles

Thanks for your pointers which did help me onto the right track.
The other thing that threw me is in VB if ByVal or ByRef is not specified
for the parameters then ByRef is the default but when you paste the code
into VB.net the unspecified parameters are changed by VB.Net into ByVal
because this is the default in VB.net

Fot those watching this thread the fully working VB.Net code is below

Regards
John

===========VB.Net Code===========
Private Declare Function GetFileSecurity Lib "advapi32.dll" Alias
"GetFileSecurityA" ( _
ByVal lpFileName As String, _
ByVal RequestedInformation As Integer, _
ByRef pSecurityDescriptor As Byte, _
ByVal nLength As Integer, _
ByRef lpnLengthNeeded As Integer) _
As Integer

Private Declare Function GetSecurityDescriptorOwner Lib "advapi32.dll"
( _
ByRef ppSecurityDescriptor As Byte, _
ByRef ppOwner As Integer, _
ByRef lpbOwnerDefaulted As Integer) _
As Integer

Private Declare Function LookupAccountSid Lib "advapi32.dll" Alias
"LookupAccountSidA" ( _
ByVal lpSystemName As String, _
ByVal Sid As Integer, _
ByVal name As String, _
ByRef cbName As Integer, _
ByVal ReferencedDomainName As String, _
ByRef cbReferencedDomainName As Integer, _
ByRef peUse As Integer) _
As Integer

Const OWNER_SECURITY_INFORMATION As Integer = &H1
Const ERROR_INSUFFICIENT_BUFFER As Integer = 122
Const MAX_PATH = 255

' return the name of the file owner
' runs over Windows NT or 2000, and works only with files in NTFS
partitions

Function GetFileOwner(ByVal szfilename As String) As String
Dim bSuccess As Integer ' Status variable
Dim sizeSD As Integer ' Buffer size to store Owner's
SID
Dim pOwner As Integer ' Pointer to the Owner's SID
Dim ownerName As String ' Name of the file owner
Dim domain_name As String ' Name of the first domain for
the owner
Dim name_len As Integer ' Required length for the owner
name
Dim domain_len As Integer ' Required length for the domain
name
Dim sdBuf() As Byte ' Buffer for Security Descriptor
Dim nLength As Integer ' Length of the Windows
Directory
Dim deUse As Integer ' Pointer to a SID_NAME_USE
enumerated type

' indicating the type of the account

' Call GetFileSecurity the first time to obtain the size of the
buffer
' required for the Security Descriptor.
'Const bytZero As Byte = 0
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION,
0, 0, _
sizeSD)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <>
ERROR_INSUFFICIENT_BUFFER) Then _
Exit Function

' Create a buffer of the required size and call GetFileSecurity
again
ReDim sdBuf(sizeSD - 1)
' Fill the buffer with the security descriptor of the object
specified by
' the
' filename parameter. The calling process must have the right to
view the
' specified
' aspects of the object's security status.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION,
sdBuf(0), _
sizeSD, sizeSD)
' exit if error
If bSuccess = 0 Then Exit Function

' Obtain the owner's SID from the Security Descriptor, exit if error
bSuccess = GetSecurityDescriptorOwner(sdBuf(0), pOwner, 0)
If bSuccess = 0 Then Exit Function

' Retrieve the name of the account and the name of the first domain
on
' which this SID is found. Passes in the Owner's SID obtained
previously.
' Call LookupAccountSid twice, the
' first time to obtain the required size of the owner and domain
names.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <>
ERROR_INSUFFICIENT_BUFFER) Then _
Exit Function

' Allocate the required space in the name and domain_name string
variables.
' Allocate 1 byte less to avoid the appended NULL character.
ownerName = Space(name_len - 1)
domain_name = Space(domain_len - 1)

' Call LookupAccountSid again to actually fill in the name of the
owner
' and the first domain.

bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
If bSuccess = 0 Then Exit Function

' we've found a result
GetFileOwner = ownerName.ToString

End Function
=================End Code===============
"Charles Law" <bl***@nowhere.com> wrote in message
news:uH**************@TK2MSFTNGP10.phx.gbl...
Hi John

The first thing I note in your conversion is that all variables of type Long should convert to variables of type Integer, as .NET redefines the length of long and integer.

Others will possibly come up with other issues, but that may help you on
your way.

HTH

Charles
"John Regan" <johnr@*Remove*!This_BITlambertTAKEOUTengREMOVE_TH IS.com> wrote in message news:Oc**************@TK2MSFTNGP11.phx.gbl...
Hello All

I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't
follow our company's specified folder structure and naming conventions and then send a Net send message to those users telling them to rectify.
The information I want to get is when you select the file/folder and then: Properties -> Security Tab -> Advanced Button -> Owner Tab -> Current

Owner
of this Item.

I found the following code (see VBA CODE below) which works a treat in VBA but would like to convert in into VB.Net.
I attempted to change for VB.Net (see VB.Net CODE below) but the first

call
to GetFileSecurity() fails to assign the buffer size to sizeSD so it fails later on
when calling GetSecurityDescriptorOwner() with NullReferenceException

error.

Hope someone rises to the challenge.
Thanks in advance
John

'=========================== VBA CODE==================================== Option Compare Database
Option Explicit

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

' return the name of the file owner
'
' runs over Windows NT or 2000, and works only with files in NTFS partitions

Function GetFileOwner(ByVal szfilename As String) As String
Dim bSuccess As Long ' Status variable
Dim sizeSD As Long ' Buffer size to store Owner's SID
Dim pOwner As Long ' Pointer to the Owner's SID
Dim ownerName As String ' Name of the file owner
Dim domain_name As String ' Name of the first domain for the owner
Dim name_len As Long ' Required length for the owner name
Dim domain_len As Long ' Required length for the domain name
Dim sdBuf() As Byte ' Buffer for Security Descriptor
Dim nLength As Long ' Length of the Windows Directory
Dim deUse As Long ' Pointer to a SID_NAME_USE enumerated

type ' indicating the type of the account

' Call GetFileSecurity the first time to obtain the size of the buffer ' required for the Security Descriptor.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION, 0, 0&, _
sizeSD)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <> ERROR_INSUFFICIENT_BUFFER) Then _
Exit Function

' Create a buffer of the required size and call GetFileSecurity again ReDim sdBuf(0 To sizeSD - 1) As Byte
' Fill the buffer with the security descriptor of the object specified by
' the
' filename parameter. The calling process must have the right to view the
' specified
' aspects of the object's security status.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION,
sdBuf(0), _
sizeSD, sizeSD)
' exit if error
If bSuccess = 0 Then Exit Function

' Obtain the owner's SID from the Security Descriptor, exit if error
bSuccess = GetSecurityDescriptorOwner(sdBuf(0), pOwner, 0&)
If bSuccess = 0 Then Exit Function

' Retrieve the name of the account and the name of the first domain on ' which this SID is found. Passes in the Owner's SID obtained
previously.
' Call LookupAccountSid twice, the
' first time to obtain the required size of the owner and domain

names.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName, name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <>
ERROR_INSUFFICIENT_BUFFER) Then _
Exit Function

' Allocate the required space in the name and domain_name string
variables.
' Allocate 1 byte less to avoid the appended NULL character.
ownerName = Space(name_len - 1)
domain_name = Space(domain_len - 1)

' Call LookupAccountSid again to actually fill in the name of the

owner
' and the first domain.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName, name_len, _
domain_name, domain_len, deUse)
If bSuccess = 0 Then Exit Function

' we've found a result
GetFileOwner = ownerName

End Function
'=========================== VB.Net
CODE====================================
Option Explicit On

Imports System.Runtime.InteropServices

Module Module2

Private Declare Function GetFileSecurity Lib "advapi32.dll" Alias _
"GetFileSecurityA" (ByVal lpFileName As String, ByVal
RequestedInformation _
As Long, ByVal pSecurityDescriptor As Byte, ByVal nLength As
Long, _
ByVal lpnLengthNeeded As Long) As Long
Private Declare Function GetSecurityDescriptorOwner Lib
"advapi32.dll" _
(<MarshalAsAttribute(UnmanagedType.AsAny)> ByVal

pSecurityDescriptor
As Object, ByVal pOwner As Long, ByVal 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, ByVal cbName As Long, ByVal
ReferencedDomainName As String, _
ByVal cbReferencedDomainName As Long, ByVal 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 As Long = &H1
Const ERROR_INSUFFICIENT_BUFFER As Long = 122&
Const MAX_PATH = 255

' return the name of the file owner
'
' runs over Windows NT or 2000, and works only with files in NTFS
partitions

Function GetFileOwner(ByVal szfilename As String) As String
Dim bSuccess As Long ' Status variable
Dim sizeSD As Long ' Buffer size to store Owner's SID
Dim pOwner As Long ' Pointer to the Owner's SID
Dim ownerName As String ' Name of the file owner
Dim domain_name As String ' Name of the first domain for the

owner
Dim name_len As Long ' Required length for the owner name
Dim domain_len As Long ' Required length for the domain name
Dim sdBuf() As Byte ' Buffer for Security Descriptor
Dim nLength As Long ' Length of the Windows Directory
Dim deUse As Long ' Pointer to a SID_NAME_USE

enumerated type
' indicating the type of the account

' Call GetFileSecurity the first time to obtain the size of the
buffer
' required for the Security Descriptor.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION, 0, 0&, _
sizeSD)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <>
ERROR_INSUFFICIENT_BUFFER) Then _
Exit Function

' Create a buffer of the required size and call GetFileSecurity
again
ReDim sdBuf(sizeSD)
' Fill the buffer with the security descriptor of the object
specified by
' the
' filename parameter. The calling process must have the right to
view the
' specified
' aspects of the object's security status.
bSuccess = GetFileSecurity(szfilename, OWNER_SECURITY_INFORMATION, sdBuf(0), _
sizeSD, sizeSD)
' exit if error
If bSuccess = 0 Then Exit Function

' Obtain the owner's SID from the Security Descriptor, exit if

error
bSuccess = GetSecurityDescriptorOwner(sdBuf(0), pOwner, 0&)
If bSuccess = 0 Then Exit Function

' Retrieve the name of the account and the name of the first

domain
on
' which this SID is found. Passes in the Owner's SID obtained
previously.
' Call LookupAccountSid twice, the
' first time to obtain the required size of the owner and domain
names.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllError <>
ERROR_INSUFFICIENT_BUFFER) Then _
Exit Function

' Allocate the required space in the name and domain_name string variables.
' Allocate 1 byte less to avoid the appended NULL character.
ownerName = Space(name_len - 1)
domain_name = Space(domain_len - 1)

' Call LookupAccountSid again to actually fill in the name of the owner
' and the first domain.
bSuccess = LookupAccountSid(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
If bSuccess = 0 Then Exit Function

' we've found a result
GetFileOwner = ownerName

End Function

End Module


Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.