您的位置:首页 > 其它

注册表处理之写入和删除带子项的注册表

2011-05-30 10:36 155 查看
写入带子项的注册表

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;
}


注意:处理注册表项的时候一定要记住打开之后及时关闭,与串口类似不能对已经打开没有关闭的注册表项进行操作。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: