将DWORD从HKCU复制到HKLM

时间:2013-08-16 10:27:10

标签: c++ registry

将注册表值从HKCU复制到HKLM时出现问题。它是一个DWORD,我使用此代码枚举所有密钥并在安装的某个时间复制它们。

HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
HKEY hKey;
char szProductKey[MAX_PATH], lszValue[MAX_PATH];
TCHAR    achKey[MAX_KEY_LENGTH], achClass[MAX_PATH] = TEXT(""), achValue[MAX_VALUE_NAME];  
DWORD     cbName, cchClassName = MAX_PATH, cSubKeys=0, cbMaxSubKey, cchMaxClass, cValues, cchMaxValue, cbMaxValueData, cbSecurityDescriptor; 
FILETIME ftLastWriteTime;    
PHKEY phkResult = NULL;
DWORD i, retCode, cchValue = MAX_VALUE_NAME,dwType=REG_SZ,dwSKeyValueSize,dwSize=255;

hr = WcaInitialize(hInstall, "ReadTempRegKey");
ExitOnFailure(hr, "Failed to initialize");

WcaLog(LOGMSG_STANDARD, "Initialized.");  

Orc_Reg_Sub_LM_CU();

sprintf_s(szProductKey, "SOFTWARE\\M\\%s",Orc_Get_Product_Name());
WcaLog(LOGMSG_STANDARD , szProductKey);
if( RegOpenKeyEx( HKEY_CURRENT_USER,
    szProductKey,
    0,
    KEY_READ,
    &hKey) == ERROR_SUCCESS
    )
{
    //Get the class name and the value count. 
    retCode = RegQueryInfoKey(
        hKey,                    // key handle 
        achClass,                // buffer for class name 
        &cchClassName,           // size of class string 
        NULL,                    // reserved 
        &cSubKeys,               // number of subkeys 
        &cbMaxSubKey,            // longest subkey size 
        &cchMaxClass,            // longest class string 
        &cValues,                // number of values for this key 
        &cchMaxValue,            // longest value name 
        &cbMaxValueData,         // longest value data 
        &cbSecurityDescriptor,   // security descriptor 
        &ftLastWriteTime);       // last write time 

    // Enumerate the subkeys, until RegEnumKeyEx fails.

    if (cSubKeys)
    {
        for (i=0; i<cSubKeys; i++) 
        { 
            cbName = MAX_KEY_LENGTH;
            retCode = RegEnumKeyEx(hKey, i,
                achKey, 
                &cbName, 
                NULL, 
                NULL, 
                NULL, 
                &ftLastWriteTime); 
        }
    } 

    //Enumerate the key values. 

    if (cValues) 
    {
        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 dwSize = sizeof(lszValue);
                retCode = RegQueryValueEx(hKey, achValue, NULL, &dwType,(LPBYTE)&lszValue, &dwSize);
                if (retCode == ERROR_SUCCESS)
                {

                    Orc_Reg_Stop_LM_CU();
                    dwSKeyValueSize = strlen((char*) lszValue);
                    Orc_RegValue(HKEY_LOCAL_MACHINE,
                        "SOFTWARE\\M\\Orchestrator", 
                        KEY_SET_VALUE,
                        achValue, 
                        &dwType,
                        (unsigned char *)lszValue,
                        &dwSKeyValueSize);

                }
            }
        } 
    }
}

复制后,注册表的值是正确的,但数据显示invalid dword 32-bit value是否有人知道会导致这种情况的原因?

由于

1 个答案:

答案 0 :(得分:2)

您收到invalid dword 32-bit value消息,因为传递给Orc_RegValue()的最后一个参数不正确。 REG_DWORD数据类型的正确值为sizeof(DWORD)

尝试传递dwSize而不是dwSKeyValueSize

                Orc_RegValue(HKEY_LOCAL_MACHINE,
                    "SOFTWARE\\M\\Orchestrator", 
                    KEY_SET_VALUE,
                    achValue, 
                    &dwType,
                    (unsigned char *)lszValue,
                    &dwSize);

或者你可以尝试

   dwSKeyValueSize = dwSize;

而不是

   dwSKeyValueSize = strlen((char*) lszValue);