How to make more than one change in windows registry with this code?
How to add more DWORD keys and values?

#include <Windows.h>
#include <iostream>

using namespace std;

int main()

LONG IReg;
HKEY hKey;
DWORD dwData = 6;
IReg = RegCreateKeyEx
(
    HKEY_LOCAL_MACHINE,
    L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Multimedia\\SystemProfile\\Tasks\\Games",
    0,
    NULL,
    REG_OPTION_NON_VOLATILE,
    KEY_ALL_ACCESS | KEY_WOW64_64KEY,
    NULL,
    &hKey,
    NULL
);
if (IReg != ERROR_SUCCESS)
{
    cout << "Priority add failed & Error No -" << GetLastError() << endl;

}
cout << "Priority add success" << endl;

IReg = RegSetValueEx
(
    hKey,
    L"Priority",
    NULL,
    REG_DWORD,
    (LPBYTE)&dwData,
    sizeof(dwData)
);
if (IReg != ERROR_SUCCESS)
{
    cout << "RegSetValueEx add failed & Error No -" << GetLastError() << endl;
}
cout << "RegSetValueEx add success" << endl;
RegCloseKey(hKey);
return 0;

Dani AI

Generated

Short answer: open or create the registry key once, then call RegSetValueEx for each DWORD name/value you want to add (or create subkeys with RegCreateKeyEx when you need separate keys). Building on 's suggestion, the cleanest approach is a small helper that opens the key, then iterates a list of name/value pairs so you do not duplicate error-checking or key-handling code.

#include <windows.h>
#include <vector>
#include <string>

struct KeyHandle { HKEY h; KeyHandle(HKEY v=0):h(v){} ~KeyHandle(){ if(h) RegCloseKey(h); } };

bool SetDwordValues(HKEY root, const std::wstring& subkey,
                    const std::vector<std::pair<std::wstring,DWORD>>& values)
{
    HKEY h; LONG r = RegCreateKeyExW(root, subkey.c_str(), 0, nullptr,
                                    REG_OPTION_NON_VOLATILE,
                                    KEY_SET_VALUE | KEY_WOW64_64KEY,
                                    nullptr, &h, nullptr);
    if (r != ERROR_SUCCESS) return false;
    KeyHandle kh(h);
    for (auto &p : values) {
        r = RegSetValueExW(h, p.first.c_str(), 0, REG_DWORD,
                           reinterpret_cast<const BYTE*>(&p.second),
                           sizeof(DWORD));
        if (r != ERROR_SUCCESS) return false;
    }
    return true;
}

Notes and troubleshooting: writes under HKEY_LOCAL_MACHINE require elevation—run as administrator or enable the proper manifest. Use the KEY_WOW64_64KEY or KEY_WOW64_32KEY flags if a 32-bit process must target the 64-bit view (or vice versa). Always check return codes from RegCreateKeyEx/RegSetValueEx and close the handle (RAII above helps). Test changes on a VM and export the key before modifying system areas; incorrect values under system keys can affect stability or performance.

It looks to me you would duplicate the code starting at about DWORD dwData = 6 to where this code section ends (remember I take it this is your code, not found in the wild and that you write code) and paste that above the return 0; and make changes as you wish.

Next time pay attention to code formatting in your post so there are line numbers and better code formatting.

commented: Thank you sir. I find the solution similar to your answer. Have a good day. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.