Solved

Store CTime object in registry

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
Pressing cancel button with C# UI Automation 3 51
How to determine file rename from EventLog ? 2 43
Excel file not created as expected 7 50
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

896 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

13 Experts available now in Live!

Get 1:1 Help Now