5 个解决方案
#1
注册表API
RegCreateKeyEx()
RegCloseKey()
RegDeleteKey()
RegOpenKey()
RegOpenKeyEx()
RegSetValueEx()
RegQueryInfoKey()
RegCreateKeyEx()
RegCloseKey()
RegDeleteKey()
RegOpenKey()
RegOpenKeyEx()
RegSetValueEx()
RegQueryInfoKey()
#2
用api.到msdn中查RegOpenKeyEx之类的函数.
ATLBASE.h中定义了CRegKey类也可用.
如果只是本应用的注册信息用CWinApp::SetRegistry/GetProfileXXX/WriteProfileXXX非常方便.
ATLBASE.h中定义了CRegKey类也可用.
如果只是本应用的注册信息用CWinApp::SetRegistry/GetProfileXXX/WriteProfileXXX非常方便.
#3
#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
);
}
#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
);
}
#4
可用CReg 对注册表进行操作!
#5
谢谢你们,可是我看不懂,主要原因是资料不太详细,以后再说吧
#1
注册表API
RegCreateKeyEx()
RegCloseKey()
RegDeleteKey()
RegOpenKey()
RegOpenKeyEx()
RegSetValueEx()
RegQueryInfoKey()
RegCreateKeyEx()
RegCloseKey()
RegDeleteKey()
RegOpenKey()
RegOpenKeyEx()
RegSetValueEx()
RegQueryInfoKey()
#2
用api.到msdn中查RegOpenKeyEx之类的函数.
ATLBASE.h中定义了CRegKey类也可用.
如果只是本应用的注册信息用CWinApp::SetRegistry/GetProfileXXX/WriteProfileXXX非常方便.
ATLBASE.h中定义了CRegKey类也可用.
如果只是本应用的注册信息用CWinApp::SetRegistry/GetProfileXXX/WriteProfileXXX非常方便.
#3
#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
);
}
#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
);
}
#4
可用CReg 对注册表进行操作!
#5
谢谢你们,可是我看不懂,主要原因是资料不太详细,以后再说吧