Problems reading from registry
-
I am not able to get a string value from the registry. I have looked over all the posts regarding this and have not found a solution. Here is my code:
HKEY hKey = NULL;
CHAR szBuf[100];
LPDWORD lpdwLength;
CString strReturn;::RegCreateKeyEx(HKEY_CLASSES_ROOT, "CLSID", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, NULL);
::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, lpdwLength);
::RegCloseKey(hKey);The value of szBuf after this runs is garbage, but the value is definately in the registry. Does anyone have a code example?? MSDNs code example link for
RegQueryValueEx()
doesnt link to code (go figure...) but rather to an explanation that is of no help. Nish?? You out there??? HELP!!! :confused: Thanks guys and gals. Is it football season yet?!?!?!? -
I am not able to get a string value from the registry. I have looked over all the posts regarding this and have not found a solution. Here is my code:
HKEY hKey = NULL;
CHAR szBuf[100];
LPDWORD lpdwLength;
CString strReturn;::RegCreateKeyEx(HKEY_CLASSES_ROOT, "CLSID", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, NULL);
::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, lpdwLength);
::RegCloseKey(hKey);The value of szBuf after this runs is garbage, but the value is definately in the registry. Does anyone have a code example?? MSDNs code example link for
RegQueryValueEx()
doesnt link to code (go figure...) but rather to an explanation that is of no help. Nish?? You out there??? HELP!!! :confused: Thanks guys and gals. Is it football season yet?!?!?!?A comment: ALWAYS check the return value from a function that returns one. Ex:
HKEY hKey = NULL;
CHAR szBuf[100];
LPDWORD lpdwLength;
CString strReturn;
LONG lResult = ::RegCreateKeyEx(HKEY_CLASSES_ROOT, "CLSID", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, NULL);
lResult = ::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, lpdwLength);
::RegCloseKey(hKey);by knowing the return value, you will know what is going wrong. Michel It is a lovely language, but it takes a very long time to say anything in it, because we do not say anything in it, unless it is worth taking a very long time to say, and to listen to.
- TreeBeard -
I am not able to get a string value from the registry. I have looked over all the posts regarding this and have not found a solution. Here is my code:
HKEY hKey = NULL;
CHAR szBuf[100];
LPDWORD lpdwLength;
CString strReturn;::RegCreateKeyEx(HKEY_CLASSES_ROOT, "CLSID", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE, NULL, &hKey, NULL);
::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, lpdwLength);
::RegCloseKey(hKey);The value of szBuf after this runs is garbage, but the value is definately in the registry. Does anyone have a code example?? MSDNs code example link for
RegQueryValueEx()
doesnt link to code (go figure...) but rather to an explanation that is of no help. Nish?? You out there??? HELP!!! :confused: Thanks guys and gals. Is it football season yet?!?!?!?lpdwLength should be the address of a dword, not an uninitialised pointer pointing into randomness. The value in that dword is size of the buffer you're passing in, or 0 if you're not passing a buffer. After the call it's the size of the buffer required (or the size of the buffer that was used (same thing)). It kinda says all that in the MSDN docs. So, something like this might work (beware, it hasn't seen a compiler). Or you can try a wrapper class, like the one I wrote about here.
HKEY hKey = NULL
long ret = ::RegCreateKeyEx(
HKEY_CLASSES_ROOT,
"CLSID",
0,
NULL,
REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE,
NULL,
&hKey,
NULL);if (ret == ERROR_SUCCESS)
{
CHAR szBuf[100];
DWORD dwLength = sizeof(szBuf);ret = ::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, &dwLength);
if (ret == ERROR_SUCCESS)
{
// got a value...
}::RegCloseKey(hKey);
}
else
{
// error, deal with it.
}Len Holgate www.jetbyte.com The right code, right now.
-
lpdwLength should be the address of a dword, not an uninitialised pointer pointing into randomness. The value in that dword is size of the buffer you're passing in, or 0 if you're not passing a buffer. After the call it's the size of the buffer required (or the size of the buffer that was used (same thing)). It kinda says all that in the MSDN docs. So, something like this might work (beware, it hasn't seen a compiler). Or you can try a wrapper class, like the one I wrote about here.
HKEY hKey = NULL
long ret = ::RegCreateKeyEx(
HKEY_CLASSES_ROOT,
"CLSID",
0,
NULL,
REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE,
NULL,
&hKey,
NULL);if (ret == ERROR_SUCCESS)
{
CHAR szBuf[100];
DWORD dwLength = sizeof(szBuf);ret = ::RegQueryValueEx(hKey, "E1", 0, 0, (LPBYTE)szBuf, &dwLength);
if (ret == ERROR_SUCCESS)
{
// got a value...
}::RegCloseKey(hKey);
}
else
{
// error, deal with it.
}Len Holgate www.jetbyte.com The right code, right now.
Len, Thanks for the advice. I am still learning how to decipher the cryptic documentation that is MSDN... Anyways, I tried this and now get "access violation reading location ..." during the query. The access specifier is set to
KEY_READ | KEY_WRITE | KEY_QUERY_VALUE
Any suggestions???
-
Len, Thanks for the advice. I am still learning how to decipher the cryptic documentation that is MSDN... Anyways, I tried this and now get "access violation reading location ..." during the query. The access specifier is set to
KEY_READ | KEY_WRITE | KEY_QUERY_VALUE
Any suggestions???
Try using KEY_ALL_ACCESS? Len Holgate www.jetbyte.com The right code, right now.