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

File Owner

P: n/a
I know that that topic may be old to you but I looked at other more-
than-two-year-old topics
related to mine. However, I didn't find them working for my project
at all because its errors return back to me everytime.
The error I have on that project said:

"An unhandled exception of type 'System.EntryPointNotFoundException'
occurred in TestSysaudit.exe
Additional information: Unable to find an entry point named
GetFileSecurity in DLL advapi32.dll."
Here what I have on my project: (Remember this project is under VB
and
don't have a window form because of an executable file that run
everyday). I am appreciate that if you guys help me out on solving
the problem.
Option Explicit On
Imports System
Imports System.Text
Imports Scripting
Module Module1
Const OWNER_SECURITY_INFORMATION As Long = &H1
Const ERROR_INSUFFICIENT_BUFFER As Long = 122
Private Declare Function GetFileSecurity Lib "advapi32.dll" ( _
ByVal lpFileName As String, _
ByVal RequestedInformation As Long, _
ByRef pSecurityDescriptor As Byte, _
ByVal nLength As Long, _
ByVal lpnLengthNeeded As Long) _
As Long
Private Declare Function GetSecurityDescriptorOwner Lib
"advapi32.dll" ( _
ByRef ppSecurityDescriptor As Byte, _
ByVal ppOwner As Long, _
ByVal lpbOwnerDefaulted As Long) _
As Long
Private Declare Function LookupAccountSid Lib "advapi32.dll" ( _
ByVal lpSystemName As String, _
ByVal Sid As Long, _
ByVal name As String, _
ByRef cbName As Long, _
ByVal ReferencedDomainName As String, _
ByRef cbReferencedDomainName As Long, _
ByRef peUse As Long) _
As Long
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 - 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
' 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)
' 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
' 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
Public Sub Main()
Dim fldrPathName, fname, fowner As String
Dim fso As New FileSystemObject
Dim fldr As Folder
Dim fil As File
Dim fmodified, tdate As Date
Dim cur_date, run_date As String
Dim file_name As String
Dim filecnt As Integer
file_name = "c:\printout" & Year(Now()) &
Format(Month(Now()),
"00") & Format(Day(Now()), "00")
System.IO.File.Delete(file_name)
Dim file As New System.IO.StreamWriter(file_name)
cur_date = Format(Now(), "General Date")
file.WriteLine("Date - " & cur_date)
tdate = DateAdd(DateInterval.Day, -2, Now())
run_date = Format(tdate, "General Date")
file.WriteLine(" Files Modified Since - " &
run_date)
file.WriteLine("")
' Lets look for files that are newly modified last 48 hours
file.WriteLine("Files in C:\")
fldrPathName = ("c:\")
fldr = fso.GetFolder(fldrPathName)
For Each fil In fldr.Files
fname = fil.Name
fmodified = fil.DateLastModified
fowner = GetFileOwner(fname)
If fmodified tdate Then
file.WriteLine(" " & fowner & " " & fname & " " &
Format(fmodified, "General Date"))
file.WriteLine("")
End If
Next
file.Close()
End Sub
End Module

Mar 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Keep in mind that "Function" is actually named "FunctionA" or "FunctionW"
in the DLL when it accepts strings as it have two flavors depending on
wether you want to use ansi or unicode.

It's likely you forgot the Alias "GetFileSecurityA" clause.

---
Patrice

<le****@gmail.coma écrit dans le message de news:
11**********************@o5g2000hsb.googlegroups.c om...
>I know that that topic may be old to you but I looked at other more-
than-two-year-old topics
related to mine. However, I didn't find them working for my project
at all because its errors return back to me everytime.
The error I have on that project said:

"An unhandled exception of type 'System.EntryPointNotFoundException'
occurred in TestSysaudit.exe
Additional information: Unable to find an entry point named
GetFileSecurity in DLL advapi32.dll."
Here what I have on my project: (Remember this project is under VB
and
don't have a window form because of an executable file that run
everyday). I am appreciate that if you guys help me out on solving
the problem.
Option Explicit On
Imports System
Imports System.Text
Imports Scripting
Module Module1
Const OWNER_SECURITY_INFORMATION As Long = &H1
Const ERROR_INSUFFICIENT_BUFFER As Long = 122
Private Declare Function GetFileSecurity Lib "advapi32.dll" ( _
ByVal lpFileName As String, _
ByVal RequestedInformation As Long, _
ByRef pSecurityDescriptor As Byte, _
ByVal nLength As Long, _
ByVal lpnLengthNeeded As Long) _
As Long
Private Declare Function GetSecurityDescriptorOwner Lib
"advapi32.dll" ( _
ByRef ppSecurityDescriptor As Byte, _
ByVal ppOwner As Long, _
ByVal lpbOwnerDefaulted As Long) _
As Long
Private Declare Function LookupAccountSid Lib "advapi32.dll" ( _
ByVal lpSystemName As String, _
ByVal Sid As Long, _
ByVal name As String, _
ByRef cbName As Long, _
ByVal ReferencedDomainName As String, _
ByRef cbReferencedDomainName As Long, _
ByRef peUse As Long) _
As Long
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 - 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
' 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)
' 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
' 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
Public Sub Main()
Dim fldrPathName, fname, fowner As String
Dim fso As New FileSystemObject
Dim fldr As Folder
Dim fil As File
Dim fmodified, tdate As Date
Dim cur_date, run_date As String
Dim file_name As String
Dim filecnt As Integer
file_name = "c:\printout" & Year(Now()) &
Format(Month(Now()),
"00") & Format(Day(Now()), "00")
System.IO.File.Delete(file_name)
Dim file As New System.IO.StreamWriter(file_name)
cur_date = Format(Now(), "General Date")
file.WriteLine("Date - " & cur_date)
tdate = DateAdd(DateInterval.Day, -2, Now())
run_date = Format(tdate, "General Date")
file.WriteLine(" Files Modified Since - " &
run_date)
file.WriteLine("")
' Lets look for files that are newly modified last 48 hours
file.WriteLine("Files in C:\")
fldrPathName = ("c:\")
fldr = fso.GetFolder(fldrPathName)
For Each fil In fldr.Files
fname = fil.Name
fmodified = fil.DateLastModified
fowner = GetFileOwner(fname)
If fmodified tdate Then
file.WriteLine(" " & fowner & " " & fname & " " &
Format(fmodified, "General Date"))
file.WriteLine("")
End If
Next
file.Close()
End Sub
End Module

Mar 15 '07 #2

P: n/a
Okay I have been changed from:

Private Declare Function GetFileSecurity Lib "advapi32.dll" ( _

to

Private Declare Function GetFileSecurity Lib "advapi32.dll" Alias
"GetFileSecurityA" ( _

And I encountered another error message said:

"An unhandled exception of type 'System.IndexOutOfRangeException'
occurred in TestSysaudit.exe

Additional information: Index was outside the bounds of the array."

Okay, I fixed that problem and had to replace all "Long" into
"Integer". And none of the problem show up but the owner of a file
didn't write into the output file 'printout'.

The output file show:

test.txt 3/15/2007 8:54:17 AM

which should show up as:

admin test.txt 3/15/2007 8:54:17 AM

I would be appreciated that if you help me out what I did wrong or
miss something important.

Mar 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.