#define APPKEY "Software\\Microsoft\\test"
#define USERNAME "UserName"
#define USERAGE "UserAge"
#define MAXNAMELENGTH 20
//写注册表
HKEY hkey;
DWORD dwAction;
CString m_name="XXXX1111";
DWORD m_age=0x00000001;
//create/open the key
if(RegCreateKeyEx(HKEY_CURRENT_USER,APPKEY,0L,NULL,REG_OPTION_NON_VOLATILE,\
KEY_ALL_ACCESS,NULL,&hkey,&dwAction) != ERROR_SUCCESS)
{
AfxMessageBox("error create/open the key");
return ;
}
//set the key写字符串值
if(RegSetValueEx(hkey,USERNAME,0L,REG_SZ,(LPBYTE)(LPCTSTR)m_name,m_name.GetLength()+1) != ERROR_SUCCESS)
{
AfxMessageBox("error writing Username");
return ;
}
//写入DWORD值
if(RegSetValueEx(hkey,USERAGE,0,REG_DWORD,(LPBYTE)&m_age,sizeof(DWORD))!=ERROR_SUCCESS)
{
AfxMessageBox("error writing User age");
return ;
}
// RegDeleteValue(hkey,"UserName");//删除键值
//close the key
RegCloseKey(hkey);