-1

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?

Ngdgvcb
  • 155
  • 6
  • VAX 11-785 under VMS ? APL language ? –  Sep 21 '22 at 10:46
  • Never rely on the current working directory. There are too many ways this can be changed during the running of your program that are not under the program's control. Use the module path and string manipulation to create absolute paths to the resources you need. – Richard Critten Sep 21 '22 at 10:54
  • @RichardCritten, thanks but `SetCurrentDirectory` what do I need – Ngdgvcb Sep 21 '22 at 11:02
  • @YvesDaoust sorry, didn’t understand Your questions, however I already have the answer – Ngdgvcb Sep 21 '22 at 11:03
  • @Ngdgvcb other things can change the current working directory for example the Open File dialog and other things in the background. It is a well-known issue that is easily avoided. – Richard Critten Sep 21 '22 at 11:42
  • @Ngdgvcb Please have a read of [Old New Thing - The curse of the current directory](https://devblogs.microsoft.com/oldnewthing/20101109-00/?p=12323) – Richard Critten Sep 21 '22 at 11:52
  • 2
    SetCurrentDirectory may be the answer to the question you asked, but it isn't the solution to your problem. – David Heffernan Sep 21 '22 at 12:05
  • When you open a file via a relative path, that path is relative to your applications "cwd" (Current Working Directory). The cwd depends on how the application is launched, but can be changed at run time with functions such as `chdir` and `std::filesystem::current_path`. – Jesper Juhl Sep 21 '22 at 12:07

1 Answers1

1

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"

selbie
  • 100,020
  • 15
  • 103
  • 173
  • Thanks for feedback, but if You didn't notice I wrote that I already do it - *I need to specify paths to program's icons or something else stuff absolutely, i.e. module path + icon_name*. It is uncomfortable – Ngdgvcb Sep 21 '22 at 10:47
  • 1
    That's my point - GetModuleFileName will give you the absolute path to the EXE. You can do you a quick string fixup to create absolute paths to anything relative to your EXE location. – selbie Sep 21 '22 at 10:53
  • Yes, I do it exactly like that currently, but `SetCurrentDirectory` is just what do I need – Ngdgvcb Sep 21 '22 at 11:02
  • 3
    @Ngdgvcb You'll need to fix the code that apparently relies on the current working directory. There's no way to get code that relies on the current working directory to work *reliably*, like ever. If you find yourself in a situation where you feel like calling `SetCurrentDirectory` were a solution, you'll have to reconsider. It's not a solution to *any* problem. – IInspectable Sep 21 '22 at 11:50