Solved

Saving CHARFORMAT to registry.

Posted on 2003-11-25
6
334 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:jjjkkklll
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry for the delay, I was on leave these 4 days
Rosh :)
0

Featured Post

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
ffmpeg - "rtsp://...... Operation not permitted" 4 65
zeroFront challenge 7 70
mapBully challenge 6 86
Thin secure Windows 10 5 46
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

7 Experts available now in Live!

Get 1:1 Help Now