0

If this has already been answered elsewhere, I'm sorry, I couldn't find it.

I have an interesting problem whereby I have a compiled program, prog.exe, which reads a file prog.cfg which contains the config. If I open prog.exe by double-clicking on it, everything's good. However, if I open it from a command line or batch file, I first have to set the directory. For example, if I have my program in a progs folder on the desktop, and from the desktop run progs\prog.exe, it doesn't load the config because it's looking for the config on the desktop ie in the current working directory.

This is fine if you know about it, but it's just another hoop for users to jump through. Is there a way in c++ to backtrack to the command used to launch the program to deduce whether the config file will be in the current working directory or not?

Alternatively, am I asking completely the wrong question?

Many thanks!

srthompers
  • 169
  • 8
  • When you open it by double clicking on it the current directory is the one containing the executable. In a batch file the current directory need not be that folder. If this is your program you should be able to add logic to find the configuration file regardless of what the current folder is. If it is not you can make a batch file set the current directory before executing prog.exe. – drescherjm Jan 11 '16 at 15:01
  • The following should help (and likely some would consider this question to be a dupe of it): http://stackoverflow.com/questions/124886/how-to-get-the-application-executable-name-in-windowsc-cli – Andrew Henle Jan 11 '16 at 15:19
  • @drescherjm I know that's the issue, I was asking for help with fixing it, which I've just succeeded in doing :) – srthompers Jan 11 '16 at 16:28

2 Answers2

3

You can use GetModuleFileName(nullptr, buf, bufsize) to get a path to the executable.

Note that the standard main function's argv[0] is not guaranteed to provide that path, and when it provides a path, is not guaranteed to provide a programmatically usable representation of the path.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

if i understand you correctly, you always want your .exe read your .cfg file from the folder your .exe located? right?

try argv[0].

argv[0] is your full .exe path when you run your exe, and it is a parameter of main function.

0xFFFFFFFF
  • 852
  • 5
  • 9
  • It usually *isn't* the full path. – molbdnilo Jan 11 '16 at 15:26
  • 1
    In Windows, `main` arguments are ANSI-encoded, and so even when `argv[0]` contains a representation of the full path, it doesn't necessarily provide a faithful (programmatically usable) representation. Using `argv[0]` can work with a `wmain`, which is a common Windows extension. However, not all WIndows compilers support `wmain`. – Cheers and hth. - Alf Jan 11 '16 at 15:46
  • As already pointed out, it isn't always the full path. If you use the wide character version, however, it is usually a valid path relative to the current directory. (But it doesn't have to be, so GetModuleFileName is a better choice.) – Harry Johnston Jan 11 '16 at 21:43