RegQueryValueEx doesn't work with Release version, but works fine with Debug
I am trying to read some ODBC data from the registry and I am using RegQueryValueEx for that. The problem is when I compile the release version it just can't read the registry values.
The code:
CString odbcFuns::getOpenedKeyRegValue(HKEY hKey, CString valName)
{
CString retStr;
char *strTmp = (char*)malloc(MAX_DSN_STR_LENGTH * sizeof(char));
memset(strTmp, 0, MAX_DSN_STR_LENGTH);
DWORD cbData;
long rret = RegQueryValueEx(hKey, valName, NULL, NULL, (LPBYTE)strTmp, &cbData);
if (rret != ERROR_SUCCESS)
{
free(strTmp);
return CString("?");
}
strTmp[cbData] = '\0';
retStr.Format(_T("%s"), strTmp);
free(strTmp);
return retStr;
}
I found a workaround for this - I turned off optimization (/ Od), but it seems strange to me that I need to do this. Is there another way? I am using Visual Studio 2005. Is this a bug in VS?
I almost forgot - the error code is 2 (because the key will not be found).
a source to share
You need to initialize cbData
- set it MAX_DSN_STR_LENGTH - 1
before calling RegQueryValueEx()
.
The problem is probably configuration dependent, because the variable is initialized by the compiler in one configuration and left uninitialized in the other.
Also, you will be much better off std::vector
for code without code, a safer exception less error prone.
a source to share