1

I am trying to be able to send character "Т" (not a normal capital t, unicode decimal value 1058) from C++ to VB

However, with this method below Message is returned to VB and it appears as "Т", which is the above character encoded in ANSI.

#if defined(_MSC_VER) && _MSC_VER > 1310
# define utf8(str)  ConvertToUTF8(L##str)
const char * ConvertToUTF8(const wchar_t * pStr) {
    static char szBuf[1024];
    WideCharToMultiByte(CP_UTF8, 0, pStr, -1, szBuf, sizeof(szBuf), NULL, NULL);
    return szBuf;
}
#else
# define utf8(str)  str
#endif


BSTR _stdcall chatTest()
{
    BSTR Message;
    CString temp("temp test");
    temp+=utf8("\u0422");
    int len = temp.GetLength();
    Message = SysAllocStringByteLen ((LPCTSTR)temp, len+1 );
    return Message;
}

If I just do temp+=("\u0422"); without the utf8 function. It sends the data as "?" and its actually a question mark (sometimes unicode characters show up as question marks in VB, but still have the correct unicode decimal value.. this is not the case here... it changes it to a question mark.

In VB if I output the String variable that has data from Message when it is "Т" to a text file it appears as the "Т".

So as far as I can tell its in UTF8 in C++, then somehow gets converted to ANSI in VB (or before its sent?), and then when outputted to a file its changed back to UTF8?

I just need to keep the "Т" intact when sending from C++ to VB. I know VB strings can hold that character because from another source within VB I am able to store it (it appears as a "?", but has the proper unicode decimal value).

Any help is greatly appreciated.

Thanks

user1088794
  • 93
  • 10
  • 1
    Why use such a beautiful language like c++ and then ruin it by using VB? :P – Sellorio Feb 01 '13 at 03:41
  • 1
    You might want to read http://stackoverflow.com/a/6072627/13760 for the details of how C++11 solves this problem for you :) – Carl Feb 01 '13 at 03:42
  • Mr Universe, because I suck :) – user1088794 Feb 01 '13 at 03:54
  • Which function do you use to print? Are you printing to a console or what else? – Emilio Garavaglia Feb 01 '13 at 07:53
  • @Emilio I tried basically everything (I think). In C++ I tried MessageBox, printf, cout, ofstream to print to file. In MessageBox the "Т" character shows up as "Т" (I googled this, and I dont think MessageBox can show some unicode characters) In printf, and cout it shows up as "?" iirc when outputted to file with ofstream it shows up properly as "Т" In VB, I just use Print #, and it shows up in the file as "Т", but during runtime in VB it shows up as "Т" – user1088794 Feb 01 '13 at 09:05
  • Similar to MessageBox I dont think VB can show some unicode characters by default, but when its done "properly" I think it shows up as a "?" with the correct unicode code. – user1088794 Feb 01 '13 at 09:06
  • Here is the ghetto solution I came up with that works for what I need. I made another function in C++ that I send a string to from VB and it just sends it back. Somehow that converts all the unicode characters to "?" and then in C++ for the other string I use MultiByteToWideChar and it turns the same characters into "?" (not sure why).. but now the data from both Strings match and that solves my problem – user1088794 Feb 01 '13 at 09:50

1 Answers1

1

A BSTR is not UTF-8, it's UTF-16 which is what you get with the L"" prefix. Take out the UTF-8 conversion and use CStringW. And use LPCWSTR instead of LPCTSTR.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622