Solved

Saving CHARFORMAT to registry.

Posted on 2003-11-25
6
340 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adapt this command to show who installed 29 108
sum28 challenge 31 111
NotAlone Challenge 20 83
Problem to App 4 80
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 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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

775 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