Skip to content

Commit 87e48df

Browse files
committed
Fix #2198
1 parent 3f44c80 commit 87e48df

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

httplib.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3078,8 +3078,24 @@ inline bool mmap::open(const char *path) {
30783078
auto wpath = u8string_to_wstring(path);
30793079
if (wpath.empty()) { return false; }
30803080

3081-
hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,
3082-
OPEN_EXISTING, NULL);
3081+
static std::once_flag create_file2_check_flag;
3082+
static bool has_CreateFile2 = false;
3083+
3084+
std::call_once(create_file2_check_flag, []() {
3085+
auto hKernel32 = GetModuleHandleW(L"kernel32.dll");
3086+
if (hKernel32) {
3087+
auto pCreateFile2 = GetProcAddress(hKernel32, "CreateFile2");
3088+
if (pCreateFile2) { has_CreateFile2 = true; }
3089+
}
3090+
});
3091+
3092+
if (has_CreateFile2) {
3093+
hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,
3094+
OPEN_EXISTING, NULL);
3095+
} else {
3096+
hFile_ = ::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
3097+
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
3098+
}
30833099

30843100
if (hFile_ == INVALID_HANDLE_VALUE) { return false; }
30853101

0 commit comments

Comments
 (0)