Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Reading and writing files to the System Directory in Windows Vista using C++, MFC and fopen

Reading and writing files to the System Directory in Windows Vista using C++, MFC and fopen

Scheduled Pinned Locked Moved C / C++ / MFC
c++questioncsharpvisual-studiobusiness
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    D_code_writer
    wrote on last edited by
    #1

    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

    D C R 3 Replies Last reply
    0
    • D D_code_writer

      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

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • D D_code_writer

        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

        C Offline
        C Offline
        Covean
        wrote on last edited by
        #3

        As normal user you will not get access to the system directory of vista. On vista and above many security issues changed! Try your to start your exe "as adminstrator" and you will see it will work.

        Greetings Covean

        1 Reply Last reply
        0
        • D D_code_writer

          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

          R Offline
          R Offline
          Rolf Kristensen
          wrote on last edited by
          #4

          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.

          D 1 Reply Last reply
          0
          • R Rolf Kristensen

            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.

            D Offline
            D Offline
            D_code_writer
            wrote on last edited by
            #5

            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

            R 1 Reply Last reply
            0
            • D D_code_writer

              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

              R Offline
              R Offline
              Rolf Kristensen
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups