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. Casting ULARGE_INTEGER as FILETIME

Casting ULARGE_INTEGER as FILETIME

Scheduled Pinned Locked Moved C / C++ / MFC
c++comhelptutorialquestion
7 Posts 2 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.
  • B Offline
    B Offline
    Ben Aldhouse
    wrote on last edited by
    #1

    I've been wondering why my recent article 'How To Calculate The Age Of A File' has been getting poor votes. I wondered if the fact that I hadn't used the 'HighPart' and 'LowPart' members of the ULARGE_INTEGER was bad. Then again is my use of the following code bad?

    SystemTimeToFileTime (& NowSystemTime, (FILETIME \*) & ulSysTime);
    SystemTimeToFileTime (&FileSystemTime, (FILETIME \*) & ulFileTime);
    

    Since posting my article, I have read 'Date and Time in C++' by RK_2000 and noted that he said Although [FILETIME and LARGE_INTEGER] have the same binary format, the address of all FILETIME structures must begin on a 32 bit boundary whereas the address of all LARGE_INTEGER must begin on 64 bit boundary. So, do not use these interchangeably. ... so this cast I've been using is wrong, then? In the meantime, I will assume that this is a flaw that people have spotted in my code and rewrite it without this cast. Thanks for your help, Ben.

    D 1 Reply Last reply
    0
    • B Ben Aldhouse

      I've been wondering why my recent article 'How To Calculate The Age Of A File' has been getting poor votes. I wondered if the fact that I hadn't used the 'HighPart' and 'LowPart' members of the ULARGE_INTEGER was bad. Then again is my use of the following code bad?

      SystemTimeToFileTime (& NowSystemTime, (FILETIME \*) & ulSysTime);
      SystemTimeToFileTime (&FileSystemTime, (FILETIME \*) & ulFileTime);
      

      Since posting my article, I have read 'Date and Time in C++' by RK_2000 and noted that he said Although [FILETIME and LARGE_INTEGER] have the same binary format, the address of all FILETIME structures must begin on a 32 bit boundary whereas the address of all LARGE_INTEGER must begin on 64 bit boundary. So, do not use these interchangeably. ... so this cast I've been using is wrong, then? In the meantime, I will assume that this is a flaw that people have spotted in my code and rewrite it without this cast. Thanks for your help, Ben.

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

      Ben Aldhouse wrote:

      I've been wondering why my recent article 'How To Calculate The Age Of A File' has been getting poor votes.

      Because if people have the ability to vote low, then for no other reason, they will. Ignore the vote and move on.

      Ben Aldhouse wrote:

      ...I will assume that this is a flaw that people have spotted in my code...

      Doubtful. A truly interested and worthy reader will let you know.

      "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      B 1 Reply Last reply
      0
      • D David Crow

        Ben Aldhouse wrote:

        I've been wondering why my recent article 'How To Calculate The Age Of A File' has been getting poor votes.

        Because if people have the ability to vote low, then for no other reason, they will. Ignore the vote and move on.

        Ben Aldhouse wrote:

        ...I will assume that this is a flaw that people have spotted in my code...

        Doubtful. A truly interested and worthy reader will let you know.

        "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        B Offline
        B Offline
        Ben Aldhouse
        wrote on last edited by
        #3

        Thanks, David, for your encouraging reply. I have tried the code without the cast and I can't get sensible results. This is what I now have...

        WIN32\_FIND\_DATA FindFileData;
        HANDLE hFind;
        hFind = FindFirstFile(strPath, &FindFileData);
        CString strYears, strMonths, strDays;
        
        SYSTEMTIME stNow; 
        GetSystemTime(&stNow);
        const FILETIME  ftFile = FindFileData.ftCreationTime;
        
        FILETIME ftNow;
        ULARGE\_INTEGER ulNowTime, ulFileTime, ulAge;
        
        SystemTimeToFileTime (& stNow, &ftNow);
        
        ulNowTime.HighPart = ftNow.dwHighDateTime;
        ulNowTime.LowPart = ftNow.dwLowDateTime;
        ulFileTime.HighPart = ftFile.dwHighDateTime;
        ulFileTime.LowPart = ftFile.dwLowDateTime;
        
        ulAge.HighPart = ulNowTime.HighPart - ulFileTime.HighPart;
        ulAge.LowPart = ulNowTime.LowPart - ulFileTime.LowPart;
        
        FILETIME ftAge;
        SYSTEMTIME stAge;
        
        ftAge.dwHighDateTime = ulAge.HighPart;
        ftAge.dwLowDateTime = ulAge.LowPart;
        
        SystemTimeToFileTime(&stAge,&ftAge);
        
        strYears.Format("%d", stAge.wYear-1601);
        strMonths.Format("%d",stAge.wMonth-1);
        strDays.Format("%d",stAge.wDay-1);
        

        I'm foxed. I should really take your advice and move on... Ben.

        D 1 Reply Last reply
        0
        • B Ben Aldhouse

          Thanks, David, for your encouraging reply. I have tried the code without the cast and I can't get sensible results. This is what I now have...

          WIN32\_FIND\_DATA FindFileData;
          HANDLE hFind;
          hFind = FindFirstFile(strPath, &FindFileData);
          CString strYears, strMonths, strDays;
          
          SYSTEMTIME stNow; 
          GetSystemTime(&stNow);
          const FILETIME  ftFile = FindFileData.ftCreationTime;
          
          FILETIME ftNow;
          ULARGE\_INTEGER ulNowTime, ulFileTime, ulAge;
          
          SystemTimeToFileTime (& stNow, &ftNow);
          
          ulNowTime.HighPart = ftNow.dwHighDateTime;
          ulNowTime.LowPart = ftNow.dwLowDateTime;
          ulFileTime.HighPart = ftFile.dwHighDateTime;
          ulFileTime.LowPart = ftFile.dwLowDateTime;
          
          ulAge.HighPart = ulNowTime.HighPart - ulFileTime.HighPart;
          ulAge.LowPart = ulNowTime.LowPart - ulFileTime.LowPart;
          
          FILETIME ftAge;
          SYSTEMTIME stAge;
          
          ftAge.dwHighDateTime = ulAge.HighPart;
          ftAge.dwLowDateTime = ulAge.LowPart;
          
          SystemTimeToFileTime(&stAge,&ftAge);
          
          strYears.Format("%d", stAge.wYear-1601);
          strMonths.Format("%d",stAge.wMonth-1);
          strDays.Format("%d",stAge.wDay-1);
          

          I'm foxed. I should really take your advice and move on... Ben.

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

          What exactly is this code snippet supposed to be doing?

          "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          B 1 Reply Last reply
          0
          • D David Crow

            What exactly is this code snippet supposed to be doing?

            "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            B Offline
            B Offline
            Ben Aldhouse
            wrote on last edited by
            #5

            I was trying to find the difference between a file's creation date, the current time and get the resulting data in a useful format. Doing this I was aware of the following warning from the MSDN library

            Remarks
            It is not recommended that you add and subtract values from the SYSTEMTIME
            structure to obtain relative times. Instead, you should

            -Convert the SYSTEMTIME structure to a FILETIME structure. 
            -Copy the resulting FILETIME structure to a ULARGE\_INTEGER structure. 
            -Use normal 64-bit arithmetic on the ULARGE\_INTEGER value. 
            

            However, I think at this point I was put off because the promised 'HighPart' and 'LowPart' members of ULARGE_INTEGER didn't appear in the autocomplete list on my editor. I have since written in the members anyway and found that they work without any trouble. Since your last reply I have had a walk around the block and had another look at the code. I discovered that when I was removing some lines whilst taking out the code that used that (FILETIME *)&ULARGE_INTEGER cast, I removed one line I should have left in and edited and left in another line towards the end of the code that should have been removed... Here is the (now, at last) working code with comments. Thanks for your encouragement and for showing an interest, Ben

            WIN32\_FIND\_DATA FindFileData;
            HANDLE hFind;
            hFind = FindFirstFile(strPath, &FindFileData);
            CString strYears, strMonths, strDays;
            
            SYSTEMTIME stNow; 
            

            //The current time is aquired and stored in SYSTEMTIME format
            GetSystemTime(&stNow);

            //The creation time of the file in question is obtained and
            //stored in FILETIME format
            const FILETIME ftFile = FindFileData.ftCreationTime;

            //A reference to the current time in FILETIME format is created
            FILETIME ftNow;
            WIN32_FIND_DATA FindFileData;
            HANDLE hFind;
            hFind = FindFirstFile(strPath, &FindFileData);
            CString strYears, strMonths, strDays;

            SYSTEMTIME stNow; 
            

            //The current time is aquired and stored in SYSTEMTIME format

            GetSystemTime(&stNow);
            

            //The creation time of the file in question is aquired in a FILETIME
            //structure then converted inta a SYSTEMTIME structure

            const FILETIME  ftFile = FindFileData.ftCreationTime;
            FILETIME ftNow;
            SystemTimeToFileTime (& stNow, &ftNow);
            

            //The contents of the FILETIME versions of the current time
            //and the creation time of the file are now stored in
            //ULARGE_INTEGER structures

            ULARGE\_INTEGER ulNowTime, ulFileTime, ulAge;
            ulNowTime.HighPart = ftNow.dwHighDateTime;
            
            D 1 Reply Last reply
            0
            • B Ben Aldhouse

              I was trying to find the difference between a file's creation date, the current time and get the resulting data in a useful format. Doing this I was aware of the following warning from the MSDN library

              Remarks
              It is not recommended that you add and subtract values from the SYSTEMTIME
              structure to obtain relative times. Instead, you should

              -Convert the SYSTEMTIME structure to a FILETIME structure. 
              -Copy the resulting FILETIME structure to a ULARGE\_INTEGER structure. 
              -Use normal 64-bit arithmetic on the ULARGE\_INTEGER value. 
              

              However, I think at this point I was put off because the promised 'HighPart' and 'LowPart' members of ULARGE_INTEGER didn't appear in the autocomplete list on my editor. I have since written in the members anyway and found that they work without any trouble. Since your last reply I have had a walk around the block and had another look at the code. I discovered that when I was removing some lines whilst taking out the code that used that (FILETIME *)&ULARGE_INTEGER cast, I removed one line I should have left in and edited and left in another line towards the end of the code that should have been removed... Here is the (now, at last) working code with comments. Thanks for your encouragement and for showing an interest, Ben

              WIN32\_FIND\_DATA FindFileData;
              HANDLE hFind;
              hFind = FindFirstFile(strPath, &FindFileData);
              CString strYears, strMonths, strDays;
              
              SYSTEMTIME stNow; 
              

              //The current time is aquired and stored in SYSTEMTIME format
              GetSystemTime(&stNow);

              //The creation time of the file in question is obtained and
              //stored in FILETIME format
              const FILETIME ftFile = FindFileData.ftCreationTime;

              //A reference to the current time in FILETIME format is created
              FILETIME ftNow;
              WIN32_FIND_DATA FindFileData;
              HANDLE hFind;
              hFind = FindFirstFile(strPath, &FindFileData);
              CString strYears, strMonths, strDays;

              SYSTEMTIME stNow; 
              

              //The current time is aquired and stored in SYSTEMTIME format

              GetSystemTime(&stNow);
              

              //The creation time of the file in question is aquired in a FILETIME
              //structure then converted inta a SYSTEMTIME structure

              const FILETIME  ftFile = FindFileData.ftCreationTime;
              FILETIME ftNow;
              SystemTimeToFileTime (& stNow, &ftNow);
              

              //The contents of the FILETIME versions of the current time
              //and the creation time of the file are now stored in
              //ULARGE_INTEGER structures

              ULARGE\_INTEGER ulNowTime, ulFileTime, ulAge;
              ulNowTime.HighPart = ftNow.dwHighDateTime;
              
              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              Have you considered the COleDateTimeSpan class?

              "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

              "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

              B 1 Reply Last reply
              0
              • D David Crow

                Have you considered the COleDateTimeSpan class?

                "Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                B Offline
                B Offline
                Ben Aldhouse
                wrote on last edited by
                #7

                Hmm. Looking this up in MSDN I have found out that CTime has constructors for accepting SYSTEMTIME and FILETIME objects. Looks like I'll be rewriting the code with an even better solution!

                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