473,725 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 GetSecurityDesc riptorOwner() with NullReferenceEx ception error.

Hope someone rises to the challenge.
Thanks in advance
John

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

Private Declare Function GetFileSecurity Lib "advapi32.d ll" Alias _
"GetFileSecurit yA" (ByVal lpFileName As String, ByVal
RequestedInform ation _
As Long, pSecurityDescri ptor As Byte, ByVal nLength As Long, _
lpnLengthNeeded As Long) As Long
Private Declare Function GetSecurityDesc riptorOwner Lib "advapi32.d ll" _
(pSecurityDescr iptor As Any, pOwner As Long, lpbOwnerDefault ed As Long)
As _
Long
Private Declare Function LookupAccountSi d Lib "advapi32.d ll" Alias _
"LookupAccountS idA" (ByVal lpSystemName As String, ByVal Sid As Long, _
ByVal name As String, cbName As Long, ByVal ReferencedDomai nName As
String, _
cbReferencedDom ainName As Long, peUse As Long) As Long
Private Declare Function GetWindowsDirec tory Lib "kernel32" Alias _
"GetWindowsDire ctoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long

Const OWNER_SECURITY_ INFORMATION = &H1
Const ERROR_INSUFFICI ENT_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(By Val 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.LastDllErr or <> ERROR_INSUFFICI ENT_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 = GetSecurityDesc riptorOwner(sdB uf(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 LookupAccountSi d twice, the
' first time to obtain the required size of the owner and domain names.
bSuccess = LookupAccountSi d(vbNullString, pOwner, ownerName, name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllErr or <> ERROR_INSUFFICI ENT_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_le n - 1)

' Call LookupAccountSi d again to actually fill in the name of the owner
' and the first domain.
bSuccess = LookupAccountSi d(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.d ll" Alias _
"GetFileSecurit yA" (ByVal lpFileName As String, ByVal
RequestedInform ation _
As Long, ByVal pSecurityDescri ptor As Byte, ByVal nLength As Long, _
ByVal lpnLengthNeeded As Long) As Long
Private Declare Function GetSecurityDesc riptorOwner Lib "advapi32.d ll" _
(<MarshalAsAttr ibute(Unmanaged Type.AsAny)> ByVal pSecurityDescri ptor
As Object, ByVal pOwner As Long, ByVal lpbOwnerDefault ed As Long) As _
Long
Private Declare Function LookupAccountSi d Lib "advapi32.d ll" Alias _
"LookupAccountS idA" (ByVal lpSystemName As String, ByVal Sid As
Long, _
ByVal name As String, ByVal cbName As Long, ByVal
ReferencedDomai nName As String, _
ByVal cbReferencedDom ainName As Long, ByVal peUse As Long) As Long
Private Declare Function GetWindowsDirec tory Lib "kernel32" Alias _
"GetWindowsDire ctoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long

Const OWNER_SECURITY_ INFORMATION As Long = &H1
Const ERROR_INSUFFICI ENT_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(By Val 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.LastDllErr or <>
ERROR_INSUFFICI ENT_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 = GetSecurityDesc riptorOwner(sdB uf(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 LookupAccountSi d twice, the
' first time to obtain the required size of the owner and domain
names.
bSuccess = LookupAccountSi d(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllErr or <>
ERROR_INSUFFICI ENT_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_le n - 1)

' Call LookupAccountSi d again to actually fill in the name of the
owner
' and the first domain.
bSuccess = LookupAccountSi d(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
2 5044
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_BITlamber tTAKEOUTengREMO VE_THIS.com> wrote
in message news:Oc******** ******@TK2MSFTN GP11.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 GetSecurityDesc riptorOwner() with NullReferenceEx ception error.
Hope someone rises to the challenge.
Thanks in advance
John

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

Private Declare Function GetFileSecurity Lib "advapi32.d ll" Alias _
"GetFileSecurit yA" (ByVal lpFileName As String, ByVal
RequestedInform ation _
As Long, pSecurityDescri ptor As Byte, ByVal nLength As Long, _
lpnLengthNeeded As Long) As Long
Private Declare Function GetSecurityDesc riptorOwner Lib "advapi32.d ll" _
(pSecurityDescr iptor As Any, pOwner As Long, lpbOwnerDefault ed As Long) As _
Long
Private Declare Function LookupAccountSi d Lib "advapi32.d ll" Alias _
"LookupAccountS idA" (ByVal lpSystemName As String, ByVal Sid As Long, _ ByVal name As String, cbName As Long, ByVal ReferencedDomai nName As
String, _
cbReferencedDom ainName As Long, peUse As Long) As Long
Private Declare Function GetWindowsDirec tory Lib "kernel32" Alias _
"GetWindowsDire ctoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long

Const OWNER_SECURITY_ INFORMATION = &H1
Const ERROR_INSUFFICI ENT_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(By Val 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.LastDllErr or <> ERROR_INSUFFICI ENT_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 = GetSecurityDesc riptorOwner(sdB uf(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 LookupAccountSi d twice, the
' first time to obtain the required size of the owner and domain names. bSuccess = LookupAccountSi d(vbNullString, pOwner, ownerName, name_len, _ domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllErr or <> ERROR_INSUFFICI ENT_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_le n - 1)

' Call LookupAccountSi d again to actually fill in the name of the owner ' and the first domain.
bSuccess = LookupAccountSi d(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.d ll" Alias _
"GetFileSecurit yA" (ByVal lpFileName As String, ByVal
RequestedInform ation _
As Long, ByVal pSecurityDescri ptor As Byte, ByVal nLength As Long, _ ByVal lpnLengthNeeded As Long) As Long
Private Declare Function GetSecurityDesc riptorOwner Lib "advapi32.d ll" _ (<MarshalAsAttr ibute(Unmanaged Type.AsAny)> ByVal pSecurityDescri ptor As Object, ByVal pOwner As Long, ByVal lpbOwnerDefault ed As Long) As _
Long
Private Declare Function LookupAccountSi d Lib "advapi32.d ll" Alias _
"LookupAccountS idA" (ByVal lpSystemName As String, ByVal Sid As
Long, _
ByVal name As String, ByVal cbName As Long, ByVal
ReferencedDomai nName As String, _
ByVal cbReferencedDom ainName As Long, ByVal peUse As Long) As Long
Private Declare Function GetWindowsDirec tory Lib "kernel32" Alias _
"GetWindowsDire ctoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long

Const OWNER_SECURITY_ INFORMATION As Long = &H1
Const ERROR_INSUFFICI ENT_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(By Val 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.LastDllErr or <>
ERROR_INSUFFICI ENT_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 = GetSecurityDesc riptorOwner(sdB uf(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 LookupAccountSi d twice, the
' first time to obtain the required size of the owner and domain
names.
bSuccess = LookupAccountSi d(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllErr or <>
ERROR_INSUFFICI ENT_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_le n - 1)

' Call LookupAccountSi d again to actually fill in the name of the
owner
' and the first domain.
bSuccess = LookupAccountSi d(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
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.N et Code===========
Private Declare Function GetFileSecurity Lib "advapi32.d ll" Alias
"GetFileSecurit yA" ( _
ByVal lpFileName As String, _
ByVal RequestedInform ation As Integer, _
ByRef pSecurityDescri ptor As Byte, _
ByVal nLength As Integer, _
ByRef lpnLengthNeeded As Integer) _
As Integer

Private Declare Function GetSecurityDesc riptorOwner Lib "advapi32.d ll"
( _
ByRef ppSecurityDescr iptor As Byte, _
ByRef ppOwner As Integer, _
ByRef lpbOwnerDefault ed As Integer) _
As Integer

Private Declare Function LookupAccountSi d Lib "advapi32.d ll" Alias
"LookupAccountS idA" ( _
ByVal lpSystemName As String, _
ByVal Sid As Integer, _
ByVal name As String, _
ByRef cbName As Integer, _
ByVal ReferencedDomai nName As String, _
ByRef cbReferencedDom ainName As Integer, _
ByRef peUse As Integer) _
As Integer

Const OWNER_SECURITY_ INFORMATION As Integer = &H1
Const ERROR_INSUFFICI ENT_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(By Val 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.LastDllErr or <>
ERROR_INSUFFICI ENT_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 = GetSecurityDesc riptorOwner(sdB uf(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 LookupAccountSi d twice, the
' first time to obtain the required size of the owner and domain
names.
bSuccess = LookupAccountSi d(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllErr or <>
ERROR_INSUFFICI ENT_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_le n - 1)

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

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

' we've found a result
GetFileOwner = ownerName.ToStr ing

End Function
=============== ==End Code=========== ====
"Charles Law" <bl***@nowhere. com> wrote in message
news:uH******** ******@TK2MSFTN GP10.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_BITlamber tTAKEOUTengREMO VE_THIS.com> wrote in message news:Oc******** ******@TK2MSFTN GP11.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 GetSecurityDesc riptorOwner() with NullReferenceEx ception

error.

Hope someone rises to the challenge.
Thanks in advance
John

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

Private Declare Function GetFileSecurity Lib "advapi32.d ll" Alias _
"GetFileSecurit yA" (ByVal lpFileName As String, ByVal
RequestedInform ation _
As Long, pSecurityDescri ptor As Byte, ByVal nLength As Long, _
lpnLengthNeeded As Long) As Long
Private Declare Function GetSecurityDesc riptorOwner Lib "advapi32.d ll" _
(pSecurityDescr iptor As Any, pOwner As Long, lpbOwnerDefault ed As

Long)
As _
Long
Private Declare Function LookupAccountSi d Lib "advapi32.d ll" Alias _
"LookupAccountS idA" (ByVal lpSystemName As String, ByVal Sid As Long, _
ByVal name As String, cbName As Long, ByVal ReferencedDomai nName As
String, _
cbReferencedDom ainName As Long, peUse As Long) As Long
Private Declare Function GetWindowsDirec tory Lib "kernel32" Alias _
"GetWindowsDire ctoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long

Const OWNER_SECURITY_ INFORMATION = &H1
Const ERROR_INSUFFICI ENT_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(By Val 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.LastDllErr or <> ERROR_INSUFFICI ENT_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 = GetSecurityDesc riptorOwner(sdB uf(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 LookupAccountSi d twice, the
' first time to obtain the required size of the owner and domain

names.
bSuccess = LookupAccountSi d(vbNullString, pOwner, ownerName, name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllErr or <>
ERROR_INSUFFICI ENT_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_le n - 1)

' Call LookupAccountSi d again to actually fill in the name of the

owner
' and the first domain.
bSuccess = LookupAccountSi d(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.d ll" Alias _
"GetFileSecurit yA" (ByVal lpFileName As String, ByVal
RequestedInform ation _
As Long, ByVal pSecurityDescri ptor As Byte, ByVal nLength As
Long, _
ByVal lpnLengthNeeded As Long) As Long
Private Declare Function GetSecurityDesc riptorOwner Lib
"advapi32.d ll" _
(<MarshalAsAttr ibute(Unmanaged Type.AsAny)> ByVal

pSecurityDescri ptor
As Object, ByVal pOwner As Long, ByVal lpbOwnerDefault ed As Long) As _
Long
Private Declare Function LookupAccountSi d Lib "advapi32.d ll" Alias _
"LookupAccountS idA" (ByVal lpSystemName As String, ByVal Sid As
Long, _
ByVal name As String, ByVal cbName As Long, ByVal
ReferencedDomai nName As String, _
ByVal cbReferencedDom ainName As Long, ByVal peUse As Long) As Long
Private Declare Function GetWindowsDirec tory Lib "kernel32" Alias _
"GetWindowsDire ctoryA" (ByVal lpBuffer As String, _
ByVal nSize As Long) As Long

Const OWNER_SECURITY_ INFORMATION As Long = &H1
Const ERROR_INSUFFICI ENT_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(By Val 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.LastDllErr or <>
ERROR_INSUFFICI ENT_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 = GetSecurityDesc riptorOwner(sdB uf(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 LookupAccountSi d twice, the
' first time to obtain the required size of the owner and domain
names.
bSuccess = LookupAccountSi d(vbNullString, pOwner, ownerName,
name_len, _
domain_name, domain_len, deUse)
' exit if any error
If (bSuccess = 0) And (Err.LastDllErr or <>
ERROR_INSUFFICI ENT_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_le n - 1)

' Call LookupAccountSi d again to actually fill in the name of the owner
' and the first domain.
bSuccess = LookupAccountSi d(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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1900
by: Shane | last post by:
Hi, Thanks in advance for the help. I have been to many websites and tried several solutions to my problem, but have fixed part of it. It's time to come humbly to the newsgroups for help :-) By the way, please don't reply just to tell me that my code isn't optimized and that I should use a delimited database rather than a line by line record. If I had my way, I'd do it differently too.
5
1580
by: Nevets Steprock | last post by:
I have been building a website diligently for the past three months and everything has been working well so far. Yesterday, I added a link on my javascript menu. This link is supposed to go to a pdf in a folder instead of the usual html page. When I try to test the link an authentication dialog box pops up requesting username and password. Since the webuser has to log in to the main site anyway, I just want the file to pop up...
7
3651
by: Dave Coate | last post by:
Hi everyone, I am looking for a way to 'override' file security and read the Owner of a file to which I have no access. I am a system administrator, as such I have administrative rights to all the computers in the company. Some of my user base has full control rights to their files and have elected to remove my access to some files. It is possible for an administrator to regain access, but it is a messy process and can be time consuming....
3
4011
by: Dave Coate | last post by:
Hello again, I am going to re-post a question. I got some excellent suggestions from Rob and Mattias on this but their ideas did not solve the problem. Here is the original post: ***************************************************** I am looking for a way to 'override' file security and read the Owner of a file to which I have no access. I am a system administrator, as such I have administrative rights to all the computers in the...
1
9643
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej and I was wondering if anyone here would be able to give me some tips for young players such as myself, for learning the language. Is this the best Newsgroup for support with JAVA?
22
2189
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being grabbed, BUT it doesn't. I have tried and tried.....please help example: C:\Projects\Darryl\Queue Review Files\2-24\Cam 7\Cam7-20060224170000-01.jpg Cam7 but all I keep getting is Cam1, as the beginning of the jpg name,...:( HELP!
7
1781
by: rn5a | last post by:
This is the second time I am asking this question in this newsgroup since I haven't got a solution or response from anyone in my previous post & I need to resolve this issue desperately. Sorry for the double post but I just didn't have any other option other than re-posting the same question in this newsgroup. Consider the following code in a VB class file: Namespace LoginUserFetchDB Public Class ZForZebra : Inherits SoapHeader
9
2003
by: Synapse Syndrome | last post by:
Hi I've been given what I am told is a PHP script to be used on my server. I do not know any PHP. I am trying to use a feature of a program called ArchiCAD. This feauture allows CAD drawing files to be published on a webpage, and viewed and commented on using a java applet. The problem is that with ArchiCAD v10, it now makes spaces and uses other
10
4600
by: SimeonD | last post by:
Hi Is there a way I can fnd the Owner of a folder, using vb.net? I know how to find the permissions, but I can't figure how to find the owner. Thanks SimeonD
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.