您的位置:首页 > 其它

封装了一个简单的注册表操作类

2005-08-12 15:19 671 查看
不想写文字了 测试了下 可以使用 想知道详细的用法加我QQ吧 76359121
MSN :navyblue1982@hotmail.com
#if !defined(AFX_REGISTRY_H__E0610A5D_7166_4D02_9D7E_11AF7CF8E229__INCLUDED_)
#define AFX_REGISTRY_H__E0610A5D_7166_4D02_9D7E_11AF7CF8E229__INCLUDED_
//创建新键的返回值
#include <winreg.h>
const UINT nReturnNewKeySuccess=1;
const UINT nReturnNewKeyFail=0;
class RegToToolBar
{
public: //Software/Microsoft/Internet Explorer/Extensions
RegToToolBar(HKEY hKey=HKEY_LOCAL_MACHINE,CString szIeKey="Software//Microsoft//Internet Explorer//Extensions",CString szNewKey="//{06DD38D3-D187-11CF-A80D-00C04FD74AD8}");//主键
//UINT OpenKey(CString );
UINT NewAKey();//新字键的名称
UINT AddStrToKey(LPCTSTR,LPCTSTR);//第一个是要添加新新项的名称,第二个是数值
void CloseHkey();//关闭主键
~RegToToolBar();
private:
//主键
HKEY m_hKey;
//要新建的目录
CString m_szIeKey;
//要新建键
CString m_szNewKey;
//要打开的目录
CString m_szOpenItem;
};
#endif//AFX_REGISTRY_H__E0610A5D_7166_4D02_9D7E_11AF7CF8E229__INCLUDED_

////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RegToToolBar.h"
RegToToolBar::RegToToolBar(HKEY hKey,CString szIeKey,CString szNewKey)
{
m_szIeKey=szIeKey;
m_szNewKey=szNewKey;
m_hKey=hKey;
m_szOpenItem=m_szIeKey+m_szNewKey;//新建的目录
}
UINT RegToToolBar::NewAKey()
{
ASSERT(m_hKey);
ASSERT(m_szOpenItem);

HKEY hKey;
DWORD dw;
long lReturn=::RegCreateKeyEx(m_hKey,m_szOpenItem,0L,NULL,REG_OPTION_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dw);
if(ERROR_SUCCESS==lReturn)
{
m_hKey=hKey;
return nReturnNewKeySuccess;
}
return nReturnNewKeyFail;
}
UINT RegToToolBar::AddStrToKey(LPCTSTR szNewItem,LPCTSTR szNewValue)
{
ASSERT(m_hKey);
ASSERT(szNewItem);
ASSERT(szNewValue);
long lReturn=RegSetValueEx(m_hKey,szNewItem,0L,REG_SZ,(const BYTE*)szNewValue,strlen(szNewValue));
if(lReturn==ERROR_SUCCESS)
return nReturnNewKeySuccess;

return nReturnNewKeyFail;
}
void RegToToolBar::CloseHkey()
{
if(m_hKey)
{
RegCloseKey(m_hKey);
m_hKey=NULL;
}
}
RegToToolBar::~RegToToolBar()
{
CloseHkey();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: