Reading and writing files to the System Directory in Windows Vista using C++, MFC and fopen
-
Hey Guys, I have an intresting problem for everyone to consider. One of my software projects reads and writes files to the system directory. It automatically detects the location of the Windows system directory, and then uses fopen to either read or write a file depending on what is required. So here is the conundrum. These techniques work like a charm on my projects that I programmed using Visual C++ v6.0. No problem what so ever. I recently ported this project to Visual Studio 2008 using C++ and MFC, and for XP business as usual. However for Vista, disaster. The nature of the problem is that when I use fopen it can't open the file in the windows system directory. Basically I get (fp = fopen( m_keyPath, "w" or "r" )) == NULL. Here's the twist in the tale. Any where else it's fine. It's just the windows system directory. Any ideas?? Best Regards Danny
-
Hey Guys, I have an intresting problem for everyone to consider. One of my software projects reads and writes files to the system directory. It automatically detects the location of the Windows system directory, and then uses fopen to either read or write a file depending on what is required. So here is the conundrum. These techniques work like a charm on my projects that I programmed using Visual C++ v6.0. No problem what so ever. I recently ported this project to Visual Studio 2008 using C++ and MFC, and for XP business as usual. However for Vista, disaster. The nature of the problem is that when I use fopen it can't open the file in the windows system directory. Basically I get (fp = fopen( m_keyPath, "w" or "r" )) == NULL. Here's the twist in the tale. Any where else it's fine. It's just the windows system directory. Any ideas?? Best Regards Danny
Danny Nowlan wrote:
These techniques work like a charm on my projects that I programmed using Visual C++ v6.0.
The problem has nothing to do with VS6. Your program should not be writing/crearing files in the \Windows\System32 folder. That folder has been off limits for years, but since it has not been enforced, folks have continued to abuse it. Use
SHGetFolderPath()
to get the path of a folder that you can write to. For example,CSIDL_COMMON_APPDATA
,CSIDL_LOCAL_APPDATA
,CSIDL_COMMON_DOCUMENTS
,CSIDL_PERSONAL
."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Hey Guys, I have an intresting problem for everyone to consider. One of my software projects reads and writes files to the system directory. It automatically detects the location of the Windows system directory, and then uses fopen to either read or write a file depending on what is required. So here is the conundrum. These techniques work like a charm on my projects that I programmed using Visual C++ v6.0. No problem what so ever. I recently ported this project to Visual Studio 2008 using C++ and MFC, and for XP business as usual. However for Vista, disaster. The nature of the problem is that when I use fopen it can't open the file in the windows system directory. Basically I get (fp = fopen( m_keyPath, "w" or "r" )) == NULL. Here's the twist in the tale. Any where else it's fine. It's just the windows system directory. Any ideas?? Best Regards Danny
-
Hey Guys, I have an intresting problem for everyone to consider. One of my software projects reads and writes files to the system directory. It automatically detects the location of the Windows system directory, and then uses fopen to either read or write a file depending on what is required. So here is the conundrum. These techniques work like a charm on my projects that I programmed using Visual C++ v6.0. No problem what so ever. I recently ported this project to Visual Studio 2008 using C++ and MFC, and for XP business as usual. However for Vista, disaster. The nature of the problem is that when I use fopen it can't open the file in the windows system directory. Basically I get (fp = fopen( m_keyPath, "w" or "r" )) == NULL. Here's the twist in the tale. Any where else it's fine. It's just the windows system directory. Any ideas?? Best Regards Danny
Could you explain why you need to read/write files in the System32 directory ? There is a reason why Microsoft tries to protect its system directories.
-
Could you explain why you need to read/write files in the System32 directory ? There is a reason why Microsoft tries to protect its system directories.
Hey Snakefoot, I use it as a convenient location to keep operating files without bothering with the registry. Let me give an assurance that I am not in the business of writing viruses or Malware. They may have the best of intentions, but people like this cause alot more harm than they do good. Danny
-
Hey Snakefoot, I use it as a convenient location to keep operating files without bothering with the registry. Let me give an assurance that I am not in the business of writing viruses or Malware. They may have the best of intentions, but people like this cause alot more harm than they do good. Danny
When you mean operating file, then I guess you mean data files that belongs to your application. Such files should not be placed in the Windows-directory. What if another application suddenly decides to also use this location, and by mistakes uses your filename-format ? You should read this guide from Microsoft Data and Settings Management[^] (Though written for Win2k, then it also holds for Vista/Win7)
TCHAR szAppData[MAX_PATH];
...
hr = SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, szAppData);
PathAppend(szAppData, "Company\Product\File.txt")- CSIDL_APPDATA - Per user, roaming [user profile]\Application data
- CSIDL_LOCAL_APPDATA - Per user, non-roaming [user profile]\Local Settings\Application data
- CSIDL_COMMON_APPDATA - Hidden folder for normal users. Per machine (non-user specific & non- roaming) All Users\Application data
- CSIDL_COMMON_DOCUMENTS - Visible folder for normal users. Default files in this folder is readonly for other users than the creator, the application must change the security permissions on its application-folder to change this behavior. Per machine (non-user specific & non- roaming) All Users\Documents
modified on Monday, December 14, 2009 10:39 AM