Entries tagged Visual Studio

Conversion between UTF-16 and UTF-8 in C++

Posted on Jun 5, 2014

#include <fstream>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>

// Unicode representation in MS Windows uses the 2-byte wchar_t type.
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utfconv;

// string conversion
std::wstring wide = L"Hello, World! 안녕하세요?";  // wide string with utf-16 encoding
std::string narrow = utfconv.to_bytes(wide);     // conversion from utf-16 to utf-8
wide = utfconv.from_bytes(narrow);               // back from utf-8 to utf-16

// conversion during file I/O
std::wofstream fout;                             // wide output stream
fout.open("test.txt", fout.out);
fout.imbue(std::locale(fout.getloc(), new std::codecvt_utf8_utf16<wchar_t>));
fout << wide << std::endl;                       // this stream is stored as utf-8
fout << utfconv.from_bytes(narrow) << std::endl; // the same as the above line

std::wifstream fin;
fin.open("test.txt", fin.in);
fin.imbue(std::locale(fin.getloc(), new std::codecvt_utf8_utf16<wchar_t>));
std::wstring hello, world, anyoung, tline;
fin >> hello >> world >> anyoung;      // utf-8 stream is converted to utf-16 string
std::getline(fin, tline);              // read out the end of the line
std::getline(fin, tline);              // read the next line

Printing Unicode strings in Windows console

Posted on May 12, 2014

Win32 Console Applications do not display Unicode strings properly. There is a simple solution of using _setmode(). You need to include two headers.

#include <io.h>
#include <fcntl.h>

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
	_setmode(_fileno(stdout), _O_U16TEXT);

	wcout << L"안녕하세요?" << endl;
	// or
	// wprintf(L"%s\n", L"안녕하세요?");

	return 0;

MFC support for MBCS deprecated in Visual Studio 2013

Posted on Nov 18, 2013

It is time to move onto Unicode. The multi-byte character set (MBCS) in MFC will not be supported in the future versions of Visual Studio. You can still compile the MBCS projects with VS2013, but you need to install the MBCS libraries via a separate download, which is available here. You will see a deprecation warning, when an application is built using MBCS, though. This warning can be suppressed by adding the NO_WARN_MBCS_MFC_DEPRECATION preprocessor definition.

For more information, see http://blogs.msdn.com/b/vcblog/archive/2013/07/08/mfc-support-for-mbcs-deprecated-in-visual-studio-2013.aspx.

LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt

Posted on May 20, 2013

After VS2012 is installed, VS2010 may fail with the LNK1123 in compiling some of old projects that were perfectly fine previously. This seems to have something to do with .NET framework 4.5 which comes with VS2012. To fix this problem, install VS2010 sp1. If it is not possible, try the following.

It turned out that I had two versions of this utility in my path. One at C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\cvtres.exe and one at C:\Windows\Microsoft.NET\Framework\v4.0.30319\cvtres.exe. After VS2012 install, the VS2010 version of cvtres.exe will no longer work. If that’s the first one in your path, and the linker decides it needs to convert a .res file to COFF object format, the link will fail with LNK1123. Just delete/rename the older version of the utility, or re-arrange your PATH variable, so that the version that works comes first. see the original post

Visual Studio 2012 and DirectX SDK

Posted on Jan 12, 2013

The DirectX SDK is included as part of the Windows SDK, starting from Windows 8. However, D3DX is not considered the canonical API for using Direct3D in Windows 8 and therefore isn’t included in the Windows SDK shipped with Visual Studio 2012. So, when you compile an old project which depends on D3DX, you still need to get “d3dx9.h”, “d3dx10.h”, or “d3dx11.h” from the June 2010 DirectX SDK. This causes a confliction between the June 2010 DirectX SDK and the DirectX SDK included in the Windows SDK (ver 8.0). For example, you will see a bunch of warning C4005 like the following.