From 87e48df64e2d0846623598c38342ff564c03f8a4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 8 Aug 2025 21:53:44 -0400 Subject: [PATCH] Fix #2198 --- httplib.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 52b1b829f7..9d8a4b27fe 100644 --- a/httplib.h +++ b/httplib.h @@ -3078,8 +3078,24 @@ inline bool mmap::open(const char *path) { auto wpath = u8string_to_wstring(path); if (wpath.empty()) { return false; } - hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, - OPEN_EXISTING, NULL); + static std::once_flag create_file2_check_flag; + static bool has_CreateFile2 = false; + + std::call_once(create_file2_check_flag, []() { + auto hKernel32 = GetModuleHandleW(L"kernel32.dll"); + if (hKernel32) { + auto pCreateFile2 = GetProcAddress(hKernel32, "CreateFile2"); + if (pCreateFile2) { has_CreateFile2 = true; } + } + }); + + if (has_CreateFile2) { + hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, + OPEN_EXISTING, NULL); + } else { + hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + } if (hFile_ == INVALID_HANDLE_VALUE) { return false; }