aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2026-02-19 19:23:20 +0800
committerYuqian Yang <crupest@crupest.life>2026-02-19 19:23:20 +0800
commit924f4b472712d0cfc55b81dcb3eaed3f8a478288 (patch)
tree17dd285be426dbc350b3bb8b1e56b3292503d7ba
parent6bbadfc6b1eaf14f68a27cb8e378f5e09ab38de6 (diff)
downloadcru-924f4b472712d0cfc55b81dcb3eaed3f8a478288.tar.gz
cru-924f4b472712d0cfc55b81dcb3eaed3f8a478288.tar.bz2
cru-924f4b472712d0cfc55b81dcb3eaed3f8a478288.zip
fix windows build.
-rw-r--r--include/cru/base/platform/win/Stream.h5
-rw-r--r--src/base/platform/win/Base.cpp2
-rw-r--r--src/base/platform/win/Stream.cpp28
3 files changed, 20 insertions, 15 deletions
diff --git a/include/cru/base/platform/win/Stream.h b/include/cru/base/platform/win/Stream.h
index e49bf48b..104b3bd7 100644
--- a/include/cru/base/platform/win/Stream.h
+++ b/include/cru/base/platform/win/Stream.h
@@ -26,7 +26,7 @@ class CRU_BASE_API Win32HandleStream : public io::Stream {
Index DoWrite(const std::byte* buffer, Index offset, Index size) override;
public:
- const Win32Handle& GetHandle() { return handle_; }
+ HANDLE GetHandle() { return handle_; }
CRU_STREAM_IMPLEMENT_CLOSE_BY_DO_CLOSE
@@ -34,7 +34,8 @@ class CRU_BASE_API Win32HandleStream : public io::Stream {
void DoClose();
private:
- Win32Handle handle_;
+ HANDLE handle_;
+ bool auto_close_;
};
CRU_BASE_API IStream* ToComStream(io::Stream* stream);
diff --git a/src/base/platform/win/Base.cpp b/src/base/platform/win/Base.cpp
index 0aae6466..43357c35 100644
--- a/src/base/platform/win/Base.cpp
+++ b/src/base/platform/win/Base.cpp
@@ -33,6 +33,6 @@ UniDirectionalWin32PipeResult OpenUniDirectionalPipe(Win32PipeFlag flag) {
HANDLE_FLAG_INHERIT));
}
- return {Win32Handle(read, true), Win32Handle(write, true)};
+ return {Win32Handle(read), Win32Handle(write)};
}
} // namespace cru::platform::win
diff --git a/src/base/platform/win/Stream.cpp b/src/base/platform/win/Stream.cpp
index 72466905..10b80a16 100644
--- a/src/base/platform/win/Stream.cpp
+++ b/src/base/platform/win/Stream.cpp
@@ -42,9 +42,9 @@ HANDLE OpenHandle(std::string_view path, OpenFileFlag flags) {
IStream* stream;
- auto handle =
- ::CreateFileW(cru::string::ToUtf16WString(path).c_str(), access, 0, nullptr,
- creation_disposition, FILE_ATTRIBUTE_NORMAL, nullptr);
+ auto handle = ::CreateFileW(cru::string::ToUtf16WString(path).c_str(), access,
+ 0, nullptr, creation_disposition,
+ FILE_ATTRIBUTE_NORMAL, nullptr);
if (handle == INVALID_HANDLE_VALUE) {
throw Win32Error("Failed to call CreateFileW.");
@@ -68,13 +68,14 @@ Win32HandleStream::Win32HandleStream(std::string_view path, OpenFileFlag flags)
Win32HandleStream::Win32HandleStream(HANDLE handle, bool auto_close,
bool can_seek, bool can_read,
bool can_write)
- : Win32HandleStream(Win32Handle(handle, auto_close), can_seek, can_read,
- can_write) {}
+ : Stream(SupportedOperations(can_seek, can_read, can_write)),
+ handle_(handle),
+ auto_close_(auto_close) {}
Win32HandleStream::Win32HandleStream(Win32Handle&& handle, bool can_seek,
bool can_read, bool can_write)
- : Stream(SupportedOperations(can_seek, can_read, can_write)),
- handle_(std::move(handle)) {}
+ : Win32HandleStream(handle.Release(), true, can_seek, can_read, can_write) {
+}
Win32HandleStream::~Win32HandleStream() { DoClose(); }
@@ -92,14 +93,14 @@ Index Win32HandleStream::DoSeek(Index offset, SeekOrigin origin) {
LARGE_INTEGER n_offset, new_pos;
n_offset.QuadPart = offset;
- CheckWinReturn(::SetFilePointerEx(handle_.Get(), n_offset, &new_pos, method));
+ CheckWinReturn(::SetFilePointerEx(handle_, n_offset, &new_pos, method));
return new_pos.QuadPart;
}
Index Win32HandleStream::DoRead(std::byte* buffer, Index offset, Index size) {
DWORD real_read;
- auto r = ::ReadFile(handle_.Get(), static_cast<LPVOID>(buffer + offset), size,
+ auto r = ::ReadFile(handle_, static_cast<LPVOID>(buffer + offset), size,
&real_read, nullptr);
if (r == FALSE) {
auto e = ::GetLastError();
@@ -113,15 +114,18 @@ Index Win32HandleStream::DoRead(std::byte* buffer, Index offset, Index size) {
Index Win32HandleStream::DoWrite(const std::byte* buffer, Index offset,
Index size) {
DWORD real_write;
- CheckWinReturn(::WriteFile(handle_.Get(),
- static_cast<LPCVOID>(buffer + offset), size,
- &real_write, nullptr));
+ CheckWinReturn(::WriteFile(handle_, static_cast<LPCVOID>(buffer + offset),
+ size, &real_write, nullptr));
return real_write;
}
void Win32HandleStream::DoClose() {
CRU_STREAM_BEGIN_CLOSE
+ if (auto_close_) {
+ ::CloseHandle(handle_);
+ }
+
handle_ = {};
}