C and C++ Read and write code examples of of binary data in the registry

  • 2020-07-21 09:16:57
  • OfStack

1. RegOpenKeyEx function:

Prototype:

[

LONG RegOpenKeyEx(
HKEY hKey, // To open the primary key name
LPCTSTR lpSubKey, // The child key or path that needs to be opened
DWORD ulOptions, // reserved, 0
REGSAM samDesired, // Operates the permission flag
PHKEY phkResult // Refers to the handle to which you open the key (returned by pointer)
);

]

Return value: Non-0 on success, ERROR_SUCCESS on success.
Explanation: this function is responsible for opening the specified key or child key, if it does not exist he does not create.

See Microsoft's official documentation: http: / / msdn microsoft. com/zh - cn/aa912084

2. RegSetValueEx function:

Prototype:

[

LONG RegSetValueEx(
HKEY hKey, // Handle to open keys
LPCTSTR lpValueName, // To query the name of the value, pass \"\" as the default value under the query key
DWORD Reserved, // reserved
DWORD dwType, // Write the type of key value
CONST BYTE *lpData, // Address of variable data
DWORD cbData // The length of the variable
);

]

Return value: non-0 on success, ERROR_SUCCESS on success
Set the value of a specific name under a child key.

See Microsoft's official documentation: http: / / msdn microsoft. com/zh - cn/aa916717 #

3. RegQueryValueEx function:

Prototype:

[

LONG RegQueryValueEx(
HKEY hKey, // Handle to open keys
LPTSTR lpValueName, // To query the name of the value, pass \"\" as the default value under the query key
LPDWORD lpReserved, // reserved, 0
LPDWORD lpType, // Type of query
LPBYTE lpData, // Address of data storage
LPDWORD lpcbData // Data length +1
);

]

Return value: non-0 if unsuccessful, ERROR_SUCCESS if successful
Read the value of a specific name under a child key.

See Microsoft's official documentation: http: / / msdn microsoft. com/zh - cn/aa914692

Write code examples for base 2 data:


# include <windows.h>
# include <tchar.h>
 
int main(void)
{
	HKEY hKey;
	HKEY rootKey = HKEY_CURRENT_USER;
	TCHAR * subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"
					 "\\MenuOrder\\Start Menu2\\Programs\\test";
	TCHAR * keyValue = "c:\\test.exe";
	long openReg;
	long setRegValue;
	DWORD dwType = REG_BINARY;
	BYTE value[256] = "c:\\test.exe";
	openReg = RegOpenKeyEx(rootKey, subKey, 0, KEY_WRITE, &hKey);
	if (openReg == ERROR_SUCCESS)
	{
		setRegValue = RegSetValueEx(hKey, _T("order"), 0, dwType, value, 256);
		if (setRegValue == ERROR_SUCCESS)
		{
			MessageBox(NULL, _T("Write Sucess"), _T("call"), MB_OK);
		}
		else
		{
			MessageBox(NULL, _T("Write Fail"), _T("call"), MB_OK);
		}
		RegCloseKey(hKey);
	}
	return 0;
}

Code examples for reading base 2 data:


# include <windows.h>
# include <tchar.h>
 
int main(void)
{
	HKEY hKey;
	HKEY rootKey = HKEY_CURRENT_USER;
	TCHAR * subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"
					 "\\MenuOrder\\Start Menu2\\Programs\\test";
	long openRegResult;
	long readRegResult;
	DWORD dwType = REG_BINARY;
	REGSAM mode = KEY_READ;
	BYTE value[256] = {0};
	DWORD length = 256;
	openRegResult = RegOpenKeyEx(rootKey, subKey, 0, mode, &hKey);
	if (ERROR_SUCCESS == openRegResult)
	{
		readRegResult = RegQueryValueEx(hKey, _T("order"), 0, &dwType, value, &length);
		if (ERROR_SUCCESS == readRegResult)
		{
			MessageBox(NULL, _T(value), _T("call"), MB_OK);
		}
		else
		{
			MessageBox(NULL, _T("ERROR"), _T("call"), MB_OK);	
		}
	}
	RegCloseKey(hKey);
	return 0;
}

Note: Read and write other types of registry key values are similar to the above and will not be explained separately.


Related articles: