VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



'Get File information by it's Handle using GetFileInformationByHandle API

by Karthikeyan (187 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Tue 23rd January 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

'Get File information by it's Handle using GetFileInformationByHandle API

API Declarations


Private Const OF_READWRITE = &H2
Private Const OF_REOPEN = &H8000
Private Const OF_SHARE_COMPAT = &H0
Private Const OF_SHARE_DENY_NONE = &H40
Private Const OF_SHARE_DENY_READ = &H30
Private Const OF_SHARE_DENY_WRITE = &H20
Private Const OF_SHARE_EXCLUSIVE = &H10
Private Const OF_VERIFY = &H400
Private Const OF_WRITE = &H1
Private Const OFS_MAXPATHNAME = 128
Private Type SYSTEMTIME
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
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type BY_HANDLE_FILE_INFORMATION
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
dwVolumeSerialNumber As Long
nFileSizeHigh As Long
nFileSizeLow As Long
nNumberOfLinks As Long
nFileIndexHigh As Long
nFileIndexLow As Long
End Type
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hfile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Rate 'Get File information by it's Handle using GetFileInformationByHandle API



'http://www.geocities.com/marskarthik
'http://marskarthik.virtualave.net
'Email: [email protected]
Private Sub Form_Load()
Dim fhi As BY_HANDLE_FILE_INFORMATION
Dim ctime As FILETIME, atime As FILETIME, wtime As FILETIME
Dim ftime As SYSTEMTIME
Dim buff As OFSTRUCT
Dim rval As Long, hfile As Long
hfile = OpenFile("H:\Pagefile.sys", buff, OF_READ)
GetFileInformationByHandle hfile, fhi
ctime = fhi.ftCreationTime
atime = fhi.ftLastAccessTime
wtime = fhi.ftLastWriteTime
MsgBox "Volume Serial Number is " & Hex(fhi.dwVolumeSerialNumber)
MsgBox "Filesize is " & fhi.nFileSizeLow & " Bytes"
'Convert File Time Zone to Local
rval = FileTimeToLocalFileTime(ctime, ctime)
'Convert File Time Format to System Time Format
rval = FileTimeToSystemTime(ctime, ftime)
MsgBox "File is created on " & ftime.wDay & "-" & ftime.wMonth & "-" & ftime.wYear & _
Chr(13) & "File is created at " & ftime.wHour & ":" & ftime.wMinute & ":" & ftime.wSecond
'Convert File Time Zone to Local
rval = FileTimeToLocalFileTime(wtime, wtime)
'Convert File Time Format to System Time Format
rval = FileTimeToSystemTime(wtime, ftime)
MsgBox "File is modified on " & ftime.wDay & "-" & ftime.wMonth & "-" & ftime.wYear & _
Chr(13) & "File is modified at " & ftime.wHour & ":" & ftime.wMinute & ":" & ftime.wSecond
'Convert File Time Zone to Local
rval = FileTimeToLocalFileTime(atime, atime)
'Convert File Time Format to System Time Format
rval = FileTimeToSystemTime(atime, ftime)
MsgBox "File is Lastly Accessed on " & ftime.wDay & "-" & ftime.wMonth & "-" & ftime.wYear & _
Chr(13) & "File is Lastly Accessed at " & ftime.wHour & ":" & ftime.wMinute & ":" & ftime.wSecond
CloseHandle hfile
End
End Sub

Download this snippet    Add to My Saved Code

'Get File information by it's Handle using GetFileInformationByHandle API Comments

No comments have been posted about 'Get File information by it's Handle using GetFileInformationByHandle API. Why not be the first to post a comment about 'Get File information by it's Handle using GetFileInformationByHandle API.

Post your comment

Subject:
Message:
0/1000 characters