Solved

Can I save an object to the registry?

Posted on 2003-10-26
7
305 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
Comment Utility
Using a REG_BINARY type you can save virtually anything.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
Comment Utility
>>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
Comment Utility
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
Comment Utility
actually I supply a default:
    m_sInetPswdDate      = gApp.GetProfileString("Inet","sInetPswdDate", "1900-01-01" ); // YYYY-MM-DD
0
 
LVL 5

Author Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Define unique primary key 9 81
mapBully challenge 6 88
mapAB Challlenge 35 84
Detect file exist or not 3 76
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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 …
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

9 Experts available now in Live!

Get 1:1 Help Now