3
I need to record data in the Windows registry in my application. How to work with Windows registry data handling using C++? What is the correct way to record new data in the registry without errors?
3
I need to record data in the Windows registry in my application. How to work with Windows registry data handling using C++? What is the correct way to record new data in the registry without errors?
2
Writing in the record of Windows
/* 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’re using Visual Studio remember to make the following changes:
Configuration Properties -> General
Configuration Properties -> C/C++ -> General
Alter Common Language Runtime Support for "Common Language Runtime Support (/clr)"
Also remember to make the validations in the code.
2
In addition to the solutions already presented, it is possible to use some Winapis that provide you with access to the Registry and handling it.
RegOpenKeyEx
: Opens specified key for read or write. RegCreateKeyEx
: Creates a key in the Registry, if it already exists, the function will open such key. Remember that it is possible to create four keys hierarchically by specifying a sequence to the parameter lpSubKey
, something like that subkey1 subkey2 subkey3 subkey4. RegDeleteKeyEx
: Deletes a Registry key and its values. The deleted key is not removed until the last operation identifier is closed.RegDeleteKeyValue
: Excludes the Subkey and specified value.RegDeleteTree
: Excludes the subclasses and key values specified in a form recursive.RegDeleteValue
: Deletes a named value by specifying the key.RegSetKeyValue
: Used to change the name of a value or key, or modify the data of a value.RegSetValueEx
: Changes the data and guy(REG_SZ, REG_MULTI_SZ,..) of a specified value, indicating the key.RegQueryValueEx
: Retrieves data and 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 null termination(REG_SZ, REG_MULTI_SZ, and REG_EXPAND_SZ) while the other function may not properly store the null characters.These must 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 function 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, ®Key);
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!";
}
}
0
As never used, I will post only what I found on the MSDN site.
The following code uses the key
CurrentUser
to create an instance of class writingRegistryKey
, corresponding to the Software key. The methodCreateSubKey
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: http://msdn.microsoft.com/en-us/library/bwt6b955.aspx
Browser other questions tagged c++
You are not signed in. Login or sign up in order to post.
I found these posts: http://stackoverflow.com/q/34065/4178863 and http://stackoverflow.com/q/15084380/4178863. You also have: http://www.tenouk.com/ModuleO1.html. If you think you are I can create an answer on top of them.
– Franchesco
Are you using C++/CLI? Or normal C++ ?
– Maniero