c++支持UTF-16字符集,对UTF-16字符的读写通过std::wstring实现。下面的例子展示如何获取exe文件的绝对路径(其中包含汉字),并且显示在界面上。下面的代码受了WarZone2100源码main.cpp 函数getCurrentApplicationPath_WIN() 的启发。
// WStringTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include
#include
#include
#include
#include
int main()
{
std::wcout.imbue(std::locale("chs"));//必须加这一句,才能输出有汉字的wstring。
std::vector buffer(1024, 0);
short sNameLen = GetModuleFileNameW(NULL, &buffer[0], buffer.size() - 1);
buffer[sNameLen] = 0;
std::wcout<
代码在vs2012中编译,且字符集是UNICODE:
效果: