Platform SDK: Files and I/O

GetFileSize

The GetFileSize function retrieves the size of a specified file.

This function stores the file size in a DWORD value. To retrieve a file size that is larger than a DWORD value, use the GetFileSizeEx function.

DWORD GetFileSize(
  HANDLE hFile,           // handle to file
  LPDWORD lpFileSizeHigh  // high-order word of file size
);

Parameters

hFile
[in] Handle to the file whose size is to be returned. This handle must have been created with either GENERIC_READ or GENERIC_WRITE access to the file.
lpFileSizeHigh
[out] Pointer 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 Values

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 -1. To get extended error information, call GetLastError.

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

Remarks

You cannot use the GetFileSize function with a handle of a nonseeking device such as a pipe or a communications device. To determine the file type for hFile, use the GetFileType function.

The GetFileSize function retrieves the uncompressed size of a file. Use the GetCompressedFileSize function to obtain the compressed size of a file.

Note that if the return value is -1 and lpFileSizeHigh is non-NULL, an application must call GetLastError to determine whether the function has succeeded or failed. The following sample code illustrates this point:

// Case One: calling the function with 
//           lpFileSizeHigh == NULL 
 
// Try to obtain hFile's size 
dwSize = GetFileSize (hFile, NULL) ; 
 
// If we failed ... 
if (dwSize == -1) { 
 
    // Obtain the error code. 
    dwError = GetLastError() ; 
 
    // Deal with that failure. 
    . 
    . 
    . 
 
    } // End of error handler 
 
 
// 
// Case Two: calling the function with 
//           lpFileSizeHigh != NULL 
 
// Try to obtain hFile's huge size. 
dwSizeLow = GetFileSize (hFile, & dwSizeHigh) ; 
 
// If we failed ... 
if (dwSizeLow == -1 
    && 
    (dwError = GetLastError()) != NO_ERROR ){ 
 
    // Deal with that failure. 
    . 
    . 
    . 
 
    } // End of error handler. 

MAPI: For more information, see Syntax and Limitations for Win32 Functions Useful in MAPI Development.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 or later.
  Header: Declared in Winbase.h; include Windows.h.
  Library: Use Kernel32.lib.

See Also

File I/O Overview, File I/O Functions, GetCompressedFileSize, GetFileSizeEx, GetFileType