Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Can I save an object to the registry?

Posted on 2003-10-26
7
Medium Priority
?
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

688 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