Thread: Writing FILETIME or SYSTEMTIME to File?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    5

    Writing FILETIME or SYSTEMTIME to File?

    Hi all,

    I am very new to VC++, (VB Convert), and am only writing my second C++ program.

    I am looking to write either a FILETIME struct or a SYSTEMTIME struct to a file for later usage. I am using the CreateFile, WriteFile, CloseHandle APIs to handle writing to file.

    But I'm not sure how to handle the data if I want to retrieve it later.
    • Is it possible to write the FILETIME to a file in binary using the WriteFile API?
    • or... is there a way to write the 2 DWORD values of the FILETIME struct as binary that can easily be retrieved as 2 separate values later?
    • Or should I leave it as system time and then put it into the file as a string value that I can tokenize during retrieval?


    Code I have thus far is

    Code:
    SYSTEMTIME stCurrent;
    FILETIME ftCurrent;
    HANDLE hWriteFile;
    LPDWORD lpBytesWritten = 0;
    long lRetVal;
    
    GetSystemTime(&stCurrent);
    SystemTimeToFileTime(&stCurrent, &ftCurrent);
    
    hWriteFile = CreateFile("C:\\Temp\\file.dat", 
         GENERIC_WRITE,
         0, 
         NULL, 
         CREATE_ALWAYS, 
         FILE_ATTRIBUTE_NORMAL, 
         NULL);
    
    lRetVal = WriteFile(hWriteFile,
         &ftCurrent,
         sizeof(ftCurrent),
         lpBytesWritten,
         NULL);
    
    CloseHandle(hWriteFile);
    
    if(lRetVal == ERROR_SUCCESS){return true;]
         else{return false;}

    Thanks one and all for any advice you can lend!

    PS...
    The purpose of the class I'm working on is to catch the current system time. Then, following an application installation, compare the lastwritetime of the CLASSES_ROOT and LOCAL_MACHINE reg keys to see what was changed by the installation.
    I am calling the GetSystemTime API and then, potentially, using the SystemTimeToFileTime API to convert to FILETIME.
    Last edited by Tojam; 09-22-2003 at 10:31 AM.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    5
    Never mind... Got it handled. I was putting in the wrong data type into the WriteFile API call, should be the address of a DWORD value rather than the LPDWORD (lpBytesWritten).

    Thanks!!

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Alternatively you could use the free store, and continue going the way you were:

    Code:
    LPDWORD lpBytesWritten = new DWORD;
    .
    .
    .
    lRetVal = WriteFile(hWriteFile,
         &ftCurrent,
         sizeof(ftCurrent),
         lpBytesWritten,
         NULL);
    .
    .
    .
    delete lpBytesWritten;
    If you don't know yet, the "new" operator allocates space for the following data type and returns a pointer to that space. So, lpBytesWritten now contains the address of a DWORD. The "delete" operator ensures that the allocated memory is released.

    In this case, it isn't really necessary to use new and delete, but it's handy to know about them.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM