gpt4 book ai didi

uwp - 如何将字节复制到 Windows::Storage::Streams::Buffer 或 Buffer 以通过 Windows::Storage::Streams::DataWriter::Write Buffer() 输出

转载 作者:行者123 更新时间:2023-12-04 12:53:14 25 4
gpt4 key购买 nike

我正在使用一个示例 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;
}

但是我一直无法找到如何将 native 结构复制到缓冲区中,以便可以使用 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’s CComPtr, although some operations that CComPtr 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 how CComPtr works. With the new ComPtr, a failure to understand how it works is more likely to lead to a compiler error than a runtime bug.)



以及 ComPtr in the DirectXTK wiki

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's CComPtr 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com