Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I'm trying to convert 8 bit ASCII to a Wide Character string to be displayed in a message box. However, what I'm doing is resulting in Chinese characters being displayed instead.

tchKey comes in from Install Shield via a MsiGetProperty.

Here's what I have....

Thank you,
Glenn

C
<pre lang="vb">
    TCHAR tchKey[MAX_PATH];
    char achKey[MAX_PATH];
    wchar_t wWork[255];

... removed code ...

    CW2A convKey(tchKey);
    strcpy_s(achKey, sizeof(achKey), convKey);

    dwBuffer = strlen(achKey);

    for (int iIndex = 0; iIndex < (int)dwBuffer; iIndex++)
    {
        if (strchr(BASE32_CHARSET, achKey[iIndex]) == 0)
        {
            strcpy_s(&achKey[iIndex], sizeof(achKey) - iIndex, &achKey[iIndex + 1]);
            iIndex--;
            dwBuffer--;
            }
        }

swprintf(wWork, sizeof(wWork), L&amp;quot;%ls&amp;quot;, achKey);
MessageBox(GetForegroundWindow(),
    wWork,
    TEXT(&amp;quot;Error&amp;quot;),
    MB_OK | MB_ICONERROR | MB_APPLMODAL);&lt;/pre&gt;</pre>
Posted

you must use MessageBoxW if your compiler settings are for ANSI-Code. (1 byte char)
 
Share this answer
 
Comments
gmhanna 4-Dec-14 13:24pm    
Still Chinese characters using MessageBoxW
Sergey Alexandrovich Kryukov 4-Dec-14 17:32pm    
Also provide correct non-Chinese data, some valid wide string. To start with, use a constant wide-string literal...
—SA
This line is wrong:
swprintf(wWork, sizeof(wWork), L"%ls", achKey);

First error:
Parameters are for snwprintf.
Second error:
You are specifying a wide string in the format but passing a char string. Use "%hs" instead of "%ls".

So it must be
snwprintf(wWork, sizeof(wWork), L"%hs", achKey);

or
swprintf(wWork, L"%hs", achKey);


[EDIT]
When not using a Unicode build, you must also use MessageBoxW and pass the title as wide string:
C++
MessageBoxW(GetForegroundWindow(),
    wWork,
    L("Error"),
    MB_OK | MB_ICONERROR | MB_APPLMODAL);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900