Solved

How to use the GetFileTime API?

Posted on 1998-11-13
5
882 Views
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?
0
Comment
Question by:Olli083097
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:ajmendes
Comment Utility
Hi,
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
EndType
64-bit number specifying the elapsed time since January 1, 1601, in 100-nanosecond increments.


0
 

Author Comment

by:Olli083097
Comment Utility
I need to know HOW to do it?!

But thanx for the help anyway...!
0
 
LVL 1

Accepted Solution

by:
prozak earned 50 total points
Comment Utility
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 ...
0
 
LVL 1

Expert Comment

by:prozak
Comment Utility
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?
0
 

Author Comment

by:Olli083097
Comment Utility
Thanx!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now