Page 1 of 1

How to set/read registry key in C++ tutorial Rate Topic: -----

#1 aniri  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 657
  • Joined: 24-November 09

Posted 04 May 2010 - 05:15 AM

Registry keys are used to store various values used by programs you use.

Setting or getting the values of registry keys is really easy.

First, you need to open the registry key:

HKEY key;
if (RegOpenKey(HKEY_LOCAL_MACHINE, TEXT("Software\\MyKey\\"), &key) != ERROR_SUCCESS)
{
   cout << "Unable to open registry key";
}


To write a value to a registry key you need to use:

if (RegSetValueEx(key, TEXT("value_name"), 0, REG_SZ, (LPBYTE)"value_data", strlen("value_data")*sizeof(char)) != ERROR_SUCCESS)
{
   RegCloseKey(key);
   cout <<"Unable to set registry value value_name";
}
else
{
   cout << "value_name was set" << endl;
}


The RegSetValueEx function is used to set a value in the registry. Its parameters are: the variable holding the opened registry key, the name of the key value you want to set, the type of the key value you want to set (REG_SZ is for string values, REG_DWORD is for integer values), the data to be stored for that value and the size of the data.

To read from a registry key you have to use:

char value[SIZE];
DWORD value_length = SIZE;
RegQueryValueEx(key, "value_name", NULL, REG_SZ, (LPBYTE)&value, &value_length);
cout << "the value read from the registry is: " << value << endl;


The function used for retrieving registry values is RegQueryValueEx. You need to specify as parameters the variable holding the opened registry key, the name of the values to be read, the type of the value, a variable to hold the result and the size of the result.

And that’s it!

Don’t forget to close the registry key at the end:

RegCloseKey(key);


Is This A Good Question/Topic? 0
  • +

Replies To: How to set/read registry key in C++

#2 olibenu  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 45
  • View blog
  • Posts: 537
  • Joined: 31-December 08

Posted 15 May 2010 - 04:19 AM

header files used? is it only windows.h?
Was This Post Helpful? 0
  • +
  • -

#3 Wreithkassan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-July 13

Posted 17 July 2013 - 02:31 PM

View Postolibenu, on 15 May 2010 - 04:19 AM, said:

header files used? is it only windows.h?


using Microsoft.Win32;
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1