We help IT Professionals succeed at work.

Regsitry???

ee_lcpaa
ee_lcpaa asked
on
209 Views
Last Modified: 2013-11-20
Hi,

I have built two applications.

In Application A,

I have use this in the CWinApp file.

WriteProfileString("Options","Key1", "Testing");

Then in application B, I hope can get the value of "Testing" from the registry.

How can I do this?

Thank you!!!
Comment
Watch Question

Commented:
Since the settings are stored under a registry key in HKEY_CURRENT_USER\Software\Company\Product\... your key and settings.
use the same string in SetRegistryKey i.e. SetRegistryKey("same string in both the apps")
and then to retrieve the value use
LPCSTR value = GetProfileString("Options","Key1")
If you are not storing the info in the registry(i.e. by not calling SetRegistryKey), the settings are stored in a private INI file in the windows directory in the file appname.ini.
CERTIFIED EXPERT

Commented:
With Win9x you should call WriteProfileString( NULL, NULL, NULL ); after you wrote all datas you want to flush the cached version of win.ini to disk...

ZOPPO

Commented:
use
CString value;
value = GetProfileString("Options","Key1",NULL);
The string will be stored in value...
Remember to Set the same registry key.

Commented:
use

CWinApp* pApp = AfxGetApp();
char value[100];
strcpy(value,pApp->GetProfileString("Options","Key1",NULL));


The string will be stored in value...
The string in the value should be "Testing"
Remember to Set the same registry key.

Commented:
You can always use the Registry APIs to call any key in the registry. Perhaps it's not perfect to use the same registry key for 2 different programs. But thats up to the programmer....

Commented:
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("App A");

....

// 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);

....



This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
If you want to use the Registry API functions, then you should do it in a safe way, you should always call RegQueryValueEx two times. The first time to get the length of the value, then you should use that length to create a char value of the proper size. And then call QueryValue again to get the value. Also make sure to Close the Registry Handle when you are done.

Example:

_TXCHAR *pszRes = NULL;
DWORD dwSize;
HKEY hKey;
if (RegOpenKeyEx(HKEY_CURRENT_USER, lpszSubKey, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
      {
            if (RegQueryValueEx(hKey, lpszValueName, NULL, NULL, NULL, &dwSize) == ERROR_SUCCESS)
            {
                  pszRes = new _TXCHAR[dwSize];
                  RegQueryValueEx(hKey, lpszValueName, NULL, NULL, pszRes, &dwSize);
                  delete [] pszRes;
                  RegCloseKey(hKey);
                 }
}

Commented:
Sorry forgot, you will have the Registry value in the pszRes variable. So don't delete it before you have used it...

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.