今天为了写一个系统管理的小工具,参照Borland C++ Builder 的TRegistry 的接口,写了个CRegistry类. 目前只实现了
ReadString() 方法,用来读取Windows 注册表中的 REG_SZ 项的值. 也没有进行错误处理。我想以后再慢慢的完善改进吧。
/**/
/* *Filename: Registry.h *Created By: bob.liu *Create Date: 2007.2.24 */
#pragma
once
class
CRegistry
...
{public: CRegistry(DWORD Access=KEY_READ); ~CRegistry(void); BOOL OpenKey(LPCTSTR Key, BOOL CanCreate=false); void CloseKey(void); LPTSTR ReadString(LPCTSTR lpValueName);public: DWORD m_wAccess; HKEY m_hCurrentKey; LPCTSTR m_CurrentPath; //if this doesn't work, a CString will be used HKEY m_hRootKey; /**//* TRegistry properties Access CurrentKey CurrentPath LazyWrite RootKey */}
;
/**/
/* *Filename: Registry.cpp *Created By: bob.liu *Create Date: 2007.2.24 */
#include
"
StdAfx.h
"
#include
"
. egistry.h
"
CRegistry::CRegistry(DWORD Access)
...
{ m_wAccess = Access;}
CRegistry::
~
CRegistry(
void
)
...
{ RegCloseKey(m_hCurrentKey); TRACE("%s ",m_CurrentPath);}
void
CRegistry::CloseKey(
void
)
...
{ RegCloseKey(m_hCurrentKey);}
BOOL CRegistry::OpenKey(LPCTSTR szSubKey, BOOL CanCreate)
...
{ RegOpenKeyEx(m_hRootKey, //hKey szSubKey, //subkey 0, //options,reserved, must be 0 m_wAccess, //samDesired &m_hCurrentKey); //phkResult m_CurrentPath = szSubKey; return TRUE;}
LPTSTR CRegistry::ReadString(LPCTSTR lpValueName)
...
{ LONG lRet; DWORD dwType; DWORD dwCount; LPBYTE lpData = NULL; lRet = RegQueryValueEx( m_hCurrentKey, //hKey, root key lpValueName, NULL, //lpReserved &dwType, //LPDWORD lpType, lpData, &dwCount //LPDWORD lpcbData ); if(ERROR_SUCCESS == lRet) ...{ lpData = new BYTE[dwCount]; lRet = RegQueryValueEx( m_hCurrentKey, //hKey, root key lpValueName, NULL, //lpReserved &dwType, //LPDWORD lpType, lpData, &dwCount //LPDWORD lpcbData ); } return (LPTSTR) lpData; //it's caller's responsibility to delete this memory}