Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

The SBJ MVC Framework - The Design View, Responding to Model Change

, 19 Mar 2009
A Model-View-Controller Framework that integrates with the MFC Doc/View architecture.
SbjDevBin202.zip
SbjDevSrc202.zip
SbjCore
res
bigcat.ico
main.bmp
properties.bmp
properties_hc.bmp
properties_wnd_hc.ico
Shapes
Data
res
buttons.bmp
filelarge.bmp
filesmall.bmp
filesmall.png
main.bmp
Shapes.ico
ShapesDoc.ico
Toolbar.bmp
Toolbar256.bmp
treeimages.bmp
Write - Copy.png
Write.png
Write1.png
writelarge.bmp
writesmall - Copy.bmp
writesmall - Copy.png
writesmall.bmp
writesmall.png
XmlMvc
res
//------------------------------------------------------------------------------
// $Workfile: RegistryEx.cpp $
// $Header: /DevNet/SbjCore/SbjCore/Sys/RegistryEx.cpp 2     4/18/07 6:40p Steve $
//
//	Copyright � 2006 Tartus, Inc.
// All rights reserved.
//
// The information contained herein is confidential, proprietary to
// Tartus, Inc., and considered a trade secret as defined
// in section 499C of the penal code of the State of California.  Use
// of this information by anyone other than authorized employees of
// Tartus, Inc. is granted only under a written non-disclosure
// agreement, which expressly prescribes the scope and manner of such
// use.
//
// *** Authors ***
//	 Neal O'Hara
//	 John La Porta
//	 Zhimin Lin
//	 Andreas Christmann
//	 Steve Johnson
//	 Frankie Jefferson
//	 Tartus, Inc.
//
// $Revision: 2 $
//
//-----------------------------------------------------------------------------

#include "stdafx.h"
#include "RegistryEx.h"

namespace SbjCore
{
	namespace Sys
	{

		RegistryEx::RegistryEx(	
			HKEY hKeyToOpen /*= HKEY_CURRENT_USER*/, 
			LPCTSTR lpszSubKey /*= NULL*/,
			LPCTSTR lpszComputerName /*= NULL*/)
		{
			m_hRegistry = (hKeyToOpen);
			m_hKey = (NULL);
			if ((lpszComputerName != NULL) && (_tcslen(lpszComputerName)) 
				&& (hKeyToOpen != HKEY_CLASSES_ROOT) && (hKeyToOpen != HKEY_CURRENT_USER))
			{
				LONG lRslt = ::RegConnectRegistry( (TCHAR *) lpszComputerName, hKeyToOpen, &m_hRegistry);

				if (lRslt !=  ERROR_SUCCESS) // then use local machine
				{
					m_hRegistry = hKeyToOpen;
				}
			}

			if (lpszSubKey != NULL)
			{
				m_hKey = CreateKey(NULL, lpszSubKey);
			}
		}


		RegistryEx::~RegistryEx()
		{
		}


		HKEY RegistryEx::CreateKey(HKEY hKey, LPCTSTR lpszKey)
		{
			ASSERT(m_hRegistry != NULL);

			HKEY hNewKey = NULL;
			HKEY hRootKey = m_hRegistry;

			if (hKey != NULL)
			{
				hRootKey = hKey;
			}

			(void)::RegCreateKeyEx(hRootKey, lpszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE | KEY_READ, NULL, &hNewKey, NULL);

			CloseKey(hKey);

			// Make this the current key
			if(hNewKey)
			{
				m_hKey = hNewKey;
			}

			return hNewKey;
		}

		HKEY RegistryEx::OpenKey(HKEY hKey, LPCTSTR lpszKey)
		{
			ASSERT(m_hRegistry != NULL);

			HKEY hRootKey = hKey;
			HKEY hNewKey = NULL;

			if (NULL == hKey)
			{
				hRootKey = m_hRegistry;
			}

			LONG lRslt = ::RegOpenKeyEx(hRootKey, lpszKey, 0, KEY_WRITE | KEY_READ, &hNewKey);

			if (ERROR_SUCCESS == lRslt)
			{
				// Make this the current key
				if(hNewKey)
				{
					m_hKey = hNewKey;
				}

				CloseKey(hKey);
			}
			else
			{
				hNewKey = NULL;
			}

			return hNewKey;
		}


		void RegistryEx::CloseKey(HKEY& hKey)
		{
			if (hKey != NULL)
			{
				::RegCloseKey(hKey);
			}

			hKey = NULL;
		}

		bool RegistryEx::GetRegValue(LPCTSTR lpszValue, CString& sData, const LPCTSTR lpszDefault /*= NULL*/)
		{
			ASSERT(m_hRegistry != NULL);

			bool bRslt = false;
			LONG lRslt = ERROR_SUCCESS;
			DWORD dwType = REG_SZ;
			DWORD dwSize = 64;
			LPBYTE lpData = new BYTE[dwSize];

			::ZeroMemory(lpData, dwSize);

			sData.Empty();

			lRslt = ::RegQueryValueEx(m_hKey, lpszValue, NULL, &dwType, lpData, &dwSize);

			if (ERROR_MORE_DATA == lRslt)
			{
				if (lpData != NULL)
				{
					delete [] lpData;
				}

				lpData = new BYTE[dwSize];

				::ZeroMemory(lpData, dwSize);

				if (ERROR_SUCCESS == ::RegQueryValueEx(m_hKey, lpszValue, NULL, &dwType, lpData, &dwSize))
				{
					sData = lpData;
					bRslt = true;
				}
			}
			else if (ERROR_SUCCESS == lRslt)
			{
				sData = lpData;
				bRslt = true;
			}

			if (lpData != NULL)
			{
				delete [] lpData;
			}

			if (!bRslt && (lpszDefault != NULL))
			{

				sData = lpszDefault;
				bRslt = SetRegValue(lpszValue, lpszDefault);
			}


			return bRslt;
		}

		bool RegistryEx::GetRegValue(LPCTSTR lpszValue, DWORD& dwData, const DWORD dwDefault /*= -1*/) 
		{
			ASSERT(m_hRegistry != NULL);

			bool bRslt = false;

			DWORD dwType = REG_DWORD;
			DWORD dwSize = sizeof(DWORD);

			dwData = 0;

			bRslt = ERROR_SUCCESS == ::RegQueryValueEx(m_hKey, lpszValue, NULL, &dwType, (LPBYTE)&dwData, &dwSize);

			if (!bRslt && (dwDefault != -1))
			{

				dwData = dwDefault;
				bRslt = SetRegValue(lpszValue, dwDefault);
			}

			return bRslt;

		}


		bool RegistryEx::SetRegValue(LPCTSTR lpszValue, LPCTSTR lpszData)
		{
			ASSERT(m_hRegistry != NULL);

			bool bRslt = false;
			DWORD dwType = REG_SZ;

			if (ERROR_SUCCESS == ::RegSetValueEx(m_hKey, (LPCSTR)lpszValue, 0, dwType, (LPBYTE)lpszData, (DWORD)_tcslen(lpszData)))
			{
				//4.8.2004 SBJ Removed call to ::RegFlushKey 
				//        ::RegFlushKey(m_hKey);
				bRslt = true;
			}

			return bRslt;
		}

		bool RegistryEx::SetRegValue(LPCTSTR lpszValue, DWORD dwData)
		{
			ASSERT(m_hRegistry != NULL);

			bool bRslt = false;
			DWORD dwType = REG_DWORD;

			if (ERROR_SUCCESS == ::RegSetValueEx(m_hKey, (LPCSTR)lpszValue, 0, dwType, (LPBYTE)&dwData, sizeof(DWORD)))
			{
				//4.8.2004 SBJ Removed call to ::RegFlushKey 
				//        ::RegFlushKey(m_hKey);
				bRslt = true;
			}

			return bRslt;
		}

		//--------------------  AskAndCreate  --------------------------
		//  Displays the file open dialog to locate the specified file.
		//  If the file is located, and reg key specified by lpszKey and
		//  lpszValue is created with the value being the filename
		//  chosen from the file open dialog.  True is returne if the key
		//  is created, otherwise false.
		//--------------------------------------------------------------
		bool RegistryEx::FindAndCreate(
			LPCTSTR lpszKey,
			LPCTSTR lpszValue,
			LPCTSTR lpszFileExt,
			LPCTSTR lpszExtType)
		{
			bool bSuccess = false;

			if(m_hRegistry)
			{
				CString rPath = "" ;
				CString strExtType( (lpszExtType) ? lpszExtType : _T("Specified type") );

				CString strFileTypes;
				strFileTypes.Format(_T("%s (*.%s)|*.%s|All Files (*.*)|*.*||"), strExtType, lpszFileExt, lpszFileExt);

				CFileDialog openDlg(TRUE,                                  // Open Dialog
					lpszFileExt,                           // extension for specified files
					rPath,			                       // No Default file name
					OFN_HIDEREADONLY | OFN_PATHMUSTEXIST,  // Don't create the path
					strFileTypes,                          // file types to look for
					AfxGetMainWnd());                      // parent window

				CString strTitle (_T("File not found"));

				openDlg.m_ofn.lpstrTitle = strTitle;

				if (openDlg.DoModal() == IDOK)
				{
					rPath = openDlg.GetPathName();

					// create the key
					if( CreateKey(m_hRegistry, lpszKey) )
					{
						// set the value
						if( SetRegValue(lpszValue, rPath) )
						{
							bSuccess = true;
						}
					}
				}
			}

			return bSuccess;
		}

		int RegistryEx::EnumerateRegValues(t_RegValuesMap& values)
		{
			UINT nIndex = 0;
			CString sKeyName;
			while (EnumerateValues(nIndex, sKeyName))
			{
				CString sValue;
				GetRegValue(sKeyName, sValue);
				values[sKeyName] = sValue;
				nIndex++;
			}
			return nIndex;
		}

	}
}

//*** Modification History ***
// $Log: /DevNet/SbjCore/SbjCore/Sys/RegistryEx.cpp $
// 
// 2     4/18/07 6:40p Steve
// 
// 1     4/02/07 4:56p Steve
// 
// 4     11/21/06 2:24p Steve
// 
// 3     4/26/06 12:00p Steve

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

SBJ

United States United States
Real name is Steve Johnson. Programming since 1979. Started on a Heathkit Micro with a DEC LSI-11 and UCSD Pascal. Moved to PCs & DOS as soon as Turbo Pascal became available. Did some Assembly, ISR, TSR etc. All this while working for a Manufacturing Co. for 8 years. Had my own solo Co. doing barcode labeling software for 4 years (terrible business man, all I wanted to do was code). Since then working for various software companies. Moved to Windows around the time of 3.1 with Borland C then C++. Then on to VC++ and MFC, and just about anything I could get my hands on or had to learn for my job, and been at it ever since. Of course recently I've been playing with .NET, ASP, C#, WPF etc.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 19 Mar 2009
Article Copyright 2009 by SBJ
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid