Solved

Regsitry???

Posted on 2000-02-27
9
187 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 91
Folder Comparison 12 53
Dell Alienware Graphics Amplifier Driver for non-Alienware systems 18 195
Excel file not created as expected 7 51
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…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

864 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

21 Experts available now in Live!

Get 1:1 Help Now