GetFileSize()

Purpose
The FindClose function retrieves the size, in bytes, of the specified file.
 
Library Transcribed by Date of page Updated on
Kernel32.dll J.Paris 25.04.06  
 
Restrictions on use
none
 
Declare 32-bit

Declare Function GetFileSize Lib "kernel32.dll"

        ( ByVal hFile As Integer, lpFileSizeHigh As Integer

         ) As Integer

Required Type definition
 
 
Parameters

hFile

 

 

lpFileSizeHigh

Specifies an open handle of the file whose size is being returned. The handle must have been created with either GENERIC_READ or GENERIC_WRITE access to the file

 

Points to the variable where the high-order word of the file size is returned. This parameter can be NULL if the application does not require the high-order word.

 
Return value

If the function succeeds, the return value is the low-order doubleword of the file size, and, if lpFileSizeHigh is non-NULL, the function puts the high-order doubleword of the file size into the variable pointed to by that parameter.

If the function fails and lpFileSizeHigh is NULL, the return value is 0xFFFFFFFF. To get extended error information, call GetLastError.

If the function fails and lpFileSizeHigh is non-NULL, the return value is 0xFFFFFFFF and GetLastError will return a value other than NO_ERROR.

 
Example

Declare Function GetFileSize Lib "kernel32.dll" ( ByVal hFile As Integer, lpFileSizeHigh As Integer

) As Integer

Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (

     ByVal lpFileName As String, ByVal dwDesiredAccess As Integer,

     ByVal dwShareMode As Integer, lpSecurityAttributes As SECURITY_ATTRIBUTES,

     ByVal dwCreationDisposition As Integer,

     ByVal dwFlagsAndAttributes As Integer,

     ByVal hTemplateFile As Integer

) As Integer

Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Integer) As Integer

Declare function filesizeget(byval afile as string, highorder as integer, loworder as integer) as logical

Declare sub main

'==========================================

Sub main

'==========================================

Dim afile as string

Dim ret as logical

Dim H_B, L_B as integer

afile=FileOpenDlg("","","*.*","Select file to 'size up' in bytes")

if afile="" then exit sub end if

ret=filesizeget(afile,H_B,L_B)

if not ret then

     Note "Could not open file "+afile

   else

     If H_B = 0 Then

         Note afile+chr$(10)+chr$(10)+ "File size: "+ str$(L_B)+ " bytes"

       Else

         Note afile+chr$(10)+chr$(10)+"File size: "+str$(H_B)+ " + * 2^32 + "+ str$(L_B)+ " bytes (in base-10)"

     End If

end if

end sub

'==========================================

function filesizeget(byval afile as string, highorder as integer, loworder as integer) as logical

'==========================================

Dim hndlfile As Integer

Dim sattrib as SECURITY_ATTRIBUTES

Dim retval as integer

hndlfile = CreateFile(afile, GENERIC_READ, FILE_SHARE_READ, sattrib, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)

If hndlfile = -1 Then

filesizeget=0

exit function ' abort the program

End If

highorder = 0

loworder = GetFileSize(hndlfile, highorder)

filesizeget=1

retval = CloseHandle (hndlfile)

end function

 
 
Comments
Main handles double integer values returned by function as strings.
 
See also