winapiintfiletime

How to create FILETIME in Win32?


I have a value __int64 that is a 64-bit FILETIME value. FILETIME has a dwLowDateTime and dwHighDateTime. When I try to assign a __int64 to a FILETIME I get a C2440. How do I assign the __int64 to the FILETIME?

Or how do I split the __int64 so that I can assign the low part to dwLowDateTime and the high part to dwHighDateTime?


Solution

  • Here's the basic outline.

    __int64 t;
    FILETIME ft;
    
    ft.dwLowDateTime = (DWORD)t;
    ft.dwHighDateTime = (DWORD)(t >> 32);
    

    NOT RECOMMENDED approach

    ft = *(FILETIME *)(&t);
    

    It'll work due to the clever arrangement of FILETIME, but the sacrifice in portability and clarity is not worth it. Use only in times of proven dire need and wrap it in asserts to prove it will work.