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

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

PBYTE m_TheData; 
SIZE_T m_TheDataSize;

TIA !!

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

Serialize()的类重写中,您可以使用以下代码读取/写入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;
}

随时要求进一步的澄清和/或解释。

关于SIZE_T类型的注释:
尽管使用的方式与标准size_t类型几乎相同,但特定于MSVC的{​​{1}}是在SIZE_T中定义的,由basetsd.h或{ {1}}如下:Windows.h,其中afxwin.h本身定义为typedef ULONG_PTR SIZE_T;(64位版本)或ULONG_PTR(32位版本)。 / p>

本文链接:https://www.f2er.com/3084305.html

大家都在问