How to getting an owner of file



I know that that topic may be old to you but I looked at other 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

.



Relevant Pages

  • Re: Alpha search to load a list box
    ... Dim strTemp As String ... Private Sub LblAlpha_MouseDown(Button As Integer, Shift As Integer, X ... Dim StartX As Long, WidthX As Long ... Private Declare Function apiSelectObject Lib "gdi32" Alias ...
    (microsoft.public.access.formscoding)
  • Re: Alpha search to load a list box
    ... Dim strTemp As String ... Private Declare Function apiSelectObject Lib "gdi32" Alias ... Dim newfont As Long ' Handle to our Font Object we created. ...
    (microsoft.public.access.formscoding)
  • ListView.SelectedItem cannot be modified
    ... Dim objFind As LV_FINDINFO ... Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd ... lpClassName As String, ByVal lpWindowName As String) As Long ... 'CompareDates: This is the sorting routine that gets passed to the ...
    (microsoft.public.vb.controls)
  • Re: Need to call windows scheduler.
    ... Private Declare Function OpenSCManager Lib "advapi32.dll" Alias ... "OpenSCManagerA" (ByVal lpMachineName As String, ... Dim lhSCM As Long, lhService As Long, sState As String, lReturn ...
    (microsoft.public.access.formscoding)
  • Re: changing vbOKCancel button title
    ... Private Declare Function GetCurrentThreadId Lib "kernel32" _ ... ByVal lpCaption As String, _ ... Dim mbFlags2 As VbMsgBoxStyle ... SetDlgItemText wParam, vbAbort, But1 ...
    (microsoft.public.excel.misc)

Loading