From ad40bd6a009c5fd237f2972987a528d59a8526ba Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 8 Feb 2024 20:59:34 -0700 Subject: [PATCH] Implement file mapping for UWP apps (#1775) Fixes #1773. --- httplib.h | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/httplib.h b/httplib.h index dfdd260..f5436e2 100644 --- a/httplib.h +++ b/httplib.h @@ -2686,21 +2686,29 @@ inline bool mmap::open(const char *path) { close(); #if defined(_WIN32) - hFile_ = ::CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, - OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + std::wstring wpath; + for (size_t i = 0; i < strlen(path); i++) { + wpath += path[i]; + } + + hFile_ = ::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, + OPEN_EXISTING, NULL); if (hFile_ == INVALID_HANDLE_VALUE) { return false; } - size_ = ::GetFileSize(hFile_, NULL); + LARGE_INTEGER size{}; + if (!::GetFileSizeEx(hFile_, &size)) { return false; } + size_ = static_cast(size.QuadPart); - hMapping_ = ::CreateFileMapping(hFile_, NULL, PAGE_READONLY, 0, 0, NULL); + hMapping_ = ::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, + NULL); if (hMapping_ == NULL) { close(); return false; } - addr_ = ::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0); + addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0); #else fd_ = ::open(path, O_RDONLY); if (fd_ == -1) { return false; }