MFC:序列化指向字节数组的指针?

MFC: Serialize pointer to array of bytes?

试图坚持 MFC 框架,在 serialize() 中处理指向字节的指针的正确方法是什么?例如,假设您有:

PBYTE m_TheData; 
SIZE_T m_TheDataSize;

在 class 覆盖 Serialize() 中,您可以 read/write 您的 m_TheData 字节数组成员 - 及其大小 - 使用如下代码:

void MyClass::Serialize(CArchive &ar)
{
    CDocument::Serialize(ar); // Replace "CDocument" with your IMMEDIATE base class!
    // Note: the "SIZE_T" type varies between 32- and 64-bit platforms/builds ...
    uint64_t dsFixed; // ... use this to guarantee a 64-bit 'size' write/read!
    if (ar.IsStoring()) {
        //... Write other stuff...
        // ...making sure you keep STRICT order compliance between write and read
        dsFixed = uint64_t(m_TheDataSize);
        ar << dsFixed; // Write the array size first...
        ar.Write(m_TheData, INT(m_TheDataSize)); // ...then the data
        //...
    }
    else {
        //... Read other stuff (see note above)
    //  delete m_TheData;    // Do this if it's not a newly-created class?
        ar >> dsFixed; // Read the array size first...
        m_TheDataSize = SIZE_T(dsFixed);
        m_TheData = new BYTE[m_TheDataSize]; // Allocate space for the data...
        ar.Read(m_TheData, UINT(m_TheDataSize)); // ...then load data from archive
        //...
    }
    return;
}

随时要求进一步澄清and/or解释。

关于 SIZE_T 类型的注释:
尽管使用方式与标准 size_t 类型大致相同,但定义了 MSVC 特定的 SIZE_T(在 basetsd.h 中,由 Windows.h 或 [ 间接包含=19=]),如下所示:typedef ULONG_PTR SIZE_T;,其中 ULONG_PTR 本身定义为 typedef unsigned __int64 ULONG_PTR;(64 位构建)或 typedef unsigned long ULONG_PTR;(32 位构建)。