Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetFileInformationByHandle

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,
maybe it's just beginners misfortune but i'm having troubles concerning this function.
I don't know how to specify the parameter hFile since it is a long integer.I don't see how to specify the path and the filename of the file from which I want to retrieve this information.

Can somebody help ??

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 Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
 
You just need to establish a handle to the file you want information about. There's different ways to do this using API file functions -- here's how to do it using the OpenFile function, notice that by calling the function with hFile = OpenFile(etc), the value for the handle (which are always longs) is assigned to hFile. Then you can just plug it into your function call.

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 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

Const OFS_MAXPATHNAME = 128
Const OF_CREATE = &H1000
Const OF_READ = &H0
Const OF_WRITE = &H1

Private Sub Form_Load()

sFile = "c:\filetest.bat"

Dim hFile As Long, FileInfo As BY_HANDLE_FILE_INFORMATION
Dim OF As OFSTRUCT

hFile = OpenFile(sFile, OF, OF_READ)

GetFileInformationByHandle hFile, FileInfo

CloseHandle hFile ' close the handle to the file

MsgBox "File size: " + CStr(FileInfo.nFileSizeLow), vbInformation


End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top