Solved

Store CTime object in registry

Posted on 2004-05-02
4
326 Views
Last Modified: 2013-11-20
Hello !

I am writing an app that uses "Reminders"; i.e. the user can enter a subject, details and due time as a reminder for them to do something.  The subject and details of the reminder are stored in CStrings (obtained from CEdit controls) and the data/time are stored in CTime objects (obtained from CDateTimeCtrl controls).  When that time arrives, a dialogue box pops up telling the user so.  In essence, it is very similar to Outlook's "tasks".  

All of this information is to be stored in the registry, but I cannot see how to store a CTime object in a manner so it can re-read back.  I need to read it back to re-initialise the CDateTimeCtrl controls when re-displaying all of the reminders the next time the app is started and the user chooses to view them.

Any ideas ?

TIA !
0
Comment
Question by:mrwad99
  • 2
4 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 350 total points
ID: 10971339
You can keep CTime as 6 different int values in the Registry:

CTime time;
// fill time

WriteProfileInt("Time", "year", time.GetYear());
...
WriteProfileInt("Time", "second", time.GetSecond());

To read CTime from Registry read all these values and use CTime constructor:

CTime( int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, int nDST = -1 );

You can keep all these values as one string formatting it using String.Format and extracting values from it using sscanf. For example, time in Registry may look as:

"2004 5 2 12 25 55"
0
 
LVL 30

Accepted Solution

by:
Zoppo earned 100 total points
ID: 10971502
Hi ,

You can even read/write CTime object binary from/to the Registry, i.e.:

...
      HKEY hKey;
      LONG lRes = RegOpenKeyEx( HKEY_CURRENT_USER, "test", 0, KEY_ALL_ACCESS, &hKey );

      if ( ERROR_SUCCESS != lRes )
      {
            // does not exist ... create
            lRes = RegCreateKey( HKEY_CURRENT_USER, "test", &hKey );

            if ( ERROR_SUCCESS != lRes )
            {
                  AfxMessageBox( "Error: cannot create \"HK_CU/Test\"" );
                  return -1;
            }

            CTime time( 1975, 5, 21, 22, 15, 0 ); // just a sample date

            // write to registry
            RegSetValueEx( hKey, "test_time", 0, REG_BINARY, (BYTE*)&time, sizeof( CTime ) );
      }
      else
      {
            // read from Registry
            DWORD dwType, dwSize = 0;
            lRes = RegQueryValueEx( hKey, "test_time", 0, &dwType, NULL, &dwSize );

            if ( ERROR_SUCCESS == lRes && REG_BINARY == dwType && sizeof( CTime ) == dwSize )
            {
                  // ok, it's a CTime
                  CTime time;
                  RegQueryValueEx( hKey, "test_time", 0, NULL, (BYTE*)&time, &dwSize );
                  AfxMessageBox( time.Format( "%A, %B %d, %Y" ) );
            }
      }
...

hope that helps,

ZOPPO
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10971522
Thanks both; am just trying all suggestions - will be back ASAP :)
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10971589
Right, both answers were perfectly acceptable so I split the 200 points between you both.  AlexFM I have given you another 250 points as you effectively answered another of my questions - http:Q_20963387.html - although you did so at http:Q_20963870.html - and never accepted credit for it.

Thanks all.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now