I'm creating a function for get the directory from the current exe
file, when I use the variable inside the function ExePath
, I get the correct directory, but if I use the return value from the function ExePath
in another function, I get Japanese characters. This is my code:
#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();
}
What am I doing wrong?