473,385 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.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
2 3408
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: pdav | last post by:
Hi! Is there any solution to create a directory with one script with mkdir(), and then write a file (or move an uploaded file) in this directory with another script? The problem is, that the...
10
by: Steve | last post by:
Hi all i am just starting to get back into VB and i need a little help. I am writing a program that asks a user to type in a set of numbers/letters (in this case shipping containers). Once the...
1
by: Ajay | last post by:
hi! i have an application that allows admin users to create template files, which are stored on the server. currently the file are created with the owner "nobody" is it possibly to change the...
2
by: John Regan | last post by:
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...
6
by: kai rosenthal | last post by:
Hello, with ls -l on windows I get -rw-r--r-- 1 500 everyone 320 Nov 09 09:35 myfile How can I get on windows with a standard python 2.2 (without windows extensions) the information "500" and...
4
by: tasahmed | last post by:
Hello Friends, I wrote a function which scans the current working directory and lists out details such as directory/file owner, permission etc. The output of this script can be viewing in the...
3
by: eholz1 | last post by:
Hello PHP Group, I am having trouble setting permissions correctly so that the magickwand api (php 5.2) can read and write images. I usually read a file from one directory, create a magickwand...
1
by: ahammad | last post by:
Hello, I have an XML file that I need to extract data from. The file has multiple lines, one of which contains the following tag: <Owner> owner's name goes here </Owner> I need to be able to...
3
by: Okonita | last post by:
Hi all, I am having problem completing this restore operation. "db2 restore database AAMI01 from /pap/data/backups taken at 20071002130554 to /pap/data/db01 into AAMI01 NEWLOGPATH /pap/data/new/...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.