Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Can I save an object to the registry?

Posted on 2003-10-26
7
Medium Priority
?
337 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
jkr earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

772 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