// RegeditDlg.cpp : implementation file
//#include "stdafx.h"
#include "Regedit.h"
#include "RegeditDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CRegeditDlg dialogCRegeditDlg::CRegeditDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRegeditDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRegeditDlg)
m_strOwner = _T("");
m_strCompany = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CRegeditDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRegeditDlg)
DDX_Text(pDX, IDC_OWNER, m_strOwner);
DDX_Text(pDX, IDC_COMPANY, m_strCompany);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CRegeditDlg, CDialog)
//{{AFX_MSG_MAP(CRegeditDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_QUERY, OnQuery)
ON_BN_CLICKED(ID_CHANGE, OnChange)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CRegeditDlg message handlersBOOL CRegeditDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // 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
}// 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 CRegeditDlg::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 CRegeditDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CRegeditDlg::OnQuery() 
{
// TODO: Add your control notification handler code here
UpdateData(true);
HKEY hKEY;//定义有关的hKEY,在查询结束时要关闭 //打开与路径 data_Set相关的hKEY
LPCTSTR data_Set="Software\\Microsoft\\Windows NT\\CurrentVersion\\";
//访问注册表,hKEY则保存此函数所打开的键的句柄
long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set,0,KEY_READ,&hKEY));
if(ret0!=ERROR_SUCCESS)//如果无法打开hKEY,则中止程序的执行
{
AfxMessageBox("错误:无法打开有关的hKEY");
return;
} //查询有关的数据
LPBYTE owner_Get=new BYTE[80];//定义用户姓名 owner_Get
DWORD type_1=REG_SZ;//定义数据类型
DWORD cbData_1=80;//定义数据长度 long ret1=::RegQueryValueEx(hKEY,"RegisteredOwner",NULL,&type_1,owner_Get,&cbData_1);
if(ret1!=ERROR_SUCCESS)
{
AfxMessageBox("错误:无法查询有关的注册表信息");
return;
} //查询公司名
LPBYTE company_Get=new BYTE[80];//定义公司名称 company_Get
DWORD type_2=REG_SZ;//定义数据类型
DWORD cbData_2=80;//定义数据长度 long ret2=::RegQueryValueEx(hKEY,"RegisteredOrganization",NULL,&type_2,company_Get,&cbData_2);
if(ret2!=ERROR_SUCCESS)
{
AfxMessageBox("错误:无法查询有关的注册表信息");
return;
} //显示信息
m_strOwner=CString(owner_Get);
m_strCompany=CString(company_Get);
delete[] owner_Get;
delete[] company_Get; //程序结束,关闭打开的hKEY
::RegCloseKey(hKEY);
UpdateData(false);
}LPBYTE CString_To_LPBYTE(CString str)
{
LPBYTE lpb=new BYTE[str.GetLength()+1];
for(int i=0;i<str.GetLength();i++)
lpb[i]=str[i];
lpb[str.GetLength()]=0;
return lpb;
}void CRegeditDlg::OnChange() 
{
// TODO: Add your control notification handler code here
UpdateData(true); HKEY hKEY;//定义有关的hKEY,在查询结束时要关闭 //打开与路径 data_Set相关的hKEY
LPCTSTR data_Set="Software\\Microsoft\\Windows NT\\CurrentVersion\\";
//访问注册表,hKEY则保存此函数所打开的键的句柄
long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set,0,KEY_READ,&hKEY));
if(ret0!=ERROR_SUCCESS)//如果无法打开hKEY,则中止程序的执行
{
AfxMessageBox("错误:无法打开有关的hKEY");
return;
} //设置有关的数据
LPBYTE owner_Set=CString_To_LPBYTE(m_strOwner);//定义用户姓名 owner_Set
DWORD type_1=REG_SZ;//定义数据类型
DWORD cbData_1=m_strOwner.GetLength()+1;//定义数据长度 long ret1=::RegSetValueEx(hKEY,"RegisteredOwner",NULL,type_1,owner_Set,cbData_1);
if(ret1!=ERROR_SUCCESS)
{
AfxMessageBox("错误:无法设置有关的注册表信息");
return;
} //查询公司名
//CString_To_LPBYTE,请参考下面的函数
LPBYTE company_Set=CString_To_LPBYTE(m_strCompany);//定义公司名称 company_Set
DWORD type_2=REG_SZ;//定义数据类型
DWORD cbData_2=m_strCompany.GetLength()+1;//定义数据长度 long ret2=::RegSetValueEx(hKEY,"RegisteredOrganization",NULL,type_2,company_Set,cbData_2);
if(ret2!=ERROR_SUCCESS)
{
AfxMessageBox("错误:无法设置有关的注册表信息");
return;
}
else
{
AfxMessageBox("注册表修改完成");
}
//程序结束,关闭打开的hKEY
::RegCloseKey(hKEY);
UpdateData(false);
}