views:

94

answers:

5

I wrote this C++ application that needs to check an INI file (“preference.ini”), and eventually modify it (e.g. if the user does not want to see the introduction form anymore). I created it in WinXP, and it works fine on the system where I compiled it (in many locations, including “Program Files”).

Problem:

  1. In Win 7, it works fine if I put the complete program folder under “C”:\” (e.g. “C:\MyProgram”), but if I put it in “C:\Program Files (x86)\MyProgram”, it just retrieves some mysterious data (values not present in my INI file). When I change some settings and save them to file, it (apparently) save the changes (get no errors, but the changes are not there when I go and open the file...
  2. I had some similar issue on a system with another WinXP system (not the one where I compiled it.

I used 'getcwd' to define the path at runtime, and I verified that it is getting it right, even under "Program Files (x86)":

char currentPath[MAXPATH];
getcwd(currentPath, MAXPATH);
std::string licensePath(currentPath);
licensePath.append("\\dat\\preference.ini");'

Any ideas? Thanks in advance for your help.

+2  A: 

It could be related to that Windows use virtualization of the file system. You could read here about it. Check if your INI file is located in <root>\Users\<User_name>\AppData\Local\VirtualStore.

Kirill V. Lyadvinsky
+6  A: 

The answer is as @Kirill has already said - Win7 won't let you write data into Program Files unless you have higher than normal permissions (Run as Administrator). In this case it may be redirecting your file writes so that they still apear to work, but the data itself is not stored in Progam Files.

To add to his answer: In general (unless you want to run your app as an administrator), you should not write any program data to the Program Files folder.

Application settings should be stored in one of the AppData folders. You can get to your user's appdata manually by going to your start menu Search box (Vista/Win7) and typing %appdata%.

To find this location in your code, use SHGetFolderPath with CSIDL_APPDATA (current user) or CSIDL_COMMON_APPDATA (all users).

Jason Williams
Is that C++? It looks like C# code
Default
@Michael: Oops, sorry. Forgot to check the language! Corrected to describe the C++ code needed. Thanks for pointing that out.
Jason Williams
A: 

Seems to me that the licensePath: getcwd() + "\\dat\\preference.ini" is not what you would expect.

Log this value (console or in a log file) and see what exactly is the value of licencePath is when running you program from different folders.

Ando
A: 

This article is about game development but has the best description of how and why this happens that I've been able to find

http://msdn.microsoft.com/en-us/library/ee419001(VS.85).aspx

This paragraph from the article describes what is happening most likely -

Attempting to create or write a file or directory under a folder which does not grant write permission to the process will fail under Windows Vista if the application does not have administrative privileges. If your 32-bit game executable is running in legacy mode, because it did not declare a requested execution level, its write operations will succeed, but they will be subjected to virtualization as described in the section "UAC Compatibility with Older Games" later in this article.

John Burton
A: 

Thank you all for your prompt and useful answers. Problem solved...you made my day. I got to register to StackOverflow... this is an excellent forum.

Cheers.

      Aba
Aba