今天编写的类:
CRegistry.h文件:
#include "afxwin.h"
#include <winreg.h>
#include <windows.h>
#include <Ndis.h">
class CRegistry :public CObject
{
public:
CRegistry(HKEY hKey=HKEY_LOCAL_MACHINE);
~CRegistry();
//对注册表的基本操作
public:
BOOL RegOpen(LPCTSTR lpSubKey);
void Close();
BOOL CreateKey(LPCTSTR lpSubKey);
BOOL DeleteValue(LPCTSTR lpValueName);
BOOL DeleteKey(HKEY hKey,LPCTSTR lpSubKey);
//对注册表的读写
public:
BOOL Write(LPCTSTR lpSubkey,int nVal);
BOOL Write(LPCTSTR lpSubKey,DWORD dwVal);
BOOL Write(LPCTSTR lpSubKey,LPCTSTR lpVal);
BOOL Read(LPCTSTR lpValue,int* nVal);
BOOL Read(LPCTSTR lpValue,DWORD* dwVal);
BOOL Read(LPCTSTR lpValue,LPCTSTR* lpVal);
protected:
HKEY m_hKey;
}
CRegistry.cpp文件:
#include "windows.h"
#include "CRegistry.h"
CRegistry::CRegistry(HKEY hKey)
{
m_hKey=hKey;//需要进行操作的根键
}
CRegistry::~CRegistry()
{
Close();
}
BOOL CRegistry::RegOpen(LPCTSTR lpSubKey)
{
ASSERT(m_hKey);
ASSERT(lpSubKey); HKEY hKey;
long iRet=RegOpenKeyEx(m_hKey,lpSubKey,0,KEY_ALL_ACCESS,&hKey);
if(iRet==ERROR_SUCCESS)
{
m_hKey=hKey;
return TRUE;
}
return FALSE;
}
void CRegistry::Close()
{
if(m_hKey)
{
RegCloseKey(m_hKey);
m_hKey=NULL;
}
}
BOOL CRegistry::CreateKey(LPCTSTR lpSubKey)
{
    ASSERT(m_hKey);
ASSERT(lpSubKey); HKEY hKey;
DWORD dwType;
long iRet=RegCreateKeyEx(m_hKey,lpSubKey,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwType);
if(iRet==ERROR_SUCCESS)
{
m_hKey=hKey;
return TRUE;
}
return FALSE;
}
BOOL CRegistry::DeleteValue(LPCTSTR lpValueName)
{
ASSERT(m_hKey);
long iRet=RegDeleteValue(m_hKey,lpValueName);
if(iRet==ERROR_SUCCESS)
{
return TRUE;
}
return FALSE;
}
BOOL CRegistry::DeleteKey(HKEY hKey,LPCTSTR lpSubKey)
{
ASSERT(m_hKey);
long iRet=RegDeleteKey(m_hKey,lpSubKey);
if(iRet==ERROR_SUCCESS)
{
return TRUE;
}
return FALSE;
}
BOOL CRegistry::Write(LPCTSTR lpSubKey,int nVal)
{//将指定的值写入指定的键
ASSERT(m_hKey);
ASSERT(lpSubKey);

DWORD dwValue;
dwValue=(DWORD)nVal;
long iRet=RegSetValueEx(m_hKey,lpSubKey,0,REG_DWORD,(const BYTE*)&dwValue,sizeof(DWORD));
if(iRet==ERROR_SUCCESS)
{
return TRUE;
}
return FALSE;
}
BOOL CRegistry::Write(LPCTSTR lpSubKey,DWORD dwVal)
{
ASSERT(m_hKey);
ASSERT(lpSubKey); long iRet=RegSetValueEx(m_hKey,lpSubKey,0,REG_DWORD,(const BYTE*)&dwVal,sizeof(DWORD));
if(iRet==ERROR_SUCCESS)
{
return TRUE;
}
return FALSE;
}
BOOL CRegistry::Write(LPCTSTR lpSubKey,LPCTSTR lpVal)
{
ASSERT(m_hKey);
ASSERT(lpSubKey);
ASSERT(lpVal); long iRet=RegSetValueEx(m_hKey,lpSubKey,0,REG_SZ,(const BYTE*)lpVal,strlen(lpVal)+1);
if(iRet==ERROR_SUCCESS)
{
return TRUE;
}
return FALSE;
}
BOOL CRegistry::Read(LPCTSTR lpValue,int* nVal)
{
ASSERT(m_hKey);
ASSERT(lpValue); DWORD dwType;
DWORD dwSize=sizeof(DWORD);
DWORD buffer;
long iRet=RegQueryValueEx(m_hKey,lpValue,0,&dwType,(BYTE*)&buffer,&dwSize);
if(iRet==ERROR_SUCCESS)
{
*nVal=(int)buffer;
return TRUE;
}
return FALSE;
}
BOOL CRegistry::Read(LPCTSTR lpValue,DWORD* dwVal)
{
ASSERT(m_hKey);
ASSERT(lpValue); DWORD dwType;
DWORD dwSize=sizeof(DWORD);
DWORD buffer;
long iRet=RegQueryValueEx(m_hKey,lpValue,0,&dwType,(BYTE*)&buffer,&dwSize);
if(iRet==ERROR_SUCCESS)
{
*dwVal=buffer;
return TRUE;
}
return FALSE;
}
BOOL CRegistry::Read(LPCTSTR lpValue,LPCTSTR* lpVal)
{
ASSERT(m_hKey);
ASSERT(lpValue); DWORD dwType;
DWORD dwSize=200;
char szString[2550]; long iRet=RegQueryValueEx(m_hKey,lpValue,0,&dwType,(BYTE*)szString,&dwSize);
if(iRet==ERROR_SUCCESS)
{
*lpVal=szString;
return TRUE;
}
return FALSE;
}编译出现如下错误:
d:\安装文件2\vc\2010 vc项目开发\cregistry\cregistry.h(4) : error C2504: 'CObject' : base class undefined
d:\安装文件2\vc\2010 vc项目开发\cregistry\cregistry.cpp(3) : error C2143: syntax error : missing ';' before 'PCH creation point'
d:\安装文件2\vc\2010 vc项目开发\cregistry\cregistry.cpp(13) : error C2065: 'ASSERT' : undeclared identifier
执行 cl.exe 时出错.请教高手