Q: How to get the application directory?
A:
Code:
// With STL string
#include <string>
char szAppPath[MAX_PATH] = "";
std::string strAppDirectory;
::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);
// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.substr(0, strAppDirectory.rfind("\\"));
// With CString
char szAppPath[MAX_PATH] = "";
CString strAppDirectory;
::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);
// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.Left(strAppDirectory.ReverseFind('\\'));
// With standard string
char szAppPath[MAX_PATH] = "";
char szAppDirectory[MAX_PATH] = "";
::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);
// Extract directory
strncpy(szAppDirectory, szAppPath, strrchr(szAppPath, '\\') - szAppPath);
szAppDirectory[strlen(szAppDirectory)] = '\0';