现在的位置: 首页 > 综合 > 正文

如何用CRegKey类来操作注册表zz

2017年12月28日 ⁄ 综合 ⁄ 共 2695字 ⁄ 字号 评论关闭
用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键值。

一般来说最主要的操作就是这些了,是不是很简单啊。
form:http://www.icwin.net/ShowArtitle.ASP?art_id=7723&cat_id=39

抱歉!评论已关闭.