Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2653656
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2007-12-06 14:29:01

用CRegKey类来操作注册表是非常方便的。CRegKey类并不是一个MFC类,而是一个ATL类,所以在使用的时候不要忘记在StdAfx.h头文件中加入#include


1.打开需要查询注册表键:
原型是:LONG Open( HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired = KEY_ALL_ACCESS );
只有打开了一个注册表键才能对其值进行操作。
hKeyParent:打开的键的句柄。
lpszKeyName:键所在的注册表的路径。
samDesired:注册表访问的安全性。
例子:
CRegKey rk;
LPCTSTR lp="Software\\Compupacific\\NewEn\\AddIns\\AddButton";
if(rk.Open(HKEY_CURRENT_USER,lp)== ERROR_SUCCESS)
{
AfxMessageBox(“Successful!”0);
}
2.获取注册表中某一键的键值:
LONG QueryValue( DWORD& dwValue, LPCTSTR lpszValueName );
LONG QueryValue( LPTSTR szValue, LPCTSTR lpszValueName, DWORD* pdwCount );
有两个函数,第一个是查询整型值,第二个是查询字符串类型的值。下面分别对它们进行举例:
//取整型值
CRegKey rk;
DWORD dValue ;

LPCTSTR lp="Software\\Compupacific\\NewEn\\AddIns\\AddButton";
if(rk.Open(HKEY_CURRENT_USER,lp)== ERROR_SUCCESS)
{
if(rk.QueryValue( dValue,"LoadBehavior")==ERROR_SUCCESS)
{
CString temp;
temp.Format("%d",dValue);
SetDlgItemText(IDC_EDIT1,temp);
}
else
{
AfxMessageBox("Query Error");
}
}
else
{
AfxMessageBox("Open error!");
}
rk.Close();
//取字符串类型的值
CRegKey rk;
HKEY m_hKey;
DWORD pCount=1024;
CString KeyValue;
char szValue[1024];


LPCTSTR lp="Software\\Compupacific\\NewEn\\AddIns\\AddButton";
if(rk.Open(HKEY_CURRENT_USER,lp)== ERROR_SUCCESS)
{
LPCTSTR lKeyName="Description";
if(rk.QueryValue(szValue,lKeyName,& pCount)== ERROR_SUCCESS)
{
KeyValue=szValue;
SetDlgItemText(IDC_EDIT1,KeyValue);
}
else
{
SetDlgItemText(IDC_EDIT1,"Query error");
}
//rk.SetValue(lKeyName,"HH");
}
else
{
SetDlgItemText(IDC_EDIT1,"Open error");
}
rk.Close();
3.加入一个键值:
LONG SetValue( DWORD dwValue, LPCTSTR lpszValueName );
LONG SetValue( LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL );
LONG SetValue( HKEY hKeyParent, LPCTSTR lpszKeyName, LPCTSTR lpszValue, LPCTSTR lpszValueName = NULL );
有三个重载函数,大同小异。我们针对第二个举例,举一反三:
LONG lResult = 0;
CRegKey reg;
//open the required registry key
LPCTSTR lpszKey = "Software\\Microsoft\\Internet Explorer\\Toolbar";
lResult = reg.Open(HKEY_CURRENT_USER,lpszKey);


//check if opened successfully
if(ERROR_SUCCESS != lResult)
{
return FALSE;
}
//set the value
lResult = reg.SetValue(m_szFilePath,"BackBitmap");
if(ERROR_SUCCESS != lResult)
{
return FALSE;
}
//done, close and return success
reg.Close();

m_szFilepath是一张图片的位置,通过这个函数,你的IE工具栏的背景就变成的你指定的图片了,很爽吧。




4. 删除一个键值:
LONG DeleteValue( LPCTSTR lpszValue );
lpszValue:你要删除的键值的名字.
例子:
LONG lResult = 0;
CRegKey reg;
//open the required registry key
LPCTSTR lpszKey = "Software\\Microsoft\\Internet Explorer\\Toolbar";
lResult = reg.Open(HKEY_CURRENT_USER,lpszKey);


//check if opened successfully
if(ERROR_SUCCESS != lResult)
{
return FALSE;
}
//delete the value "BackBitmap" from toolbar
lResult = reg.DeleteValue("BackBitmap");


//check if deleted successfully
if(ERROR_SUCCESS != lResult)
{
return FALSE; //perhaps value not found, if skin is not set
}
//done, return success
reg.Close();


这样就去掉了你给IE工具栏设定的背景图片,也就是删掉了IE工具栏的BackBitmap键值。
阅读(1043) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~