Solved

Store CTime object in registry

Posted on 2004-05-02
4
332 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 31

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Rewrite URL With Encoded Characters 6 79
Generic progress indicator 6 117
WinWaitActive parameters 12 31
firstChar challenge 13 115
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.

831 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