得到exe运行目录


#include 
#include 
using namespace std;

//tchar 转 string
static string TCHAR2STRING(TCHAR* STR)
{

    int iLen = WideCharToMultiByte(CP_ACP, 0, STR, -1, NULL, 0, NULL, NULL);

    char* chRtn = new char[iLen*sizeof(char)];

    WideCharToMultiByte(CP_ACP, 0, STR, -1, chRtn, iLen, NULL, NULL);

    std::string str(chRtn);

    delete[] chRtn;

    return str;

}

//得到exe当前运行目录 路径
static int g_getCurDirPath(TCHAR* szPath)
{
    //TCHAR szPath[MAX_PATH];
    if (!GetModuleFileName(NULL, szPath, MAX_PATH))
    {
        printf("GetModuleFileName failed (%d)\n", GetLastError());
        return FALSE;
    }
    cout << "当前目录路径:" << endl;
    wcout.imbue(locale("chs")); // 如果是中文还要这样设置一下才能打印出来
    wcout << szPath << endl;
    string str(TCHAR2STRING(szPath));
    cout << str << endl;
}

你可能感兴趣的:(得到exe运行目录)