Can relative path be changed?

Viewed 67

If open some file using my program it seems, that the current relative path is set by shell not to the executable folder but to the file's folder.

It is uncomfortable for me, because I need to specify paths to program's icons or something else stuff absolutely, i.e. module path + icon_name.

Can it be done?

1 Answers

Just invoke GetModuleFileName and pass NULL as the first parameter to get the path to your EXE. Then do the appropriate string parsing or use Shell APIs to convert that to the path to your module.

wchar_t szPathToMyExe[MAX_PATH];
GetModuleFileName(nullptr, szPathToMyExe, MAX_PATH);
wstring path = szPathToMyExe;            // "C:\Path\To\My\Program.exe"
auto pos = strPath.find_last_of("\\");
wstring directory = path.substr(pos);    // "C:\Path\To\My"

Related