Solved

Saving CHARFORMAT to registry.

Posted on 2003-11-25
6
344 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ORA-01403: no data found 43 106
Sed question 2 104
Change to event 1 119
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 104
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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