Unicode字符集环境下亲测可用:
//------------------------------------------------------------------宽字符串转换到窄字符串
char* pC = NULL;
wchar_t wStr[20] = L"宽字符串";
int iLen = WideCharToMultiByte( CP_ACP,0,wStr,-1,NULL,0,NULL,NULL);
if( iLen > 0 )
{
pC = ( char* )HeapAlloc( GetProcessHeap() ,0 ,iLen );
if( !pC ) return;
WideCharToMultiByte( CP_ACP ,0 ,wStr ,-1 ,pC ,iLen ,NULL ,NULL );
printf( "%s \n", pC );
HeapFree( GetProcessHeap() ,0 ,pC );
}
//------------------------------------------------------------------窄字符串转换到宽字符串
char cStr[20] = "这是窄字符串";
wchar_t* pWideString = NULL;
int iLenWide = MultiByteToWideChar( CP_ACP ,0 ,cStr ,-1 ,NULL ,0 );
if ( iLenWide > 0 )
{
pWideString = ( wchar_t* )malloc( iLenWide * sizeof(wchar_t) );
if( !pWideString ) return 0;
MultiByteToWideChar( CP_ACP ,0 ,cStr ,-1 ,pWideString ,iLenWide );
MessageBox( NULL, pWideString , 0 , 0 );
free( pWideString );
}