开发者

C++ Portable way of programmatically finding the executable root directory [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

how to find the location of the executable in C

Hi,

I am looking for a portable way to find the root directory of a program (in C++). For instance, under Linux, a user could copy code to /opt, add it to the PATH, then execute it:

cp -r my_special_code /opt/
export PATH=${PATH}:/opt/my_special_code/
cd /home/tony/
execution_of_my_special_code

(where "execute_my_special_code" is the program in /opt/my_special_code).

Now, as a developer of "execution_of_my_special_code", is there a portable programmatical way of finding out that the executable is in /opt/my_special_code?

A second example is on MS Windows: What if my current working directory is on one hard drive (e.g. "C:\开发者_Python百科") and the executable is placed on another (e.g. "D:\")?

Ultimately, the goal is to read some predefined configuration files that are packaged with the program code without forcing the user to enter the installation directory.

Thanks a lot in advance!


There is no portable way of doing this. Neither the C nor C++ standard require the argv array to hold this information and there are no library routines in the standard libraries that provide it.

You will need to find a non-portable way of obtaining this information in a platform-specific way, such as GetModuleFileName under Windows or reading /proc/self/exe under Linux.

In any case, it's actually a bad idea to store configuration information with the executable since that means it's the same for all users. The UNIX way of doing this is to store program-specific information in the /etc tree and user-specific info in the user's home directory. For example, for your application called dodgybob, the user's configuration file could be called $HOME/.dodgybob or $HOME/.dodgybobrc.

For Windows, you should be storing program-specific information in the registry (HKLM) and user-specific information either in the registry (HKCU) or in the user's own areas (e.g., Documents & Settings\<username>\Local Settings although the right way to do that is with SHGetSpecialFolderPath passing CSIDL_APPDATA or (Vista and later) with known folder IDs, though the old method still works on Vista as a stub function).


There is no ISO c++ code for this but you could do a conditional compiling and use _getcwd (Windows) or getcwd (Linux):

#define GetCurrentDir _getcwd
std::string SettingsHandler::getFullPath() {
    char chPath[2048];

    if (!GetCurrentDir(chPath, sizeof(chPath))) {
        return "ERROR";
    }

    chPath[sizeof(chPath) - 1] = '/0';

    return chPath;
}

Since you want to do cross-platform-programming you should store your settings/configurations in a xml file. This way you don't have to create two parser (one for the registry and another one for a linux solution) and you don't have to deal with different windows versions!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜