Click here to Skip to main content
15,892,674 members
Articles / Desktop Programming / MFC

Dynamic Items

Rate me:
Please Sign up or sign in to vote.
4.93/5 (15 votes)
24 Feb 20034 min read 200.7K   6.7K   85  
A class and an easy way to dynamically add items stored in a file to a menu
// RegistryWrp.cpp : implementation file
//

#include "stdafx.h"
#include "RegistryWrp.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define APPLICATION_KEY "Software\\Excoffier\\Notify 1.1"
/////////////////////////////////////////////////////////////////////////////
// CRegistryWrp message handlers
///////////////////////////////////////////////////////////////////////////////////

void CRegistryWrp::WriteKeyToRegistry(CString strRegKey, CString strKey)
{
	// Write new string in the registry

    HKEY key;
    DWORD size, type, disposition;
    if (RegOpenKeyEx(HKEY_CURRENT_USER,APPLICATION_KEY,0,
        KEY_WRITE,&key)!=ERROR_SUCCESS)
    {	
        RegCreateKeyEx(HKEY_CURRENT_USER,APPLICATION_KEY,0,"",
            REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,
            &key,&disposition);
    }
	type = REG_SZ;
    size = strKey.GetLength();
    RegSetValueEx(key,strRegKey,0,type,
        (LPBYTE)LPCSTR(strKey),size);

    RegCloseKey(key);
}


/////////////////////////////////////////////////////////////////////////////

void CRegistryWrp::WriteKeyToRegistry(CString strRegKey, int nKey)
{
	// Write new integer in the registry
    HKEY key;
    DWORD size, type, disposition;
    if (RegOpenKeyEx(HKEY_CURRENT_USER,APPLICATION_KEY,0,
        KEY_WRITE,&key)!=ERROR_SUCCESS)
    {	
        RegCreateKeyEx(HKEY_CURRENT_USER,APPLICATION_KEY,0,"",
            REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,
            &key,&disposition);
    }
	type = REG_DWORD;
    size = 4;
    RegSetValueEx(key,strRegKey,0,type,(LPBYTE)&nKey,size);

    RegCloseKey(key);
}

/////////////////////////////////////////////////////////////////////////////

void CRegistryWrp::GetKeyFromRegistry(CString strRegKey, CString &strKey)
{
	// Read new string in the registry
	HKEY key;
    DWORD size, type;
    if (RegOpenKeyEx(
        HKEY_CURRENT_USER,APPLICATION_KEY,0,KEY_READ,&key) ==
        ERROR_SUCCESS)
    {
        size=128;
        type = REG_SZ;
        LPSTR psz = strKey.GetBuffer(size);
        RegQueryValueEx(
            key,strRegKey,0,&type,(LPBYTE)psz,&size);
        strKey.ReleaseBuffer();

		RegCloseKey(key);
	}

}

/////////////////////////////////////////////////////////////////////////////

void CRegistryWrp::GetKeyFromRegistry(CString strRegKey, int &nKey)
{
	// Read new string in the registry
	HKEY key;
    DWORD size, type;
    if (RegOpenKeyEx(
        HKEY_CURRENT_USER,APPLICATION_KEY,0,KEY_READ,&key) ==
        ERROR_SUCCESS)
    {
        size=4;
        type = REG_DWORD;
        RegQueryValueEx(key,strRegKey,0,&type,
            (LPBYTE)&nKey,&size);
	    RegCloseKey(key);
	}
}

/////////////////////////////////////////////////////////////////////////////

BOOL CRegistryWrp::IsValueExists(CString strRegKey)
{
	// Verify existence of a value in the registry

	HKEY hkey;
	LONG lReturn=0;

	if (ERROR_SUCCESS == RegOpenKeyEx (HKEY_CURRENT_USER, APPLICATION_KEY, 0L, KEY_ALL_ACCESS, &hkey) )
	{	lReturn = RegQueryValueEx(HKEY_CURRENT_USER, strRegKey, NULL, NULL, NULL, NULL);
		RegCloseKey(hkey);
	}
	return lReturn;
}
/////////////////////////////////////////////////////////////////////////////

BOOL CRegistryWrp::IsKeyExists(CString strPath)
{
	// Verify existence of a key in the registry

	HKEY hkey;
	BOOL bRet=FALSE;
	ASSERT(strPath);

	if (ERROR_SUCCESS == RegOpenKeyEx (HKEY_CURRENT_USER, strPath, 0L, KEY_ALL_ACCESS, &hkey) )
	{	bRet=TRUE;
		RegCloseKey(hkey);
	}
	return bRet;
}

/////////////////////////////////////////////////////////////////////////////

BOOL CRegistryWrp::CreateKey(CString strPath)
{
	// Create a new key in the registry

    HKEY key;
    DWORD  disposition;
	BOOL bRet=FALSE;
    if (RegOpenKeyEx(HKEY_CURRENT_USER,strPath,0,
        KEY_WRITE,&key)!=ERROR_SUCCESS)
    {
        RegCreateKeyEx(HKEY_CURRENT_USER,strPath,0,"",
            REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,
            &key,&disposition);
		bRet = TRUE;
	
		RegCloseKey(key);
    }
	return bRet;

}

/////////////////////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Team Leader
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions