How to use the GetFileTime API?

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?
Who is Participating?
prozakConnect With a Mentor Commented:
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 ...
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.

Olli083097Author Commented:
I need to know HOW to do it?!

But thanx for the help anyway...!
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?
Olli083097Author Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.