You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C++/WinRT读取InMemoryRandomAccessStream至Buffer失败问题排查

问题:InkStrokeContainer序列化后Buffer始终为空

尝试将winrt::Windows::UI::Input::Inking::InkStrokeContainer的内容序列化为ISF格式并存储到winrt::Windows::Storage::Streams::Buffer中,代码运行在工作线程:

// This code is running in a worker thread
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::UI::Input;
using namespace winrt::Windows::UI::Input::Inking;
using namespace winrt::Windows::Storage::Streams;


InkStrokeContainer inkContainer;

// Add some strokes to the inkContainer

InMemoryRandomAccessStream stream;
inkContainer.SaveAsync(stream, InkPersistenceFormat::Isf).get();

Buffer buffer(stream.Size());
stream.ReadAsync(buffer, buffer.Capacity(), InputStreamOptions::None).get();

std::cout << "Buffer size: " << buffer.Length() << "Stream size: " << stream.Size() << std::endl;

执行后输出始终为:

Buffer size: 0 Stream size: 1102

Buffer中没有写入任何内容,无法定位问题。

解决方案

问题出在流的读写位置上:调用inkContainer.SaveAsync完成后,InMemoryRandomAccessStream的当前位置会停留在流的末尾,此时调用ReadAsync会从末尾开始读取,自然无法获取到数据。

只需在创建Buffer并读取之前,将流的位置重置到起始处即可:

// This code is running in a worker thread
using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::UI::Input;
using namespace winrt::Windows::UI::Input::Inking;
using namespace winrt::Windows::Storage::Streams;


InkStrokeContainer inkContainer;

// Add some strokes to the inkContainer

InMemoryRandomAccessStream stream;
inkContainer.SaveAsync(stream, InkPersistenceFormat::Isf).get();

// 关键:重置流的位置到起始点
stream.Seek(0);

Buffer buffer(stream.Size());
stream.ReadAsync(buffer, buffer.Capacity(), InputStreamOptions::None).get();

std::cout << "Buffer size: " << buffer.Length() << "Stream size: " << stream.Size() << std::endl;

修改后,ReadAsync会从流的开头读取数据,Buffer就能正确获取到ISF格式的内容。

内容的提问来源于stack exchange,提问作者mynameisroh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 16:55:17