Try hard coding it, the access violation is probably coming from asking the system for information about a file that the user may or may not have permissions to know about. if you need a more dynamic solution try using an environment variable that refers to the location of the file or the users "home" folder
Don't put the ini file along the application /unless you really have to/. The common user, even the administrator /when app not explicitly elevated/ has no right to write into the Program Files folder. Use environment var %ProgramData% if you want to write the ini accessible for all users, and use env var %USERPROFILE%\AppData\Roaming if you want to write user specific data accessible only by the current user. You can use also "SHGetFolderPath" in order to obtain these folder via API.
Here's a function I wrote to get the Application Data folder in C++Builder.
If you're using older versions of C++Builder, you might find you have to change this to use AnsiStrings instead of Unicode (replace the "UnicodeString
"s with "AnsiString
"s, and change the call to "SHGetSpecialFolderPathW
" to read "SHGetSpecialFolderPath
").
GetAppDataFolder.h:
#ifndef GetAppDataFolderH
#define GetAppDataFolderH
UnicodeString GetAppDataFolder(bool roaming = true);
#endif
GetAppDataFolder.cpp:
// Helper function to get the location of the current user's Application Data folder (used for
// storing per-user application settings).
#include <vcl.h>
#pragma hdrstop
/* roaming: True for application data that can be accessed by the same user on different
machines. If you have per-user settings that are only relevant to a particular
computer, e.g., screen resolution, set 'roaming' to false.
*/
UnicodeString GetAppDataFolder(bool roaming /* = true */)
{
UnicodeString retVal;
int csidl = roaming ? CSIDL_APPDATA : CSIDL_LOCAL_APPDATA;
wchar_t thePath[MAX_PATH];
if (SHGetSpecialFolderPathW(NULL, thePath, csidl, 0) == TRUE) {
retVal = thePath;
}
return retVal;
}
Any reasons for/against storing your app configuration in the registry? I'm not suggesting you redo the code that brought up the question, just curious for my own future projects.