使用 WriteProfileBinary 从注册表读取和写入 std::vector<int>

Reading and writing a std::vector<int> to / from registry with WriteProfileBinary

我在这里陷入困境!

我有一个简单的 <std::vector<int> 容器,我想做的是将它读/写到二进制注册表项。我开始于:

    std::vector<int> vSortedColumnIndexInfo = dlgColumns.SortedEditorColumnIndexInfo();
    theApp.WriteProfileBinary(_T("Options"), _T("EditorSortedColumnIndexInfo"), 
        vSortedColumnIndexInfo.data, 
        vSortedColumnIndexInfo.size);

但这不会编译:

error C3867: std::vector<int,std::allocator<int>>::data: non-standard syntax; use & to create a pointer to member

error C3867: std::vector<int,std::allocator<int>>::size: non-standard syntax; use & to create a pointer to member

为什么这么说?从二进制注册表项读取/写入 std::vector<int> 的正确方法是什么?如果需要,可以从 int 更改。


更新 1

根据我现在的评论:

std::vector<int> vSortedColumnIndexInfo = dlgColumns.SortedEditorColumnIndexInfo();
theApp.WriteProfileBinary(_T("Options"), _T("EditorSortedColumnIndexInfo"), 
    reinterpret_cast<BYTE*>(vSortedColumnIndexInfo.data()),
    gsl::narrow<UINT>(vSortedColumnIndexInfo.size() * sizeof(int)));

因为我知道我的值将低于 256,所以我决定坚持使用 BYTE 作为容器。

写入注册表

std::vector<BYTE> vExcludedColumns;

// POpulate vector

UINT uSize = gsl::narrow<UINT>(sizeof(BYTE) * vExcludedColumns.size());
theApp.WriteProfileBinary(
    strSection, 
    _T("AssignStatesEx"),
    vExcludedColumns.data(),
    uSize
);
theApp.WriteProfileInt(strSection, _T("AssignStatesExSize"), uSize);

从注册表读取

std::vector<BYTE> vExcludedColumns;
UINT uSize = theApp.GetProfileInt(strSection, _T("AssignStatesExSize"), 0);
UINT uSizeRead = 0;
BYTE* temp = nullptr;
theApp.GetProfileBinary(strSection, _T("AssignStatesEx"), &temp, &uSizeRead);
if (uSizeRead == uSize)
{
    vExcludedColumns.resize(uSizeRead, 0);
    memcpy(vExcludedColumns.data(), temp, uSizeRead);
}
delete[] temp;
temp = nullptr;

我相信这仍然适用于 32 位和 64 位。

如果可以改进或简化此代码,我愿意接受评论。


已更新

这是相同的代码,但添加到 public 应用方法中:

std::vector<BYTE> CMeetingScheduleAssistantApp::GetProfileVector(CString strSection, CString strKey)
{
    std::vector<BYTE> vData;
    UINT uSize = theApp.GetProfileInt(strSection, strKey + _T("Size"), 0);
    UINT uSizeRead = 0;
    BYTE* temp = nullptr;
    theApp.GetProfileBinary(strSection, strKey, &temp, &uSizeRead);
    if (uSizeRead == uSize)
    {
        vData.resize(uSizeRead, 0);
        memcpy(vData.data(), temp, uSizeRead);
    }
    delete[] temp;
    temp = nullptr;
    return vData;
}


void CMeetingScheduleAssistantApp::WriteProfileVector(CString strSection, CString strKey, std::vector<BYTE> vData)
{
    UINT uSize = gsl::narrow<UINT>(sizeof(BYTE) * vData.size());
    theApp.WriteProfileBinary(
        strSection,
        strKey,
        vData.data(),
        uSize
    );
    theApp.WriteProfileInt(strSection, strKey + _T("Size"), uSize);
}