[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

Registry read/write from/to registry

Hi Experts,

  I need to be able to read and write to/from the registry from a C++ DLL.  This is a win32 app with no MFC supprot.  The value I need to read/write is stored in HKEY_LOCAL_MACHINE\SOFTWARE\Billco\Butler\ and it is of type REG_DWORD.
I've never done this before so any help would be greatly appreciated.

Thanks,
Axnst2
0
axnst2
Asked:
axnst2
  • 2
2 Solutions
 
axnst2Author Commented:
The registry variable name is MyRegVar.  I need to manipulate the value stored in that.  Thanks
0
 
dog_Commented:
To open:

HKEY      hSubKey ;
RegopenkeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\Billco\Butler\MyRegVar", 0, KEY_READ, &hSubKey);

See also the following functions for checking and manipulating:

RegQueryValueEx
RegDeleteKey
RegSetValueEx
RegCreateKey
0
 
jkrCommented:
The following example will read a value called 'ValueName', add something and write it back:

void AddToValue ( DWORD dwAdd) {

    HKEY hKey = NULL;
    REGSAM sam = KEY_ALL_ACCESS;
    char* pszKey = "SOFTWARE\\Billco\\Butler";

    char acKey [ MAX_PATH];

    if ( ERROR_SUCCESS != RegOpenKeyEx ( HKEY_LOCAL_MACHINE, pszKey, 0, sam, &hKey)) {

       return; // nothing to do...
    }

    DWORD dwType = REG_DWORD;
    DWORD dwSize = sizeof ( DWORD);
    DWORD dwValue;

    if ( ERROR_SUCCESS == RegQueryValueEx (
                                hKey,
                                "ValueName",
                                NULL,
                                &dwType,
                                ( LPBYTE) &dwValue,
                                &dwSize)
                                ) {

        dwValue += dwAdd;

        RegSetValueEx (
            hKey,
            "ValueName",
            NULL,
            dwType,
            ( LPBYTE) &dwValue,
            dwSize)
            );
    }

    RegCloseKey ( hKey);

}
0
 
axnst2Author Commented:
Thanks Experts
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now