gusucode.com > VC++字符转换操作的例子-源码程序 > VC++字符转换操作的例子-源码程序/code/day02_05/codes/WinChar/WinChar.cpp

    //Download by http://www.NewXing.com
// WinChar.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "stdlib.h"

/*
int WINAPI MessageBoxA(
    HWND hWnd ,
    LPCSTR lpText,
    LPCSTR lpCaption,
    UINT uType);

int WINAPI MessageBoxW(
    HWND hWnd ,
    LPCWSTR lpText,
    LPCWSTR lpCaption,
    UINT uType);

#ifdef UNICODE
#define MessageBox  MessageBoxW
#else
#define MessageBox  MessageBoxA
#endif // !UNICODE
*/
void MyMessageBox( )
{
	MessageBox( NULL, TEXT("Hello Wide"),
		TEXT("Wide"), MB_OK );
}

void Wide2Multi( )
{
	WCHAR * pwszText = L"Wide2Multi";
	//计算转换后的字符串长度
	int nLen = WideCharToMultiByte( 
		CP_ACP, 0, pwszText, wcslen(pwszText),
		  NULL, 0, NULL, NULL );
	//分配内存
	char * pszText = (char *)malloc( nLen );
	//获取结果
	WideCharToMultiByte( 
		CP_ACP, 0, pwszText, wcslen(pwszText),
		pszText, nLen, NULL, NULL );
	//
	MessageBoxA( NULL, pszText, "Multi", MB_OK );
	free( pszText );
}

void Multi2Wide( )
{
	CHAR * pszText = "Multi2Wide";
	//获取转换后需要的BUFF的长度
	int nLen = MultiByteToWideChar( CP_ACP,
		0, pszText, strlen(pszText),
		NULL, 0 );
	//分配BUFF的空间
	WCHAR * pwszText = 
		(WCHAR *)malloc( nLen * sizeof(WCHAR) );
	//进行转换
	MultiByteToWideChar( CP_ACP, 
		0, pszText, strlen(pszText),
		pwszText, nLen );
	
	MessageBoxW( NULL,pwszText, 
		L"Wide", MB_OK );
	free( pwszText );
}

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	//Multi2Wide( );
	Wide2Multi( );
	return 0;
}