Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Customizing CWinApp's Registry Functionality

Posted on 1998-07-28
5
Medium Priority
?
825 Views
Last Modified: 2013-11-20
I am trying to customize the registry functionality of CWinApp. To do this I am trying to derive another App class from it. I have overriden the GetAppRegistryKey(), GetSectionKey(), and SetRegistryKey() functions so far. I am then trying to use WriteProfileString() to write to a key that is under HKEY_LOCAL_MACHINE. I have not been able to write to anything except the HKEY_CURRENT_USERS hive. Is there a way to do this without resorting to using just the WIN32 API? If so what? Am I missing something obvious? Also would there be any problem putting this class derived from CWinApp into a DLL. If more detail is needed I can supply it.
0
Comment
Question by:awd
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
snoegler earned 200 total points
ID: 1319857
I hope i got your question right ...
If i were you i would use the registry functions,
::RegOpenKeyEx(..), ::RegSaveKey(..) and so on.
These functions seem very difficult if you get the first time in
touch with them, but after a short time you will realize that
it is not a bit more complicated than using WriteProfileString()
and so on.
To your second question: This is possible(MFC Extension DLL), but
has the disadvantage that your AppWizard won't create an application
using your dll.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1319858
Short example to save a value in the registry:

HKEY hkey;
::RegOpenKeyEx(HKEY_LOCAL_MACHINE,
               "Software\\myCorp\\myKey",
               0,KEY_ALL_ACCESS,&hkey);
CString test="Registry test."
::RegSetValue(hkey,"Program Name",REG_SZ,
              (LPCTSTR)test,test.GetLength());
::RegCloseKey(hkey);
0
 
LVL 1

Author Comment

by:awd
ID: 1319859
Thanks for the example. It complements the online help very well! I may end up doing it this way. I would like to do it by overriding the CWinApp functionality first (to make sure I have learned the concept) and then decide which way is better.

There aren't any problems trying to derive another class from CWinApp in an extension DLL? It seems like that could be a special case. I have been having trouble getting AfxGetApp() to return a reference to the derived App class insted of the CWinApp class.
0
 
LVL 6

Expert Comment

by:snoegler
ID: 1319860
>> I have been having trouble getting AfxGetApp() to return a reference to the derived App class instead of the CWinApp class

You are right, i forgot that. But i think the only way to alter this behavior is to recompile the MFC.
Look at this piece of sourcecode:

_AFXWIN_INLINE CWinApp* AFXAPI AfxGetApp()
{ return afxCurrentWinApp; }

It is defined as inline function - to override it had to be a virtual function. Because AfxGetApp()
is used internally by the MFC, the only way would be to recompile, i think.
An other way could be - which is not enough, i think - to just override the virtual functions of
CWinApp. Or - when you need the special behavior your class implements - you define a
macro like:

#define AfxGetExtApp() ((CMyWinApp*)AfxGetApp())

and use this to access the special member functions.
I am sorry, but i do not see an other way. Let's see if someone provides you an easier answer
to this.
0
 
LVL 1

Author Comment

by:awd
ID: 1319861
Thank you VERY much for your time and the help you supplied! With it I was finally able to get everything working the way I had originally tried. I was able to derive a class from CWinApp and override (if that is the correct terminology)  the relevant functions to change the way it handled the registry. And I was able to get it working after defining it in an MFC Extension DLL.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

877 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