RegSetKeyValueA 函数是否有任何向后兼容的替代方法?

Is there any backwards-compatible alternative to RegSetKeyValueA function?

在我的应用程序中,我使用 RegSetKeyValueA 在注册表中存储一些激活密钥。

阻止我的应用程序向后兼容 windows XP 的唯一瓶颈是 RegSetKeyValueA 函数。

有什么办法可以解决这个问题吗?

RegSetKeyValueW functional can be easy implemented by use RegSetValueExW 存在于 Windows 2000

LSTATUS MyRegSetKeyValueW(
                        HKEY    hKey,
                        LPCWSTR lpSubKey,
                        LPCWSTR lpValueName,
                        DWORD   dwType,
                        LPCVOID lpData,
                        DWORD   cbData
                        )
{
    LSTATUS s;

    if (lpSubKey && *lpSubKey)
    {
        s = RegCreateKeyExW(hKey, lpSubKey, 0, 0, 0, KEY_SET_VALUE, 0, &hKey, 0);

        if (s != NOERROR)
        {
            return s;
        }
    }

    s = RegSetValueExW(hKey, lpValueName, 0, dwType, 
        static_cast<PBYTE>(const_cast<void*>(lpData)), cbData);

    if (lpSubKey && *lpSubKey)
    {
        RegCloseKey(hKey);
    }

    return s;
}

并将自身代码 RegSetKeyValueW 替换为 MyRegSetKeyValueW。可以对 A 版本做同样的事情,但需要了解 A 版本将字符串参数转换为 unicode 然后调用 W 版本。所以最好直接调用 W version

你应该使用RegSetValueExA

由于签名非常相似,所以很容易将两者混淆。

RegSetValueExA

Minimum supported client Windows 2000 Professional [desktop apps only]

RegSetKeyValueA

Minimum supported client Windows Vista [desktop apps only]

使用SHSetValueA (shlwapi.lib), IE4 (Win98,2000+) 都可以使用。

如果您要设置许多值,您应该使用 RegCreateKeyA 创建键并调用 SHSetValueA 将 NULL 作为子键。