I am developing an application which stores its configuration in an INI file in the same directory as the executable. So I need to get the executable directory and build the path for the INI file.
Some WIN32 syscalls are available for this job: GetModuleFileName, PathRemoveFileSpec and PathAppend. PathRemoveFileSpec and PathAppend are defined in shlwapi.h, which IS available in the SDK. So I expect the shlwapi.lib to be available, too, right? But it isn't, so a workaround is needed.
Here's the code needed to build the new path, based on the absolute path obtained from GetModuleFileName. Some error handling is provided, but error shouldn't really happen. GetModuleFileName returns an absolute path, so I'm sure that at least one '\' will be found.
#include <cstring>
#include <windows.h>
wchar_t Path[MAX_PATH];
if (0 == ::GetModuleFileName(NULL, Path, MAX_PATH)) {
//Error that shouldn't happen
::wcscpy(Path, L"IniFile.ini");
} else {
//Find the last '\' in the absolute path
wchar_t* pos = ::wcsrchr(Path, L'\\');
if (!pos) //Error, '\' not found
pos = Path;
else
pos++;
::wcscpy(pos, L"IniFile.ini");
}
|
|
October '06 |
|
||||
| Mo | Tu | We | Th | Fr | Sa | Su |
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 | |||||
2006-2008 Giuseppe 'Cowo' Corbelli | Cowo's homepage | Cowo's photo page | Contact me | Back to top
Design by Andreas Viklund | Serendipity Template by Carl