我正在创建一个从当前exe
文件中获取目录的函数,当我在函数ExePath
中使用变量时,我得到了正确的目录,但是如果我使用返回值从另一个函数中的函数ExePath
,我得到日文字符。这是我的代码:
#include <Windows.h>
#include <iostream>
#include <string>
#include <wchar.h>
using namespace std;
LPCWSTR ExePath();
int _tmain(int argc, _TCHAR* argv[])
{
// ------ > HERE SHOWS JAPANESE CHARACTERS < -------------------------
MessageBox(NULL, ExePath(), L"Inside main function", MB_OK); // Here show foreign characters
return 0;
}
// This function returns the directory of this current exe file
LPCWSTR ExePath()
{
// Get Exe Path
WCHAR* buffer = new WCHAR[MAX_PATH];
DWORD len = GetModuleFileName(GetModuleHandle(NULL), buffer, MAX_PATH );
buffer[len] = L'\0';
// Get only the directory (without the file name)
wstring ws(buffer);
const size_t last_slash_idx = ws.rfind('\\');
if (std::string::npos != last_slash_idx)
{
ws = ws.substr(0, last_slash_idx);
}
// Show the directory
// ws.c_str() = directory
// ------ > HERE SHOW THE CORRECT DIRECTORY < ------------------------
MessageBox(NULL, ws.c_str(), L"Inside ExePath Function", MB_OK); // Here show the correct directory
return ws.c_str();
}
我做错了什么?