Link to home
Start Free TrialLog in
Avatar of awd
awd

asked on

Customizing CWinApp's Registry Functionality

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.
ASKER CERTIFIED SOLUTION
Avatar of snoegler
snoegler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of snoegler
snoegler

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);
Avatar of awd

ASKER

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.
>> 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.
Avatar of awd

ASKER

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.