blessyou312的专栏

学习笔记

VC读写注册表

读注册表:

void CRegeditDlg::OnQuery()
{
 HKEY hKEY;
 LPCTSTR data_Set="Software//Microsoft//Windows//CurrentVersion//";

 //访问注册表,hHEY则保存此函数所打开的句柄
 long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set,0,KEY_READ,&hKEY));
 if(ret0!=ERROR_SUCCESS)
 {
  MessageBox("Error!Can't open hKEY!");
  return;
 }
 //查询有关的数据
 LPBYTE owner_Get=new BYTE[80];
 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)
 {
  MessageBox("Error!Can't query the value!");
  return;
 }

 LPBYTE company_Get=new BYTE[80];
 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)
 {
  MessageBox("Error!Can't query the value!");
  return;
 }

 //将owner_Get company_Get转换成CString
 m_strOwner=CString(owner_Get);
 m_strCompany=CString(company_Get);
 delete[] owner_Get;
 delete[] company_Get;

 //close the connection
 ::RegCloseKey(hKEY);
 UpdateData(FALSE);
}

 

 

 

写注册表:

void CRegeditDlg::OnModify()
{
 UpdateData(TRUE);
 HKEY hKEY;
 LPCTSTR data_Set="Software//Microsoft//Windows//CurrentVersion";

 long ret0=(::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set,0,KEY_WRITE,&hKEY));
 if(ret0!=ERROR_SUCCESS)
 {
  MessageBox("Error! Can't open hKEY!");
  return;
 }
 LPBYTE owner_Set=CString_To_LPBYTE(m_strOwner);
 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)
 {
  MessageBox("Error! Can't write the Reg!");
  return;
 }

 LPBYTE company_Set=CString_To_LPBYTE(m_strCompany);
 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(ret1=ERROR_SUCCESS)
 {
  MessageBox("Error! Can't write the Reg!");
  return;
 }
 else
 {
  MessageBox("Success!!");
 }
}

LPBYTE CRegeditDlg::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;
}
 

阅读更多
文章标签: null delete byte query
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

VC读写注册表

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭