Solved

How to use the GetFileTime API?

Posted on 1998-11-13
5
897 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
ID: 1444770
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
ID: 1444771
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
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 ...
0
 
LVL 1

Expert Comment

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

Author Comment

by:Olli083097
ID: 1444774
Thanx!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to open Waze.com/livemap from address saved in DB? 26 177
Macro which automatically sends attachment to Outlook 14 61
Copy a row 12 57
clicking a shape in a frame array vb6 3 40
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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

22 Experts available now in Live!

Get 1:1 Help Now