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!!!
Who is Participating?
premadhasConnect With a Mentor Commented:
I've used the registry functions like this. Though complicated works fine.

                      LONG Check;
                      HKEY handleMsgWnd;
                     Check = RegCreateKeyEx(  HKEY_CURRENT_USER,  //handleofan open key
                                                        (_T("Software\\YourCompanyName\\AppName\\KeyName")),  //addressofsubkey name
                                                        0,  // reserved
                                                        NULL, // address of class string
                                                        REG_OPTION_NON_VOLATILE,  // specialoptions flag
                                                        KEY_ALL_ACCESS  ,  // desired security access
                                                        NULL,  // address of key security structure
                                                        &handleMsgWnd, // address of buffer for opened handle
                                                        lpdwDisposition // address of disposition value buffer
                PUCHAR rxBuf = new unsigned char[80];
      LPDWORD lpType = new DWORD;
      LPDWORD lpdwDisposition = new DWORD;
      *lpdwDisposition = 80;
                PHKEY = new phk;

      m_tStrFileImport = theApp.GetProfileString("Appln Name","KeyName");
      if( RegQueryValueEx(*phk,      // handle of key to query
                        (_T("KeyName")),  // address of name of value to query
                        0,                        // reserved
                        lpType,                  // address of buffer for value type
                        rxBuf,                  // address of data buffer
                        lpdwDisposition  // address of data buffer size
                        ) != ERROR_SUCCESS)
            AfxMessageBox("Registry Query Unsuccessful");

Hope it helps
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.
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...

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

CWinApp* pApp = AfxGetApp();
char value[100];

The string will be stored in value...
The string in the value should be "Testing"
Remember to Set the same registry key.
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....
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);
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);
m_pszProfileName = _tcsdup(m_pszAppName);


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.


_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;
Sorry forgot, you will have the Registry value in the pszRes variable. So don't delete it before you have used it...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.