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

📄 registry.cpp

📁 Visual C++下的界面设计
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	delete[] sectionName;

	return name;
}


/*******************************************************************/
/* Operations */

/*
void Descend(const char* Section)

ARGUMENTS
	Section - name of the subsection to be assumed as the current section

DESCRIPTION
	You would typically use this function in conjunction with
	Ascend(). When an object that keeps some of its data in the
	registry initializes itself, Descend() is called to obtain
	access to its own private area within the parent object's
	registry space. Call Ascend() to return to the parent object's
	registry space upon termination.

	You can think of Descend() and Ascend() as 'PUSH' and 'POP'
	operations; before a subroutine is executed registers are
	pushed onto the stack thus giving the subroutine the freedom
	to do almost anything without worrying about corrupting the
	state of data at the point of call. When the subroutine is
	done it pops the old data. If you use Descend() or Ascend(),
	you should always pair the calls.
*/
void CRegistry::Descend(const char* Section)
{
	ASSERT(Section != NULL);
	ASSERT(strlen(Section) > 0);

	mActiveSection += "\\";
	mActiveSection += Section;

	CString rootSection = mRootSection;

	Close();
	OpenRoot(mRootArea, mActiveSection, mAccess);
	
	mRootSection = rootSection;
}

/*
void Ascend()

DESCRIPTION
	Backs up to the parent section of the current section.
	The parent section is set to be the current section.

	Note that Ascend() will only back up to the root section.
	It will not go any further no matter how many times it
	gets called.
*/
void CRegistry::Ascend()
{
	// Ascending above the root section not allowed.
	if (mActiveSection == mRootSection)
		return;

	int pos = mActiveSection.ReverseFind('\\');
	ASSERT(pos != -1);
	mActiveSection = mActiveSection.Left(pos);

	CString rootSection = mRootSection;

	Close();
	OpenRoot(mRootArea, mActiveSection, mAccess);
	
	mRootSection = rootSection;
}

/*
bool KeyExists(const char* Key)

ARGUMENTS
	Key - name of a key

DESCRIPTION
	Checks for existence of the key named 'Key' in the
	current section. Returns 'true' if the key exists 
	and 'false' otherwise.
*/
bool CRegistry::KeyExists(const char* Key)
{
	LONG result = ::RegQueryValueEx(mActiveSectionKey, Key, NULL, NULL, NULL, NULL);
	if (result != ERROR_SUCCESS)
		return false;

	return true;
}

/*
bool SectionExists(const char* Section)

ARGUMENTS
	Section - name of a section

DESCRIPTION
	Checks for existence of the subsection named 'Section' in
	the current section. Returns 'true' if the subsection exists
	and 'false' otherwise.
*/
bool CRegistry::SectionExists(const char* Section)
{
	REGSAM accessMask = 0;
	if (mAccess & accessRead)
		accessMask |= KEY_ENUMERATE_SUB_KEYS | KEY_EXECUTE | KEY_QUERY_VALUE | KEY_READ;
	if (mAccess & accessWrite)
		accessMask |= KEY_CREATE_LINK | KEY_CREATE_SUB_KEY | KEY_SET_VALUE | KEY_WRITE;

	HKEY tempKey;
	LONG result = ::RegOpenKeyEx(mActiveSectionKey, Section, 0, accessMask, &tempKey);
	if (result != ERROR_SUCCESS)
		return false;

	::RegCloseKey(tempKey);

	return true;
}

/*******************************************************************/
/*
	The following functions store key-value pairs into the current section.

	If the specified key does not exist, it is created; otherwise
	its value is overwritten with the new one.
*/

void CRegistry::Store(const char* Key, signed char Value)
{
	StoreDWORD(Key, (DWORD)Value);
}

void CRegistry::Store(const char* Key, unsigned char Value)
{
	StoreDWORD(Key, (DWORD)Value);
}

void CRegistry::Store(const char* Key, signed short Value)
{
	StoreDWORD(Key, (DWORD)Value);
}

void CRegistry::Store(const char* Key, unsigned short Value)
{
	StoreDWORD(Key, (DWORD)Value);
}

void CRegistry::Store(const char* Key, signed int Value)
{
	StoreDWORD(Key, (DWORD)Value);
}

void CRegistry::Store(const char* Key, unsigned int Value)
{
	StoreDWORD(Key, (DWORD)Value);
}

void CRegistry::Store(const char* Key, signed long Value)
{
	StoreDWORD(Key, (DWORD)Value);
}

void CRegistry::Store(const char* Key, unsigned long Value)
{
	StoreDWORD(Key, (DWORD)Value);
}

void CRegistry::Store(const char* Key, float Value)
{
	CString value;
	value.Format("%f", Value);

	StoreString(Key, value);
}

void CRegistry::Store(const char* Key, double Value)
{
	// Resolution of the 'double' type is 16 decimal digits.
	// We use 17 just in case.
	CString value;
	value.Format("%.17e", Value);

	StoreString(Key, value);
}

void CRegistry::Store(const char* Key, const CString& Value)
{
	StoreString(Key, Value);
}

/*******************************************************************/
/*
	The following functions restore key-value pairs from the current section.

	If the specified key does not exist, the value of 'Value' is unchanged.
*/

void CRegistry::Restore(const char* Key, signed char& Value)
{
	DWORD value = Value;
	RestoreDWORD(Key, value);

	Value = (signed char)value;
}

void CRegistry::Restore(const char* Key, unsigned char& Value)
{
	DWORD value = Value;
	RestoreDWORD(Key, value);

	Value = (unsigned char)value;
}

void CRegistry::Restore(const char* Key, signed short& Value)
{
	DWORD value = Value;
	RestoreDWORD(Key, value);

	Value = (signed short)value;
}

void CRegistry::Restore(const char* Key, unsigned short& Value)
{
	DWORD value = Value;
	RestoreDWORD(Key, value);

	Value = (unsigned short)value;
}

void CRegistry::Restore(const char* Key, signed int& Value)
{
	DWORD value = Value;
	RestoreDWORD(Key, value);

	Value = (signed int)value;
}

void CRegistry::Restore(const char* Key, unsigned int& Value)
{
	DWORD value = Value;
	RestoreDWORD(Key, value);

	Value = (unsigned int)value;
}

void CRegistry::Restore(const char* Key, signed long& Value)
{
	DWORD value = Value;
	RestoreDWORD(Key, value);

	Value = (signed long)value;
}

void CRegistry::Restore(const char* Key, unsigned long& Value)
{
	DWORD value = Value;
	RestoreDWORD(Key, value);

	Value = (unsigned long)value;
}

void CRegistry::Restore(const char* Key, float& Value)
{
	CString value;
	value.Format("%f", Value);

	RestoreString(Key, value);

	Value = (float)atof(value);
}

void CRegistry::Restore(const char* Key, double& Value)
{
	// Resolution of the 'double' type is 16 decimal digits.
	// We use 17 just in case.
	CString value;
	value.Format("%.17e", Value);

	RestoreString(Key, value);

	Value = atof(value);
}

void CRegistry::Restore(const char* Key, CString& Value)
{
	RestoreString(Key, Value);
}


/*******************************************************************/
/* Implementation */

void CRegistry::OpenRoot(HKEY Area, const CString& RootSection, int Access)
{
	ASSERT(Area != NULL);
	ASSERT(Access != 0);
	ASSERT((Access & accessRead) || (Access & accessWrite));

	mRootArea = Area;
	mRootSection = RootSection;
	mActiveSection = mRootSection;
	mAccess = Access;
	
	REGSAM accessMask = 0;
	if (mAccess & accessRead)
		accessMask |= KEY_ENUMERATE_SUB_KEYS | KEY_EXECUTE | KEY_QUERY_VALUE | KEY_READ;
	if (mAccess & accessWrite)
		accessMask |= KEY_CREATE_LINK | KEY_CREATE_SUB_KEY | KEY_SET_VALUE | KEY_WRITE;

	DWORD disposition;
	LONG result = ::RegCreateKeyEx(mRootArea, mRootSection, 0, NULL, 0, accessMask, NULL, &mActiveSectionKey, &disposition);
	if (result != ERROR_SUCCESS)
	{
		mRootArea = NULL;
		mRootSection = "";
		mActiveSectionKey = NULL;
		mActiveSection = "";
		mAccess = 0;
	}
}

void CRegistry::Close()
{
	if (mActiveSectionKey)
		::RegCloseKey(mActiveSectionKey);

	// Don't clear the information necessary to reopen the registry.
}

void CRegistry::StoreDWORD(const char* Key, DWORD Value)
{
	::RegSetValueEx(mActiveSectionKey, Key, 0, REG_DWORD, (unsigned char*)&Value, sizeof(Value));
}

void CRegistry::RestoreDWORD(const char* Key, DWORD& Value)
{
	DWORD type = 0;
	::RegQueryValueEx(mActiveSectionKey, Key, NULL, &type, NULL, NULL);
	
	if (type != REG_DWORD)
		return;

	DWORD valueLen = sizeof(Value);
	::RegQueryValueEx(mActiveSectionKey, Key, NULL, NULL, (unsigned char*)&Value, &valueLen);
}

void CRegistry::StoreString(const char* Key, const CString& Value)
{
	::RegSetValueEx(mActiveSectionKey, Key, 0, REG_SZ, (const unsigned char*)(const char*)Value, Value.GetLength() + 1);
}

void CRegistry::RestoreString(const char* Key, CString& Value)
{
	DWORD type = 0;
	DWORD len = 0;
	::RegQueryValueEx(mActiveSectionKey, Key, NULL, &type, NULL, &len);
	
	if (type != REG_SZ)
		return;

	char* data = new char[len];
	data[0] = '\0';
	LONG result = ::RegQueryValueEx(mActiveSectionKey, Key, NULL, NULL, (unsigned char*)data, &len);

	if (result == ERROR_SUCCESS)
		Value = data;

	delete[] data;
}

⌨️ 快捷键说明

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