Solved

Toggle SetRegistryKey

Posted on 1998-03-12
3
635 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 49
while loop over for loop 7 102
either24  challenge 19 97
Use of condition with 'serial' in ansible 2 58
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

776 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