如何在VC++中使用注册表(5)
2、修改用户信息的代码
因用户输出的是CString类型的字符串,要先将其转换为LPBYTE类型,以便以后函数的调用。下面是转换函数:
LPBYTE CString_To_LPBYTE(CString str)
{
LPBYTE lpb=new BYTE [str.GetLength( )+1];
for(int i=0;i lpb[str.GetLength( )]=0;
return lpb;
}
以下是具体的修改注册表用户信息的代码:
CString str_username,str_company;
HKEY hKey;
LPCTSTR path=“SoftwareMicrsoftMS Setup(ACME)User Info”;
LONG return0(::RegOpenKeyEx(HKEY_CURRENT_USER,path,0,KEY_WRITE,&hKey));
if(return0!=ERROR_SUCCESS)
{
MessageBox(“错误:无法打开有关的键!”);
return;
}
LPBYTE username_Set=Cstring_To_LPBYTE(str_username);
DWORD type_1=REG_SZ;
DWORD cbData_1=str_username.GetLength( )+1;
LONG return1=::RegSetalueEx(hKey,"DefName",NULL,type_1,username_Set,cbData_1);
if (return1!=ERROR_SUCCESS)
{
MessageBox(“错误:无法修改有关注册表信息!”);
return;
}
LPBYTE company_Set=Cstring_To_LPBYTE(str_company);
DWORD type_2=REG_SZ;
DWORD cbData_2=str_COMPANY.GetLength( )+1;
LONG return2=::RegSetalueEx(hKey,"DefCompany",NULL,type_2,company_Set,cbData_2);
if (return2!=ERROR_SUCCESS)
{
MessageBox(“错误:无法修改有关注册表信息!”);
return;
}
- 上一篇:VC++实现拨号上网程序
- 下一篇:在VC中修改显示器的分辩率和色彩

