win32下使用相对exe文件的绝对路径资源
在使用VC++进行开发时,如果按F5进行Debug时,当前相对资源是相对工程的vcxproj的文件夹目录,而直接双击运行exe时,资源是相对exe的文件夹目录。为了兼容这二者,最好使用绝对路径,这样无法是Debug还是直接双击运行exe时都不会遇到无法加载资源的问题。
char* WcharToChar(const wchar_t* wp) { char *m_char; int len = WideCharToMultiByte(CP_ACP, 0, wp, wcslen(wp), NULL, 0, NULL, NULL); m_char = new char[len + 1]; WideCharToMultiByte(CP_ACP, 0, wp, wcslen(wp), m_char, len, NULL, NULL); m_char[len] = '\0'; return m_char; } wchar_t* CharToWchar(const char* c) { wchar_t *m_wchar; int len = MultiByteToWideChar(CP_ACP, 0, c, strlen(c), NULL, 0); m_wchar = new wchar_t[len + 1]; MultiByteToWideChar(CP_ACP, 0, c, strlen(c), m_wchar, len); m_wchar[len] = '\0'; return m_wchar; } //int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) int _tmain(int argc, _TCHAR* argv[]) { // https://stackoverflow.com/questions/1528298/get-path-of-executable HMODULE hModule = GetModuleHandleW(NULL); WCHAR path[MAX_PATH]; GetModuleFileNameW(hModule, path, MAX_PATH); // setting -> link add Shlwapi.lib #include <Shlwapi.h> PathRemoveFileSpec(path); //OutputDebugStringW(path); wcscat(path, L"\\main.lua"); char *pcstr = WcharToChar(path); return 0; }