⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registry.cpp

📁 一个不同目录中多个文件拷贝的助手程序
💻 CPP
字号:
//-----------------------------------------------------------------------//
// This is a part of the Multicopier.									 //	
// Autor  :  Ahmed Ismaiel Zakaria										 //
// (C) 2002 FCIS Egypt  All rights reserved								 //
// This code is provided "as is", with absolutely no warranty expressed  //
// or implied. Any use is at your own risk.								 //		
// You must obtain the author's consent before you can include this code //
// in a software library.												 //
// If the source code in  this file is used in any application			 //
// then acknowledgement must be made to the author of this program		 //	
// ahmed_ismaiel@hotmail.com											 //
//-----------------------------------------------------------------------//

#include "StdAfx.h"
#include "registry.h"

CRegDWORD::CRegDWORD(void)
{
	m_value = 0;
	m_defaultvalue = 0;
	m_key = "";
	m_base = HKEY_CURRENT_USER;
	m_read = FALSE;
	m_force = FALSE;
}
/**
 * Constructor.
 * @param key the path to the key, including the key. example: "Software\\Company\\SubKey\\MyValue"
 * @param def the default value used when the key does not exist or a read error occured
 * @param force set to TRUE if no cache should be used, i.e. always read and write directly from/to registry
 * @param base a predefined base key like HKEY_LOCAL_MACHINE. see the SDK documentation for more information.
 */
CRegDWORD::CRegDWORD(CString key, DWORD def, BOOL force, HKEY base)
{
	m_value = 0;
	m_defaultvalue = def;
	m_force = force;
	m_base = base;
	m_read = FALSE;
	key.TrimLeft("\\");
	m_path = key.Left(key.ReverseFind('\\'));
	m_path.TrimRight("\\");
	m_key = key.Right(key.GetLength() - key.ReverseFind('\\'));
//	m_key.Trim("\\"); back ward compatability
	m_key.TrimRight("\\");
	m_key.TrimLeft ("\\");
	read();
}


CRegDWORD::~CRegDWORD(void)
{
	write();
}

DWORD	CRegDWORD::read()
{
	ASSERT(m_key != "");
	if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE, &m_hKey)==ERROR_SUCCESS)
	{
		int size = sizeof(m_value);
		DWORD type;
		if (RegQueryValueEx(m_hKey, m_key, NULL, &type, (BYTE*) &m_value,(LPDWORD) &size)==ERROR_SUCCESS)
		{
			ASSERT(type==REG_DWORD);
			m_read = TRUE;
			RegCloseKey(m_hKey);
			return m_value;
		}
		else
		{
			RegCloseKey(m_hKey);
			return m_defaultvalue;
		}
	}
	return m_defaultvalue;
}

void CRegDWORD::write()
{
	ASSERT(m_key != "");
	DWORD disp;
	if (RegCreateKeyEx(m_base, m_path, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &m_hKey, &disp)!=ERROR_SUCCESS)
	{
		return;
	}
	if (RegSetValueEx(m_hKey, m_key, 0, REG_DWORD,(const BYTE*) &m_value, sizeof(m_value))==ERROR_SUCCESS)
	{
		m_read = TRUE;
	}
	RegCloseKey(m_hKey);
}


CRegDWORD::operator DWORD()
{
	if ((m_read)&&(!m_force))
		return m_value;
	else
	{
		return read();
	}
}

CRegDWORD& CRegDWORD::operator =(DWORD d)
{
	if ((d==m_value)&&(!m_force))
	{
		//no write to the registry required, its the same value
		return *this;
	}
	m_value = d;
	write();
	return *this;
}

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


CRegString::CRegString(void)
{
	m_value = "";
	m_defaultvalue = "";
	m_key = "";
	m_base = HKEY_CURRENT_USER;
	m_read = FALSE;
	m_force = FALSE;
}
/**
 * Constructor.
 * @param key the path to the key, including the key. example: "Software\\Company\\SubKey\\MyValue"
 * @param def the default value used when the key does not exist or a read error occured
 * @param force set to TRUE if no cache should be used, i.e. always read and write directly from/to registry
 * @param base a predefined base key like HKEY_LOCAL_MACHINE. see the SDK documentation for more information.
 */
CRegString::CRegString(CString key, CString def, BOOL force, HKEY base)
{
	m_value = "";
	m_defaultvalue = def;
	m_force = force;
	m_base = base;
	m_read = FALSE;
	key.TrimLeft("\\");
	m_path = key.Left(key.ReverseFind('\\'));
	m_path.TrimRight("\\");
	m_key = key.Right(key.GetLength() - key.ReverseFind('\\'));
//	m_key.Trim("\\");
m_key.TrimRight("\\");
	m_key.TrimLeft ("\\");
	read();
}


CRegString::~CRegString(void)
{
	write();
}

CString	CRegString::read()
{
	ASSERT(m_key != "");
	if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE, &m_hKey)==ERROR_SUCCESS)
	{
		int size = 0;
		DWORD type;
		char *pStr = " ";
		RegQueryValueEx(m_hKey, m_key, NULL, &type, (BYTE*) pStr, (LPDWORD) &size);
		pStr = new char[size];
		if (RegQueryValueEx(m_hKey, m_key, NULL, &type, (BYTE*) pStr,(LPDWORD) &size)==ERROR_SUCCESS)
		{
			m_value = CString(pStr);
			delete pStr;
			ASSERT(type==REG_SZ);
			m_read = TRUE;
			RegCloseKey(m_hKey);
			return m_value;
		}
		else
		{
			delete pStr;
			RegCloseKey(m_hKey);
			return m_defaultvalue;
		}
	}
	return m_defaultvalue;
}

void CRegString::write()
{
	ASSERT(m_key != "");
	DWORD disp;
	if (RegCreateKeyEx(m_base, m_path, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &m_hKey, &disp)!=ERROR_SUCCESS)
	{
		return;
	}
	if (RegSetValueEx(m_hKey, m_key, 0, REG_SZ, (BYTE *)(LPCSTR)m_value, m_value.GetLength()+1)==ERROR_SUCCESS)
	{
		m_read = TRUE;
	}
	RegCloseKey(m_hKey);
}


CRegString::operator CString()
{
	if ((m_read)&&(!m_force))
		return m_value;
	else
	{
		return read();
	}
}

CRegString& CRegString::operator =(CString s)
{
	if ((s==m_value)&&(!m_force))
	{
		//no write to the registry required, its the same value
		return *this;
	}
	m_value = s;
	write();
	return *this;
}


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

CRegRect::CRegRect(void)
{
	m_value = CRect(0,0,0,0);
	m_defaultvalue = CRect(0,0,0,0);
	m_key = "";
	m_base = HKEY_CURRENT_USER;
	m_read = FALSE;
	m_force = FALSE;
}
/**
 * Constructor.
 * @param key the path to the key, including the key. example: "Software\\Company\\SubKey\\MyValue"
 * @param def the default value used when the key does not exist or a read error occured
 * @param force set to TRUE if no cache should be used, i.e. always read and write directly from/to registry
 * @param base a predefined base key like HKEY_LOCAL_MACHINE. see the SDK documentation for more information.
 */
CRegRect::CRegRect(CString key, CRect def, BOOL force, HKEY base)
{
	m_value = CRect(0,0,0,0);
	m_defaultvalue = def;
	m_force = force;
	m_base = base;
	m_read = FALSE;
	key.TrimLeft("\\");
	m_path = key.Left(key.ReverseFind('\\'));
	m_path.TrimRight("\\");
	m_key = key.Right(key.GetLength() - key.ReverseFind('\\'));
//	m_key.Trim("\\");
	m_key.TrimRight("\\");
	m_key.TrimLeft ("\\");
	read();
}


CRegRect::~CRegRect(void)
{
	write();
}

CRect	CRegRect::read()
{
	ASSERT(m_key != "");
	if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE, &m_hKey)==ERROR_SUCCESS)
	{
		int size = 0;
		DWORD type;
		LPRECT pRect = NULL;
		RegQueryValueEx(m_hKey, m_key, NULL, &type, (BYTE*) pRect, (LPDWORD) &size);
		pRect = (LPRECT)new char[size];
		if (RegQueryValueEx(m_hKey, m_key, NULL, &type, (BYTE*) pRect,(LPDWORD) &size)==ERROR_SUCCESS)
		{
			m_value = CRect(pRect);
			delete pRect;
			ASSERT(type==REG_BINARY);
			m_read = TRUE;
			RegCloseKey(m_hKey);
			return m_value;
		}
		else
		{
			delete pRect;
			RegCloseKey(m_hKey);
			return m_defaultvalue;
		}
	}
	return m_defaultvalue;
}

void CRegRect::write()
{
	ASSERT(m_key != "");
	DWORD disp;
	if (RegCreateKeyEx(m_base, m_path, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &m_hKey, &disp)!=ERROR_SUCCESS)
	{
		return;
	}
	
	if (RegSetValueEx(m_hKey, m_key, 0, REG_BINARY, (BYTE *)(LPRECT)m_value, sizeof(m_value))==ERROR_SUCCESS)
	{
		m_read = TRUE;
	}
	else
	{
	}
	RegCloseKey(m_hKey);
}


CRegRect::operator CRect()
{
	if ((m_read)&&(!m_force))
		return m_value;
	else
	{
		return read();
	}
}

CRegRect& CRegRect::operator =(CRect s)
{
	if ((s==m_value)&&(!m_force))
	{
		//no write to the registry required, its the same value
		return *this;
	}
	m_value = s;
	write();
	return *this;
}



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

CRegPoint::CRegPoint(void)
{
	m_value = CPoint(0,0);
	m_defaultvalue = CPoint(0,0);
	m_key = "";
	m_base = HKEY_CURRENT_USER;
	m_read = FALSE;
	m_force = FALSE;
}
/**
 * Constructor.
 * @param key the path to the key, including the key. example: "Software\\Company\\SubKey\\MyValue"
 * @param def the default value used when the key does not exist or a read error occured
 * @param force set to TRUE if no cache should be used, i.e. always read and write directly from/to registry
 * @param base a predefined base key like HKEY_LOCAL_MACHINE. see the SDK documentation for more information.
 */
CRegPoint::CRegPoint(CString key, CPoint def, BOOL force, HKEY base)
{
	m_value = CPoint(0,0);
	m_defaultvalue = def;
	m_force = force;
	m_base = base;
	m_read = FALSE;
	key.TrimLeft("\\");
	m_path = key.Left(key.ReverseFind('\\'));
	m_path.TrimRight("\\");
	m_key = key.Right(key.GetLength() - key.ReverseFind('\\'));
//	m_key.Trim("\\");
	m_key.TrimRight("\\");
	m_key.TrimLeft ("\\");
	read();
}


CRegPoint::~CRegPoint(void)
{
	write();
}

CPoint	CRegPoint::read()
{
	ASSERT(m_key != "");
	if (RegOpenKeyEx(m_base, m_path, 0, KEY_EXECUTE, &m_hKey)==ERROR_SUCCESS)
	{
		int size = 0;
		DWORD type;
		POINT* pPoint = NULL;
		RegQueryValueEx(m_hKey, m_key, NULL, &type, (BYTE*) pPoint, (LPDWORD) &size);
		pPoint = (POINT *)new char[size];
		if (RegQueryValueEx(m_hKey, m_key, NULL, &type, (BYTE*) pPoint,(LPDWORD) &size)==ERROR_SUCCESS)
		{
			m_value = CPoint(*pPoint);
			delete pPoint;
			ASSERT(type==REG_BINARY);
			m_read = TRUE;
			RegCloseKey(m_hKey);
			return m_value;
		}
		else
		{
			delete pPoint;
			RegCloseKey(m_hKey);
			return m_defaultvalue;
		}
	}
	return m_defaultvalue;
}

void CRegPoint::write()
{
	ASSERT(m_key != "");
	DWORD disp;
	if (RegCreateKeyEx(m_base, m_path, 0, "", REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &m_hKey, &disp)!=ERROR_SUCCESS)
	{
		return;
	}
	
	if (RegSetValueEx(m_hKey, m_key, 0, REG_BINARY, (BYTE *)(POINT *)&m_value, sizeof(m_value))==ERROR_SUCCESS)
	{
		m_read = TRUE;
	}
	RegCloseKey(m_hKey);
}


CRegPoint::operator CPoint()
{
	if ((m_read)&&(!m_force))
		return m_value;
	else
	{
		return read();
	}
}

CRegPoint& CRegPoint::operator =(CPoint s)
{
	if ((s==m_value)&&(!m_force))
	{
		//no write to the registry required, its the same value
		return *this;
	}
	m_value = s;
	write();
	return *this;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -