FindClose

BOOL FindClose(
    HANDLE hFindFile
);

The FindClose function closes a search handle opened by a call to the FindFirstFile or FindFirstFileEx function.

Parameters

Parameter Description
hFindFile A handle to the search result that was returned by a previous call to FindFirstFile or FindFirstFileEx.

Return value

If the function succeeds, the return value is nonzero.

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

Remarks

It is important to call FindClose for every handle returned by a successful call to FindFirstFile or FindFirstFileEx, whether or not subsequent searches were successful. Failure to do so can result in a memory leak.

This function frees resources associated with the search handle.

Example

This example demonstrates closing the search handle after iterating through files.


#include <windows.h>
#include <stdio.h>

int main() {
    WIN32_FIND_DATA findFileData;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    
    // Start searching for files in the current directory
    hFind = FindFirstFile(TEXT("*.*"), &findFileData);

    if (hFind == INVALID_HANDLE_VALUE) {
        printf("FindFirstFile failed. Error: %lu\n", GetLastError());
        return 1;
    }

    printf("Files found:\n");
    do {
        printf("  %s\n", findFileData.cFileName);
    } while (FindNextFile(hFind, &findFileData) != 0);

    // Check if FindNextFile failed with an error other than ERROR_NO_MORE_FILES
    DWORD dwError = GetLastError();
    if (dwError != ERROR_NO_MORE_FILES) {
        printf("FindNextFile failed. Error: %lu\n", dwError);
    }

    // Close the search handle
    if (FindClose(hFind)) {
        printf("Search handle closed successfully.\n");
    } else {
        printf("FindClose failed. Error: %lu\n", GetLastError());
    }

    return 0;
}
            

Requirements

Item Value
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header fileapi.h (include windows.h)
Library Kernel32.lib
DLL Kernel32.dll
Unicode and ANSI versions FindClose (Unicode) and FindCloseA (ANSI)

See also