在windows环境中用vs2015编译程序后,函数GetModuleFileName获取到的路径使用printf打印出来只有磁盘符,而不是整个路径。vs2015编译时设置的字符集是unicode,(其中:Unicode通常用两个字节表示一个字符,这是与ASCII编码不同的地方,网上很多资料有描述)我遇到问题是,用GetModuleFileName获取到路径后,还想保存到char型的字符数组中,网上关于怎么打印出来有很多资料,但是很少有简单解释怎么保存的,下面附上我的打印和保存的代码段。
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <atlstr.h>
using namespace std;
string UnicodeToUtf8(const wstring& wstr) {
// 预算-缓冲区中多字节的长度
int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
// 给指向缓冲区的指针变量分配内存
char *pAssii = (char*)malloc(sizeof(char)*ansiiLen);
// 开始向缓冲区转换字节
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);
string ret_str = pAssii;
free(pAssii);
return ret_str;
}
char* UnicodeToAnsi(const wchar_t* szStr)
{
int nL