Solved

Store CTime object in registry

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 208
fizzArray  challenge 1 107
Not needed 13 128
I could not set window to top 4 40
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

713 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