KEY_WOW64_32KEY在32位和64位编译之间给出不同的结果

时间:2012-08-28 04:12:54

标签: c++ registry 64-bit wow64

您好我正在尝试使用32位和64位编译代码读取和写入注册表值。每个编译都应该读/写到同一个位置。 我尝试过使用KEY_WOW64_32KEY无济于事。

当我使用32位目标编译以下代码时,它会写入并读取该值。 当我使用64位目标进行编译时,它无法找到或写入密钥。

编辑:以下代码适用于Windows XP 64位上的32位和64位版本。 问题出现在Windows 7 64位上。

任何帮助表示赞赏, 罗布。

#include <windows.h>
#include <stdio.h>

#define ISVALIDHANDLE(x) (x != NULL && x != INVALID_HANDLE_VALUE)
#define KEY_ACCESS_DEF        (KEY_ALL_ACCESS | KEY_READ | KEY_WRITE | KEY_WOW64_32KEY)
#define KEYPATH "SOFTWARE\\X\\Y"
#define KEYNAME "Z"

static bool
doInstallKey(HKEY key, const char *keypath, const char *keyname, const char *keyvalue)
{
    HKEY                 rootKey, myKey;
    bool                 r = false;

    RegOpenKeyEx(key, NULL, 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &rootKey);

    if( ISVALIDHANDLE(rootKey) )
    {
        HRESULT                res;
        unsigned long        createType;

        res = RegCreateKeyEx( rootKey, keypath, 0, NULL,
                    REG_OPTION_NON_VOLATILE,
                    KEY_ACCESS_DEF, NULL, &myKey, &createType );

        if( ISVALIDHANDLE(myKey) )
        {
            RegSetValueEx(myKey, keyname, 0,
                REG_SZ, (unsigned char *)keyvalue, strlen(keyvalue)+1);


            RegCloseKey(myKey);

            r  = true;
        }
        else
        {
            // If we cant create it, maybe it already exits,
            // just change the value.

            res = RegOpenKeyEx(rootKey, keypath, 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &myKey);

            if( ISVALIDHANDLE(myKey) )
            {
                RegSetValueEx(myKey, keyname, 0,
                    REG_SZ, (unsigned char *)keyvalue, strlen(keyvalue)+1);

                RegCloseKey(myKey);

                r  = true;
            }
            else
            {
                // error
            }
        }

        RegCloseKey(rootKey);
    }

    return r;
}

static int
doGetInstallKey(HKEY key, const char *keypath, const char *keyname,
            char *keyvalue, unsigned long keylen)
{
    HKEY     rootKey, myKey;
    int         valid = 0;

    keyvalue[0] = 0;

    RegOpenKeyEx(key, NULL, 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &rootKey);

    if( ISVALIDHANDLE(rootKey) )
    {
        RegOpenKeyEx(key, keypath, 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &myKey);

        if( ISVALIDHANDLE(myKey) )
        {
            unsigned long        type = 0;
            HRESULT                res;

            res = RegQueryValueEx(myKey, keyname, NULL,
                        &type, (unsigned char *)keyvalue, &keylen);

            if (res == ERROR_SUCCESS)
                valid = keylen;

            RegCloseKey(myKey);
        }

        RegCloseKey(rootKey);
    }

    return valid;
}


int
main(int argc, char *argv[])
{
    char    buf[1024];
    int        len = 1023;

    // read key
    doGetInstallKey(HKEY_LOCAL_MACHINE, KEYPATH, KEYNAME, buf, len);
    fprintf(stdout, "BEFORE: %s\n", buf);

    // write key
    doInstallKey(HKEY_LOCAL_MACHINE, KEYPATH, KEYNAME, "def64");

    // read key
    doGetInstallKey(HKEY_LOCAL_MACHINE, KEYPATH, KEYNAME, buf, len);
    fprintf(stdout, "AFTER: %s\n", buf);

    return 0;
}

2 个答案:

答案 0 :(得分:1)

在Windows 7上进行测试时,您是否以管理员身份运行?您可能会遇到Registry Virtualization

答案 1 :(得分:0)

尝试替换

#define KEY_ACCESS_DEF        (KEY_ALL_ACCESS | KEY_READ | KEY_WRITE | KEY_WOW64_32KEY)

#define KEY_ACCESS_DEF        (KEY_READ | KEY_WRITE | KEY_WOW64_32KEY)