请问,在VC++中如何实现对注册表的读写操作?

sr388 2001-05-23 11:31:00
我的程序要读取和修改win me中注册表的数据,可以VC++中好像没提供这样的指令,我应该怎么做?
...全文
351 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sr388 2001-09-11
  • 打赏
  • 举报
回复
谢谢你们,可是我看不懂,主要原因是资料不太详细,以后再说吧
seesi 2001-05-23
  • 打赏
  • 举报
回复
#if !defined(AFX_BBREGISTERKEY_H__7559F1EC_3707_49C0_9A02_E06D41CDD472__INCLUDED_)
#define AFX_BBREGISTERKEY_H__7559F1EC_3707_49C0_9A02_E06D41CDD472__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class AFX_EXT_CLASS CRegisterKey
{
public:
CRegisterKey();
virtual ~CRegisterKey();

public:
//删除键值
//NT、95下均可以同用
//如无指明pszValueName值,则删除该键下面的所有字项和内容
BOOL DeleteStandardProfile(LPCTSTR pszSubKey,LPCTSTR pszKeyName);//删除标准应用程序所用的注册表项
BOOL DeleteKey(HKEY hKey,LPCTSTR pszSubKey,LPCTSTR pszValueName=NULL);//删除注册表项

//打开注册表某键
LONG Open (HKEY hKeyRoot, LPCTSTR pszPath);//打开注册表某键

//关闭注册表
void Close();//建议打开读写后及时关闭注册表项,虽然析构时候可以自动调用关闭

LONG Write (LPCTSTR pszKey, DWORD dwVal);//写数据
LONG Write (LPCTSTR pszKey, LPCTSTR pszVal);//写字符串
LONG Write (LPCTSTR pszKey, const BYTE* pData, DWORD dwLength);//写二进制数

//下面三个函数,读取注册表数据,如果该项不存在,则返回FALSE。
LONG Read (LPCTSTR pszKey, DWORD& dwVal);//读数据
LONG Read (LPCTSTR pszKey, CString& sVal);//读字符串
LONG Read (LPCTSTR pszKey, BYTE* pData, DWORD& dwLength);//读二进制数

//枚举子键
LONG GetEnumKeys(HKEY hKeyRoot, LPCTSTR pszPath, CStringArray &strArray);
//枚举项
LONG GetEnumValues(HKEY hKeyRoot, LPCTSTR pszPath, CStringArray &strArray);

protected:
HKEY m_hKey;
CString m_sPath;
};

#endif // !defined(AFX_BBREGISTERKEY_H__7559F1EC_3707_49C0_9A02_E06D41CDD472__INCLUDED_)




// BBRegisterKey.cpp: implementation of the CRegisterKey class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BBRegisterKey.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRegisterKey::CRegisterKey()
{
m_hKey = NULL;
}

CRegisterKey::~CRegisterKey()
{
Close();
}

LONG CRegisterKey::Open (HKEY hKeyRoot, LPCTSTR pszPath)
{
DWORD dw;
m_sPath = pszPath;

return RegCreateKeyEx (hKeyRoot, pszPath, 0L, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL,
&m_hKey, &dw);
}

void CRegisterKey::Close()
{
if (m_hKey)
{
RegCloseKey (m_hKey);
m_hKey = NULL;
}
}

LONG CRegisterKey::Write (LPCTSTR pszKey, DWORD dwVal)
{
ASSERT(m_hKey);
ASSERT(pszKey);
return RegSetValueEx (m_hKey, pszKey, 0L, REG_DWORD,
(CONST BYTE*) &dwVal, sizeof(DWORD));
}

LONG CRegisterKey::Write (LPCTSTR pszKey, LPCTSTR pszData)
{
ASSERT(m_hKey);
ASSERT(pszKey);
ASSERT(pszData);
ASSERT(AfxIsValidAddress(pszData, strlen(pszData), FALSE));

return RegSetValueEx (m_hKey, pszKey, 0L, REG_SZ,
(CONST BYTE*) pszData, strlen(pszData) + 1);
}

LONG CRegisterKey::Write (LPCTSTR pszKey, const BYTE* pData,
DWORD dwLength)
{
ASSERT(m_hKey);
ASSERT(pszKey);
ASSERT(pData && dwLength > 0);
ASSERT(AfxIsValidAddress(pData, dwLength, FALSE));

return RegSetValueEx (m_hKey, pszKey, 0L, REG_BINARY,
pData, dwLength);
}

LONG CRegisterKey::Read (LPCTSTR pszKey, DWORD& dwVal)
{
ASSERT(m_hKey);
ASSERT(pszKey);

DWORD dwType;
DWORD dwSize = sizeof (DWORD);
DWORD dwDest;

LONG lRet = RegQueryValueEx (m_hKey, (LPSTR) pszKey, NULL,
&dwType, (BYTE *) &dwDest, &dwSize);

if (lRet == ERROR_SUCCESS)
dwVal = dwDest;

return lRet;
}

LONG CRegisterKey::Read (LPCTSTR pszKey, CString& sVal)
{
ASSERT(m_hKey);
ASSERT(pszKey);

DWORD dwType;
DWORD dwSize = 200;
char string[200];

LONG lReturn = RegQueryValueEx (m_hKey, (LPSTR) pszKey, NULL,
&dwType, (BYTE *) string, &dwSize);

if (lReturn == ERROR_SUCCESS)
sVal = string;

return lReturn;
}

LONG CRegisterKey::Read (LPCTSTR pszKey, BYTE* pData, DWORD& dwLen)
{
ASSERT(m_hKey);
ASSERT(pszKey);

DWORD dwType;

return RegQueryValueEx (m_hKey, (LPSTR) pszKey, NULL,
&dwType, pData, &dwLen);
}

LONG CRegisterKey::GetEnumKeys(HKEY hKeyRoot, LPCTSTR pszPath, CStringArray &strArray)
{
int nIndex;
long lRetCode;
char szValue[MAX_PATH];

if ((lRetCode = Open(hKeyRoot, pszPath)) != ERROR_SUCCESS)
return lRetCode;

strArray.RemoveAll();
for (nIndex = 0, lRetCode = ERROR_SUCCESS; lRetCode == ERROR_SUCCESS; nIndex++)
{
lRetCode = RegEnumKey(m_hKey, nIndex, szValue, MAX_PATH);
if (lRetCode == ERROR_SUCCESS)
strArray.Add(szValue);
}

Close();

return ERROR_SUCCESS;
}

LONG CRegisterKey::GetEnumValues(HKEY hKeyRoot, LPCTSTR pszPath, CStringArray &strArray)
{
int nIndex;
long lRetCode;
char szValue[MAX_PATH];
DWORD dwValue;

if ((lRetCode = Open(hKeyRoot, pszPath)) != ERROR_SUCCESS)
return lRetCode;

strArray.RemoveAll();
for (nIndex = 0, lRetCode = ERROR_SUCCESS; lRetCode == ERROR_SUCCESS; nIndex++)
{
szValue[0] = '\0';
dwValue = MAX_PATH;
lRetCode = RegEnumValue(m_hKey, nIndex, szValue, &dwValue, NULL, NULL, NULL, NULL);
if (lRetCode == ERROR_SUCCESS)
strArray.Add(szValue);
}

Close();

return ERROR_SUCCESS;
}

BOOL CRegisterKey::DeleteKey(HKEY hKey,LPCTSTR pszSubKey,LPCTSTR pszValueName)
{

ASSERT( pszSubKey != 0 ) ;
ASSERT( hKey != 0 ) ;

HKEY hSubKey ;
LONG lRet = RegOpenKeyEx(
hKey, // key handle at root level
pszSubKey, // path name of child key
0, // reserved
KEY_WRITE | KEY_READ, // requesting access
&hSubKey // address of key to be returned
);

if( lRet != ERROR_SUCCESS )
{
ASSERT(0);
TRACE0( "CRegistry::DeleteKey(): RegOpenKeyEx() failed\n" ) ;
return FALSE ;
}

if( pszValueName )
{
// 如果参数pszValueName不为NULL则仅删除该键
lRet = RegDeleteValue( hSubKey, pszValueName ) ;
RegCloseKey( hSubKey ) ;
RegCloseKey( hKey ) ;
if( lRet != ERROR_SUCCESS )
{
ASSERT(0);
TRACE0( "CRegistry::DeleteKey(): RegDeleteValue() failed\n" );
return FALSE ;
}
}
else
{


DWORD dwSubKeyCnt = 0 ;
do
{
// 得到子键的信息
DWORD dwMaxSubKey ;
LONG lRet = RegQueryInfoKey(
hSubKey,
0, // buffer for class name
0, // length of class name string
0, // reserved
&dwSubKeyCnt, // # of subkeys
&dwMaxSubKey, // length of longest subkey
0, // length of longest class name string
0, // # of values
0, // length of longest value name
0, // length of longest value data
0, // security descriptor
0 // last write time
) ;
if( lRet != ERROR_SUCCESS )
{
TRACE0( "CRegistry::DeleteKey(): RegQueryInfoKey() failed.\n" ) ;
RegCloseKey( hSubKey ) ;
return FALSE ;
}

if( dwSubKeyCnt > 0 )
{

LPTSTR pszKeyName = new TCHAR [ dwMaxSubKey + 1 ] ;
DWORD dwKeyNameLen = dwMaxSubKey ;
lRet = RegEnumKey(
hSubKey,
0, // index
pszKeyName, // address of buffer for key name string
dwKeyNameLen+1 // max. length of key name string
) ;

if( lRet != ERROR_SUCCESS )
{
TRACE0( "CRegistry::DeleteKey(): RegEnumKey() failed\n" ) ;
delete [] pszKeyName ;
RegCloseKey( hSubKey ) ;
return FALSE ;
}
if( ! DeleteKey( hSubKey,pszKeyName, pszValueName ) )
{
delete [] pszKeyName ;
RegCloseKey( hSubKey ) ;
return FALSE ;
}
delete [] pszKeyName ;
}
} while( dwSubKeyCnt > 0 ) ;

RegCloseKey( hSubKey ) ;

lRet = RegDeleteKey( hKey, pszSubKey ) ;

//RegCloseKey( hKey ) ;

if( lRet != ERROR_SUCCESS )
{
TRACE0( "CRegistry::DeleteKey(): RegDeleteKey() failed\n" ) ;
return FALSE ;
}
}

return TRUE ;
}

//该函数在文档视图结构的应用程序调用
BOOL CRegisterKey::DeleteStandardProfile(LPCTSTR pszSubKey, LPCTSTR pszKeyName)
{
CString strAppTitle;
strAppTitle.LoadString(AFX_IDS_APP_TITLE);
CString strSubKey;
CString strProfileKey;
strProfileKey=AfxGetApp()->m_pszRegistryKey ;
//得到应用程序所在的键
strSubKey = "Software\\" + strProfileKey + "\\"
+ strAppTitle + "\\";
return DeleteKey(HKEY_CURRENT_USER,
strSubKey+pszSubKey,
pszKeyName
);
}
jazzrabbit 2001-05-23
  • 打赏
  • 举报
回复
用api.到msdn中查RegOpenKeyEx之类的函数.
ATLBASE.h中定义了CRegKey类也可用.
如果只是本应用的注册信息用CWinApp::SetRegistry/GetProfileXXX/WriteProfileXXX非常方便.
ydogg 2001-05-23
  • 打赏
  • 举报
回复
注册表API

RegCreateKeyEx()
RegCloseKey()
RegDeleteKey()
RegOpenKey()
RegOpenKeyEx()
RegSetValueEx()
RegQueryInfoKey()

VC_LOADING 2001-05-23
  • 打赏
  • 举报
回复
可用CReg 对注册表进行操作!

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧