Solved

Saving CHARFORMAT to registry.

Posted on 2003-11-25
6
337 Views
Last Modified: 2013-11-20
Hi, this should be pretty easy and I'm surprised I can't find it anywhere on the net.  

In my program, the user can choose the default charformat for a richeditctrl.  I want to save (and retrieve) that CHARFORMAT structure to the registry so the users choice remains persistent between sessions.  I'm assuming I can save the most important elements and then reconstruct it on the other side.  But all the conversions, and "dwMask", ect. has me a bit confused.  Can someone show me how to do this?  Thank you.
0
Comment
Question by:jjjkkklll
  • 4
  • 2
6 Comments
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 200 total points
ID: 9822381
Use REG_BINARY type for saving structure.


      CHARFORMAT sCHARFORMAT;

      HKEY lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,      
                                                "SOFTWARE\\YourApplication"),
                                                0,
                                                KEY_QUERY_VALUE | KEY_SET_VALUE,
                                                &hKey);

      if ( lResult == ERROR_SUCCESS )
      {
            lResult = RegSetValueEx(hKey,  "CHARFORMAT", 0, REG_BINARY, &sCHARFORMAT, sizeof(CHARFORMAT));
            
            RegCloseKey(hKey);
      }


Rosh :)
0
 

Author Comment

by:jjjkkklll
ID: 9826363
Oh, very cool.  I didn't know you could store the entire CHARFORMAT structure like that.  Then to get it out, I do the same thing only use RegQueryValueEx instead of RegSetValueEx, correct?  Is everything else the same?  (BTW, I'm not at my development computer - I'll let you know as soon as I try the code.)
0
 

Author Comment

by:jjjkkklll
ID: 9829309
Hmm, well I gave it a shot.  First the compiler said hKey was an undeclared identifier.  So I added HKEY hKey; at the top.  No problem.  Then I had to change your HKEY to LONG to make lResult work out.  Okay, everything okay so far.  When I added your RegSetValueEx statement I got the following error:

'RegSetValueExA' : cannot convert parameter 5 from 'struct _charformat *' to 'const unsigned char *'

Here's my code:

      HKEY hKey;
      LONG lResult = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\MyAppName", 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hKey);

      if(lResult == ERROR_SUCCESS)
      {
            lResult = RegSetValueEx(hKey, "CHARFORMAT", 0, REG_BINARY, &sCHARFORMAT, sizeof(CHARFORMAT));
      
      }

      RegCloseKey(hKey);

Note: sCHARFORMAT is the name of my member CHARFORMAT that the RichEditCtrl uses for its default CHARFORMAT.  (Defined in the header as CHARFORMAT sCHARFORMAT;).

Not sure where to go from here.
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.

 

Author Comment

by:jjjkkklll
ID: 9838763
Okay, I think I figured it out on my own (it seems to work, anyway).  Here's the final code:

Writing:
      HKEY hKey;
      LONG lResult = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\MyApp", 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hKey);

      if(lResult == ERROR_SUCCESS)
      {
            lResult = RegSetValueEx(hKey, "CHARFORMAT", 0, REG_BINARY, (LPBYTE)&cf, sizeof(CHARFORMAT));
      
      }

      RegCloseKey(hKey);

Reading:
      HKEY hKey;
      LONG lResult = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\MyApp", 0, KEY_QUERY_VALUE | KEY_SET_VALUE, &hKey);

      if(lResult == ERROR_SUCCESS)
      {
            DWORD cb = sizeof(cf);
            lResult = RegQueryValueEx(hKey, "CHARFORMAT", 0, NULL, (LPBYTE)&cf, &cb);
            if(lResult == ERROR_SUCCESS)
            {
                  GetRichEditCtrl().SetDefaultCharFormat(cf);
            }

Remember I have a member variable CHARFORMAT cf;

Any comments on this code?  Good?  Bad?  Definitely ugly.
0
 

Author Comment

by:jjjkkklll
ID: 9844059
Okay, well the code seems to work for me.  I'm giving the points and closing the question.  If anyone has any comments, please add them.  Thanks.
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9847959
Sorry for the delay, I was on leave these 4 days
Rosh :)
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

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

14 Experts available now in Live!

Get 1:1 Help Now