Solved

Regsitry???

Posted on 2000-02-27
9
186 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!!!
0
Comment
Question by:ee_lcpaa
9 Comments
 
LVL 2

Expert Comment

by:vbk_bgm
Comment Utility
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.
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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
0
 
LVL 1

Expert Comment

by:hiap_ho
Comment Utility
use
CString value;
value = GetProfileString("Options","Key1",NULL);
The string will be stored in value...
Remember to Set the same registry key.
0
 
LVL 1

Expert Comment

by:hiap_ho
Comment Utility
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.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:joakimf
Comment Utility
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....
0
 

Expert Comment

by:leowlf
Comment Utility
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);

....



0
 

Accepted Solution

by:
premadhas earned 80 total points
Comment Utility
Hi,
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
Regards,
Prem.
0
 
LVL 1

Expert Comment

by:joakimf
Comment Utility
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);
                 }
}
0
 
LVL 1

Expert Comment

by:joakimf
Comment Utility
Sorry forgot, you will have the Registry value in the pszRes variable. So don't delete it before you have used it...
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

11 Experts available now in Live!

Get 1:1 Help Now