Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
File Services
File Systems
File Management
 GetFileInformationByHandle Function

  Switch on low bandwidth view
GetFileInformationByHandle Function

Retrieves file information for the specified file.

For a more advanced version of this function, see GetFileInformationByHandleEx.

To set file information using a file handle, see SetFileInformationByHandle.

Syntax

C++
BOOL WINAPI GetFileInformationByHandle(
  __in   HANDLE hFile,
  __out  LPBY_HANDLE_FILE_INFORMATION lpFileInformation
);

Parameters

hFile [in]

A handle to the file that contains the information to be retrieved.

This handle should not be a pipe handle.

lpFileInformation [out]

A pointer to a BY_HANDLE_FILE_INFORMATION structure that receives the file information.

Return Value

If the function succeeds, the return value is nonzero and file information data is contained in the buffer pointed to by the lpFileInformation parameter.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

Depending on the underlying network components of the operating system and the type of server connected to, the GetFileInformationByHandle function may fail, return partial information, or full information for the given file.

You can compare the VolumeSerialNumber and FileIndex members returned in the BY_HANDLE_FILE_INFORMATION structure to determine if two paths map to the same target; for example, you can compare two file paths and determine if they map to the same directory.

Transacted Operations

If there is a transaction bound to the thread at the time of the call, then the function returns the compressed file size of the isolated file view. For more information, see About Transactional NTFS.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinBase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll

See Also

File Management Functions
GetFileInformationByHandleEx
SetFileInformationByHandle

Send comments about this topic to Microsoft

Build date: 7/9/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
If all you want to do is get file information...      Jack Mathews   |   Edit   |   Show History
... without reading or changing the file at all, then use FindFirstFile to do that. Over a network share, using FindFirstFile is dozens of times after than opening the file and querying attributes.
Tags What's this?: Add a tag
Flag as ContentBug
You ned to use GetFileInformationByHandle if you want to get current (not stale) file information      Balbes   |   Edit   |   Show History

If you want to get up-to-date information, you'd better use GetFileInformationByHandle.

From FindFileFirst MSDN doc: "In rare cases, file attribute information on NTFS file systems may not be current at the time you call this function. To be assured of getting the current NTFS file system file attributes, call the GetFileInformationByHandle function."

I dare to say that on heavily loaded system running software heavily optimized for performance (i.e. which doesn't call FlushFileBuffers on the left and on the right) these cases are not rare at all, and it is relatively easy to reproduce.

The thing is that FindFirstFile() retrieves information from directory entry which is updated lazily. Maximum latency for directory entry update (if memory serves me well) -- in absense of FlushFileBuffers() calls on modified handle -- is 5 minutes; that's the best we can hope for.

Tags What's this?: Add a tag
Flag as ContentBug
<<< This handle should not be a pipe handle. >>>      eriklitze   |   Edit   |   Show History
To be more specific about ( This handle should not be a pipe handle. )

If it's a pipe handle the API and similiar API will hang your thread/process.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker