Solved

Can I save an object to the registry?

Posted on 2003-10-26
7
313 Views
Last Modified: 2013-11-20
Basically I want to save a CTime object to the registry, and access it later.  Is this possible, or do I have to save it to a file?
0
Comment
Question by:lwinkenb
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 9624101
Using a REG_BINARY type you can save virtually anything.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9624283
jhance is right however, I'd not save an object in the registry (even thoug it might work for relatively simple types like 'CTime'), but it's members or an easy-to-use representation - take a look at the available constructors for CTime - these are

CTime( );

CTime( const CTime& timeSrc );

CTime( time_t time );

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

CTime( WORD wDosDate, WORD wDosTime, int nDST = -1 );

CTime( const SYSTEMTIME& sysTime, int nDST = -1 );

CTime( const FILETIME& fileTime, int nDST = -1 );

So, your choice is to save the time either as a "time_t", DOS date and time, SYSTEMTIME or FILETIME. I'd suggest saving it as a "time_t", because the handling is the easiest - use

CTime ct; //...

time_t t = ct.GetTime ();

// save that to the registry e.g. as a REG_DWORD

//...

time_t = GetTimeFromRegistry ();

CTime ct ( t);
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9624296
Thanks for the comments guys.

would it work with WriteProfileBinary()?

I tried it with
WriteProfileBinary("settings","time",timeObject,sizeof(timeObject));

But it couldnt cast the time_t object.
0
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.

 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 9624312
>>would it work with WriteProfileBinary()?

I do not even know that API - if you want to use the registry, work with 'RegSetValueEx()' et. al. - e.g.

   time_t = ...;

   HKEY hkApp, hkCompany;
   DWORD     disposition;
   int size = sizeof ( time_t);
   if( ERROR_SUCCESS == RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\My Company"), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkCompany, &disposition))
   {
         if( ERROR_SUCCESS == RegCreateKeyEx(hkCompany, TEXT("My Application"), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkApp, &disposition))
         {
              if(RegSetValueEx(hkApp, TEXT("Time"), 0, REG_SZ, (LPBYTE)&t, size) == ERROR_SUCCESS))
              {
                    // success...
              }

              RegCloseKey(hkApp);
         }
   }
   RegCloseKey(hkCompany);
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9626248
Here is what I do:

To save:
      m_sInetPswdDate= CTime::GetCurrentTime().Format( "%Y-%m-%d" );
      gApp.WriteProfileString("Inet","sInetPswdDate", m_sInetPswdDate );

To read:
      m_sInetPswdDate      = gApp.GetProfileString("Inet","sInetPswdDate", "" ); // YYYY-MM-DD

      int nYear=   atoi( m_sInetPswdDate.Left(4)   );
      int nMonth= atoi( m_sInetPswdDate.Mid(5,2) );
      int nDay=    atoi( m_sInetPswdDate.Mid(8,2) );

      CTime ctInetPswd( nYear, nMonth, nDay, 0,0,0 );

The advantage is that the date is in cleartext in the registry and I can handle that better when I get a support call.

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 9626254
actually I supply a default:
    m_sInetPswdDate      = gApp.GetProfileString("Inet","sInetPswdDate", "1900-01-01" ); // YYYY-MM-DD
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 9628348
Thanks for the comments guys.  I ended up implementing the solution jkr suggested.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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