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

7
  • 1
    Why use such a beautiful language like c++ and then ruin it by using VB? :P Commented Feb 1, 2013 at 3:41
  • 1
    You might want to read stackoverflow.com/a/6072627/13760 for the details of how C++11 solves this problem for you :) Commented Feb 1, 2013 at 3:42
  • Mr Universe, because I suck :) Commented Feb 1, 2013 at 3:54
  • Which function do you use to print? Are you printing to a console or what else? Commented Feb 1, 2013 at 7: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 "Т" Commented Feb 1, 2013 at 9:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

When I use LPCWSTR instead of LPCTSTR. I get error 'SysAllocStringByteLen' : cannot convert parameter 1 from 'LPCWSTR' to 'LPCSTR'
@user1088794 try using SysAllocStringLen instead.
Doesn't work. When it sends to VB it still converts the character to a "?". And also has gaps between all the characters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.