How to read and write registry keys in VC

How can I read, write and make registrykeys?
Please make an example.
Thanks! :)
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

If using MS VC++ / MFC use SetRegistryKey() and GetProfileString(), WriteProfileString, GetProfileInt() and WriteProfileInt() from your CWinApp object
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

   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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
   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;
TMMAuthor Commented:
Thanks! It was very helpful;)!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.