Solved

Regsitry???

Posted on 2000-02-27
9
189 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
ID: 2564015
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 31

Expert Comment

by:Zoppo
ID: 2564082
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
ID: 2564088
use
CString value;
value = GetProfileString("Options","Key1",NULL);
The string will be stored in value...
Remember to Set the same registry key.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 
LVL 1

Expert Comment

by:hiap_ho
ID: 2564097
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
 
LVL 1

Expert Comment

by:joakimf
ID: 2564106
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
ID: 2564143
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
ID: 2565024
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
ID: 2565108
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
ID: 2565115
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

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.

Question has a verified solution.

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

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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