Solved

Can I save an object to the registry?

Posted on 2003-10-26
7
307 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
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.

 
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

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
How to determine file rename from EventLog ? 2 43
either24  challenge 19 90
Detect file exist or not 3 134
Problem to open Excel file 15 99
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…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

863 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

22 Experts available now in Live!

Get 1:1 Help Now