Q: current directory in Windows
Posted: Fri Feb 01, 2013 9:47 am
I ran into an annoying problem in WinBoard that I don't understand. WinBoard is supposed to interpret all filenames (e.g. of engine directories, GUI book, position file) relative to the folder it is installed in. I therefore configure WinBoard in the installer such that all filenames specified in the various ini files are relative path names, so that the user can install it in whatever folder he wants.
To make this work, WinBoard uses a variable 'installDir' to translate any relative filenames to absolute paths. The installDir path is obtained at startup by calling the MicroSoft function SearchPath with a NULL argument for the path.
The problem is that this does not always work! Especially now that I am creating an installer including the 'portable' version of WinBoard, where you start WinBoard in the various modes by dragging files on top of its icon. I then often get complaints that INI files containing the settings for the mode WinBoard needs to start in for processing that file type (e.g. a PGN or a tourney file) can not be opened. Sometimes it works, but specifically when I used that WinBoard to point at a file in another folded in the browser dialog (e.g. for installing another board texture), the drag & drop startup always fails. I figured out that this is because it is then looking for them in the folder from which you last pointed out a file by browsing: the winboard.debug file is created there.
This situation continues to exist even when I close WinBoard and restart it. But when I let WinBoard print the value of 'installDir' in its debug file, it does print the path name of the folder where it is installed. So it seems I can cure the problem by doing a SetCurrentDirectory to the installDir that is retrieved by SearchPath.
The problem is that I don't understand why this is needed. If the installDir isn't already the current directory, what criterion does Windows use to decide what to return for it with SearchPath? I have many files called winboard.exe on my computer, in many different folders. How can it know which one to return if the current directory is in a completely unrelated place. Are their multiple 'current directories' on Windows? Is SearchPath a safe function to use for this?
I noticed this on Windows 7.
To make this work, WinBoard uses a variable 'installDir' to translate any relative filenames to absolute paths. The installDir path is obtained at startup by calling the MicroSoft function SearchPath with a NULL argument for the path.
The problem is that this does not always work! Especially now that I am creating an installer including the 'portable' version of WinBoard, where you start WinBoard in the various modes by dragging files on top of its icon. I then often get complaints that INI files containing the settings for the mode WinBoard needs to start in for processing that file type (e.g. a PGN or a tourney file) can not be opened. Sometimes it works, but specifically when I used that WinBoard to point at a file in another folded in the browser dialog (e.g. for installing another board texture), the drag & drop startup always fails. I figured out that this is because it is then looking for them in the folder from which you last pointed out a file by browsing: the winboard.debug file is created there.
This situation continues to exist even when I close WinBoard and restart it. But when I let WinBoard print the value of 'installDir' in its debug file, it does print the path name of the folder where it is installed. So it seems I can cure the problem by doing a SetCurrentDirectory to the installDir that is retrieved by SearchPath.
Code: Select all
if (SearchPath(NULL, "WinBoard.exe", NULL, MSG_SIZ, installDir, &filepart)) {
*filepart = NULLCHAR;
SetCurrentDirectory(installDir); // [HGM] newly added
} else {
GetCurrentDirectory(MSG_SIZ, installDir);
}
I noticed this on Windows 7.