Harlan Carvey wrote:

> All, 
> 
> I'm using Dave Roth's Win32::API::Prototype with Perl
> v.5.8 on XP.  I'm running a script (below) to pull the
> UTC filetimes from files.  
> 
> At this point, all I'm trying to do is retrieve the
> UTC filetimes from the file and convert them to system
> times (w/o passing them through
> FileTimeToLocalFileTime() first).  However, all of the
> MAC times are the same:
> 
> D:\Perl>mac2.pl mac2.pl
> Using Win32 API, UTC times:
> Creation Time:  Sat 8/23/2003 1:00:55:950
> Last Access  :  Sat 8/23/2003 1:00:55:950
> Last Write   :  Sat 8/23/2003 1:00:55:950
> 
> 
> Using stat()
> mac2.pl 5556 bytes
> Creation Time:  Fri Aug 22 19:24:54 2003
> Last Access  :  Sat Aug 23 15:47:55 2003
> Last Write   :  Fri Aug 22 21:00:55 2003
> 
> Thoughts?  Ideas?  Assistance is greatly appreciated.

Works for me (I did make a few changes and anally re-formatted
your code ;) :

use strict;
use Win32::API::Prototype;

# Set up the necessary API calls

ApiLink ('kernel32.dll', 'HANDLE CreateFile (LPCTSTR pszPath, DWORD dwAccess,
  DWORD dwShareMode, PVOID SecurityAttributes, DWORD dwCreationDist,
  DWORD dwFlags, HANDLE hTemplate)') || die "Cannot locate CreateFile ()";

ApiLink ('kernel32.dll', 'BOOL CloseHandle (HANDLE hFile)') ||
  die "Cannot locate CloseHandle ()";

ApiLink ('kernel32.dll', 'BOOL SystemTimeToFileTime (SYSTEMTIME *lpSystemTime,
  LPFILETIME lpFileTime)') || die "Cannot locate SystemTimeToFileTime ()";

ApiLink ('kernel32.dll', 'BOOL GetFileTime (HANDLE hfile,
  LPFILETIME lpCreationTime, LPFILETIME lpLastAccessTime,
  LPFILETIME lpLastWriteTime)') || die "Cannot locate GetFileTime ()";

ApiLink ('kernel32.dll', 'BOOL FileTimeToSystemTime (FILETIME *lpFileTime,
  LPSYSTEMTIME lpSystemTime)') || die "Cannot locate FileTimeToSystemTime ()";

ApiLink ('kernel32.dll', 'BOOL FileTimeToLocalFileTime (FILETIME *lpFileTime,
  LPFILETIME lpLocalFileTime )') ||
  die "Cannot locate FileTimeToLocalFileTime ()";

ApiLink('kernel32.dll', 'VOID GetSystemTime (LPSYSTEMTIME lpSystemTime)') ||
  die "Cannot locate GetSystemTime ()";

# Get file

my $file = shift || die "You must enter a filename.\n";

# Determine whether the file even exists or not

die "$file not found.\n" unless (-e $file);

# Create a filehandle to the file
# Must use GENERIC_WRITE in order to modify the object

my $OPEN_EXISTING = 3;
my $GENERIC_READ = 0x80000000;
my $GENERIC_WRITE = 0x40000000;
my $FILE_SHARE_READ = 0x00000001;

my $hFile = CreateFile ($file, $GENERIC_READ|$GENERIC_WRITE, $FILE_SHARE_READ,
  0, $OPEN_EXISTING, 0, 0) || die "Cannot open the file '$file': " .
  Win32::FormatMessage (Win32::GetLastError) . "\n";

my $lpLastAccessTime = pack "S8", 0, 0, 0, 0, 0, 0, 0, 0;
my $lpLastWriteTime = pack "S8", 0, 0, 0, 0, 0, 0, 0, 0;
my $lpCreationTime = pack "S8", 0, 0, 0, 0, 0, 0, 0, 0;

if ($hFile) {

        if (GetFileTime ($hFile, $lpCreationTime, $lpLastAccessTime,
          $lpLastWriteTime)) {

                CloseHandle ($hFile);
                # UTC times successfully retrieved.
                # First, convert the FILETIME objects to SYSTEMTIME objects
                print "Using Win32 API, UTC times:\n";
                my $a_time = sys_STR (ft_LT_ST ($lpLastAccessTime));
                my $m_time = sys_STR (ft_LT_ST ($lpLastWriteTime));
                my $c_time  = sys_STR (ft_LT_ST ($lpCreationTime));
                print "Creation Time:\t$c_time\n";
                print "Last Access  :\t$a_time\n";
                print "Last Write   :\t$m_time\n\n";
                
        } else {
                print "Error in GetFileTime: " . Win32::FormatMessage (
                  Win32::GetLastError) . "\n";
        }
} else {
        print "Error in getting file handle: " . Win32::FormatMessage (
          Win32::GetLastError) . "\n";
}

getTimes ($file);

# Convert a FILETIME object to a SYSTEMTIME object

sub ft_ST {
        my $lpFileTime = $_[0];

my $lpSystemTime = pack "S8", 0, 0, 0, 0, 0, 0, 0, 0;
if (FileTimeToSystemTime ($lpFileTime, $lpSystemTime)) {
        return $lpSystemTime;
} else {
        my $err = Win32::FormatMessage (Win32::GetLastError);
        print "Error in SystemTimeToFileTime: $err\n";
        return undef;
}

}

# Convert a FILETIME object to a SYSTEMTIME object, getting the
# local time first

sub ft_LT_ST {
        my $lpFileTime = $_[0];

my $lpSystemTime = pack "S8", 0, 0, 0, 0, 0, 0, 0, 0;
my $lpLocalFileTime = pack "S8", 0, 0, 0, 0, 0, 0, 0, 0;
if (FileTimeToLocalFileTime ($lpFileTime, $lpLocalFileTime)) {
        $lpSystemTime = ft_ST ($lpLocalFileTime);
} else {
        my $err = Win32::FormatMessage (Win32::GetLastError);
        print "Error in FileTimeToLocalFileTime: $err\n";
}
return $lpSystemTime;

}

# Convert returned SystemTime into a string

sub sys_STR {
        my $lpSystemTime = $_[0];
        my @WDAY = qw(Sun Mon Tues Wednes Thurs Fri Sat);
        my @MON = qw(NUL Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

my ($wYear, $wMonth, $wDayOfWeek, $wDay, $wHour, $wMinute, $wSecond,
  $wMilliseconds) = unpack "S8", $lpSystemTime;
sprintf "%s %s %u %02u:%02u:%02u %u", $WDAY[$wDayOfWeek], $MON[$wMonth],
  $wDay, $wHour, $wMinute, $wSecond, $wYear;

}

sub getTimes {
        my $file = $_[0];

my ($size, $atime, $mtime, $ctime) = (stat $file)[7..10];
my $a_time = localtime $atime;
my $m_time = localtime $mtime;
my $c_time = localtime $ctime;
print "\nUsing stat()\n";
print "$file\t$size bytes\n";
print "Creation Time:\t$c_time\n";
print "Last Access  :\t$a_time\n";
print "Last Write   :\t$m_time\n";

}

__END__


-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)

_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to