diff --git a/CMakeLists.txt b/CMakeLists.txt index d39958a05c..66436298c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,6 +124,22 @@ if(CMAKE_SIZEOF_VOID_P LESS 8) message(WARNING "Pointer size ${CMAKE_SIZEOF_VOID_P} is not supported. Please use a 64-bit compiler.") endif() +# check CreateFile2 for mingw +INCLUDE(CheckCXXSourceCompiles) +if(MINGW) + check_cxx_source_compiles("#include + #include + int main() { + HANDLE h = CreateFile2(L\"nul\", GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, NULL); + if (h != INVALID_HANDLE_VALUE) CloseHandle(h); + return 0; + }" HAVE_CREATEFILE2) + + if(HAVE_CREATEFILE2) + add_compile_definitions(HAVE_CREATEFILE2) + endif() +endif() + # Set some variables that are used in-tree and while building based on our options set(HTTPLIB_IS_COMPILED ${HTTPLIB_COMPILE}) set(HTTPLIB_IS_USING_CERTS_FROM_MACOSX_KEYCHAIN ${HTTPLIB_USE_CERTS_FROM_MACOSX_KEYCHAIN}) diff --git a/httplib.h b/httplib.h index 52b1b829f7..4ba5bbb59a 100644 --- a/httplib.h +++ b/httplib.h @@ -3078,8 +3078,13 @@ inline bool mmap::open(const char *path) { auto wpath = u8string_to_wstring(path); if (wpath.empty()) { return false; } +#if defined(HAVE_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); +#endif if (hFile_ == INVALID_HANDLE_VALUE) { return false; }