//------------------------------------------------------------------------------
// $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