Using Win32 functions in Visual FoxPro Image Gallery
Handle and Object
..msdn
CloseHandle
DuplicateHandle
GetHandleInformation
SetHandleInformation
Code examples:
Comparing file times
Copying files as a transacted operation (Vista)
Creating a console window for Visual FoxPro application
Creating a file, then moving it to another destination
Creating a mailslot
Enumerating Processes -- Win9*
Enumerating Processes -- WinNT
How to check whether the system is 32-bit or 64-bit
How to convert a bitmap file to monochrome format (1 bpp)
How to enable the SE_SHUTDOWN_NAME privilege for the application
How to find when the application started
How to make application automatically close all documents it opened
How to prevent users from accessing the Windows Desktop and from switching to other applications
How to run FoxPro application under different user name (impersonating user)
How to set Creation Date/Time for a folder (WinNT)
How to Start a Process as Another User (NT/XP/2K)
How to suspend or hibernate your system
HOWTO: Use the Win32 API to Access File Dates and Times
Locking and unlocking file of a VFP table
Monitoring changes occurring within a directory
Moving shortcut to a specified position on the Windows Desktop
Obtaining I/O counts for the current process
Obtaining names and positions for shortcuts located on the Windows Desktop
Obtaining physical parameters for a drive: sectors, clusters, cylinders...
Passing data records between VFP applications via the Clipboard
Peer-to-peer LAN messenger built with Mailslot API functions
Reading and setting system access privileges for the current process
Reading and setting the priority class values for the current process and thread
Retrieving file information for the VFP executable running
Running MSDOS Shell as a child process with redirected input and output (smarter RUN command)
Setting the date and time that a file was created
Starting external program from VFP and waiting for its termination
Storing content of the Clipboard to a bitmap file
Storing screen shot of a form to bitmap file
Subclassing CommandButton control to create BackColor property
Terminating all running applications from a VFP program
Testing serial ports
Using File Mapping for enumerating files opened by Visual FoxPro
Using GetFileSize
Using InternetSetFilePointer when resuming interrupted download from the Internet
Using mailslots to send messages on the network
Using named pipes for interprocess communication
Using shared memory to exchange data between two FoxPro applications
Using the CreateFile
Using the DeleteFile
Using the Semaphore object
Using the Semaphore object to allow only one instance of VFP application running
Vertical Label control
HOWTO: Use the Win32 API to Access File Dates and Times

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Versions:
click to open
Before you begin:
This sample code originates from HOWTO: Use the Win32 API to Access File Dates and Times

Unfortunately two regrettable errors make the original code unusable: missing @ character in GetFileTime call, and hex2dec function, never being tested I guess. So I was bold enough to make some fixes to it:
 
*********************************************************
* Function getftime
* Return the create date and time, last access date and time,
* last write date and time from a passed filename.
* 
* Passed:
* lcFileName (by value): fully-qualified filename
* 
* Returns:
* Logical .t. if completed successfully, .f. if not.
* 
* Return variables:
* ldCreateDate (date, by reference): Creation date
* lcCreateTime (character, by reference): Creation time (HH:MM:SS.mm)
* ldAccessDate (date, by reference): Last access date
* lcAccessTime (character, by reference): Last access time (HH:MM:SS.mm)
* lcLastWriteDate (date, by reference): Last write date
* lcLastWriteTime (character, by reference): Last write time (HH:MM:SS.mm)
 
STORE {} TO m.createdate, m.accessdate, m.lastwritedate
STORE "" TO m.createtime, m.accesstime, m.lastwritetime
 
m.OK = getftime("C:\Test.txt", @m.createdate, @m.createtime, ;
    @m.accessdate, @m.accesstime, @m.lastwritedate, @m.lastwritetime)
 
IF m.OK
    ? "Create date:      ", m.createdate
    ? "Create time:      " , m.createtime
    ? "Last access date: ", m.accessdate
    ? "Last access time: ", m.accesstime
    ? "Last write date:  ", m.lastwritedate
    ? "Last write time:  ", m.lastwritetime
ELSE
    ? "Error getting file times"
ENDIF
* End of usage example.
 
FUNCTION getftime
PARAMETERS lcFileName, ldCreateDate, lcCreateTime, ldAccessDate,;
    lcAccessTime, ldLastWriteDate, lcLastWriteTime
 
* DEFINEs for file share mode.
#DEFINE FILE_SHARE_READ  1
#DEFINE FILE_SHARE_WRITE 2
 
* DEFINEs for access right
 #DEFINE GENERIC_READ  hex2dec("80000000")
 #DEFINE GENERIC_WRITE hex2dec("40000000")
 
* DEFINES for Create Mode.
#DEFINE CREATE_NEW  1
#DEFINE CREATE_ALWAYS 2
#DEFINE OPEN_EXISTING 3
#DEFINE OPEN_ALWAYS 4
#DEFINE TRUNCATE_EXISTING 5
 
* File flag.
#DEFINE FILE_ATTRIBUTE_NORMAL hex2dec("80")
 
    * Blank filename or nonexistent file, no need to continue.
    IF EMPTY(lcFileName) OR NOT FILE(lcFileName)
        RETURN .F.
    ENDIF
 
    DECLARE INTEGER CreateFile IN kernel32;
        STRING lpFileName, INTEGER dwDesiredAccess,;
        INTEGER dwShareMode, INTEGER lpSecurityAttributes,;
        INTEGER dwCreationDisposition,;
        INTEGER dwFlagsAndAttributes, INTEGER hTemplateFile
 
    DECLARE INTEGER GetFileTime IN kernel32;
        INTEGER hFile, STRING @lpCreationTime,;
        STRING @lpLastAccessTime, STRING @lpLastWriteTime
 
    DECLARE INTEGER FileTimeToLocalFileTime IN kernel32;
        STRING @lpFileTime, STRING @lpLocalFileTime
 
    DECLARE INTEGER FileTimeToSystemTime IN kernel32;
        STRING @lpFileTime, STRING @lpSystemTime
 
    DECLARE INTEGER GetLastError IN kernel32
    DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject
 
    * 2 DWORDs * 4 bytes each = an 8-byte buffer.
    STORE SPACE(8) TO CreationTime, LastAccessTime, lastwritetime
 
    * Same buffers for local time.
    STORE SPACE(8) TO LocalCreationTime,;
        LocalLastAccessTime, LocalLastWriteTime
 
    * 16-byte buffers for when we convert from the FILETIME
    * structs to the more human-readable SYSTEMTIME structures.
    STORE SPACE(16) TO ctSystemTime, laSystemTime, lwSystemTime
 
    * Open an existing file.
    lhFileHandle = CreateFile(lcFileName, GENERIC_READ,;
        FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
 
    IF lhFileHandle <= 0
        =MESSAGEB ("Error in CreateFile - " + lcFileName + ": Error code - " + ;
            LTRIM(STR(GetLastError())))
        RETURN .F.
    ENDIF
 
    * Make the GetFileTime call, to get Create, Access and Update times.
    m.retval = GetFileTime (lhFileHandle,;
        @CreationTime, @LastAccessTime, @lastwritetime)
 
    IF m.retval = 0
    * 32 = Sharing violation
    * 87 = Invalid parameter
        =MESSAGEB("Error calling GetFileTime. Error code: " + ;
            LTRIM(STR(GetLastError())))
        RETURN .F.
    ENDIF
 
* Successfully performed the GetFileTime call -
* now convert each of the three times to local time.
* If any of them fail, then exit.
    IF     FileTimeToLocalFileTime (@CreationTime, @LocalCreationTime) = 0;
        OR FileTimeToLocalFileTime (@LastAccessTime,@LocalLastAccessTime) = 0;
        OR FileTimeToLocalFileTime (@lastwritetime, @LocalLastWriteTime) = 0 
 
        = MESSAGEB("FileTimeToLocalFileTime Failed. Error code: " + ;
            LTRIM(STR(GetLastError())))
        RETURN .F.
    ENDIF
 
* Now you have a local filetime, convert each to a systemtime that
* you can work with.
* If any of the calls fail, then exit.
    IF     FileTimeToSystemTime (@LocalCreationTime, @ctSystemTime) = 0;
        OR FileTimeToSystemTime (@LocalLastAccessTime, @laSystemTime) = 0;
        OR FileTimeToSystemTime (@LocalLastWriteTime, @lwSystemTime) = 0
 
        = MESSAGEB("FileTimeToSystemTime call failed. Error code: " ;
            + LTRIM(STR(GetLastError())))
        RETURN .F.
    ENDIF
 
    ctstYear = Str2Word(LEFT(ctSystemTime, 2))
    ctstMonth = Str2Word(SUBSTR(ctSystemTime, 3, 2))
    ctstDayofWeek = Str2Word(SUBSTR(ctSystemTime, 5, 2))
    ctstDay = Str2Word(SUBSTR(ctSystemTime, 7, 2))
    ctstHour = Str2Word(SUBSTR(ctSystemTime, 9, 2))
    ctstMinute = Str2Word(SUBSTR(ctSystemTime, 11, 2))
    ctstSecond = Str2Word(SUBSTR(ctSystemTime, 13, 2))
    ctstMilliSecond = Str2Word(SUBSTR(ctSystemTime, 15, 2))
 
* Unpack the integers in last access time SystemTime structure.
    lastYear = Str2Word(LEFT(laSystemTime, 2))
    lastMonth = Str2Word(SUBSTR(laSystemTime, 3, 2))
    lastDayofWeek = Str2Word(SUBSTR(laSystemTime, 5, 2))
    lastDay = Str2Word(SUBSTR(laSystemTime, 7, 2))
    lastHour = Str2Word(SUBSTR(laSystemTime, 9, 2))
    lastMinute = Str2Word(SUBSTR(laSystemTime, 11, 2))
    lastSecond = Str2Word(SUBSTR(laSystemTime, 13, 2))
    lastMilliSecond = Str2Word(SUBSTR(laSystemTime, 15, 2))
 
* Unpack the integers in last write time SystemTime structure.
    lwstYear = Str2Word(LEFT(lwSystemTime, 2))
    lwstMonth = Str2Word(SUBSTR(lwSystemTime, 3, 2))
    lwstDayofWeek = Str2Word(SUBSTR(lwSystemTime, 5, 2))
    lwstDay = Str2Word(SUBSTR(lwSystemTime, 7, 2))
    lwstHour = Str2Word(SUBSTR(lwSystemTime, 9, 2))
    lwstMinute = Str2Word(SUBSTR(lwSystemTime, 11, 2))
    lwstSecond = Str2Word(SUBSTR(lwSystemTime, 13, 2))
    lwstMilliSecond = Str2Word(SUBSTR(lwSystemTime, 15, 2))
 
* Close the file handle.
    llRetCode = CloseHandle(lhFileHandle)
 
* Format the return variables.
    ldCreateDate = CTOD(LTRIM(STR(ctstMonth)) + "/" + ;
        LTRIM(STR(ctstDay)) + "/" + LTRIM(STR(ctstYear)))
 
    lcCreateTime = PADL(LTRIM(STR(ctstHour)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(ctstMinute)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(ctstSecond)), 2, "0") + "." + ;
        PADL(LTRIM(STR(ctstMilliSecond)), 3, "0")
 
    ldAccessDate = CTOD(LTRIM(STR(lastMonth)) + "/" + ;
        LTRIM(STR(lastDay)) + "/" + LTRIM(STR(lastYear)))
 
    lcAccessTime = PADL(LTRIM(STR(lastHour)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(lastMinute)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(lastSecond)), 2, "0") + "." + ;
        PADL(LTRIM(STR(lastMilliSecond)), 3, "0")
 
    ldLastWriteDate = CTOD(LTRIM(STR(lwstMonth))+ ;
        "/"+LTRIM(STR(lwstDay)) + "/" + LTRIM(STR(lwstYear)))
 
    lcLastWriteTime = PADL(LTRIM(STR(lwstHour)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(lwstMinute)), 2, "0") + ":" + ;
        PADL(LTRIM(STR(lwstSecond)), 2, "0") + "." + ;
        PADL(LTRIM(STR(lwstMilliSecond)), 3, "0")
 
* Function hex2dec - Convert a character hex value into decimal.
* Handy for dealing with Hex declarations in API calls.
* Passed: m.hex (character hex value)
* Returns: Decimal value
FUNCTION hex2dec
PARAMETER m.hex
    m.decimal = 0
    FOR i = LEN(m.hex) TO 1 STEP -1
        m.decimal = m.decimal + (AT(SUBSTR(m.hex, i, 1), ;
            "0123456789ABCDEF") - 1) * 16 ^ (LEN(m.hex) - i)
    NEXT
RETURN m.decimal
 
* Function str2word - Converts low-high format string representation
* to a 16-bit integer (word) value. Useful for unrolling structure
* members containg WORD types.
* 
* Passed: Low-high string representation of 16-bit integer
* Returns: numeric value
FUNCTION Str2Word
PARAMETERS m.wordstr
    PRIVATE i, m.retval
 
    m.retval = 0
    FOR i = 0 TO 8 STEP 8
        m.retval = m.retval + (ASC(m.wordstr) * (2^i))
        m.wordstr = RIGHT(m.wordstr, LEN(m.wordstr) - 1)
    NEXT
RETURN m.retval
 
 

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
8038 bytes  
Created: 2001-10-28 10:13:20  
Modified: 2007-05-04 08:01:54  
Visits in 7 days: 133  
Listed functions:
CloseHandle
CreateFile
FileTimeToLocalFileTime
FileTimeToSystemTime
GetFileTime
GetLastError
Printer friendly API declarations
My comment:
It seems that calling MODIFY FILE in VFP6 completely removes the previous file state, because the file creation time changes to become equivalent to the last write time. This is in contrast to the Notepad editing, which preserves the original creation datetime value.

FoxPro ADIR() function places information about files into an array and then returns the number of files. It returns only date and time the file was last modified.

In C# version, I use managed FileInfo object instead of accessing file times through the System.Runtime.InteropServices namespace.
Word Index links for this example:
Translate this page:
  Spanish    Portuguese    German    French    Italian  
FreeTranslation.com offers instant, free translations of text or web pages.
User Contributed Notes:
There are no notes on this subject.


Copyright © 2001-2013 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

Privacy policy
Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.1.55-log), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (50.16.36.153)
1.35 hrs.Example: 'Locking mouse and keyboard input for the VFP application'
 Function: 'waveInStart'
1.75 hrs.Example: 'Dial the Net Automatically'
 Example: 'How to delete all print jobs for a printer'
 Example: 'Accessing examples contained in this reference from a VFP application'
6.43 hrs.Function: 'GetActiveWindow'
6.88 hrs.Article: 'Use Windows API calls to program the MessageBox dialog's timeout interval, button captions, and position. '
 Example: 'Winsock: connecting to a news server (NNTP, port 119)'
7.9 hrs.Example: 'How to obtain Content-Type value for a file type from the System Registry'
 Function: 'GetClipboardData'
Google
Advertise here!