#include "Utils.h" Utils::Utils() { } Utils::~Utils() { } Utils &Utils::instance() { static Utils m_instance; return m_instance; } // 不要忘记在使用完wchar_t*后delete[]释放内存 WCHAR* Utils::A2W(const CHAR* ansi) { // 第一次调用返回转换后的字符串长度,用于确认为wchar_t*开辟多大的内存空间 int size = MultiByteToWideChar(CP_OEMCP, 0, ansi, strlen(ansi) + 1, NULL, 0); WCHAR* buffer = new WCHAR[size]; // 第二次调用将单字节字符串转换成双字节字符串 MultiByteToWideChar(CP_OEMCP, 0, ansi, strlen(ansi) + 1, buffer, size); return buffer; } // 不要忘记使用完char*后delete[]释放内存 CHAR* Utils::W2A(const WCHAR* unicode) { //第一次调用确认转换后单字节字符串的长度,用于开辟空间 int length = WideCharToMultiByte(CP_OEMCP, 0, unicode, wcslen(unicode), NULL, 0, NULL, NULL); char* buffer = new char[length + 1]; //第二次调用将双字节字符串转换成单字节字符串 WideCharToMultiByte(CP_OEMCP, 0, unicode, wcslen(unicode), buffer, length, NULL, NULL); buffer[length] = '\0'; return buffer; //如果想要转换成string,直接赋值即可 //string pKey = pCStrKey; }