日志原文:http://dualf.blog.sohu.com/76037381.html
编写软件注册表的读写是基本的操作,这里直接将源代码贴出来,大家共享并扩展,希望大家还可以回来,将您的代码贴出来,共同进步,这里对网友的帮助表示感谢。
// RegOp.cpp: implementation of the RegOp class.
//注册软件
//
#include "stdafx.h"
#include "RegOp.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//
// Construction/Destruction
//
RegOp::RegOp()
{
}
RegOp::~RegOp()
{
}
RegOp::err RegOp::SetStrValue(HKEY hMainKey, CString szSubKey, CString szItem, CString szValue)
{
HKEY hRsltKey;
//=========================================打开注册表
long lRslt = RegOpenKey(hMainKey, szSubKey, &hRsltKey);
if (lRslt != ERROR_SUCCESS)
{
return this->ERR_OPENREGKEY;
}
//===================================设置注册表值
lRslt = RegSetValueEx(hRsltKey, szItem, NULL, REG_SZ,
LPBYTE(szValue.GetBuffer(szValue.GetLength())), szValue.GetLength());
if (lRslt != ERROR_SUCCESS)
{
return ERR_SETREGVALUE;
}
//============================================关闭注册表键
if (hMainKey)
{
RegCloseKey(hMainKey);
}
if (hRsltKey)
{
RegCloseKey(hRsltKey);
}
return ERR_SUCCESS;
}
RegOp::err RegOp::GetStrValue(HKEY hMainKey, CString szSubKey, CString szItem, CString& szValue)
{
HKEY hRsltKey;
//打开注册表
if (ERROR_SUCCESS != RegOpenKey(hMainKey, szSubKey, &hRsltKey))
{
return ERR_OPENREGKEY;
}
LPBYTE lpData = new BYTE[256];
DWORD dwType = REG_SZ;
DWORD dwNum = 256;
//查询注册表项值
long lRslt = RegQueryValueEx(hRsltKey, szItem, 0, &dwType, lpData, &dwNum);
if (lRslt != ERROR_SUCCESS)
{
return ERR_QUERYVALUE;
}
szValue = (CString)lpData;
//关闭打开的键
if (hMainKey)
{
RegCloseKey(hMainKey);
}
if (hRsltKey)
{
RegCloseKey(hRsltKey);
}
return ERR_SUCCESS;
}
RegOp::err RegOp::AddSubKey(HKEY hMainKey, CString szSubKey)
{
HKEY hRsltKey;
//================================判断要增加的注册表子键是否已存在
if (ERROR_SUCCESS == RegOpenKey(hMainKey, szSubKey, &hRsltKey))
{
return this->ERR_SUBKEYEXIST;
}
//==================================如果不存在,则增加此子键
long lRslt = RegCreateKey(hMainKey, szSubKey, &hRsltKey);
// long lCrtRslt = RegCreateKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\WZJ", &hRsltKey);
if (lRslt != ERROR_SUCCESS)
{
return this->ERR_CREATESUBKEY;
}
//===============================关闭打开的注册表键句柄
if(hMainKey)
{
RegCloseKey(hMainKey);
}
if (hRsltKey)
{
RegCloseKey(hRsltKey);
}
return this->ERR_SUCCESS;
}
RegOp::err RegOp::DeleteStrValue(HKEY hMainKey, CString szSubKey, CString szItem)
{
HKEY hRsltKey;
//打开注册表
long lRslt = RegOpenKey(hMainKey, szSubKey, &hRsltKey);
if (lRslt != ERROR_SUCCESS)
{
return ERR_OPENREGKEY;
}
//删除注册表项
lRslt = RegDeleteValue(hRsltKey, szItem);
if (lRslt != ERROR_SUCCESS)
{
return ERR_DELETEVALUE;
}
return ERR_SUCCESS;
}
RegOp::err RegOp::DeleteSubKey(HKEY hMainKey, CString szSubKey)
{
//=================================删除子键
long lRslt = RegDeleteKey(hMainKey, szSubKey);
if (lRslt) {
return ERR_DELETESUBKEY;
}
//关闭打开的键
if (hMainKey)
{
RegCloseKey(hMainKey);
}
return ERR_SUCCESS;
}
以下是.h文件
// RegOp.h: interface for the RegOp class.
//
//
#if !defined(AFX_REGOP_H__0459CCAE_B265_4678_8E70_CE31BA8C2784__INCLUDED_)
#define AFX_REGOP_H__0459CCAE_B265_4678_8E70_CE31BA8C2784__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class RegOp
{
public:
enum err
{
ERR_OPENREGKEY,
ERR_SETREGVALUE,
ERR_QUERYVALUE,
ERR_DELETEVALUE,
ERR_SUBKEYEXIST,
ERR_CREATESUBKEY,
ERR_DELETESUBKEY,
ERR_SUCCESS
};
enum hkey
{
DHKEY_CLASSES_ROOT = 0x80000000,
DHKEY_CURRENT_USER = 0x80000001,
DHKEY_LOCAL_MACHINE = 0x80000002,
DHKEY_USERS = 0x80000003,
DHKEY_CURRENT_CONFIG = 0x80000005
};
public:
RegOp();
virtual ~RegOp();
public:
//设置或修改注册表字符串类型值
err SetStrValue(HKEY hMainKey, CString szSubKey, CString szItem, CString szValue);
//查询得到注册表字符串类型值
err GetStrValue(HKEY hMainKey, CString szSubKey, CString szItem, CString& szValue);
//删除注册表中项
err DeleteStrValue(HKEY hMainKey, CString szSubKey, CString szItem);
//增加注册表子键
err AddSubKey(HKEY hMainKey, CString szSubKey);
//删除注册表子键
err DeleteSubKey(HKEY hMainKey, CString szSubKey);
};
#endif // !defined(AFX_REGOP_H__0459CCAE_B265_4678_8E70_CE31BA8C2784__INCLUDED_)
大家用的时候也要记得回来丰富我的小站。谢谢大家!