关闭

VC读写注册表

491人阅读 评论(0) 收藏 举报

读注册表:

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;
}
 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:85051次
    • 积分:1463
    • 等级:
    • 排名:千里之外
    • 原创:58篇
    • 转载:8篇
    • 译文:0篇
    • 评论:11条
    文章分类
    最新评论