Solved

Customizing CWinApp's Registry Functionality

Posted on 1998-07-28
5
803 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 50 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

8 Experts available now in Live!

Get 1:1 Help Now