fix: enable Windows 7 compatibility for static build

This commit is contained in:
sladro 2025-11-07 09:59:30 +08:00
parent 003b9306e7
commit 20ce7caaa8
2 changed files with 30 additions and 5 deletions

View File

@ -21,6 +21,10 @@ include(cmake/deps_occ.cmake)
# Add an option to enable static builds
OPTION(BUILD_STATIC "Build STP2GLB as a static executable." OFF)
if (WIN32 AND DEFINED WIN32_WINNT)
add_compile_definitions(_WIN32_WINNT=${WIN32_WINNT})
endif ()
if (BUILD_STATIC)
message(STATUS "Building STP2GLB as a static executable.")

View File

@ -32,9 +32,9 @@
#endif
#ifdef _WIN32
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0601
#error \
"cpp-httplib doesn't support Windows 8 or lower. Please use Windows 10 or later."
"cpp-httplib doesn't support platforms older than Windows 7 SP1."
#endif
#endif
@ -3083,8 +3083,14 @@ 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);
hFile_ =
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0602
::CreateFile2(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ,
OPEN_EXISTING, NULL);
#else
::CreateFileW(wpath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#endif
if (hFile_ == INVALID_HANDLE_VALUE) { return false; }
@ -3101,7 +3107,17 @@ inline bool mmap::open(const char *path) {
size_ = static_cast<size_t>(size.QuadPart);
hMapping_ =
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0602
::CreateFileMappingFromApp(hFile_, NULL, PAGE_READONLY, size_, NULL);
#else
::CreateFileMappingW(
hFile_, NULL, PAGE_READONLY,
static_cast<DWORD>((static_cast<unsigned long long>(size_) >> 32) &
0xffffffffULL),
static_cast<DWORD>(static_cast<unsigned long long>(size_) &
0xffffffffULL),
NULL);
#endif
// Special treatment for an empty file...
if (hMapping_ == NULL && size_ == 0) {
@ -3115,7 +3131,12 @@ inline bool mmap::open(const char *path) {
return false;
}
addr_ = ::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0);
addr_ =
#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x0602
::MapViewOfFileFromApp(hMapping_, FILE_MAP_READ, 0, 0);
#else
::MapViewOfFile(hMapping_, FILE_MAP_READ, 0, 0, 0);
#endif
if (addr_ == nullptr) {
close();