c++ - 无法将参数 1 从 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' 转换为 'const char *'

标签 c++ mfc atl cstring

如何正确转换这种方式?

    VARIANT varIndex;
    CString csIndex;
    //Index BSTR
    csIndex = (LPCSTR)(_bstr_t)vtIndex;
    csIndex.MakeUpper();
    if (csIndex.Left(3) == PROCESSUS_TABLE)
    {
        lIndex = atoi((LPCSTR)csIndex.Mid(3));
        if ((unsigned long)lIndex<0)
            return E_INVALIDARG;
    }

错误信息:

C2664: 'int atoi(const char *)' : cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *'

我找不到解决这个问题的方法,请问有什么好主意吗?

最佳答案

变量'csIndex'是unicoce字符串(wchar_t),而宏LPCSTR是针对ansi字符串(char)。

所以你应该使用 Unicode 函数,代码将是:

lIndex = _wtoi((LPCWSTR)csIndex.Mid(3));

这一行没有问题:

csIndex = (LPCSTR)(_bstr_t)vtIndex;

因为智能指针类型_bstr_t可以自动处理char*/wchar_t*转换。

关于c++ - 无法将参数 1 从 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' 转换为 'const char *',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22916601/

相关文章:

c++ - 用于移动应用(iOS、Android)的 Crypto++ 库的子集

c++ - 将 TaskDialogConfig 与 std::string 一起使用

c++ - 在CListCtrl上设置排序指示器正在清除列标题

windows - 使用 ATL CWindowImpl 永不结束 WM_PAINT 循环

c# - 对象公寓与线程公寓

c++ - 奇怪的双指针空检查

c++ - PCC-S-02015, "unable to open include file"

c++ - C++ 字符串文字的安全性和可靠性如何?

c++ - 使用 C++ 自动执行下拉菜单、按钮点击

c++ - R^3(x,y,z)中快速随机访问和顺序访问的数据结构