- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用一个示例 UWP C++/CX 程序,该程序创建两个 UDP 网络通信线程,它们使用 Windows::Storage::Streams::DataWriter
相互发送数据。和 Windows::Storage::Streams::DataReader
同时更新显示来回数据的显示窗口。
使用 Platform::String
的初始实现变量以及 DataWriter->WriteString()
工作正常。但是现在我想实现一个二进制协议(protocol),它具有包含各种类型信息的可变长度缓冲区。DataReader->ReadBuffer()
和 DataWriter->WriteBuffer()
需要 Windows::Storage::Streams::Buffer
.
访问 ReadBuffer()
返回的缓冲区我正在使用一个函数,如果在 Obtaining pointers to data buffers (C++/CX) 上可以在网络上找到其源代码这类似于 Getting an array of bytes out of Windows::Storage::Streams::IBuffer 中的答案
#include <robuffer.h>
byte* GetPointerToPixelData(IBuffer^ pixelBuffer, unsigned int *length)
{
if (length != nullptr)
{
*length = pixelBuffer->Length;
}
// Query the IBufferByteAccess interface.
Microsoft::WRL::ComPtr<IBufferByteAccess> bufferByteAccess;
reinterpret_cast<IInspectable*>(pixelBuffer)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));
// Retrieve the buffer data.
byte* pixels = nullptr;
bufferByteAccess->Buffer(&pixels);
return pixels;
}
DataWriter->WriteBuffer()
将其发送出去。 .
Windows::Storage::Streams::Buffer
用于
DataWriter->WriteBuffer()
?
最佳答案
似乎可以使用相同的函数来获取指向 Buffer
的指针。从 Buffer
复制数据的内存区域进入 native 内存区域也可用于获取指针,该指针可用于将数据复制到 Buffer
.见 Obtaining pointers to data buffers (C++/CX).并查看COM Coding Practices有关 IID_PPV_ARGS()
的信息宏和 ATL 智能指针类 CComPtr
.见 QueryInterface: Navigating in an Object也是。ComPtr
是 ATL CComPtr
的 WRL 版本(见 Native WinRT Inheritance 以及 Windows Runtime C++ Template Library (WRL) 链接到 WRL 和 WinRT 的各种支持主题)。
(
ComPtr
is a WRL smart pointer. It automates some aspects of COM that otherwise get tedious fast. It’s similar to ATL’sCComPtr
, although some operations thatCComPtr
performed implicitly now require explicit code, largely because those implicit operations were responsible for a lot of bugs in code written by people who didn’t really understand howCComPtr
works. With the newComPtr
, a failure to understand how it works is more likely to lead to a compiler error than a runtime bug.)
Microsoft::WRL::ComPtr
is a C++ template smart-pointer for COM objects that is used extensively in Windows Runtime (WinRT) C++ programming. It works in Win32 desktop applications as well. It is similar to ATL'sCComPtr
with some useful improvements.Microsoft::WRL:::ComPtr
is in the Windows 8.x SDK and Windows 10 SDK, which, unlike ATL, is available when using the Express versions of Visual Studio. It is used extensively in DirectX Tool Kit to properly handle COM reference counting maintenance.
#include <robuffer.h>
byte* GetPointerToPixelData(IBuffer^ pixelBuffer, unsigned int *length)
{
if (length != nullptr)
{
*length = pixelBuffer->Length;
}
// Query the IBufferByteAccess interface.
Microsoft::WRL::ComPtr<IBufferByteAccess> bufferByteAccess;
reinterpret_cast<IInspectable*>(pixelBuffer)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));
// Retrieve the buffer data.
byte* pixels = nullptr;
bufferByteAccess->Buffer(&pixels);
return pixels;
}
Buffer
的内存容量。 (您可以检查缓冲区的
Capacity
属性)。从本地变量复制到
Buffer
之后您还必须设置
Length
属性到
Buffer
中设置的数据的实际字节数.
// test structs for binary data to parse.
struct struct1 {
unsigned char uchMajorClass;
unsigned char uchMinorClass;
long lVal1;
};
void testfunc (IOutputStream^ outputStream)
{
auto dataWriter = ref new DataWriter(outputStream);
struct1 myStruct1 = { 1, 11,0 };
Buffer ^myBuffer = ref new Buffer(sizeof(struct1)); // allocate Buffer of desired size
unsigned int len;
struct1 *sp = (struct1 *)GetPointerToPixelData(myBuffer, &len); // get native pointer
// we could use myBuffer->Capacity at this point to check the capacity or
// max size in bytes of the buffer.
*sp = myStruct1; // copy data from native into the Buffer
myBuffer->Length = sizeof(*sp); // set the data length
dataWriter->WriteBuffer(myBuffer);
}
关于uwp - 如何将字节复制到 Windows::Storage::Streams::Buffer 或 Buffer 以通过 Windows::Storage::Streams::DataWriter::Write Buffer() 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49848996/
我是一名优秀的程序员,十分优秀!