Solved

Toggle SetRegistryKey

Posted on 1998-03-12
3
611 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 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);
      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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
List out all word 7 223
dog bark java program 15 82
viewing source code from eclipse 13 74
Problem to event 3 49
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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 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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

13 Experts available now in Live!

Get 1:1 Help Now