zoukankan      html  css  js  c++  java
  • 碰到一个微软的bug:CWinAppEx::GetString

    在调试公司项目代码的时候,有一个系统设置的功能,里面需要从注册表中去读取数据,因为使用了MFC框架所以,为了简化代码直接使用了CWinAppEx::GetString 、CWinAppEx::SetString、CWinAppEx::GetInt、CWinAppEx::SetInt等等框架内函数,因为使用它之间只需要SetRegistryKey(_T("Application Name"));设置一下就好了,远比自己调用win32 API或者CRegKey类用起来方便多了。

    发现一个GetString的在实现的时候有一个bug,起初是我在读取一个注册表String时发现,CWinAppEx::GetString即使传入了第二个参数lpzDefault没能成功访问注册表的话返回还是空字符串!

    debug进入代码中看:

    首先可以发现GetString实际上是调用的GetSectionString函数:

    CString CWinAppEx::GetString(LPCTSTR lpszEntry, LPCTSTR lpszDefault /*= ""*/)
    {
        return GetSectionString(_T(""), lpszEntry, lpszDefault);
    }

    afxwinappex.cpp:

    CString CWinAppEx::GetSectionString( LPCTSTR lpszSubSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault /*= ""*/)
    {
        ENSURE(lpszSubSection != NULL);
        ENSURE(lpszEntry != NULL);
        ENSURE(lpszDefault != NULL);
    
        CString strRet = lpszDefault;
    
        CString strSection = GetRegSectionPath(lpszSubSection);
    
        CSettingsStoreSP regSP;
        CSettingsStore& reg = regSP.Create(FALSE, TRUE);
    
        if (reg.Open(strSection))
        {
            reg.Read(lpszEntry, strRet);
        }
        return strRet;
    }

    从代码中来看,前面几行都没有问题,按F11进入到reg.Read(lpszEntry, strRet);

    afxsettingsstore.cpp:

    BOOL CSettingsStore::Read(LPCTSTR lpszValueName, CString& strValue)
    {
        ENSURE(lpszValueName != NULL);
    
        strValue.Empty();
    
        DWORD dwCount = 0;
        if (m_reg.QueryStringValue(lpszValueName, NULL, &dwCount) != ERROR_SUCCESS)
        {
            return FALSE;
        }
    
        if (dwCount == 0)
        {
            return TRUE;
        }
    
        LPTSTR szValue = new TCHAR [dwCount + 1];
    
        BOOL bRes = m_reg.QueryStringValue(lpszValueName, szValue, &dwCount) == ERROR_SUCCESS;
        if (bRes)
        {
            strValue = szValue;
        }
    
        delete [] szValue;
        return bRes;
    }

    在这里可以看到strValue.Empty();在最开始的时候就被调用了,明显不对,就算你是否有正确的值传进来,也不该首先就直接将default value清空啊,坑啊!

    搜索看到国外有个哥们也遇到相同的问题:http://www.bcgsoft.com/cgi-bin/forum/topic.asp?TOPIC_ID=4485

  • 相关阅读:
    django-form表单
    django-登录页面添加验证码
    opensatck误删除service项目
    运维脚本
    大数据平台架构组件选择与运用场景
    HDP2.4安装(五):集群及组件安装
    Spark(三): 安装与配置
    Spark(二): 内存管理
    Spark(一): 基本架构及原理
    ElasticSearch基本使用
  • 原文地址:https://www.cnblogs.com/hyb1/p/3242949.html
Copyright © 2011-2022 走看看