Re: Not Reading String data properly from windows registry
"CrimeMaster" <ajmal798@gmail.com> wrote in message
news:1150800505.663040.258890@h76g2000cwa.googlegroups.com...
Hi
i have made a Key under the HKEY_LOCAL_MACHINE\Software\My
Toolbar\Setting.
i have placed some values under the Setting key.
i m trying to access the values under that key and want to map them to
a dialog box ,means they should be visible in their corresponding Edit
controls when i display the dialog.i have used the following code in
OnInitDialog() function to map the values to their corresponding
controls but when the dialog displayed some values are repeatedly shown
in different controls.tell me where i m doing a mistake .
CRegKey regKey;
TCHAR szPath[] = _T("SOFTWARE\\MySpace.com ToolBar\\Setting");
TCHAR szBuff[1024];
DWORD dwSize=dwSize = sizeof(szBuff)/sizeof(szBuff[0]);
LONG err=regKey.Open(HKEY_LOCAL_MACHINE,szPath);
if (err!=ERROR_SUCCESS)
regKey.Close();
regKey.QueryValue(szBuff, _T("FirstName"), &dwSize);
m_FirstName = szBuff;
regKey.QueryValue(szBuff, _T("LastName"), &dwSize);
m_LastName= szBuff;
regKey.QueryValue(szBuff, _T("PresentAddress"), &dwSize);
m_PresentAddress= szBuff;
regKey.QueryValue(szBuff, _T("PermanentAddress"), &dwSize);
m_PermanentAddress = szBuff;
regKey.QueryValue(szBuff, _T("City"), &dwSize);
m_City= szBuff;
regKey.QueryValue(szBuff, _T("Country"), &dwSize);
m_Country = szBuff;
regKey.QueryValue(szBuff, _T("ZipCode"), &dwSize);
m_Zip= szBuff;
regKey.QueryValue(szBuff, _T("PhoneNo"), &dwSize);
m_Phone=szBuff;
regKey.QueryValue(szBuff, _T("MobileNo"), &dwSize);
m_Mobile = szBuff;
regKey.QueryValue(szBuff, _T("AutofillEmail"), &dwSize);
m_EmailAddress = szBuff;
UpdateData(false);
when the dialog displayed often LastName value from registry is
displayed in the m_PresentAddress and m_PermanentAddress edit
controls.and City Value fron registry id
also displayed in m_Country edit control.in the same way some values
are retrieving from registry and when i try to map them with edit
controls on the Dialog box .why did they are ?
Well I would hazard a guess it is because your not testing the return value
of the QueryValue call. You have to remember that you are REusing the input
buffer. You can do a szBuff[0]= _T('\0'); between each call and it should
stop this particular problem. There is an additional problem with your code
however, you need to reset the value of dwSize between each call as well
since that will change depending on how long the string was that was
returned. This value is also used in the beginning to let the function know
how big the supplied buffer is. In other words, say the first name was Bob.
After the call to regKey.QueryValue(szBuff, _T("FirstName"), &dwSize);,
dwSize should be 3. If you do not reset the value to dwSize your telling
the next call, regKey.QueryValue(szBuff, _T("LastName"), &dwSize);, that the
buffer is 3 in length not 1024.
You should change this line
DWORD dwSize=dwSize = sizeof(szBuff)/sizeof(szBuff[0]);
to
DWORD dwSize= sizeof(szBuff) / sizeof(szBuff[0]);
the double assignment is unneeded and I am surprised if it actually passed
the compiler.
--
============
Frank Hickman
Microsoft MVP
NobleSoft, Inc.
============
Replace the _nosp@m_ with @ to reply.