From c9d634876d6841110594373ba2186f8577a382d7 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Sun, 8 Oct 2023 13:08:53 -0400 Subject: [PATCH] Handle special handle value from FindFirstFileA in FindNextFileA (#52) --- common.h | 1 + dll/kernel32.cpp | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/common.h b/common.h index 7e26a0e..64ad01c 100644 --- a/common.h +++ b/common.h @@ -50,6 +50,7 @@ typedef unsigned char BYTE; #define ERROR_PATH_NOT_FOUND 3 #define ERROR_ACCESS_DENIED 5 #define ERROR_INVALID_HANDLE 6 +#define ERROR_NO_MORE_FILES 18 #define ERROR_READ_FAULT 30 #define ERROR_HANDLE_EOF 38 #define ERROR_NOT_SUPPORTED 50 diff --git a/dll/kernel32.cpp b/dll/kernel32.cpp index 519c78f..657b652 100644 --- a/dll/kernel32.cpp +++ b/dll/kernel32.cpp @@ -707,12 +707,12 @@ namespace kernel32 { return (void *) 1; } - FindFirstFileHandle *handle = new FindFirstFileHandle(); + auto *handle = new FindFirstFileHandle(); if (!std::filesystem::exists(path.parent_path())) { - wibo::lastError = 3; // ERROR_PATH_NOT_FOUND + wibo::lastError = ERROR_PATH_NOT_FOUND; delete handle; - return (void *) 0xFFFFFFFF; + return INVALID_HANDLE_VALUE; } std::filesystem::directory_iterator it(path.parent_path()); @@ -720,9 +720,9 @@ namespace kernel32 { handle->pattern = path.filename().string(); if (!findNextFile(handle)) { - wibo::lastError = 2; // ERROR_FILE_NOT_FOUND + wibo::lastError = ERROR_FILE_NOT_FOUND; delete handle; - return (void *) 0xFFFFFFFF; + return INVALID_HANDLE_VALUE; } setFindFileDataFromPath(lpFindFileData, *handle->it++); @@ -752,8 +752,15 @@ namespace kernel32 { } int WIN_FUNC FindNextFileA(void *hFindFile, WIN32_FIND_DATA *lpFindFileData) { - FindFirstFileHandle *handle = (FindFirstFileHandle *) hFindFile; + // Special value from FindFirstFileA + if (hFindFile == (void *) 1) { + wibo::lastError = ERROR_NO_MORE_FILES; + return 0; + } + + auto *handle = (FindFirstFileHandle *) hFindFile; if (!findNextFile(handle)) { + wibo::lastError = ERROR_NO_MORE_FILES; return 0; }