How to use the GetFileTime API?

Posted on 1998-11-13
Last Modified: 2008-03-17
How do I use the GetFileTime API call?

BTW:       Could you explain what the prefixes means?
      E.G. nDrive, hFile, lpCreationTime, lpszLongPath   and fPictureOwnsHandle.
      What does the n, h, lp, lpsz and f mean?
Question by:Olli083097
  • 2
  • 2

Expert Comment

ID: 1444770
the declaration is like this:
Declare function GetFileTime& Lib "kernel32" (ByVal hfile as long, lpCreationTime as FILETIME, lpLastAccessTime as FILETIME, lpLastWriteTime as FILETIME) as Long

hFile: Long - a handle to a file
lpCreationTime: FILETIME - a FILETIME structure to load the last access time of the file
lpLastAccessTime: FILETIME - a FILETIME structure to load the last access time of the file (not supported by FAT file systems)
lpLastWriteTime: FILETIME - a FILETIME structure to load the last access time of the file

Type FILETIME  '8 Bytes
  dwLowDateTime as Long
  dwHighDateTime as Long
64-bit number specifying the elapsed time since January 1, 1601, in 100-nanosecond increments.


Author Comment

ID: 1444771
I need to know HOW to do it?!

But thanx for the help anyway...!

Accepted Solution

prozak earned 50 total points
ID: 1444772
Well I'm not sure what it is you want to do but here's some example code on how to use the GetFileTime API function:

'This is used by the GetFileTime function to store the file time
Public Type typFILETIME
  dwLowDateTime As Long
  dwHighDateTime As Long
End Type

'Constant to define the size of the buffer in the OFSTRUCT structure
Const cOFS_MAXPATHNAME As Integer = 128

'This is a structure that OpenFile will populate for you
Public Type typOFSTRUCT
  cBytes As Byte
  fFixedDisk As Byte
  nErrCode As Integer
  Reserved1 As Integer
  Reserved2 As Integer
  szPathName As String * cOFS_MAXPATHNAME
End Type

'This is a structure returned by FileTimeToSystemTime in order
'  to get the time information in a usable format
Public Type typSYSTEMTIME
  wYear As Integer
  wMonth As Integer
  wDayOfWeek As Integer
  wDay As Integer
  wHour As Integer
  wMinute As Integer
  wSecond As Integer
  wMilliseconds As Integer
End Type

'OpenFile opens a file, we need to call it to get a handle to
'  call GetFileTime with
Declare Function OpenFile& Lib "kernel32" (ByVal lpFileName As String, _
lpReOpenBuff As typOFSTRUCT, ByVal wStyle As Long)

'CloseHandle closes the file we opened with OpenFile
Declare Function CloseHandle& Lib "kernel32" (ByVal hObject As Long)

'GetFileTime - we already know what this does!
Declare Function GetFileTime& Lib "kernel32" (ByVal hFile As Long, _
lpCreationTime As typFILETIME, lpLastAccessTime As typFILETIME, _
lpLastWriteTime As typFILETIME)

'FileTimeToSystemTime converts a FILETIME structure to a
'  SYSTEMTIME structure so we can get some meaningful
'  information about the file
Declare Function FileTimeToSystemTime& Lib "kernel32" (lpFileTime As typFILETIME, _
lpSystemTime As typSYSTEMTIME)

'This is a function that's useful for format the SYSTEMTIME
'  structure to a string
Private Function FormatSysTimeForUser(tSysTime As typSYSTEMTIME) As String
  Dim strRet As String
  With tSysTime
    strRet = CStr(.wMonth) & "/" & CStr(.wDay) & "/" & CStr(.wYear) & " "
    strRet = strRet & Format(CDate(CStr(.wHour) & ":" & CStr(.wMinute) & ":" & CStr(.wSecond)), _
      "HH:MM:SS AMPM")
  End With
  FormatSysTimeForUser = strRet
End Function

'This is the function that displays the filetime
Public Sub DisplayFileTime()
  Dim tCreateTime As typFILETIME
  Dim tAccessTime As typFILETIME
  Dim tWriteTime As typFILETIME
  Dim hFile As Long
  Dim tReopenBuf As typOFSTRUCT
  Dim tSysTime As typSYSTEMTIME
  Dim strMsg As String
  'Open the file to get a handle to pass to GetFileTime, the
  '  0 at the end means we want to open the file Read Only
  hFile = OpenFile("c:\autoexec.bat", tReopenBuf, 0)
  If hFile = -1 Then
    MsgBox "Could not open file!"
    Exit Sub
  End If
  'Get the file time
  If GetFileTime(hFile, tCreateTime, tAccessTime, tWriteTime) = 0 Then
    Call CloseHandle(hFile)
    MsgBox "Could not get file time!"
    Exit Sub
  End If
  'Convert the creation time
  If FileTimeToSystemTime(tCreateTime, tSysTime) = 0 Then
    Call CloseHandle(hFile)
    MsgBox "Error converting create time!"
    Exit Sub
  End If
  'Format the creation time part of the message
  strMsg = "Creation Time: " & FormatSysTimeForUser(tSysTime) & vbCrLf
  'Convert the access time
  If FileTimeToSystemTime(tAccessTime, tSysTime) = 0 Then
    Call CloseHandle(hFile)
    MsgBox "Error converting access time!"
    Exit Sub
  End If
  'Format the access time part of the message
  strMsg = strMsg & "Access Time: " & FormatSysTimeForUser(tSysTime) & vbCrLf
  'Convert the write time
  If FileTimeToSystemTime(tWriteTime, tSysTime) = 0 Then
    Call CloseHandle(hFile)
    MsgBox "Error converting write time!"
    Exit Sub
  End If
  'Format the write time part of the message
  strMsg = strMsg & "Write Time: " & FormatSysTimeForUser(tSysTime)
  'Close the handle to the file
  Call CloseHandle(hFile)

  'Display the message  
  MsgBox strMsg
End Sub

Hope this helps you some. As for the other part of your question, MS uses something called Hungarian notation for their variables and parameters. This simply encapsulates some type information into the name of the variable or parameter.

lp - Long pointer (or far pointer - a 32-bit pointer)
lpsz - Long pointer to a zero terminated string (C-style string)
n - number (typically a WORD (16-bit value) or Integer in VB)
h - handle (typically a DWORD (32-bit value) or Long in VB)

That's just a few. I'm not really sure what f means. At first glance I would thing f would be a float (Single in VB) but Windows doesn't have any native support for floating point numbers so it can't be that. If you have any more questions, let me know ...

Expert Comment

ID: 1444773
Oops, I just saw the f in fFixedDrive part of the typOFSTRUCT structure. I have no clue what the f means :) I checked the help in VC++ 5.0 and couldn't find anything. Anybody else know?

Author Comment

ID: 1444774

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question