Solved

How to use the GetFileTime API?

Posted on 1998-11-13
5
947 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

687 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