Click to See Complete Forum and Search --> : Windows Registry


Borbit
May 5th, 1999, 12:19 PM
How do i load a String from the windows registry? I manage to save one, but i can't load it.

Shawn Fessenden
May 5th, 1999, 02:15 PM
Easiest way is to use CWinApp::GetProfileString, provided you call CWinApp::SetProfileString. Otherwise, use RegQueryValueEx.

"Geek used to be a four-letter word. Now it's a six-figure one."

LALeonard
May 5th, 1999, 03:19 PM
How 'bout an example:

// Write a string to the registry.
bool MyRegistry::SaveValueString(const CString& sValueName, const CString& sValue)
{
VALIDATE;
_ASSERTE(m_hRootKey && "m_hRootKey is NULL");
_ASSERTE(m_hSubKey && "m_hSubKey is NULL");
_ASSERTE(! m_sSubKey.IsEmpty() && "m_sSubKey is empty");

// Build the buffer.
bool bReturn(false);
LONG lnReturn(ERROR_SUCCESS);
char szData[256];
const BYTE* pData = reinterpret_cast<const BYTE*> (szData);
wsprintf(szData, "%s", sValue);
_ASSERTE(lstrlen(szData) < sizeof(szData));

// Write out the value.
bReturn = (ERROR_SUCCESS == (lnReturn = ::RegSetValueEx(
m_hSubKey, // Handle of key to set value for
sValueName, // Address of value to set
NULL, // Reserved
REG_SZ, // Type
pData, // Value data
lstrlen(szData)))); // Size of value data

// Done. Did we succeed?
if (! bReturn) {
DRSCRUB_LOG(REGISTRY_FAILURE, STSEV_Warning,
"::RegSetValueEx" & lnReturn);
}

return bReturn;
}

I would seriously consider using one of the many Registry wrapper classes you can find at this site, or writing one of your own.

LA Leonard - http://www.DefinitiveSolutions.com