How to read from HKEY_LOCAL_MACHINE in the registry 
Author Message
 How to read from HKEY_LOCAL_MACHINE in the registry

I am trying to get to the User Information that InstallShield express writes
to the windows registry.  I understand that it gets placed in
HKEY_LOCAL_MACHINE\SOFTWARE\<Company>\<AppName>\<Version> and I have
verified that the data is there using REGEDIT.   However, the application
that I am writing wants to read from HKEY_CURRENT_USER not
HKEY_LOCAL_MACHINE.  Do you know how to get an MFC application that is
written with VC++ 5.0 to read from the correct registry key?  I have tried
the GetProfileString() and WriteProfileString() functions, but they access
HKEY_CURRENT_USER.  I have not had any success with OpenRegKey() and
RegQueryValue(), but maybe I am not using them correctly.  Here is a code
snippet from some of the things I have tried without success.
.
.
.
// called in CEMCALC95dApp::InitInstance()
SetRegistryKey(_T("ENERTECH CONSULTANTS"));
.
.
.

// read the User Name, Company Name and Serial Number from the Windows
Registry
BOOL CEMCALC95dApp::ReadFromRegistry()
{  
 char szTemp[256];
 BOOL bSuccess = FALSE;
 long lRet = 0;
 long lLen = 0;
 HKEY hkKey;

 lRet = RegOpenKey(HKEY_LOCAL_MACHINE, "Software", &hkKey);
// lRet = 0 after call, indicating failure

 lRet = RegQueryValue(hkKey, "ENERTECH CONSULTANTS\\EMCALC 95d\\1.0.0.1\\Company",  szTemp, &lLen);
// lRet = 0 after call, indicating failure

 bSuccess = WriteProfileString("1.0.0.1", "Entry", "Value");
// successful, but writes to HKEY_CURRENT_USER
// registry reads HKEY_CURRENT_USER\Software\ENERTECH CONSULTANTS\1.0.0.1\Entry = "Value"

 m_strUserName = GetProfileString("1.0.0.1", "Name", "Unregistered User");
// this call failed completely and returns the default string "Unregistered User"

// m_strCompanyName = GetProfileString("1.0.0.1", "Company", "Unregistered Company");
// m_strSerialNumber = GetProfileString("1.0.0.1", "Serial", "00000");

 return TRUE;

Quote:
}



Mon, 31 Jul 2000 03:00:00 GMT  
 How to read from HKEY_LOCAL_MACHINE in the registry

To read a string from the LOCAL_MACHINE segment of the registry, you could
try something like this

BOOL GetCompanyName(CString& r_csCompanyName)
{
        HKEY hKey = NULL;
        CString csSubKey ("Software\\ENERTECH CONSULTANTS\\EMCALC 95d\\1.0.0.1");
        LONG lRet = ::RegCreateKeyEx(   HKEY_LOCAL_MACHINE,
                                        csSubKey,
                                        NULL,
                                        NULL,
                                        REG_OPTION_NON_VOLATILE,
                                        KEY_ALL_ACCESS | KEY_READ,
                                        NULL,
                                        &hKey,
                                        NULL);
        if (lRet != ERROR_SUCCESS)
                return FALSE;

        DWORD dwSize, dwType;
        LONG lRet = ::RegQueryValueEx(hKey, "Company", NULL, &dwType, NULL,
&dwSize);

        if (lRet == ERROR_SUCCESS)
        {
                int iBuffer = dwSize/sizeof(TCHAR);
                LPBYTE lpByte   = (LPBYTE)r_csCompanyName.GetBuffer(iBuffer);
                lRet = ::RegQueryValueEx(hKey, "Company", NULL, &dwType, lpByte,
&dwSize);
                r_csCompanyName.ReleaseBuffer();
        }

        RegCloseKey(hKey);
        return (lRet == ERROR_SUCCESS);

Quote:
}



Tue, 01 Aug 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Registry - HKEY_LOCAL_MACHINE?

2. Accessing HKEY_LOCAL_MACHINE registry.

3. Registry HKEY_LOCAL_MACHINE

4. Problems reading and writing to HKEY_LOCAL_MACHINE

5. Read the registry to find out where .NET is installed

6. how to read and write registry entry

7. Can't read from registry

8. Reading registry key

9. Read Registry Key

10. Registry - Reading and Writing

11. Reading Binary Data from the Registry

12. reading registry from COM

 

 
Powered by phpBB® Forum Software