Solved

Saving CHARFORMAT to registry.

Posted on 2003-11-25
6
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

623 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