Display contents of wchar_t variable

Nov 14, 2011 at 10:19am
How can I display the contetns of wctype_t variable inside a VC6 program ?



#include <windows.h>
#include <wchar.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)

{

wctype_t a[]=L"Hello";
//MessageBox (0, a , a , 0) ;
return 0;
}
Nov 14, 2011 at 1:04pm
If your vc project is using the multi-byte char set, as the use of WinMain suggests, then:

1
2
3
4
5
    ...

    MessageBoxW (0, a , a , 0) ;

    ...


or

1
2
3
4
5
6
7
    ...

    char buffer[1024] = "";
    wsprintf(buffer, "%S", a); // a CAPITAL S
    MessageBox (0, buffer , buffer , 0) ;

    ...


etc...

Andy
Last edited on Nov 14, 2011 at 1:05pm
Nov 15, 2011 at 10:18am
I got that error message after I ran :

char buffer[1024] = "";

static wchar_t c[9]="Hello!WC";


wsprintf(buffer, "%S", c); // a CAPITAL S
MessageBox (0, buffer , buffer , 0) ;


Error message :
C:\programs\Vc_Studio6\AIS01\ch01\widecharacter04\widecharacter04.cpp(23) : error C2440: 'initializing' : cannot convert from 'char [9]' to 'unsigned short [9]'
There is no context in which this conversion is possible
Error executing cl.exe.

widecharacter04.exe - 1 error(s), 0 warning(s)
Last edited on Nov 15, 2011 at 10:27am
Nov 15, 2011 at 11:59am
use
1
2
#define UNICODE
#include <windows.h> 


-About your last error: The error seems in this line:
static wchar_t c[9]="Hello!WC";
use this
static wchar_t c[9] = L"Hello!WC";
Nov 16, 2011 at 7:13pm
As EssGeEich said, you need an L

But this fixes the case without UNICODE defined

1
2
3
4
5
6
7
8
9
// UNICODE not defined before windows.h
#include <windows.h>

char buffer[1024] = "";

static wchar_t c[9]=L"Hello!WC"; // with added L

wsprintf(buffer, "%S", c); // a CAPITAL S
MessageBox (0, buffer , buffer , 0) ;


With UNICODE defined most of the Windows API calls which take strings switch to use UNICODE

1
2
3
4
5
6
7
8
9
10
// with UNICODE not defined before windows.h
#define UNICODE
#include <windows.h>

wchar_t buffer[1024] = L""; // with added L

static wchar_t c[9]=L"Hello!WC"; // with added L

wsprintf(buffer, L"%s", c); // now a small s, and an added L
MessageBox (0, buffer , buffer , 0) ;


or even

1
2
3
4
5
6
7
8
9
10
11
12
// using TCHAR macro, etc
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <tchar.h>

TCHAR buffer[1024] = _T("");

static TCHAR c[9]=_T("Hello!WC");

wsprintf(buffer, _T("%s"), c);
MessageBox (0, buffer , buffer , 0) ;


TCHAR swaps to wchar_t when built with UNICODE/_UNICODE defined, and char otherwise.

And MessageBox is a macro which defines to MessageBoxW with UNICODE defined, and MessageBoxA otherwise.

Andy

P.S. UNICODE controls the Windows header, whereas _UNICODE controls the C run-time headers
Last edited on Nov 16, 2011 at 7:17pm
Topic archived. No new replies allowed.