Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Toggle SetRegistryKey

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

CProjApp::InitInstance()
{
  SetRegistryKey(_T("ThisProj"));
  ...        
}

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);
 ...
}
0
Comment
Question by:leowlf
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 200 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);
      free((void*)m_pszRegistryKey);
      m_pszRegistryKey = _tcsdup(lpszRegistryKey);
      free((void*)m_pszProfileName);
      m_pszProfileName = _tcsdup(m_pszAppName);
      AfxEnableMemoryTracking(bEnable);
#endif
}

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.
0
 

Author Comment

by:leowlf
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.
0
 
LVL 23

Expert Comment

by:chensu
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);
free((void*)m_pszProfileName);
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);
free((void*)m_pszProfileName);
m_pszProfileName = _tcsdup(m_pszAppName);

...

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Integration Management Part 2
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

569 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