RegQueryValueEx not working with a Release version but working fine with Debug

Posted by Nux on Stack Overflow See other posts from Stack Overflow or by Nux
Published on 2010-05-20T10:44:13Z Indexed on 2010/05/20 10:50 UTC
Read the original article Hit count: 227

Filed under:
|
|

Hi. I'm trying to read some ODBC details form a registry and for that I use RegQueryValueEx. The problem is when I compile the release version it simply cannot read any registry values.

The code is:

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've found a workaround for this - I disabled Optimization (/Od), but it seems strange that I needed to do that. Is there some other way? I use Visual Studio 2005. Maybe it's a bug in VS?

Almost forgot - the error code is 2 (as the key wouldn't be found).

© Stack Overflow or respective owner

Related posts about visual-studio-2005

Related posts about c++