RegOpenKeyEx---Deleting a Key with Subkeys
来源:互联网 发布:澳门网络真人博客官网 编辑:程序博客网 时间:2024/05/18 04:00
The example in this topic uses the RegOpenKeyEx, RegEnumKeyEx, and RegDeleteKey functions to delete a registry key with subkeys.
To test this example, create the following registry key by using Regedt32.exe, and then add a few values and subkeys:
HKEY_CURRENT_USER\Software\TestDir
After running the code, use the F5 key to refresh the registry data, and notice that theTestDir key is deleted.
#include <windows.h>#include <stdio.h>#include <strsafe.h>//*************************************************************//// RegDelnodeRecurse()//// Purpose: Deletes a registry key and all its subkeys / values.//// Parameters: hKeyRoot - Root key// lpSubKey - SubKey to delete//// Return: TRUE if successful.// FALSE if an error occurs.////*************************************************************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;}//*************************************************************//// RegDelnode()//// Purpose: Deletes a registry key and all its subkeys / values.//// Parameters: hKeyRoot - Root key// lpSubKey - SubKey to delete//// Return: TRUE if successful.// FALSE if an error occurs.////*************************************************************BOOL RegDelnode (HKEY hKeyRoot, LPTSTR lpSubKey){ TCHAR szDelKey[MAX_PATH*2]; StringCchCopy (szDelKey, MAX_PATH*2, lpSubKey); return RegDelnodeRecurse(hKeyRoot, szDelKey);}void __cdecl main(){ BOOL bSuccess; bSuccess = RegDelnode(HKEY_CURRENT_USER, TEXT("Software\\TestDir")); if(bSuccess) printf("Success!\n"); else printf("Failure.\n");}
0 0
- RegOpenKeyEx---Deleting a Key with Subkeys
- Deleting a changelist with shelved files
- Deleting a Service
- Deleting a git commit
- RegOpenKeyEx
- creating a Self Deleting Executable
- a sample for deleting duplicated records
- Total Commander 9.0a(32+64)With Key
- 解决注册表 error while deleting key 方法之一
- How to add, modify, or delete registry subkeys and values by using a registration entries (.reg) file
- A web application created a ThreadLocal with key of type [null]
- Struts2 A web application created a ThreadLocal with key of type
- struts2关于A web application created a ThreadLocal with key of type 异常解决办法
- struts2关于A web application created a ThreadLocal with key of type 异常解决办法
- struts2关于A web application created a ThreadLocal with key of type 异常解决办法
- SSH-A web application created a ThreadLocal with key of type [null]
- struts2关于A web application created a ThreadLocal with key of type 异常解决办法
- struts2关于A web application created a ThreadLocal with key of type 异常解决办法
- android侧滑删除列表
- 3.service知识点总结
- Linux命令——文件管理命令(部分常用的)
- IO流_编译期异常和运行期异常的区别
- (转)Android 消息机制学习
- RegOpenKeyEx---Deleting a Key with Subkeys
- 4.Intent 知识点总结
- 5.BroadcastReceiver知识点总结
- 无聊的游戏【NOIP2014八校联考第4场第1试10.19】
- Java中 四种引用(强引用 软引用 弱引用 虚引用)
- LeetCode - 128. Longest Consecutive Sequence - 思路详解- C++
- 6.ContentProvider知识点总结
- 约瑟夫环C++笔试题
- Tomcat 配置遇到的问题