How to write data to the Windows registry?

3

I need to write data to the Windows registry in my application. How to work with Windows registry data manipulation using C ++? What is the correct way to write new data to the registry without errors?

    
asked by anonymous 13.01.2015 / 07:34

3 answers

2

Writing to the Windows registry

/* Namespaces */
using namespace System;
using namespace Microsoft::Win32;

    //Criando uma instância gravável da classe de RegistryKey 
    RegistryKey^ rk;
    rk = Registry::LocalMachine->OpenSubKey("SOFTWARE\", true);

    //Criando sua subchave
    RegistryKey^ nk = rk->CreateSubKey("DWORD");

    //Adicionando novo valor a sua subkey
    String^ newValue = "NewValue";
    try
    {
        nk->SetValue("NewKey", newValue);
        nk->SetValue("NewKey2", 44);
    }
    catch (Exception^)
    {
        Console::WriteLine("Failed to set new values in 'NewRegKey'");
        getchar();
        return -1;
    }

If you are using Visual Studio remember to make the following changes:

Configuration Properties - > General

Configuration Properties - > C / C ++ - > General

Common Language RunTime Support "

Also remember to make the validations in the code.

    
13.01.2015 / 18:01
3

In addition to the solutions already presented, some WinAPIs that provide you with access to the registry and its manipulation.

  • RegOpenKeyEx : Opens the specified key for reading or writing.
  • RegCreateKeyEx : Creates a key in the registry , if it already exists, the function will open that key. Note that it is possible to create four hierarchical mode keys by specifying a sequence to the lpSubKey parameter, something like subkey1 \ subkey2 \ subkey3 \ subkey4 .
  • RegDeleteKeyEx : Deletes a registry key and their values. The deleted key is not removed until the last handle of the operation is closed.
  • RegDeleteKeyValue : Excludes subkey and value specified.
  • RegDeleteTree : Excludes subkeys and values of the specified key in a recursive way.
  • RegDeleteValue : Deletes a named value, specifying the key.
  • RegSetKeyValue : Used to change the name of a value or key, or modify the data of a value.
  • RegSetValueEx : Change the data and < a type (

    REG_SZ , REG_MULTI_SZ , ..) of a specified value, indicating the key.

  • RegQueryValueEx : Retrieves the data and the type of a specified value, associated with an open registry key.
  • RegGetValue : Similar to the above function, however, the difference between them is that it is more appropriate to return values from REG_SZ, REG_MULTI_SZ , and REG_EXPAND_SZ ) while the other function may not properly store the null characters.

These should be the functions that should interest you, the full list of functions that involve the Registry, can be seen here .

See an example of using the RegSetValueEx .

HKEY regKey;
std::wstring foo = TEXT("O dado a ser gravado aqui");

Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\Microsoft\Windows\CurrentVersion\Run"), 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &regKey);
if (Result == ERROR_SUCCESS)
{
    Result = RegSetValueEx( regKey, 
        TEXT("Nome do valor"), 
        0, 
        REG_SZ, 
        (const BYTE*)foo.c_str(), 
        ( foo.size() + 1 ) * sizeof( wchar_t ) );
    if (Result == ERROR_SUCCESS)
    {
        std::cout << "Done!";
    }
}
    
13.01.2015 / 22:00
1

As I have never used, I will only post what I found on the MSDN website.

  

The following code uses the CurrentUser key to create an instance of   writing of class RegistryKey , corresponding to the key of the Software. O    CreateSubKey method is used to create a new key and add the pairs   value / key.

// registry_read.cpp
// compile with: /clr
using namespace System;
using namespace Microsoft::Win32;

int main( )
{
   array<String^>^ key = Registry::CurrentUser->GetSubKeyNames( );

   Console::WriteLine("Subkeys within CurrentUser root key:");
   for (int i=0; i<key->Length; i++)
   {
      Console::WriteLine("   {0}", key[i]);
   }

   Console::WriteLine("Opening subkey 'Identities'...");
   RegistryKey^ rk = nullptr;
   rk = Registry::CurrentUser->OpenSubKey("Identities");
   if (rk==nullptr)
   {
      Console::WriteLine("Registry key not found - aborting");
      return -1;
   }

   Console::WriteLine("Key/value pairs within 'Identities' key:");
   array<String^>^ name = rk->GetValueNames( );
   for (int i=0; i<name->Length; i++)
   {
      String^ value = rk->GetValue(name[i])->ToString();
      Console::WriteLine("   {0} = {1}", name[i], value);
   }

   return 0;
}

Source: link

    
13.01.2015 / 10:46