RegistryHelper::readMultiValue

0 0 2025-05-29 本文地址:http://www.pnpon.com/fun/detail-15.html
vector<wstring> RegistryHelper::readMultiValue(wstring key, wstring valuename)
{
    vector<wstring> result;

    HKEY keyHandle = openKey(key, KEY_QUERY_VALUE | KEY_WOW64_64KEY);

    LSTATUS status;
    DWORD type;
    DWORD bufSize;
    status = RegQueryValueExW(keyHandle, valuename.c_str(), NULL, &type, NULL, &bufSize);
    if (status != ERROR_SUCCESS)
    {
        RegCloseKey(keyHandle);
        throw RegistryException(L"Error while reading registry value " + key + L"\\" + valuename + L": " + StringHelper::getSystemErrorString(status));
    }

    if (type != REG_MULTI_SZ)
    {
        RegCloseKey(keyHandle);
        throw RegistryException(L"Registry value " + key + L"\\" + valuename + L" has wrong type");
    }

    wchar_t* buf = new wchar_t[bufSize / sizeof(wchar_t) + 1];
    status = RegQueryValueExW(keyHandle, valuename.c_str(), NULL, NULL, (LPBYTE)buf, &bufSize);

    RegCloseKey(keyHandle);

    if (status != ERROR_SUCCESS)
    {
        delete buf;
        throw RegistryException(L"Error while reading registry value " + key + L"\\" + valuename + L": " + StringHelper::getSystemErrorString(status));
    }

    size_t length = bufSize / sizeof(wchar_t);
    // Remove zero-termination
    while (length > 0 && buf[length - 1] == L'\0')
        length--;

    size_t start = 0;
    for (size_t i = 0; i < length; i++)
    {
        if (buf[i] == L'\0')
        {
            result.push_back(wstring(buf + start, i - start));
            start = i + 1;
        }
    }

    if (length > start)
        result.push_back(wstring(buf + start, length - start));

    delete buf;

    return result;
}
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!