locked
Need help with SetFileTime RRS feed

  • Question

  • Hello everyone,
    My call to SetFileTime keeps on failing.. :(
    I already tried FormatMessage + GetLastError, but they keep returning <null>
    Can anyone point me in the right direction?
    Source code: http://cpp.pastebin.com/f3f0406e8

    Thanks in advance :)
    Saturday, June 28, 2008 6:39 PM

Answers

  • There are several mistakes in your program.  Follow these ground-rules of C++ programming to stay out of trouble:

    - Always initialize your local variables.  They'll contain random values, even after you called a function that failed
    - Always check the return value of functions.  That would show you that SystemTimeToFileTime failed
    - Use GetLastError() to retrieve error codes.  You'd get 87, "the parameter is incorrect"
    - Always double-check with the MSDN library what flags are supported.  That would make SetFileAttributes work
    - Always initialize structures completely, not just a single member.  That would give you a correct SYSTEMTIME
    - Practice using the debugger.  Debug + QuickWatch shows you what is wrong with sysTime.



    Hans Passant.
    • Edited by nobugz Saturday, June 28, 2008 8:39 PM getlasterror
    • Marked as answer by Appo Sunday, June 29, 2008 6:55 AM
    • Unmarked as answer by Appo Sunday, June 29, 2008 6:55 AM
    • Marked as answer by Appo Sunday, June 29, 2008 6:55 AM
    Saturday, June 28, 2008 8:18 PM

  • Look closely at what you are doing with the SYSTEMTIME structure.

    (1) It is not initialized so its members will contain random values.
    (2) You set only the year element.

    Conclusion: You are using garbage values for the date.

    Decide what you want the other elements of the structure to contain
    and set them accordingly.

    e.g. - Use GetSystemTime(&sysTime) to use the current date/time values
    and then change the year. If you just want to change the year, then set
    sysTime to the current values for the file and then change the year.

    - Wayne



    • Marked as answer by Appo Sunday, June 29, 2008 6:55 AM
    Saturday, June 28, 2008 8:22 PM

All replies

  • There are several mistakes in your program.  Follow these ground-rules of C++ programming to stay out of trouble:

    - Always initialize your local variables.  They'll contain random values, even after you called a function that failed
    - Always check the return value of functions.  That would show you that SystemTimeToFileTime failed
    - Use GetLastError() to retrieve error codes.  You'd get 87, "the parameter is incorrect"
    - Always double-check with the MSDN library what flags are supported.  That would make SetFileAttributes work
    - Always initialize structures completely, not just a single member.  That would give you a correct SYSTEMTIME
    - Practice using the debugger.  Debug + QuickWatch shows you what is wrong with sysTime.



    Hans Passant.
    • Edited by nobugz Saturday, June 28, 2008 8:39 PM getlasterror
    • Marked as answer by Appo Sunday, June 29, 2008 6:55 AM
    • Unmarked as answer by Appo Sunday, June 29, 2008 6:55 AM
    • Marked as answer by Appo Sunday, June 29, 2008 6:55 AM
    Saturday, June 28, 2008 8:18 PM

  • Look closely at what you are doing with the SYSTEMTIME structure.

    (1) It is not initialized so its members will contain random values.
    (2) You set only the year element.

    Conclusion: You are using garbage values for the date.

    Decide what you want the other elements of the structure to contain
    and set them accordingly.

    e.g. - Use GetSystemTime(&sysTime) to use the current date/time values
    and then change the year. If you just want to change the year, then set
    sysTime to the current values for the file and then change the year.

    - Wayne



    • Marked as answer by Appo Sunday, June 29, 2008 6:55 AM
    Saturday, June 28, 2008 8:22 PM
  • Thank you, it is working now :) I guess I'll never forget those rules.
    Sunday, June 29, 2008 6:54 AM