www.gusucode.com > 对注册表进行简单的操作C#源码程序 > 对注册表进行简单的操作/RegDemo20/RegDemo/RegDemoDlg.cpp

    // RegDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RegDemo.h"
#include "RegDemoDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

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

//注册表操作
HKEY hKey;
char content[256];			//所查询注册表键值的内容
DWORD dwType=REG_SZ;		//定义读取数据类型
DWORD dwLength=256;
struct HKEY__*RootKey;		//注册表主键名称
TCHAR *SubKey;				//欲打开注册表项的地址
TCHAR *KeyName;				//欲设置项的名字
TCHAR *ValueName;			//欲设置值的名称
LPBYTE SetContent_S;		//字符串类型
int SetContent_D[256];		//DWORD类型
BYTE SetContent_B[256];		//二进制类型

int ShowContent (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName);
int SetValue_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S);
int SetValue_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,int ReSetContent_D[256]);
int SetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256]);
int DeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName);
int DeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName);

//查看函数
ShowContent (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName)
{
	int i=0;	//操作结果:0==succeed
	if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_READ,&hKey)==ERROR_SUCCESS)
	{
		if(RegQueryValueEx(hKey,ReValueName,NULL,&dwType,(unsigned char *)content,&dwLength)!=ERROR_SUCCESS)
		{
			AfxMessageBox("错误:无法查询有关的注册表信息");
			i=1;
		}
		RegCloseKey(hKey);
	}
	else
	{
		AfxMessageBox("错误:无法打开有关的hKEY");
		i=1;
	}
	return i;
}

//设置字符串值函数
SetValue_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S)
{
	int i=0;	//操作结果:0==succeed
	//int StrLength;
	//StrLength=CString(SetContent_S).GetLength();

	if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
	{
		if(RegSetValueEx(hKey,ReValueName,NULL,REG_SZ,ReSetContent_S,CString(SetContent_S).GetLength())!=ERROR_SUCCESS)
		{
			AfxMessageBox("错误:无法设置有关的注册表信息");
			i=1;
		}
		RegCloseKey(hKey);
	}
	else
	{
		AfxMessageBox("错误:无法查询有关的注册表信息");
		i=1;
	}
	return i;
}

//设置DWORD值函数
SetValue_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,int ReSetContent_D[256])
{
	int i=0;	//操作结果:0==succeed
	if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
	{
		if(RegSetValueEx(hKey,ReValueName,NULL,REG_DWORD,(const unsigned char *)ReSetContent_D,4)!=ERROR_SUCCESS)
		{
			AfxMessageBox("错误:无法设置有关的注册表信息");
			i=1;
		}
		RegCloseKey(hKey);
	}
	else
	{
		AfxMessageBox("错误:无法查询有关的注册表信息");
		i=1;
	}
	return i;
}

//设置二进制值函数
SetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256])
{
	int i=0;	//操作结果:0==succeed
	if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
	{
		if(RegSetValueEx(hKey,ReValueName,NULL,REG_BINARY,(const unsigned char *)ReSetContent_B,4)!=ERROR_SUCCESS)
		{
			AfxMessageBox("错误:无法设置有关的注册表信息");
			i=1;
		}
		RegCloseKey(hKey);
	}
	else
	{
		AfxMessageBox("错误:无法查询有关的注册表信息");
		i=1;
	}
	return i;
}

//删除子项函数
DeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName)
{
	int i=0;	//操作结果:0==succeed
	if((RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey))==ERROR_SUCCESS)
	{
		if((RegDeleteKey(hKey,ReKeyName))!=ERROR_SUCCESS)
		{
			//AfxMessageBox("清除指定项失败!");
			i=1;
		}
		RegCloseKey(hKey);
	}
	else
	{
		//AfxMessageBox("错误:无法打开有关的hKEY");
		i=1;
	}
	return i;
}

//删除键值函数
DeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName)
{
	int i=0;	//操作结果:0==succeed
	if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,&hKey)==ERROR_SUCCESS)
	{
		if(RegDeleteValue(hKey,ReValueName)!=ERROR_SUCCESS)
		{
			//AfxMessageBox("清除指定值失败!");
			i=1;
		}
		RegCloseKey(hKey);
	}
	else
	{
		//AfxMessageBox("错误:无法打开有关的hKEY");
		i=1;
	}
	return i;
}

//注册表操作结束

// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRegDemoDlg dialog

CRegDemoDlg::CRegDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRegDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRegDemoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRegDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRegDemoDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRegDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CRegDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnSetValue_S)
	ON_BN_CLICKED(IDC_BUTTON2, OnSetContent_B)
	ON_BN_CLICKED(IDC_BUTTON3, OnSetContent_D)
	ON_BN_CLICKED(IDC_BUTTON4, OnDeleteValue_1)
	ON_BN_CLICKED(IDC_BUTTON5, OnDeleteValue_2)
	ON_BN_CLICKED(IDC_BUTTON6, OnDeleteValue_3)
	ON_BN_CLICKED(IDC_BUTTON7, OnDeleteKey)
	ON_BN_CLICKED(IDC_BUTTON8, OnShowContent)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRegDemoDlg message handlers

BOOL CRegDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CRegDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CRegDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRegDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CRegDemoDlg::OnSetValue_S() 
{
	// TODO: Add your control notification handler code here
	RootKey=HKEY_CURRENT_USER;     //注册表主键名称
	SubKey="Software\\Microsoft";  //欲打开注册表值的地址
	ValueName="例1";               //欲设置值的名称
	SetContent_S=LPBYTE("成功");   //值的内容
	
	if((SetValue_S(RootKey,SubKey,ValueName,SetContent_S))!=0)
		AfxMessageBox("操作失败!");
}

void CRegDemoDlg::OnSetContent_B() 
{
	// TODO: Add your control notification handler code here
	RootKey=HKEY_CURRENT_USER;     //注册表主键名称
	SubKey="Software\\Microsoft";  //欲打开注册表值的地址
	ValueName="例2";               //欲设置值的名称
	SetContent_B[0]=1;             //值的内容
	//SetContent_B[1]=0x1B;
	//SetContent_B[2]=0x2C;
	//SetContent_B[3]=0x3D;
	//SetContent_B[4]=0x4E;

	if((SetValue_B(RootKey,SubKey,ValueName,SetContent_B))!=0)
		AfxMessageBox("操作失败!");
}

void CRegDemoDlg::OnSetContent_D() 
{
	// TODO: Add your control notification handler code here
	RootKey=HKEY_CURRENT_USER;     //注册表主键名称
	SubKey="Software\\Microsoft";  //欲打开注册表值的地址
	ValueName="例3";               //欲设置值的名称
	SetContent_D[0]=4294967295;             //值的内容

	if((SetValue_D(RootKey,SubKey,ValueName,SetContent_D))!=0)
		AfxMessageBox("操作失败!");
}

void CRegDemoDlg::OnDeleteValue_1() 
{
	// TODO: Add your control notification handler code here
	RootKey=HKEY_CURRENT_USER;     //注册表主键名称
	SubKey="Software\\Microsoft";  //欲打开注册表值的地址
	ValueName="例1";               //欲设置值的名称

	if((DeleteValue (RootKey,SubKey,ValueName))!=0)
		AfxMessageBox("操作失败!");
}

void CRegDemoDlg::OnDeleteValue_2() 
{
	// TODO: Add your control notification handler code here
	RootKey=HKEY_CURRENT_USER;     //注册表主键名称
	SubKey="Software\\Microsoft";  //欲打开注册表值的地址
	ValueName="例2";               //欲设置值的名称

	if((DeleteValue (RootKey,SubKey,ValueName))!=0)
		AfxMessageBox("操作失败!");
}

void CRegDemoDlg::OnDeleteValue_3() 
{
	// TODO: Add your control notification handler code here
	RootKey=HKEY_CURRENT_USER;     //注册表主键名称
	SubKey="Software\\Microsoft";  //欲打开注册表值的地址
	ValueName="例3";               //欲设置值的名称

	if((DeleteValue (RootKey,SubKey,ValueName))!=0)
		AfxMessageBox("操作失败!");
}

void CRegDemoDlg::OnDeleteKey() 
{
	// TODO: Add your control notification handler code here
	RootKey=HKEY_CURRENT_USER;                                        //注册表主键名称
	SubKey="Software\\Microsoft\\Windows\\CurrentVersion\\Explorer";  //欲打开注册表值的地址
	KeyName="Doc Find Spec MRU";                                      //欲设置项的名称

	if((DeleteKey (RootKey,SubKey,KeyName))!=0)
		AfxMessageBox("操作失败!");
}

void CRegDemoDlg::OnShowContent() 
{
	// TODO: Add your control notification handler code here
	RootKey=HKEY_CURRENT_USER;     //注册表主键名称
	SubKey="Software\\Microsoft";  //欲打开注册表值的地址
	ValueName="例1";               //欲设置值的名称

	if ((ShowContent(RootKey,SubKey,ValueName))==0)
		MessageBox(content,"本操作是利用ShowContent()函数完成的。");
}