Tutorial
Visual C++



Práce s registry


Zdroják ke stažení (6k)

21.01.2005

Ukládání string, int, bool hodnot do registrů a opětovné načítání.
Příklad načte cestu program files z registu HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion.
Pak uloží aktuální cestu aplikace do HKEY_CURRENT_USER\Software\JanKoci\Tutorial. Do tohoto klíče ještě uloží hodnotu int a bool

Před ukládáním, nebo načítáním hodnot z registru, musíme nejdříve otevřít registrační databázi.


// Open registy key
HKEY CRegistryKey::RegistryOpenKey(HKEY hKey, LPCTSTR lpKeyName, LPBOOL lpbCreated)
{
	DWORD dwDisp = 0;
	HKEY hkResult = NULL;
	if ( RegCreateKeyEx(hKey, lpKeyName, 0, NULL, REG_OPTION_NON_VOLATILE,
		KEY_ALL_ACCESS, NULL, &hkResult, &dwDisp) != ERROR_SUCCESS )
		return NULL;
	if ( lpbCreated )
		*lpbCreated = ( dwDisp & REG_CREATED_NEW_KEY );
	return hkResult;
}



Nyní si napíšeme funkce pro ukládání string a dword .



//----------------------------------------------------------------------------------------------------//

// Write to the registry string value
BOOL CRegistryKey::RegistryWriteString(HKEY hKey, LPCTSTR lpValueName, LPCTSTR lpText)
{
	return ( RegSetValueEx(hKey, lpValueName, 0, REG_SZ, (CONST BYTE*)lpText,
		(lstrlen(lpText)+1)*sizeof(TCHAR)) ==  ERROR_SUCCESS );
}

//----------------------------------------------------------------------------------------------------//

// Write to the registry dword value
BOOL CRegistryKey::RegistryWriteDWORD(HKEY hKey, LPCTSTR lpValueName, DWORD dwValue)
{
	return  ( RegSetValueEx(hKey, lpValueName, 0, REG_DWORD,
		(CONST BYTE*)&dwValue, sizeof(DWORD)) == ERROR_SUCCESS );
}

//----------------------------------------------------------------------------------------------------//





A pro načítání string a dword hodnot z registru. Dword se po načtení z registru převádí na požadovanou hodnotu int, nebo bool


//----------------------------------------------------------------------------------------------------//

// Read from registry string value
BOOL CRegistryKey::RegistryReadString(HKEY hKey, LPCTSTR lpValueName, LPTSTR lpString)
{
	DWORD dwSize, dwType;
	dwSize = 256;
	if ( RegQueryValueEx(hKey, lpValueName, NULL, &dwType, (LPBYTE)lpString, &dwSize)
		!= ERROR_SUCCESS )
		return FALSE;
	return TRUE;
}

//----------------------------------------------------------------------------------------------------//

// Read from registry dword value
BOOL CRegistryKey::RegistryReadDWORD(HKEY hKey, LPCTSTR lpValueName, LPDWORD lpdwResult)
{
  DWORD dwValue, dwSize, dwType;
  dwSize = sizeof(DWORD);
  dwValue = 0;
  if ( RegQueryValueEx(hKey, lpValueName, NULL, &dwType, (LPBYTE)&dwValue, &dwSize)
    != ERROR_SUCCESS )
    return FALSE;
  *lpdwResult = dwValue;
  return TRUE;
}


//----------------------------------------------------------------------------------------------------//





Funkce GetWinProgramFilesPath() načítá z registrů cestu k program files. Předtím si však nadefinujeme konstanty.


//----------------------------------------------------------------------------------------------------//

#define K_REGISTRY_KEY_NAME "Software\\JanKoci\\Tutorial"
#define K_REGISTRY_KEY_WIN "SOFTWARE\\Microsoft\\Windows\\CurrentVersion"
#define K_REGISTRY_ROOT_PATH "RootPath"
#define K_REGISTRY_INT "Int"
#define K_REGISTRY_BOOL "Bool"

//----------------------------------------------------------------------------------------------------//

// Get windows program files path
bool CRegistryKey::GetWinProgramFilesPath(char *strPath)
{
	// Open registry key
	HKEY hKey = RegistryOpenKey(HKEY_LOCAL_MACHINE, K_REGISTRY_KEY_WIN, NULL);
	if(hKey)
	{
		// Read from registry key
		RegistryReadString(hKey, "ProgramFilesDir", strPath);
		// Close registry key
		RegistryCloseKey(hKey);
	}
	else
	{
		CDebug::InfoMsg("CRegistryKey::GetWinProgramFilesPath() -- Don`t load win program files from registry");
		return false;
	}

	return true;
}

//----------------------------------------------------------------------------------------------------//



Nyní zjistíme aktuální cestu aplikace a nastavíme hodnoty int a bool


// Init application
void CRegistryKey::Init()
{
	// Get application current path
	GetCurrentDirectory(MAX_PATH, m_strCurrPath);
	// Set Read and write values
	m_i = 12345;
	m_b = true;	
}



Funkce Save() a Load() zapisují a načítají aktuální cestu aplikace, int a bool hodnoty.


//----------------------------------------------------------------------------------------------------//

// Save values to the registry
void CRegistryKey::Save()
{
	// Open registry key
	HKEY hKey = RegistryOpenKey(HKEY_CURRENT_USER, K_REGISTRY_KEY_NAME, NULL);
	if(hKey)
	{
		/////////////////////////////
		// Write to the registry key
		////////////////////////
		
		// Write application path
		if(!RegistryWriteString(hKey, K_REGISTRY_ROOT_PATH, m_strCurrPath))
		{
			CDebug::InfoMsg("CRegistryKey::Save() -- Don`t write root path to the registry key");
		}
		// Write int value
		if(!RegistryWriteDWORD(hKey, K_REGISTRY_INT, m_i))
		{
			CDebug::InfoMsg("CRegistryKey::Save() -- Don`t write int value to the registry key");
		}
		// Write bool value
		if(!RegistryWriteDWORD(hKey, K_REGISTRY_BOOL, m_b))
		{
			CDebug::InfoMsg("CRegistryKey::Save() -- Don`t write bool value to the registry key");
		}
		
		// Close registry key
		if(!RegistryCloseKey(hKey))
		{
			CDebug::InfoMsg("CRegistryKey::Save() -- Don`t close registry key");
		}
	}
	else
	{
		CDebug::InfoMsg("CRegistryKey::Save() -- Don`t open registry key");
	}

}

//----------------------------------------------------------------------------------------------------//

// Load values from registry
void CRegistryKey::Load()
{
	// Open registry key
	HKEY hKey = RegistryOpenKey(HKEY_CURRENT_USER, K_REGISTRY_KEY_NAME, NULL);
	if(hKey)
	{
		/////////////////////////////
		// Read from registry key
		////////////////////////

		// Read application path
		if(!RegistryReadString(hKey, K_REGISTRY_ROOT_PATH, m_strCurrPath))
		{
			CDebug::InfoMsg("CRegistryKey::Load() -- Don`t read root path from registry key");
		}
		
		// Read int value
		DWORD dwInt;
		if(!RegistryReadDWORD(hKey, K_REGISTRY_INT, &dwInt))
		{
			CDebug::InfoMsg("CRegistryKey::Load() -- Don`t read int value from registry key");
		}
		else
		{
			// Get int from dword
			m_i = (int) dwInt;
		}

		// Read bool value
		DWORD dwBool;
		if(!RegistryReadDWORD(hKey, K_REGISTRY_BOOL, &dwBool))
		{
			CDebug::InfoMsg("CRegistryKey::Load() -- Don`t read bool value from registry key");
		}
		else
		{
			// Get bool from dword
			m_b = (bool) dwBool;
		}

		// Close registry key
		if(!RegistryCloseKey(hKey))
		{
			CDebug::InfoMsg("CRegistryKey::Load() -- Don`t close registry key");
		}
	}
	else
	{
		CDebug::InfoMsg("CRegistryKey::Load() -- Don`t open registry key");
	}
}

//----------------------------------------------------------------------------------------------------//






Home