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. question about getfileattributes function

question about getfileattributes function

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelpquestion
8 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.
  • S Offline
    S Offline
    swatgodjr
    wrote on last edited by
    #1

    i have been able to get my program to compile correctly and added in code to view the returned result of the dword value from GetFileAttributes() which should display the file attributes of the given file but when the function is called all it returns is an error message saying "the process cannot access the file because it is being used by another process." yet i made sure that before it was called i had closed the file before the function was called and even tried the function on a file that was never opened and it still gave the same error. here is the code for my function: ReportAttributes(char filename[]) { LPCTSTR pszCaption = "Debug Results:"; DWORD attribList = GetFileAttributes(filename); if(!attribList) { MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING); } else { const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM; LPVOID pTextBuffer = NULL; DWORD dwCount = FormatMessage(dwFormatControl, NULL, attribList, 0, (LPTSTR) &pTextBuffer, 0, NULL); if(0 != dwCount) { MessageBox((LPCSTR)pTextBuffer, pszCaption, MB_ICONERROR); LocalFree(pTextBuffer); } else { MessageBox("Unknown error", pszCaption, MB_OK); } } } i varied the icons in the messagebox calls to see which one came up when the error was displayed.

    N K D 3 Replies Last reply
    0
    • S swatgodjr

      i have been able to get my program to compile correctly and added in code to view the returned result of the dword value from GetFileAttributes() which should display the file attributes of the given file but when the function is called all it returns is an error message saying "the process cannot access the file because it is being used by another process." yet i made sure that before it was called i had closed the file before the function was called and even tried the function on a file that was never opened and it still gave the same error. here is the code for my function: ReportAttributes(char filename[]) { LPCTSTR pszCaption = "Debug Results:"; DWORD attribList = GetFileAttributes(filename); if(!attribList) { MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING); } else { const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM; LPVOID pTextBuffer = NULL; DWORD dwCount = FormatMessage(dwFormatControl, NULL, attribList, 0, (LPTSTR) &pTextBuffer, 0, NULL); if(0 != dwCount) { MessageBox((LPCSTR)pTextBuffer, pszCaption, MB_ICONERROR); LocalFree(pTextBuffer); } else { MessageBox("Unknown error", pszCaption, MB_OK); } } } i varied the icons in the messagebox calls to see which one came up when the error was displayed.

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      swatgodjr wrote:

      "the process cannot access the file because it is being used by another process."

      Exactly this is the reason. Make sure that this file is not being used by another application or background process, or create a temporary file and call this function on this file and see what happens. If the problem persists then make sure that you have not opened this file before this call.


      Nibu thomas A Developer Programming tips[^]  My site[^]

      S 1 Reply Last reply
      0
      • N Nibu babu thomas

        swatgodjr wrote:

        "the process cannot access the file because it is being used by another process."

        Exactly this is the reason. Make sure that this file is not being used by another application or background process, or create a temporary file and call this function on this file and see what happens. If the problem persists then make sure that you have not opened this file before this call.


        Nibu thomas A Developer Programming tips[^]  My site[^]

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

        yea as i explained i also tried this on a file i had not even touched at all and got the same error in doing so. is there maybe something wrong with my code i posted?

        N 1 Reply Last reply
        0
        • S swatgodjr

          yea as i explained i also tried this on a file i had not even touched at all and got the same error in doing so. is there maybe something wrong with my code i posted?

          N Offline
          N Offline
          Nibu babu thomas
          wrote on last edited by
          #4

          swatgodjr wrote:

          yea as i explained i also tried this on a file i had not even touched at all and got the same error in doing so. is there maybe something wrong with my code i posted?

          Well this I got from MSDN... If the function fails, **the return value is** **INVALID_FILE_ATTRIBUTES**. To get extended error information, call GetLastError. Do check for this value instead of !dwFileAttributes.


          Nibu thomas A Developer Programming tips[^]  My site[^]

          S 1 Reply Last reply
          0
          • N Nibu babu thomas

            swatgodjr wrote:

            yea as i explained i also tried this on a file i had not even touched at all and got the same error in doing so. is there maybe something wrong with my code i posted?

            Well this I got from MSDN... If the function fails, **the return value is** **INVALID_FILE_ATTRIBUTES**. To get extended error information, call GetLastError. Do check for this value instead of !dwFileAttributes.


            Nibu thomas A Developer Programming tips[^]  My site[^]

            S Offline
            S Offline
            swatgodjr
            wrote on last edited by
            #5

            actually i originally had it checking for INVALID_FILE_ATTRIBUTES but no matter what i did it kept giving me an error saying it was an undeclared identifier so i just put !attribList so that it would compile so that i could at least test it out. i included the files it needed and everything and after quite a bit of looking around i just left it the way i have it now. the code i posted was actually modified from my getlasterror function which works nicely.

            1 Reply Last reply
            0
            • S swatgodjr

              i have been able to get my program to compile correctly and added in code to view the returned result of the dword value from GetFileAttributes() which should display the file attributes of the given file but when the function is called all it returns is an error message saying "the process cannot access the file because it is being used by another process." yet i made sure that before it was called i had closed the file before the function was called and even tried the function on a file that was never opened and it still gave the same error. here is the code for my function: ReportAttributes(char filename[]) { LPCTSTR pszCaption = "Debug Results:"; DWORD attribList = GetFileAttributes(filename); if(!attribList) { MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING); } else { const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM; LPVOID pTextBuffer = NULL; DWORD dwCount = FormatMessage(dwFormatControl, NULL, attribList, 0, (LPTSTR) &pTextBuffer, 0, NULL); if(0 != dwCount) { MessageBox((LPCSTR)pTextBuffer, pszCaption, MB_ICONERROR); LocalFree(pTextBuffer); } else { MessageBox("Unknown error", pszCaption, MB_OK); } } } i varied the icons in the messagebox calls to see which one came up when the error was displayed.

              K Offline
              K Offline
              kakan
              wrote on last edited by
              #6

              I don't get your code. First, check the return value from GetFileAttributes(). If it's 0xFFFFFFFF, then call GetLastError() to get the error code. Then, do a FormatMessage with the return value from GetLastError()! (It's natural you always gets the same error message when you always calls FormatMessage with the same value, 0xFFFFFFFF).

              Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

              1 Reply Last reply
              0
              • S swatgodjr

                i have been able to get my program to compile correctly and added in code to view the returned result of the dword value from GetFileAttributes() which should display the file attributes of the given file but when the function is called all it returns is an error message saying "the process cannot access the file because it is being used by another process." yet i made sure that before it was called i had closed the file before the function was called and even tried the function on a file that was never opened and it still gave the same error. here is the code for my function: ReportAttributes(char filename[]) { LPCTSTR pszCaption = "Debug Results:"; DWORD attribList = GetFileAttributes(filename); if(!attribList) { MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING); } else { const DWORD dwFormatControl = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM; LPVOID pTextBuffer = NULL; DWORD dwCount = FormatMessage(dwFormatControl, NULL, attribList, 0, (LPTSTR) &pTextBuffer, 0, NULL); if(0 != dwCount) { MessageBox((LPCSTR)pTextBuffer, pszCaption, MB_ICONERROR); LocalFree(pTextBuffer); } else { MessageBox("Unknown error", pszCaption, MB_OK); } } } i varied the icons in the messagebox calls to see which one came up when the error was displayed.

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

                swatgodjr wrote:

                if(!attribList)

                You must read the docs a bit closer:

                if (INVALID_FILE_ATTRIBUTES == attribList)
                {
                // use GetLastError() to see what the error is
                MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING);
                }


                "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

                "Judge not by the eye but by the heart." - Native American Proverb

                S 1 Reply Last reply
                0
                • D David Crow

                  swatgodjr wrote:

                  if(!attribList)

                  You must read the docs a bit closer:

                  if (INVALID_FILE_ATTRIBUTES == attribList)
                  {
                  // use GetLastError() to see what the error is
                  MessageBox("An Error Has Occured", pszCaption, MB_ICONWARNING);
                  }


                  "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

                  "Judge not by the eye but by the heart." - Native American Proverb

                  S Offline
                  S Offline
                  swatgodjr
                  wrote on last edited by
                  #8

                  i tried to use INVALID_FILE_ATTRIBUTES and it returns an error on compiling saying that it was an undeclared identifier and also tried checking for the other error code but that just gives me the same error message i have been having when the function is called in my program. i have the correct files being included into my program so i really am not sure why it wotn let me use INVALID_FILE_ATTRIBUTES. also from what i have noticed, it never returns either error message as it goes to the main body of the function like it should.

                  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