我正在用 C++/WinRT 做一个 Hololens 项目,我想从 IBuffer 中获取可用数据,由 SpatialSurfaceMesh 的 VertexPositions() 给出。
我使用这个 project sample 作为参考,因为它或多或少做了我想要的(它获取网格的 IBuffers 并渲染它们) 我项目的不同之处在于我想获取网格的位置,而不是渲染它们。
他们用来从IBuffer中获取数据的函数是
GetDataFromIBuffer
,也就是:
template <typename t = byte>
t* GetDataFromIBuffer(Windows::Storage::Streams::IBuffer^ container)
{
if (container == nullptr)
{
return nullptr;
}
unsigned int bufferLength = container->Length;
if (!(bufferLength > 0))
{
return nullptr;
}
HRESULT hr = S_OK;
ComPtr<IUnknown> pUnknown = reinterpret_cast<IUnknown*>(container);
ComPtr<IBufferByteAccess> spByteAccess;
hr = pUnknown.As(&spByteAccess);
if (FAILED(hr))
{
return nullptr;
}
byte* pRawData = nullptr;
hr = spByteAccess->Buffer(&pRawData);
if (FAILED(hr))
{
return nullptr;
}
return reinterpret_cast<t*>(pRawData);
}
但是这是在 C++/CX 中,我需要使用 C++/WinRT。我试着翻译它(正如 this Microsoft documentation 中所说,它解释了如何做并且基于相同的项目示例) 但是我不能让它工作,这是翻译后的样子:
template <typename t = byte>
t* GetDataFromIBuffer(IBuffer container)
{
if (container == nullptr)
{
return nullptr;
}
unsigned int bufferLength = container.Length();
if (!(bufferLength > 0))
{
return nullptr;
}
HRESULT hr = S_OK;
Microsoft::WRL::ComPtr<IUnknown> pUnknown = reinterpret_cast<IUnknown*>(&container);
Microsoft::WRL::ComPtr<Windows::Storage::Streams::IBufferByteAccess> spByteAccess;
hr = pUnknown.As(&spByteAccess);
if (FAILED(hr))
{
return nullptr;
}
byte* pRawData = nullptr;
hr = spByteAccess->Buffer(&pRawData);
if (FAILED(hr))
{
return nullptr;
}
return reinterpret_cast<t*>(pRawData);
}
错误是
Unhandled exception at 0x00007FF985543848 (ntdll.dll) in Project.exe: RangeChecks instrumentation code detected an out of range array access.
它发生在
Microsoft::WRL::ComPtr<IUnknown> pUnknown = reinterpret_cast<IUnknown*>(&container);
行,更明确地说是在从IUnknown
通过reinterpret_cast返回到Microsoft::WRL::ComPtr<IUnknown>
的转换过程中。
我找不到办法使这项工作。
我真的被困在这里,你有什么想法吗?谢谢!
我还发现了一个 stackoverflow 帖子 问了一个有点类似的问题,但是当我尝试做接受的答案时:
byte* GetDataFromIBuffer(IBuffer container)
{
if (container == nullptr)
{
return nullptr;
}
unsigned int bufferLength = container.Length();
if (!(bufferLength > 0))
{
return nullptr;
}
HRESULT hr = S_OK;
IUnknown* pUnk = reinterpret_cast<IUnknown*>(&container);
Windows::Storage::Streams::IBufferByteAccess * spByteAccess = nullptr;
hr = pUnk->QueryInterface(IID_PPV_ARGS(&spByteAccess));
if (FAILED(hr))
{
return nullptr;
}
byte* pRawData = nullptr;
hr = spByteAccess->Buffer(&pRawData);
if (FAILED(hr))
{
return nullptr;
}
return reinterpret_cast<byte*>(pRawData);
}
我有完全相同的错误,但这次在线:'hr = pUnk->QueryInterface(IID_PPV_ARGS(&spByteAccess));'