Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8459035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:14:17+00:00 2026-06-10T13:14:17+00:00

Hello I am trying to read and write a value to the registry using

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:14:18+00:00Added an answer on June 10, 2026 at 1:14 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am trying to read my xml document using xpath. I have been
Hello I am trying to create a stacked barplot using the following code: test
I'm trying to write code that spawns two child processes that send each other
I'm trying to read/write files to the sdcard. I've tried doing this both on
hello i'm trying to use core data to read and write user data. the
I am trying to read a list of values from a text file, hello.txt
Hello im trying to echo only the 50 letters, but something in my code
hello fellow java developers. I'm having a very strange issue. I'm trying to read
I'm trying to read a text file line by line, and add each line
I'm trying to write a program that manipulates unicode strings read in from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.