How to read and write registry keys in VC

Posted on 2003-03-17
Medium Priority
Last Modified: 2012-06-27
How can I read, write and make registrykeys?
Please make an example.
Thanks! :)
Question by:TMM
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8154821
Sure - here's a link with complete source code for a Registry class that will do everything you need...



Expert Comment

ID: 8155574
If using MS VC++ / MFC use SetRegistryKey() and GetProfileString(), WriteProfileString, GetProfileInt() and WriteProfileInt() from your CWinApp object
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

errorx666 earned 80 total points
ID: 8156686
   The registry is composed of keys and values organized in an hierarchical structure.  To referencing a key is very like referencing a file, you need to open it with the proper permissions and obtain a handle to that key (an HKEY).  The root of the registry is always a predefined key, these are constant but vary from different versions of Windows.  The current set of predefined keys as given by Microsoft is:
HKEY_CURRENT_CONFIG // Win95 / Win98 / WinME / WinNT4.0+
HKEY_DYN_DATA // Win95 / Win98 / WinME

    These predefined keys are declared in the standard Windows include, and can be used without explicitly opening them (with some restrictions).  To identify a key you need to give the handle (HKEY) of one of its parent members and a string specifying the relative path from that key and the key name (ie {HKEY_LOCAL_MACHINE."Software/MyCompany/MyProgram/Registration"}, or {hMyKey,"Registration"} ).  After you have successfully opened the key, you can use the handle in a number of functions to enumerate and modify either its subkeys or its values.

// Functions for opening and closing keys

LONG RegOpenKey(HKEY hKey, LPCTSTR lpSubKey, PHKEY phkResult);

LONG RegOpenKeyEx(HKEY hKey,LPCTSTR lpSubKey,DWORD ulOptions,REGSAM samDesired,PHKEY phkResult);

LONG RegCloseKey(HKEY hKey);

// Functions for creating and deleting keys

LONG RegCreateKey(HKEY hKey,LPCTSTR lpSubKey,PHKEY phkResult);

LONG RegCreateKeyEx(HKEY hKey,LPCTSTR lpSubKey,DWORD Reserved,LPTSTR lpClass,DWORD dwOptions,REGSAM samDesired,LPSECURITY_ATTRIBUTES lpSecurityAttributes,PHKEY phkResult,LPDWORD lpdwDisposition);

LONG RegDeleteKey(HKEY hKey,LPCTSTR lpSubKey);

// Functions for enumerating and querying keys

LONG RegEnumKey(HKEY hKey,DWORD dwIndex,LPTSTR lpName,DWORD cchName);

LONG RegEnumKeyEx(HKEY hKey,DWORD dwIndex,LPTSTR lpName,LPDWORD lpcName,LPDWORD lpReserved,LPTSTR lpClass,LPDWORD lpcClass,PFILETIME lpftLastWriteTime);

LONG RegQueryInfoKey(HKEY hKey,LPTSTR lpClass,LPDWORD lpcClass,LPDWORD lpReserved,LPDWORD lpcSubKeys,LPDWORD lpcMaxSubKeyLen,LPDWORD lpcMaxClassLen,LPDWORD lpcValues,LPDWORD lpcMaxValueNameLen,LPDWORD lpcMaxValueLen,LPDWORD lpcbSecurityDescriptor,PFILETIME lpftLastWriteTime);

    Values are the data associated with keys, they have names to identify them (or NULL for the default value of a key) and can be one of several flavors: REG_SZ (a NULL-terminated string, REG_DWORD (an unsigned long value), REG_BINARY (raw binary data [LPVOID]), REG_EXPAND_SZ (a NULL-terminated string containing environmental variables [use ExpandEnvironmentStrings]), REG_MULTI_SZ (an array of NULL-terminated strings).

// Functions to create or modify registry values

LONG RegSetValue(HKEY hKey,LPCTSTR lpSubKey,DWORD dwType,LPCTSTR lpData,DWORD cbData);

LONG RegSetValueEx(HKEY hKey,LPCTSTR lpValueName,DWORD Reserved,DWORD dwType,CONST BYTE *lpData,DWORD cbData);

// Function to delete registry values

LONG RegDeleteValue(HKEY hKey,LPCTSTR lpValueName);

// Functions to query and enumerate registry values

LONG RegQueryValue(HKEY hKey,LPCTSTR lpSubKey,LPTSTR lpValue,PLONG lpcbValue);

LONG RegQueryValueEx(HKEY hKey,LPCTSTR lpValueName,LPDWORD lpReserved,LPDWORD lpType,LPBYTE lpData,LPDWORD lpcbData);

LONG RegEnumValue(HKEY hKey,DWORD dwIndex,LPTSTR lpValueName,LPDWORD lpcValueName,LPDWORD lpReserved,LPDWORD lpType,LPBYTE lpData,LPDWORD lpcbData);

    You'll note that many of these functions take an excessive number of arguments, however most of these are optional (usually NULL can be given for pointers).  Here a simple program to create a registry key, write a value to it, then read that value back.

#include <windows.h>

    HKEY hMyKey;
    DWORD BufferLength=10;
    char MyString[10];
    strcpy(MyString,"My string") // Set MyString
    RegCreateKey(HKEY_CURRENT_USER,"MyKey",hMyKey); // Create/open MyKey
    RegSetValueEx(hMyKey,"MyValue",REG_SZ,MyString,strlen(MyString)+1); // Store MyString
    ZeroMemory(MyString,10); // Clear MyString
    RegQueryValueEx(hMyKey,"MyValue",NULL,NULL,MyString,&BufferLength); // Load MyString
    RegCloseKey(hMyKey); // Close MyKey
    return 0;

     There are many more registry functions, this is a very deep topic.  I've tried to provide a brief overview of basic registry functionality.

Expert Comment

ID: 8156699
   Some slight errors in the code given.  Fixed here:

#include <windows.h>

   HKEY hMyKey;
   DWORD BufferLength=10;
   char MyString[10];
   strcpy(MyString,"My string"); // Set MyString
   RegCreateKey(HKEY_CURRENT_USER,"MyKey",&hMyKey); // Create/open MyKey
   RegSetValueEx(hMyKey,"MyValue",NULL,REG_SZ,(LPBYTE)MyString,strlen(MyString)+1); // Store MyString
   ZeroMemory(MyString,10); // Clear MyString
   RegQueryValueEx(hMyKey,"MyValue",NULL,NULL,(LPBYTE)MyString,&BufferLength); // Load MyString
   RegCloseKey(hMyKey); // Close MyKey
   return 0;

Author Comment

ID: 8168016
Thanks! It was very helpful;)!

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question