mercredi 11 octobre 2017

How to correct store LPTSTR in map

I need to store values from registry in map. I have such code:

    map<int, LPTSTR> mymap;

    for (i = 0, retCode = ERROR_SUCCESS; i<cValues; i++)
    {
        cchValue = MAX_VALUE_NAME;
        achValue[0] = '\0';
        retCode = RegEnumValue(hKey, i,
            achValue,
            &cchValue,
            NULL,
            NULL,
            NULL,
            NULL);

        if (retCode == ERROR_SUCCESS)
        {
            DWORD lpData = cbMaxValueData;
            buffer[0] = '\0';
            LONG dwRes = RegQueryValueEx(hKey, achValue, 0, NULL, buffer, &lpData);

            LPTSTR val = (TCHAR*)buffer;
            MessageBox(NULL, val, L"VALUE", MB_OK);

            auto r = mymap.insert(pair<int, LPTSTR>(i, val));

            for (map<int, LPTSTR>::const_iterator it = mymap.begin(); it != mymap.end(); it++)
            {
                wchar_t szMessage2[1300];
                if (SUCCEEDED(StringCchPrintf(szMessage2, ARRAYSIZE(szMessage2), L"+ %d : %s\n", it->first, it->second)))
                {
                    MessageBox(NULL, szMessage2, L"MAP", MB_OK);
                }
            }
        }
    }

How to correct save val to map? Right now all keys have the same value (latest).

Aucun commentaire:

Enregistrer un commentaire