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. fopen fails to give error when UAC is enabled

fopen fails to give error when UAC is enabled

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 5 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.
  • S Offline
    S Offline
    sindhumahe
    wrote on last edited by
    #1

    Hi, I have a use case to validate an path that is given as an input to my application. I tried to create a file in the path with fopen and added the following check : FILE *fp= fopen(Input path, "w"); if( fp == NULL) { //Invalid path } else { //Valid path } This works well for cases when the input path is invalid or not present. But in certain cases, when UAC is enabled, a file cannot be created in the C:\Program files location. So in that scenario, if my input path is C:\Program files, fopen doesnt return any error and above code executes the else part, which is wrong. Can anyone please provide suggestions about this issue? Sindhu

    C D C B 4 Replies Last reply
    0
    • S sindhumahe

      Hi, I have a use case to validate an path that is given as an input to my application. I tried to create a file in the path with fopen and added the following check : FILE *fp= fopen(Input path, "w"); if( fp == NULL) { //Invalid path } else { //Valid path } This works well for cases when the input path is invalid or not present. But in certain cases, when UAC is enabled, a file cannot be created in the C:\Program files location. So in that scenario, if my input path is C:\Program files, fopen doesnt return any error and above code executes the else part, which is wrong. Can anyone please provide suggestions about this issue? Sindhu

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      How about using PathIsDirectory[^]?

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > If it doesn't matter, it's antimatter.<

      S 1 Reply Last reply
      0
      • C Code o mat

        How about using PathIsDirectory[^]?

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > If it doesn't matter, it's antimatter.<

        S Offline
        S Offline
        sindhumahe
        wrote on last edited by
        #3

        Thanks. But PathIsDirectory() might not tell me if the user has write access to that path right?

        C 1 Reply Last reply
        0
        • S sindhumahe

          Thanks. But PathIsDirectory() might not tell me if the user has write access to that path right?

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          Ah, you didn't mention that, or i missed it, in which case, sorry. Read this[^], maybe you are hitting the same thing.

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > If it doesn't matter, it's antimatter.<

          1 Reply Last reply
          0
          • S sindhumahe

            Hi, I have a use case to validate an path that is given as an input to my application. I tried to create a file in the path with fopen and added the following check : FILE *fp= fopen(Input path, "w"); if( fp == NULL) { //Invalid path } else { //Valid path } This works well for cases when the input path is invalid or not present. But in certain cases, when UAC is enabled, a file cannot be created in the C:\Program files location. So in that scenario, if my input path is C:\Program files, fopen doesnt return any error and above code executes the else part, which is wrong. Can anyone please provide suggestions about this issue? Sindhu

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

            sindhumahe wrote:

            if my input path is C:\Program files, fopen doesnt return any error and above code executes the else part, which is wrong.

            Just for grins, have you tried using fp in the else part?

            "One man's wage rise is another man's price increase." - Harold Wilson

            "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

            "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

            1 Reply Last reply
            0
            • S sindhumahe

              Hi, I have a use case to validate an path that is given as an input to my application. I tried to create a file in the path with fopen and added the following check : FILE *fp= fopen(Input path, "w"); if( fp == NULL) { //Invalid path } else { //Valid path } This works well for cases when the input path is invalid or not present. But in certain cases, when UAC is enabled, a file cannot be created in the C:\Program files location. So in that scenario, if my input path is C:\Program files, fopen doesnt return any error and above code executes the else part, which is wrong. Can anyone please provide suggestions about this issue? Sindhu

              C Offline
              C Offline
              Chris Meech
              wrote on last edited by
              #6

              Perhaps you are simplifying your code example, but the condition of fp == NULL could be true for many more reasons than just invalid path. You might want to make a call to GetLastError[^]. The returned error number will allow you to detect all manor of error conditions. :)

              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

              D 1 Reply Last reply
              0
              • S sindhumahe

                Hi, I have a use case to validate an path that is given as an input to my application. I tried to create a file in the path with fopen and added the following check : FILE *fp= fopen(Input path, "w"); if( fp == NULL) { //Invalid path } else { //Valid path } This works well for cases when the input path is invalid or not present. But in certain cases, when UAC is enabled, a file cannot be created in the C:\Program files location. So in that scenario, if my input path is C:\Program files, fopen doesnt return any error and above code executes the else part, which is wrong. Can anyone please provide suggestions about this issue? Sindhu

                B Offline
                B Offline
                bob16972
                wrote on last edited by
                #7

                Whenever something is different when UAC is enabled, the first question to ask is "does your application have a security manifest?" and if so, "is it setup properly?" In a nutshell, the presence of the security manifest will prevent things like "file virtualization" and "registry virtualization" and when setup properly will make your application act like it did before UAC was ever invented (i.e. intuitive when code works and doesn't work and why).

                1 Reply Last reply
                0
                • C Chris Meech

                  Perhaps you are simplifying your code example, but the condition of fp == NULL could be true for many more reasons than just invalid path. You might want to make a call to GetLastError[^]. The returned error number will allow you to detect all manor of error conditions. :)

                  Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

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

                  Chris Meech wrote:

                  You might want to make a call to GetLastError[^].

                  Does fopen() call SetLastError()? I would have thought it would use errno instead.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "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

                  "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                  C 1 Reply Last reply
                  0
                  • D David Crow

                    Chris Meech wrote:

                    You might want to make a call to GetLastError[^].

                    Does fopen() call SetLastError()? I would have thought it would use errno instead.

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "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

                    "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                    C Offline
                    C Offline
                    Chris Meech
                    wrote on last edited by
                    #9

                    Good point. I've become so used to calling GetLastError to solve things, but you may be correct that errno could be checked instead. :-O [EDIT] And now that I've checked fopen[^] in MSDN, it does set errno. :slaps head: :doh:

                    Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                    D 1 Reply Last reply
                    0
                    • C Chris Meech

                      Good point. I've become so used to calling GetLastError to solve things, but you may be correct that errno could be checked instead. :-O [EDIT] And now that I've checked fopen[^] in MSDN, it does set errno. :slaps head: :doh:

                      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

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

                      You could be right, too, if fopen() is simply a wrapper around CreateFile().

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "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

                      "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

                      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