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

📄 registry.shtml.htm

📁 随书类文件![随书类]MFC_SOURCEBOOK
💻 HTM
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Zafir Anjum">
   <TITLE>Miscellaneous - A Class For Handling The Registry</TITLE>
</HEAD>
<body background="../fancyhome/back.gif" tppabs="http://www.codeguru.com/fancyhome/back.gif" bgcolor="#FFFFFF" link="#B50029" vlink="#8E2323" alink="#FF0000" bgproperties="fixed">
<table WIDTH="100%">
<tr WIDTH="100%">
<td><A HREF="http://209.66.99.126/cgi/ads.cgi?advert=myad2"><IMG SRC="../../209.66.99.126/advertise2.gif" tppabs="http://209.66.99.126/advertise2.gif" ALT="" BORDER=2></A><td>
</tr>
</table>


<CENTER>
<H3>
<FONT COLOR="#AOAO99">A Class For Handling The Registry</FONT></H3></CENTER>

<HR>

This article was contributed by <A HREF="mailto:Joerg.Koenig@rhein-neckar.de">Joerg Koenig</A>.

<P>Often one can read in the newsgroups, that someone is looking for
an MFC class that encapsulates the registry. In fact, MFC does not provide
such a class. As long as one has no need to work with the registry
in another location than HKEY_CURRENT_USER\Software\[&lt;company>\]&lt;product>,
one can use CWinApp::GetProfile*() and CWinApp::WriteProfile*().

<P>If one wants to use the registry more flexible, then one has to use the
API-functions with their dozens of parameters. I don't know why MS doesn't
provide a registry class in MFC - that would be quite easy ...

<P>The CRegistry class I've developed allows one to use the registry in
a simple way. In addition to the basic operations (loading and storing
data), the class provides some more features. For instance one can
walk the registry tree and perform some operations on any key/value-hit.
Furthermore the class encapsulates differences between NT and Win95
(Aeh - you guessed that the Win32 API should be identical on both systems?
Yes - the API is identical, but the performed action is not! Take a closer
look at the description of the RegDeleteKey() function for instance.)

<P>To handle all the different data types one can put into the registry,
the CRegistry class uses an information holder class CRegVal. 
If you have to load a string from
<BR>HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\WinLogon\DefaultUserName
<BR>you can do it as follows:

<PRE><TT><FONT COLOR="#990000">
CString strKey = "Software\\Microsoft\\Windows NT\\CurrentVersion\\WinLogon";
CRegistry reg = HKEY_LOCAL_MACHINE;  // otherwise it defaults to HKEY_CURRENT_USER
CString strDefaultUserName;
CRegVal regval;
if( reg.LoadKey(strKey, "DefaultUserName", regval) )
	if( regval.GetValue(strDefaultUserName) )
		// yup - got the name
</FONT></TT></PRE>
Simple datatypes (numbers and strings) will be supported directly by the CRegistry
class, so you can write the sample above as follows:
<PRE><TT><FONT COLOR="#990000">
CString strKey = "Software\\Microsoft\\Windows NT\\CurrentVersion\\WinLogon\\DefaultUserName";
CRegistry reg = HKEY_LOCAL_MACHINE;  // otherwise it defaults to HKEY_CURRENT_USER
CString strDefaultUserName;
if( reg.LoadKey(strKey, "DefaultUserName", strDefaultUserName) )
	// yup - got the name
</FONT></TT></PRE>
... but, if CRegistry::LoadKey() returns FALSE, this makes it harder to check
wether the type of the registry key is
wrong or the entry does not exist.
<P>CRegistry doesn't support security (but that was no handicap for me so far :-)

<P>CRegistry consists of two files:
<BR>Registry.h
<BR>Registry.cpp
<BR><A HREF="registry_source.zip" tppabs="http://www.codeguru.com/misc/registry_source.zip">Download Source</A> 9KB



⌨️ 快捷键说明

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