Toggle SetRegistryKey

Posted on 1998-03-12
Last Modified: 2013-11-19
In my CProjApp::InitInstance(), i have set it registry key.


Somewhere else in the project, i need to retrieve the info of another Application (e.g. Word97) from the system registry.  I tried the below by temporary setting the registry key to 'Word97'.  It gives me assertion error that key is not NULL which is obviously true.  Is there a way i can set the key to NULL or it there any other methods i can adopt?

void CMyClass::GetWordInfo( )
  // temporary toggle to key to Word97  and get the info  
  char szWord97Path[_MAX_PATH] ;
  ((CProjApp*)AfxGetApp())->SetRegistryKey(_T("Word97"));   // Assert error
  ((CProjApp*)AfxGetApp())->GetProfileString("Path", "Path", szWord97Path);
Question by:leowlf
  • 2
LVL 23

Accepted Solution

chensu earned 50 total points
ID: 1316958
I would suggest you access the registry directly using Regisrty Functions rather than do this. And I don't think the Word97 registry key is Word97. It should be HKEY_CURRENT_USER\Software\Microsoft\Office\8.0\Word. And there seems to be no such a subkey or value named Path. It seems that you may want to use ::FindExecutable() function to get this information.

For your information, the MFC source code of CWinApp::SetRegistryKey() is listed following.

void CWinApp::SetRegistryKey(LPCTSTR lpszRegistryKey)
      UNUSED(lpszRegistryKey); // not used in release mac builds

      ASSERT(m_pszRegistryKey == NULL);
      ASSERT(lpszRegistryKey != NULL);
      ASSERT(m_pszAppName != NULL);

#ifndef _MAC
      BOOL bEnable = AfxEnableMemoryTracking(FALSE);
      m_pszRegistryKey = _tcsdup(lpszRegistryKey);
      m_pszProfileName = _tcsdup(m_pszAppName);

The CWinApp::GetProfileString() function will then access the Registry HKEY_CURRENT_USER\Software\RegistryKey\ProfileName. If you really want to do so, you can free the memory and have it set to your new string.

Author Comment

ID: 1316959
'Word97' is just an example and i need to access the registry programmatically.
 Here is a clearer picture:

I am developing 2 applications, AppA and AppB.
User may install the 2 applications in separate drives , separate directories etc.

AppB needs to execute the AppA excutable file.  Before AppB can do that, it needs to know the AppA's full path so that it can use WinExec( ) etc.  The location of AppA is available in the registry: AppA, path, path, d:/MyAppA

I tried making AppB doing a file search for AppA.exe but this is slow.  So  i tried to toggle AppB's SetRegistryKey("AppB")  to SetRegistryKey("AppA" )  and of course it gives me an obvious assertion error.  

You mentioned that  i can free the memory and set to the new string.  How shall i go about doing that?  Sample code would be appreciated.  I don't mind crashing my system.
LVL 23

Expert Comment

ID: 1316960
OK. The registry key passed as the parameter of CWinApp::SetRegistryKey() is usually the name of a company. It is stored in a key of the following form: HKEY_CURRENT_USER\Software\<company name>\<application name>\<section name>\<value name>. So, in your case, you use CWinApp::SetRegistryKey() only once.

For AppA, the registry key is HKEY_CURRENT_USER\Software\<Your company name>\<AppA>;
for AppB, the registry key is HKEY_CURRENT_USER\Software\<Your company name>\<AppB>.

So, you can do this,

// In AppA


// call this only once
::AfxGetApp()->SetRegistryKey("Your company name");


// You are going to access the registry key of AppB.
ASSERT(m_pszProfileName != NULL);
m_pszProfileName = _tcsdup(_T("AppB"));

// Now you can use the GetProfileInt, GetProfileString, WriteProfileInt, and writeProfileString member functions of CWinApp.


// Switch back to AppA
ASSERT(m_pszProfileName != NULL);
m_pszProfileName = _tcsdup(m_pszAppName);



Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows 10 how make picture as desktop background 2 58
haveThree challenge 22 114
fix34  challenge 9 105
Thin secure Windows 10 5 75
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

920 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

11 Experts available now in Live!

Get 1:1 Help Now