// ================================================ ========================================
// Title:
// The CREG class can easily read the Registry
// Author:
// Norains
// Date:
// Sunday 8-0000l-2007
// Environment:
// Evc4.0 + standard SDK
// ================================================ ========================================
First, let's talk about the origins of this class. to be accurate, this class was my long-lived Dongdong. I accidentally discovered it yesterday when I checked Microsoft's control panel code. I think it is quite convenient to use, so copy it out and use it based on the principle of "foreign use!
/**///////////////////////////////////// /////////////////////////////////
// Reg. h: interface for the CREG class.
//
// Version:
// 1.0.0
// Date:
// 2007.04.07
/**///////////////////////////////////// //////////////////////////////////
# Ifndef reg_h
# Define reg_h
Class CREG
...{
Public:
Bool deletekey (lpctstr szname );
Bool deletevalue (lpctstr szname );
Bool setmultisz (maid, maid );
Bool setbinary (lpctstr szname, lpbyte lpbvalue, DWORD dwlen );
Bool setdw (lpctstr szname, DWORD dwvalue );
Bool setsz (maid, maid );
Bool setsz (maid, maid );
DWORD getvaluedw (lpctstr szname, DWORD dwdefault = 0 );
Lpctstr getvaluesz (lpctstr szname );
Lpbyte getvaluebinary (lpctstr szname );
DWORD getvaluebinary (lpctstr szname, lpbyte lpbvalue, DWORD dwlen );
Bool getvaluesz (lpctstr szname, lptstr szvalue, DWORD dwlen );
Bool enumvalue (lptstr pszname, DWORD dwlenname, lptstr pszvalue, DWORD dwlenvalue );
Bool enumkey (lptstr psz, DWORD dwlen );
Bool isok ();
Operator hkey ();
Void reset ();
CREG (hkey hkroot, lpctstr pszkey );
Bool open (hkey hkroot, lpctstr pszkey, regsam Sam = key_read );
Bool create (hkey hkroot, lpctstr pszkey );
CREG ();
Virtual ~ CREG ();
PRIVATE:
Hkey m_hkey;
Int m_index;
Lpbyte m_lpbvalue; // last value read, if any
};
# Endif // # ifndef reg_h
//////////////////////////////////////// //////////////////////////////
// Reg. cpp: Implementation of the CREG class.
//
/**///////////////////////////////////// //////////////////////////////////
# Include "stdafx. H"
# Include "Reg. H"
// ================================================ ======================================
// Macro define
# Define myfree (p) {If (p) localfree (p );}
// ================================================ ======================================
/**///////////////////////////////////// //////////////////////////////////
// Construction/destruction
/**///////////////////////////////////// //////////////////////////////////
CREG: CREG ()
...{
M_hkey = NULL;
M_index = 0;
M_lpbvalue = NULL;
}
CREG: CREG (hkey hkroot, lpctstr pszkey)
...{
M_hkey = NULL;
M_index = 0;
M_lpbvalue = NULL;
Open (hkroot, pszkey );
}
CREG ::~ CREG ()
...{
If (m_hkey)
...{
Regclosekey (m_hkey );
}
Myfree (m_lpbvalue );
}
//-------------------------------------------------------------------
// Description:
// Create the key
//-------------------------------------------------------------------
Bool CREG: Create (hkey hkroot, lpctstr pszkey)
...{
DWORD dwdisp;
Return error_success = regcreatekeyex (hkroot, pszkey, 0, null, reg_option_non_volatile, key_all_access, null, & m_hkey, & dwdisp );
}
//-------------------------------------------------------------------
// Description:
// Open the key
//-------------------------------------------------------------------
Bool CREG: open (hkey hkroot, lpctstr pszkey, regsam Sam)
...{
Return error_success = regopenkeyex (hkroot, pszkey, 0, Sam, & m_hkey );
}
//-------------------------------------------------------------------
// Description:
// Reset the value
//-------------------------------------------------------------------
Void CREG: reset ()
...{
If (m_hkey)
...{
Regclosekey (m_hkey );
}
Myfree (m_lpbvalue );
M_hkey = NULL;
M_index = 0;
M_lpbvalue = NULL;
}
//-------------------------------------------------------------------
// Description:
// Operator overload
//-------------------------------------------------------------------
CREG: Operator hkey ()
...{
Return m_hkey;
}
//-------------------------------------------------------------------
// Description:
// Test whether is the handle of the key OK for next operate
//-------------------------------------------------------------------
Bool CREG: isok ()
...{
Return m_hkey! = NULL;
}
//-------------------------------------------------------------------
// Description:
// Enum the key
//-------------------------------------------------------------------
Bool CREG: enumkey (lptstr psz, DWORD dwlen)
...{
If (! M_hkey)
...{
Return false;
}
Return error_success = regenumkeyex (m_hkey, m_index ++, psz, & dwlen, null );
}
//-------------------------------------------------------------------
// Description:
// Enum Registry Value
//-------------------------------------------------------------------
Bool CREG: enumvalue (lptstr pszname, DWORD dwlenname, lptstr pszvalue, DWORD dwlenvalue)
...{
DWORD dwtype;
If (! M_hkey)
...{
Return false;
}
Dwlenvalue * = sizeof (tchar); // convert length in Chars to bytes
Return error_success = regenumvalue (m_hkey, m_index ++, pszname, & dwlenname, null, & dwtype, (lpbyte) pszvalue, & dwlenvalue );
}
//-------------------------------------------------------------------
// Description:
// Get the string value
//-------------------------------------------------------------------
Bool CREG: getvaluesz (lpctstr szname, lptstr szvalue, DWORD dwlen)
...{
If (! M_hkey)
...{
Return false;
}
Dwlen * = sizeof (tchar); // convert length in Chars to bytes
Return error_success = regqueryvalueex (m_hkey, szname, null, null, (lpbyte) szvalue, & dwlen );
}
//-------------------------------------------------------------------
// Description:
// Get the binary value
//-------------------------------------------------------------------
Dword creg: getvaluebinary (lpctstr szname, lpbyte lpbvalue, DWORD dwlen)
...{
If (! M_hkey)
...{
Return false;
}
DWORD dwlenwant = dwlen;
If (error_success = regqueryvalueex (m_hkey, szname, null, null, lpbvalue, & dwlen ))
...{
Return dwlen;
}
Else
...{
Return 0;
}
}
//-------------------------------------------------------------------
// Description:
// Get the binary value
//-------------------------------------------------------------------
Lpbyte CREG: getvaluebinary (lpctstr szname)
...{
Return (lpbyte) getvaluesz (szname );
}
//-------------------------------------------------------------------
// Description:
// Get the string value
//-------------------------------------------------------------------
Lpctstr CREG: getvaluesz (lpctstr szname)
...{
Return 0;
}
//-------------------------------------------------------------------
// Description:
// Get the DWORD Value
//
// Parameters:
// Szname: [in] the value of registry
// Dwdefault: [in] the default value return when failed in getting
// DWORD Value.
//-------------------------------------------------------------------
Dword creg: getvaluedw (lpctstr szname, DWORD dwdefault)
...{
If (! M_hkey)
...{
Return false;
}
DWORD dwvalue = dwdefault;
DWORD dwlen = sizeof (DWORD );
Regqueryvalueex (m_hkey, szname, null, null, (lpbyte) & dwvalue, & dwlen );
Return dwvalue;
}
//-------------------------------------------------------------------
// Description:
// Set the string value
//-------------------------------------------------------------------
Bool CREG: setsz (maid, maid)
...{
// Prefix
If (! M_hkey)
...{
Return false;
}
Return error_success = regsetvalueex (m_hkey, szname, 0, REG_SZ, (lpbyte) szvalue, sizeof (tchar) * dwlen );
}
//-------------------------------------------------------------------
// Description:
// Set the string value
//-------------------------------------------------------------------
Bool CREG: setsz (maid, maid szvalue)
...{
Return setsz (szname, szvalue, 1 + lstrlen (szvalue ));
}
//-------------------------------------------------------------------
// Description:
// Get the DWORD Value
//-------------------------------------------------------------------
Bool CREG: setdw (lpctstr szname, DWORD dwvalue)
...{
// Prefix
If (! M_hkey)
...{
Return false;
}
Return error_success = regsetvalueex (m_hkey, szname, 0, REG_DWORD, (lpbyte) & dwvalue, sizeof (DWORD ));
}
//-------------------------------------------------------------------
// Description:
// Get the binary value
//-------------------------------------------------------------------
Bool CREG: setbinary (lpctstr szname, lpbyte lpbvalue, DWORD dwlen)
...{
// Prefix
If (! M_hkey)
...{
Return false;
}
Return error_success = regsetvalueex (m_hkey, szname, 0, REG_BINARY, lpbvalue, dwlen );
}
//-------------------------------------------------------------------
// Description:
// Set the multi value
//-------------------------------------------------------------------
Bool CREG: setmultisz (maid, maid, DWORD dwlen)
...{
Return error_success = regsetvalueex (m_hkey, szname, 0, reg_multi_sz, (lpbyte) lpszvalue, sizeof (tchar) * dwlen );
}
//-------------------------------------------------------------------
// Description:
// Delete the value
//-------------------------------------------------------------------
Bool CREG: deletevalue (lpctstr szname)
...{
// Prefix
If (! M_hkey)
...{
Return false;
}
//
Return error_success = regdeletevalue (m_hkey, szname );
}
//-------------------------------------------------------------------
// Description:
// Delete key
//-------------------------------------------------------------------
Bool CREG: deletekey (lpctstr szname)
...{
If (! M_hkey)
...{
Return false;
}
Return error_success = regdeletekey (m_hkey, szname );
}
It is very convenient to use the CREG class to read the Registry. At least you do not need to go through the registry function in the platform builder document.
Set the "screen" (DWORD type) Value of "HKEY_CURRENT_USER/controlpanel/volume" in the Registry to 2 For example: CREG Reg (HKEY_CURRENT_USER, text ("controlpanel/volume "));
Reg. setdw (text ("screen"), screen );
Reading is also quite simple:
DWORD dwvalue = reg. getvaluedw (text ("screen "));
The functions of other functions can be inferred according to the function name, and the method of use is also very simple. I will not repeat them here.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/norains/archive/2007/04/08/1556296.aspx