?
Solved

Toggle SetRegistryKey

Posted on 1998-03-12
3
Medium Priority
?
681 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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. …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

770 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