MFC
CString appPath; GetModuleFileName(NULL,appPath.GetBuffer(MAX_PATH),MAX_PATH); appPath.ReleaseBuffer(); appPath = appPath.Left( appPath.ReverseFind('\\') + 1);
|
win32
string getAppPath() { TCHAR path[MAX_PATH + 2] = { '\0' }; if (0 == GetModuleFileName(NULL, path, MAX_PATH)) { AfxMessageBox(TEXT("Get File Path Failed")); ASSERT(FALSE); } TCHAR* pf = _tcsrchr(path, '\\'); if (NULL == pf) { AfxMessageBox(TEXT("Invalide File Path")); ASSERT(FALSE); } *(pf + 1) = '\0';
string str = &path[0]; return str; }
|
Linux
int GetAppPath(std::string &sPath) { char buf[4097] = { 0 }; long size; char *ptr; size = pathconf(".", _PC_PATH_MAX); if ((ptr = new char(size)) != NULL) { memset(ptr, 0, size); sprintf(ptr, "/proc/%d/exe", getpid()); } else return -1; readlink(ptr, buf, size); delete[] ptr; ptr = NULL; sPath = buf; int nPos = sPath.find_last_of("/"); sPath = sPath.substr(0, nPos + 1); return 0; }
|