Question

Hello I am trying to read and write a value to the registry using both 32bit and 64bit compiled code. Each compilation should read/write to the same location. I have tried using the KEY_WOW64_32KEY to no avail.

When I compile the following code with a 32 bit target, it writes and reads the value. When I compile with a 64 bit target, it cannot find nor write the keys.

EDIT: The following code works fine with 32 and 64 bit versions on Windows XP 64 bit. The problem arises with Windows 7 64 bit.

Any help appreciated, Rob.

#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;
}
Was it helpful?

Solution

Are you running as Administrator when testing on Windows 7? You could be running into Registry Virtualization

OTHER TIPS

Try to replace

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

for

#define KEY_ACCESS_DEF        (KEY_READ | KEY_WRITE | KEY_WOW64_32KEY)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top