Readinf From The Registry [modified]
-
Hello, I'm trying to read from the registry a value from a key, but I don't understand some parameters from the reading function. here is a piece of code:
CRegKey key; key.Open(HKEY_LOCAL_MACHINE,"Software\\MyProject\\key"); key.SetStringValue("TRUE","TRUE"); if (key.QueryStringValue(**?**,**?**,**?**)=="TRUE") MessageBox("TRUE");
I dont understand the parameters within the QueryStringValue( ) Can you please explain for me what this parameters do and what values for instance I can set to the parameters? Another problem: Every time I execute the program I'm getting an assertation that m_hKey ! = 0 I tried to remove the ASSERT line:**ATLASSERT(m_hKey != NULL);**
but it still stops at the line of this assert and gives me the assertation although I removed it. why? and how can I remove it for real? Thanking you in advance! -- modified at 15:45 Thursday 27th July, 2006 -
Hello, I'm trying to read from the registry a value from a key, but I don't understand some parameters from the reading function. here is a piece of code:
CRegKey key; key.Open(HKEY_LOCAL_MACHINE,"Software\\MyProject\\key"); key.SetStringValue("TRUE","TRUE"); if (key.QueryStringValue(**?**,**?**,**?**)=="TRUE") MessageBox("TRUE");
I dont understand the parameters within the QueryStringValue( ) Can you please explain for me what this parameters do and what values for instance I can set to the parameters? Another problem: Every time I execute the program I'm getting an assertation that m_hKey ! = 0 I tried to remove the ASSERT line:**ATLASSERT(m_hKey != NULL);**
but it still stops at the line of this assert and gives me the assertation although I removed it. why? and how can I remove it for real? Thanking you in advance! -- modified at 15:45 Thursday 27th July, 2006Semion_N wrote:
CRegKey key; key.Open(HKEY_LOCAL_MACHINE,"Software\\MyProject\\key");
What is the return value? Open will fail if HKLM\Software\MyProject\key does not exist. Use CRegKey::Create() instead.
Semion_N wrote:
Every time I execute the program I'm getting an assertation that m_hKey ! = 0
key.open() is failing, so CRegKey::m_hKey is zero. Do not remove the ASSERTS, they are used to tell you when you do something wrong.
Semion_N wrote:
key.QueryStringValue(?,?,?)
From MSDN: Parameters pszValueName Pointer to a null-terminated string containing the name of the value to query. pszValue Pointer to a buffer that receives the string data. pnChars The size, in TCHARs, of the buffer pointed to by pszValue. When the method returns, pnChars contains the size, in TCHARs, of the string retrieved, including a terminating null character. Return Value If the method succeeds, ERROR_SUCCESS is returned. If the method fails to read a value, it returns a nonzero error code defined in WINERROR.H. If the data referenced is not of type REG_SZ, ERROR_INVALID_DATA is returned. If the method returns ERROR_MORE_DATA, pnChars equals zero, not the required buffer size in bytes.
CRegKey key;
if (ERROR_SUCCESS == key.Create(HKEY_LOCAL_MACHINE, "Software\\MyProject\\key"))
{
if (ERROR_SUCCESS == key.SetStringValue("TRUE_KEY", "TRUE_VALUE"))
{
char readbuffer[30] = {0};
ULONG buffersize = 30;
if (ERROR_SUCCESS == key.QueryStringValue("TRUE_KEY", readbuffer, &buffersize))
{
if (0 == strcmp(readbuffer, "TRUE_VALUE"))
{
MessageBox("TRUE");
}
}
}
} -
Semion_N wrote:
CRegKey key; key.Open(HKEY_LOCAL_MACHINE,"Software\\MyProject\\key");
What is the return value? Open will fail if HKLM\Software\MyProject\key does not exist. Use CRegKey::Create() instead.
Semion_N wrote:
Every time I execute the program I'm getting an assertation that m_hKey ! = 0
key.open() is failing, so CRegKey::m_hKey is zero. Do not remove the ASSERTS, they are used to tell you when you do something wrong.
Semion_N wrote:
key.QueryStringValue(?,?,?)
From MSDN: Parameters pszValueName Pointer to a null-terminated string containing the name of the value to query. pszValue Pointer to a buffer that receives the string data. pnChars The size, in TCHARs, of the buffer pointed to by pszValue. When the method returns, pnChars contains the size, in TCHARs, of the string retrieved, including a terminating null character. Return Value If the method succeeds, ERROR_SUCCESS is returned. If the method fails to read a value, it returns a nonzero error code defined in WINERROR.H. If the data referenced is not of type REG_SZ, ERROR_INVALID_DATA is returned. If the method returns ERROR_MORE_DATA, pnChars equals zero, not the required buffer size in bytes.
CRegKey key;
if (ERROR_SUCCESS == key.Create(HKEY_LOCAL_MACHINE, "Software\\MyProject\\key"))
{
if (ERROR_SUCCESS == key.SetStringValue("TRUE_KEY", "TRUE_VALUE"))
{
char readbuffer[30] = {0};
ULONG buffersize = 30;
if (ERROR_SUCCESS == key.QueryStringValue("TRUE_KEY", readbuffer, &buffersize))
{
if (0 == strcmp(readbuffer, "TRUE_VALUE"))
{
MessageBox("TRUE");
}
}
}
} -
Semion_N wrote:
How can I check if the key exist?
Call Open() the way you did in your original post and check the returned error code. If the key does not exist Open() will fail with the return value of ERROR_FILE_NOT_FOUND. Sorry it took so long to respond, I am not getting any reply emails from CP:((
You may be right
I may be crazy
-- Billy Joel --Within you lies the power for good, use it!!!