| 关键词: nbsp 注册表 HKEY AfxMessageBox TCHAR ReRootKey ReSubKey ReValueName 名称 SetContent |
近来由于需要在自己写的程序中对注册表进行操作。总结些经验,并做个一个DEMO供日后使用,现在把它拿出来和大家分享…… 小弟初学VC,有误之处还请赐教。为了使用方便,我把一些操作写成了函数,以便方便调用,具体代码如下所示:一、定义 HKEY hKey;char content[256]; //所查询注册表键值的内容DWORD dwType=REG_SZ; //定义读取数据类型DWORD dwLength=256;struct HKEY__*RootKey; //注册表主键名称TCHAR *SubKey; //欲打开注册表项的地址TCHAR *KeyName; //欲设置项的名字TCHAR *ValueName; //欲设置值的名称LPBYTE SetContent_S; //字符串类型int SetContent_D[256]; //DWORD类型BYTE SetContent_B[256]; //二进制类型int ShowContent (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName);int SetValue_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S);int SetValue_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,int ReSetContent_D[256]);int SetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256]);int DeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName);int DeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName); 二、查看函数 ShowContent (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName){ int i=0; //操作结果:0==succeed if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_READ,hKey)==ERROR_SUCCESS) { if(RegQueryValueEx(hKey,ReValueName,NULL,dwType,(unsigned char *)content,&dwLength)!=ERROR_SUCCESS) { AfxMessageBox("错误:无法查询有关的注册表信息"); i=1; } RegCloseKey(hKey); } else { AfxMessageBox("错误:无法打开有关的hKEY"); i=1; } return i;} 三、设置字符串值函数 SetValue_S (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,LPBYTE ReSetContent_S){ int i=0; //操作结果:0==succeed //int StrLength; //StrLength=CString(SetContent_S).GetLength(); if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,hKey)==ERROR_SUCCESS) { if(RegSetValueEx(hKey,ReValueName,NULL,REG_SZ,ReSetContent_S,CString(SetContent_S).GetLength()) !=ERROR_SUCCESS) { AfxMessageBox("错误:无法设置有关的注册表信息"); i=1; } RegCloseKey(hKey); } else { AfxMessageBox("错误:无法查询有关的注册表信息"); i=1; } return i;} 四、设置DWORD值函数 SetValue_D (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,int ReSetContent_D[256]){ int i=0; //操作结果:0==succeed if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,hKey)==ERROR_SUCCESS) { if(RegSetValueEx(hKey,ReValueName,NULL,REG_DWORD, (const unsigned char *)ReSetContent_D,4)!=ERROR_SUCCESS) { AfxMessageBox("错误:无法设置有关的注册表信息"); i=1; } RegCloseKey(hKey); } else { AfxMessageBox("错误:无法查询有关的注册表信息"); i=1; } return i;}五、设置二进制值函数 SetValue_B (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName,BYTE ReSetContent_B[256]){ int i=0; //操作结果:0==succeed if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,hKey)==ERROR_SUCCESS) { if(RegSetValueEx(hKey,ReValueName,NULL,REG_BINARY, (const unsigned char *)ReSetContent_B,4)!=ERROR_SUCCESS) { AfxMessageBox("错误:无法设置有关的注册表信息"); i=1; } RegCloseKey(hKey); } else { AfxMessageBox("错误:无法查询有关的注册表信息"); i=1; } return i;} 六、删除子项函数 DeleteKey (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReKeyName){ int i=0; //操作结果:0==succeed if((RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,hKey))==ERROR_SUCCESS) { if((RegDeleteKey(hKey,ReKeyName))!=ERROR_SUCCESS) { //AfxMessageBox("清除指定项失败!"); i=1; } RegCloseKey(hKey); } else { //AfxMessageBox("错误:无法打开有关的hKEY"); i=1; } return i;} 七、删除键值函数 DeleteValue (struct HKEY__*ReRootKey,TCHAR *ReSubKey,TCHAR *ReValueName){ int i=0; //操作结果:0==succeed if(RegOpenKeyEx(ReRootKey,ReSubKey,0,KEY_WRITE,hKey)==ERROR_SUCCESS) { if(RegDeleteValue(hKey,ReValueName)!=ERROR_SUCCESS) { //AfxMessageBox("清除指定值失败!"); i=1; } RegCloseKey(hKey); } else { //AfxMessageBox("错误:无法打开有关的hKEY"); i=1; } return i;}八、调用方法 void CRegDemoDlg::OnSetValue_S() //例1所使用的代码:设置字符串值{ // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注册表主键名称 SubKey="Software\\Microsoft"; //欲打开注册表值的地址 ValueName="例1"; //欲设置值的名称 SetContent_S=LPBYTE("成功"); //值的内容 if((SetValue_S(RootKey,SubKey,ValueName,SetContent_S))!=0) AfxMessageBox("操作失败!");}void CRegDemoDlg::OnSetContent_B() //例2所使用的代码:设置二进制值{ // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注册表主键名称 SubKey="Software\\Microsoft"; //欲打开注册表值的地址 ValueName="例2"; //欲设置值的名称 SetContent_B[0]=1; //值的内容 //SetContent_B[1]=0x1B; //SetContent_B[2]=0x2C; //SetContent_B[3]=0x3D; //SetContent_B[4]=0x4E; if((SetValue_B(RootKey,SubKey,ValueName,SetContent_B))!=0) AfxMessageBox("操作失败!");}void CRegDemoDlg::OnSetContent_D() //例3所使用的代码:设置DWORD值{ // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注册表主键名称 SubKey="Software\\Microsoft"; //欲打开注册表值的地址 ValueName="例3"; //欲设置值的名称 SetContent_D[0]=4294967295; //值的内容 if((SetValue_D(RootKey,SubKey,ValueName,SetContent_D))!=0) AfxMessageBox("操作失败!");}void CRegDemoDlg::OnDeleteValue_1() //例4所使用的代码{ // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注册表主键名称 SubKey="Software\\Microsoft"; //欲打开注册表值的地址 ValueName="例1"; //欲设置值的名称 if((DeleteValue (RootKey,SubKey,ValueName))!=0) AfxMessageBox("操作失败!");}void CRegDemoDlg::OnDeleteValue_2() //例4所使用的代码{ // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注册表主键名称 SubKey="Software\\Microsoft"; //欲打开注册表值的地址 ValueName="例2"; //欲设置值的名称 if((DeleteValue (RootKey,SubKey,ValueName))!=0) AfxMessageBox("操作失败!");}void CRegDemoDlg::OnDeleteValue_3() //例4所使用的代码{ // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注册表主键名称 SubKey="Software\\Microsoft"; //欲打开注册表值的地址 ValueName="例3"; //欲设置值的名称 if((DeleteValue (RootKey,SubKey,ValueName))!=0) AfxMessageBox("操作失败!");}void CRegDemoDlg::OnDeleteKey() //例5所使用的代码{ // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注册表主键名称 SubKey="Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"; //欲打开注册表值的地址 KeyName="Doc Find Spec MRU"; //欲设置项的名称 if((DeleteKey (RootKey,SubKey,KeyName))!=0) AfxMessageBox("操作失败!");}void CRegDemoDlg::OnShowContent() //例1中的[查看]{ // TODO: Add your control notification handler code here RootKey=HKEY_CURRENT_USER; //注册表主键名称 SubKey="Software\\Microsoft"; //欲打开注册表值的地址 ValueName="例1"; //欲设置值的名称 if ((ShowContent(RootKey,SubKey,ValueName))==0) MessageBox(content,"本操作是利用ShowContent()函数完成的。"); |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|