File Owner

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • levimc@gmail.com

    #1

    File Owner

    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.EntryPo intNotFoundExce ption'
    occurred in TestSysaudit.ex e


    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_INSUFFICI ENT_BUFFER As Long = 122


    Private Declare Function GetFileSecurity Lib "advapi32.d ll" ( _
    ByVal lpFileName As String, _
    ByVal RequestedInform ation As Long, _
    ByRef pSecurityDescri ptor As Byte, _
    ByVal nLength As Long, _
    ByVal lpnLengthNeeded As Long) _
    As Long


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


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


    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 - 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


    ' 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)


    ' 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


    ' 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


    Public Sub Main()


    Dim fldrPathName, fname, fowner As String
    Dim fso As New FileSystemObjec t
    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:\printou t" & Year(Now()) &
    Format(Month(No w()),
    "00") & Format(Day(Now( )), "00")


    System.IO.File. Delete(file_nam e)
    Dim file As New System.IO.Strea mWriter(file_na me)
    cur_date = Format(Now(), "General Date")


    file.WriteLine( "Date - " & cur_date)
    tdate = DateAdd(DateInt erval.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(f ldrPathName)
    For Each fil In fldr.Files
    fname = fil.Name
    fmodified = fil.DateLastMod ified
    fowner = GetFileOwner(fn ame)
    If fmodified tdate Then
    file.WriteLine( " " & fowner & " " & fname & " " &
    Format(fmodifie d, "General Date"))
    file.WriteLine( "")
    End If
    Next


    file.Close()
    End Sub


    End Module

  • Patrice

    #2
    Re: File Owner

    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 "GetFileSecurit yA" clause.

    ---
    Patrice



    <levimc@gmail.c oma écrit dans le message de news:
    1173970329.0012 88.253820@o5g20 00...legro ups.com...
    >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.EntryPo intNotFoundExce ption'
    occurred in TestSysaudit.ex e
    >
    >
    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_INSUFFICI ENT_BUFFER As Long = 122
    >
    >
    Private Declare Function GetFileSecurity Lib "advapi32.d ll" ( _
    ByVal lpFileName As String, _
    ByVal RequestedInform ation As Long, _
    ByRef pSecurityDescri ptor As Byte, _
    ByVal nLength As Long, _
    ByVal lpnLengthNeeded As Long) _
    As Long
    >
    >
    Private Declare Function GetSecurityDesc riptorOwner Lib
    "advapi32.d ll" ( _
    ByRef ppSecurityDescr iptor As Byte, _
    ByVal ppOwner As Long, _
    ByVal lpbOwnerDefault ed As Long) _
    As Long
    >
    >
    Private Declare Function LookupAccountSi d Lib "advapi32.d ll" ( _
    ByVal lpSystemName As String, _
    ByVal Sid As Long, _
    ByVal name As String, _
    ByRef cbName As Long, _
    ByVal ReferencedDomai nName As String, _
    ByRef cbReferencedDom ainName As Long, _
    ByRef peUse As Long) _
    As Long
    >
    >
    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 - 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
    >
    >
    ' 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)
    >
    >
    ' 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
    >
    >
    ' 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
    >
    >
    Public Sub Main()
    >
    >
    Dim fldrPathName, fname, fowner As String
    Dim fso As New FileSystemObjec t
    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:\printou t" & Year(Now()) &
    Format(Month(No w()),
    "00") & Format(Day(Now( )), "00")
    >
    >
    System.IO.File. Delete(file_nam e)
    Dim file As New System.IO.Strea mWriter(file_na me)
    cur_date = Format(Now(), "General Date")
    >
    >
    file.WriteLine( "Date - " & cur_date)
    tdate = DateAdd(DateInt erval.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(f ldrPathName)
    For Each fil In fldr.Files
    fname = fil.Name
    fmodified = fil.DateLastMod ified
    fowner = GetFileOwner(fn ame)
    If fmodified tdate Then
    file.WriteLine( " " & fowner & " " & fname & " " &
    Format(fmodifie d, "General Date"))
    file.WriteLine( "")
    End If
    Next
    >
    >
    file.Close()
    End Sub
    >
    >
    End Module
    >

    Comment

    • levimc@gmail.com

      #3
      Re: File Owner

      Okay I have been changed from:

      Private Declare Function GetFileSecurity Lib "advapi32.d ll" ( _

      to

      Private Declare Function GetFileSecurity Lib "advapi32.d ll" Alias
      "GetFileSecurit yA" ( _

      And I encountered another error message said:

      "An unhandled exception of type 'System.IndexOu tOfRangeExcepti on'
      occurred in TestSysaudit.ex e

      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.

      Comment

      Working...