注册表处理之写入和删除带子项的注册表
2011-05-30 10:36
155 查看
写入带子项的注册表
删除带子项的注册表
MSDN上的
自己写的
注意:处理注册表项的时候一定要记住打开之后及时关闭,与串口类似不能对已经打开没有关闭的注册表项进行操作。
bool AddRegInfoStruct(PREG_INFO_CTRL pRegInfoCtrl) { bool success = true; PREG_INFO_CTRL pRegStructCtrl = pRegInfoCtrl;//新创建一个结构体变量用于存储,否则会改变原来结构体指针中的数据 if(pRegStructCtrl) { //名称值下一个 char name[260] = {0}; strcpy(name, pRegStructCtrl->szKeyName); //创建键 HKEY hkey; DWORD dw;//-1表示创建失败 0表示创建成功 dw = RegCreateKeyEx(HKEY_CURRENT_USER,short2wide(name),0,NULL,0,0,NULL,&hkey,&dw);//创建或打开注册表 if(dw!=ERROR_SUCCESS) { //MessageBox(NULL,_T("create false"), L"Notice", MB_OK); const char * notice = "create false 11!"; log(notice, strlen(notice)); success = false; return success; } RegCloseKey(hkey); PREG_VALUE_CTRL pRegValueCtrl = pRegStructCtrl->pRegValueCtrl; //写入键值 while(pRegValueCtrl) { char valueName[260] = {0}; strcpy(valueName, pRegValueCtrl->szValueName); char value[260] = {0}; strcpy(value, pRegValueCtrl->szValue); HKEY hKey; DWORD dw; RegOpenKeyEx(HKEY_CURRENT_USER,short2wide(name),0,0,&hKey);//打开子键 wchar_t * strKeyName = short2wide(valueName); wchar_t * strKeyValue = short2wide(value); LONG lResult; lResult = RegSetValueEx(hKey,strKeyName,0,REG_SZ,(BYTE *)strKeyValue,wcslen(strKeyValue)*2); if(lResult != 0) { const char * notice = "设值失败!"; log(notice, strlen(notice)); success = false; return success; } RegCloseKey(hKey); pRegValueCtrl = pRegValueCtrl->pNext; } PREG_INFO_CTRL *pRegInfoCtrlList1 = new PREG_INFO_CTRL[MAX_PATH]; ZeroMemory(pRegInfoCtrlList1,(sizeof(PREG_INFO_CTRL))*MAX_PATH); int j = 0; while(pRegStructCtrl->pSubKeyCtrl[j] != NULL) { pRegInfoCtrlList1[j]= pRegStructCtrl->pSubKeyCtrl[j]; j++; } for(int i = 0; i<j; i++) { PREG_INFO_CTRL regInfoCtrl = pRegInfoCtrlList1[i];//取出每一个子项,每一个子项又是一个完整的结构体,此时用递归吧 AddRegInfoStruct(regInfoCtrl); } if(NULL!= pRegInfoCtrlList1) delete []pRegInfoCtrlList1; } return success; }
删除带子项的注册表
MSDN上的
MSDN上下的,之前自己也写了一个 http://msdn.microsoft.com/en-us/library/ms724235(v=vs.85).aspx BOOL RegDelnodeRecurse(HKEY hKeyRoot, LPTSTR lpSubKey) { LPTSTR lpEnd; LONG lResult; DWORD dwSize; TCHAR szName[MAX_PATH]; HKEY hKey; FILETIME ftWrite; // First, see if we can delete the key without having // to recurse. lResult = RegDeleteKey(hKeyRoot, lpSubKey); if (lResult == ERROR_SUCCESS) return TRUE; lResult = RegOpenKeyEx (hKeyRoot, lpSubKey, 0, KEY_READ, &hKey); if (lResult != ERROR_SUCCESS) { if (lResult == ERROR_FILE_NOT_FOUND) { printf("Key not found./n"); return TRUE; } else { printf("Error opening key./n"); return FALSE; } } // Check for an ending slash and add one if it is missing. lpEnd = lpSubKey + lstrlen(lpSubKey); if (*(lpEnd - 1) != TEXT('//')) { *lpEnd = TEXT('//'); lpEnd++; *lpEnd = TEXT('/0'); } // Enumerate the keys dwSize = MAX_PATH; lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL, NULL, NULL, &ftWrite); if (lResult == ERROR_SUCCESS) { do { StringCchCopy (lpEnd, MAX_PATH*2, szName); if (!RegDelnodeRecurse(hKeyRoot, lpSubKey)) { break; } dwSize = MAX_PATH; lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL, NULL, NULL, &ftWrite); } while (lResult == ERROR_SUCCESS); } lpEnd--; *lpEnd = TEXT('/0'); RegCloseKey (hKey); // Try again to delete the key. lResult = RegDeleteKey(hKeyRoot, lpSubKey); if (lResult == ERROR_SUCCESS) return TRUE; return FALSE; }
自己写的
bool DeleteRegistry(HKEY hRootKey, LPCTSTR RegPath) { DWORD dwError=ERROR_SUCCESS; DWORD dwSubKeyLength=MAX_KEY_LENGTH; LPTSTR pSubKey = NULL; TCHAR subRegPath[MAX_KEY_LENGTH]; HKEY hSubKey; if (RegPath && lstrlen(RegPath)) { if(RegOpenKeyEx(hRootKey,RegPath,0, KEY_ENUMERATE_SUB_KEYS|DELETE, &hSubKey)==ERROR_SUCCESS) { int i = 0; do{ dwSubKeyLength = MAX_KEY_LENGTH; if(RegEnumKeyEx( hSubKey, i, subRegPath, &dwSubKeyLength,NULL,NULL, NULL,NULL)== ERROR_NO_MORE_ITEMS) { //没有子项的时候才进行删除 RegCloseKey(hSubKey); dwError = RegDeleteKey(hRootKey, RegPath); break; } else DeleteRegistry(hSubKey, subRegPath);//dwError= i++; }while(dwError==ERROR_SUCCESS); } } else return FALSE; if(dwError==ERROR_SUCCESS) return TRUE; return FALSE; }
注意:处理注册表项的时候一定要记住打开之后及时关闭,与串口类似不能对已经打开没有关闭的注册表项进行操作。
相关文章推荐
- Windows 7安装软件时无法将注册值写入注册表的处理方法
- java处理Excel文件---excel文件的创建,删除,写入,读取
- 批处理文件删除注册表数据
- JAVA注册表读取写入删除
- java处理Excel文件---excel文件的创建,删除,写入,读取
- Windows 7安装软件时无法将注册值写入注册表的处理方法
- InstallScript工程中安装包写入的默认注册表信息如何删除
- C# 将程序添加到启动项 (写入注册表),及从启动项中删除
- Basic INFO:InstallScript工程中安装包写入的默认注册表信息如何删除
- C# 将程序添加到启动项 (写入注册表),及从启动项中删除
- 关于C# 读取 写入 删除 注册表简单例子
- 注册表写入读取和启动项删除
- Java处理Excel文件---excel文件的创建,删除,写入,读取
- 批处理删除注册表项与增加注册表服务
- java处理Excel文件---excel文件的创建,删除,写入,读取
- java向注册表写入和删除实例
- 注册表处理之(写入DWORD类型或者字符串类型的键项值)
- MySql注册表删除
- cookie的写入和删除功能
- 一步步学习SPD2010--第二章节--处理SP网站(4)--删除SP网站