場景
wchar[]轉換string
實現代碼
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
// wchar_t to string
void Wchar_tToString(std::string& szDst, wchar_t *wchar)
{
wchar_t * wText = wchar;
DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的運用
char *psText; // psText為char*的臨時數組,作為賦值給std::string的中間變量
psText = new char[dwNum];
WideCharToMultiByte (CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次運用
szDst = psText;// std::string賦值
delete []psText;// psText的清除
}
調用
// 存放當前程序運行路徑
WCHAR SelfFile[MAX_PATH];
//獲取當前進程路徑
GetModuleFileName(NULL, SelfFile, MAX_PATH);
// 當前程序存放路徑
string Current_Path;
Wchar_tToString(Current_Path,SelfFile);
參考
STRING轉WCHAR 和WCHAR 轉STRING
https://blog.csdn.net/sinat_35261315/article/details/72636712