cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *'

StackOverflow https://stackoverflow.com/questions/22916601

  •  29-06-2023
  •  | 
  •  

Pergunta

How to convert this way properly?

    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;
    }

Error message:

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 *'

I couldn't find how to fix this, any good idea, please?

Foi útil?

Solução

The variable 'csIndex' is a unicoce string(wchar_t), while the macro LPCSTR is for the ansi string (char).

So you should use Unicode functions, the code will be:

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

There's no problem with this line:

csIndex = (LPCSTR)(_bstr_t)vtIndex;

It is because that the smart pointer type _bstr_t can handle char*/wchar_t* conversion automatically.

Outras dicas

CComVariant::ChangeType makes it available to you through .bstrVal member, _ttoi instead of atoi:

VARIANT vtIndex;
// ...
CComVariant vStringIndex;
HRESULT nResult = vStringIndex.ChangeType(VT_BSTR, &vtIndex);
if(FAILED(nResult))
  ; // TODO: Handle error
CString csIndex(vStringIndex.bstrVal);
csIndex.MakeUpper();
if (csIndex.Left(3) == PROCESSUS_TABLE)
{
    lIndex = _ttoi(csIndex.Mid(3));
    // ...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top