old drawer/C, C++, MFC
[C++] 프로그램 실행파일의 경로 가져오기
A.Step
2012. 11. 30. 14:03
ini 파일을 읽어들여 프로그램을 셋팅하거나 할 때, 간혹 실행파일의 경로가 필요해질때가 있다. 이런 경우 아래의 소스를 이용하면 실행파일명을 제외한 나머지 경로를 구해올 수 있다.
//++++++ 실행파일의 경로를 Get ++++++//
CString strFolderPath;
// 현재 프로그램의 실행 파일이 있는 폴더명을 추출함
::GetModuleFileName(NULL, strFolderPath.GetBuffer(MAX_PATH), MAX_PATH);
strFolderPath.ReleaseBuffer();
if (strFolderPath.Find('\\') != -1) {
for (int i = strFolderPath.GetLength() - 1; i >= 0; i--) {
TCHAR ch = strFolderPath[i];
strFolderPath.Delete(i);
if (ch == '\\') break;
}
}
//++++++ 실행파일과 동일한 경로에 위치한 ini 파일의 경로를 Set++++++//
m_ini.SetPathName(strFolderPath + L"\\ini.INI");