title: How to convert CComVariant bstr to CString
body: <p>I'm a newbie with C++ and I've taken over a COM project to fix some issues.
  The current issue I'm working on is handling UTF8 strings.
  I have this piece of code:</p>
  
  <pre><code>// CString strValue;
  CStringW strValue; 
  CComVariant* val = &amp;(*result)[i].minValue;
  switch (val-&gt;vt)
  {
  case VT_BSTR:   
      //strValue = OLE2CA(val-&gt;bstrVal);
      strValue = OLE2W(val-&gt;bstrVal); // Works
      (*result)[i].name = strValue; // Works
      (*result)[i].expression = "[" + fieldName + "] = \"" + strValue + "\""; // fails
      break;
  case VT_R8:     
      //strValue.Format("%g", val-&gt;dblVal);
      strValue.Format(L"%g", val-&gt;dblVal); // Works
      (*result)[i].name = strValue; // Works
      (*result)[i].expression = "[" + fieldName + "] = " + strValue; // fails
      break;
  case VT_I4:     
      //strValue.Format("%i", val-&gt;lVal);
      strValue.Format(L"%i", val-&gt;lVal); // Works
      (*result)[i].name = strValue; // Works
      (*result)[i].expression = "[" + fieldName + "] = " + strValue; // fails
      break;
  }
  
  struct CategoriesData
  {
      public:
      CComVariant minValue;
      CComVariant maxValue;
      //CString expression;
      CStringW expression;
      //CString name;
      CStringW name;
      tkCategoryValue valueType;
      int classificationField;
      bool skip;
  };
  </code></pre>
  
  <p>The problem is with this line <code>strValue = OLE2CA(val-&gt;bstrVal);</code> When <code>val-&gt;bstrVal</code> is an unicode string like this Russian text <code>Воздух</code> strValue is converted into <code>?????</code>.</p>
  
  <p>I tried several approached and searched the internet, but can't get strValue to be <code>Воздух</code>.
  Can a <code>CString</code> contain this kind of text or should I change to another type? Is so which one? </p>
  
  <p>minValue can be a VT_BSTR, a VT_R8 or a VT_I4.</p>
  
  <p>These are the options I tried so far:</p>
  
  <pre><code>strValue = val-&gt;bstrVal;
  strValue = Utility::ConvertFromUtf8(val-&gt;bstrVal);
  strValue = Utility::ConvertToUtf8(val-&gt;bstrVal);
  temp = Utility::ConvertBSTRToLPSTR(val-&gt;bstrVal);
  strValue = W2BSTR(Utility::ConvertFromUtf8(temp));
  strValue = W2BSTR(val-&gt;bstrVal);                
  strValue = CW2A(val-&gt;bstrVal);
  strValue = (CString)val-&gt;bstrVal;
  strValue = Utility::ConvertToUtf8(OLE2W(val-&gt;bstrVal));
  </code></pre>
  
  <p><strong>Edit</strong>
  The code for the helper functions:</p>
  
  <pre><code>CStringA ConvertToUtf8(CStringW unicode) {
      USES_CONVERSION;
      CStringA utf8 = CW2A(unicode, CP_UTF8);
      return utf8;
  }
  
  CStringW ConvertFromUtf8(CStringA utf8) {
      USES_CONVERSION;
      CStringW unicode = CA2W(utf8, CP_UTF8);
      return unicode;
  }
  
  char* ConvertBSTRToLPSTR (BSTR bstrIn)
  {
    LPSTR pszOut = NULL;
    if (bstrIn != NULL)
    {
      int nInputStrLen = SysStringLen (bstrIn);
  
      // Double NULL Termination
      int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, bstrIn, nInputStrLen, NULL, 0, 0, 0) + 2; 
      pszOut = new char [nOutputStrLen];
  
      if (pszOut)
      {
        memset (pszOut, 0x00, sizeof (char)*nOutputStrLen);
        WideCharToMultiByte (CP_ACP, 0, bstrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
      }
    }
    return pszOut;
  }
  </code></pre>
  
  <p><strong>Edit2</strong>
  I added my complete switch statement.
  When I change strValue from CString to CStringW I get errors for the other cases, like <code>strValue.Format("%g", val-&gt;dblVal);</code>
  How to solve this?</p>
  
  <p><strong>Edit3</strong>
  I already fixed a similar issue, but that was converting to <code>VARIANT</code> not from:</p>
  
  <pre><code>    val-&gt;vt = VT_BSTR;
      const char* v = DBFReadStringAttribute(_dbfHandle, _rows[RowIndex].oldIndex, _fields[i]-&gt;oldIndex);
      // Old code, not unicode ready:
      //WCHAR *buffer = Utility::StringToWideChar(v);
      //val-&gt;bstrVal = W2BSTR(buffer);
      //delete[] buffer;              
      // New code, unicode friendly:
      val-&gt;bstrVal = W2BSTR(Utility::ConvertFromUtf8(v)); 
  </code></pre>
  
  <p><strong>Edit4</strong>
  Thanks to all the help so far I managed to make some changes. I've updated my initial code in this post and added all code of the function. I'm now stuck with this line:</p>
  
  <pre><code> (*result)[i].expression = "[" + fieldName + "] = \"" + strValue + "\"";    
  </code></pre>
  
  <p>I can't concatenate CStringW values.</p>
  
  <p>Some more background info: The function is part of <a href="https://github.com/MapWindow/MapWinGIS%20MapWinGIS" rel="nofollow noreferrer">MapWinGIS</a>, an Open Source GIS application, where you can show maps (shapefiles). These maps have attribute data. This data is stored in DBase IV format and can hold unicode/UTF-8 text. I already made a fix (see Edit3) to show this text properly in a grid view. The function I'm struggling now is categorizing (grouping) the data to, for example give similar values the same color. This category has a name and an expression. This expression is later on parsed to do the actual grouping. For example I have a map with states and I want to give each state a different color.
  As mentioned before, I'm new to C++ and am really outside my comfort zone. I really appreciate all the help you have given me. I hope you will help me once more.           </p>