//deletes the specified key from the registry BOOL DeleteRegistryKey(HKEY hCurrentKey, const char* pszKeyPath) { CString sPath(pszKeyPath); int nSlashPos = sPath.FindOneOf("\\/"); //see if we are at the end if(nSlashPos == -1) { //we are at the end, delete the value RegDeleteKey(hCurrentKey, sPath); return TRUE; } else { CString sCurrDir = sPath.Left(nSlashPos); //trim off the path sPath = sPath.Mid(nSlashPos + 1); //we need to recurse HKEY hNewKey; if(RegOpenKey(hCurrentKey, sCurrDir, &hNewKey) == ERROR_SUCCESS) { BOOL bRV = DeleteRegistryKey(hNewKey, sPath); RegCloseKey(hNewKey); return bRV; } else { return TRUE; } } }
BOOL SetC4FileClasses(const char *szEnginePath) { if (!SetRegFileClass("Clonk4.Scenario", "c4s", "Clonk 4 Scenario", szEnginePath, 1, C4FileClassContentType)) return FALSE; if (!SetRegFileClass("Clonk4.Group", "c4g", "Clonk 4 Group", szEnginePath, 2, C4FileClassContentType)) return FALSE; if (!SetRegFileClass("Clonk4.Folder", "c4f", "Clonk 4 Folder", szEnginePath, 3, C4FileClassContentType)) return FALSE; if (!SetRegFileClass("Clonk4.Player", "c4p", "Clonk 4 Player", szEnginePath, 4, C4FileClassContentType)) return FALSE; if (!SetRegFileClass("Clonk4.Definition", "c4d", "Clonk 4 Object Definition", szEnginePath, 6, C4FileClassContentType)) return FALSE; if (!SetRegFileClass("Clonk4.Object", "c4i", "Clonk 4 Object Info", szEnginePath, 7, C4FileClassContentType)) return FALSE; if (!SetRegFileClass("Clonk4.Material", "c4m", "Clonk 4 Material", szEnginePath, 8, "text/plain")) return FALSE; if (!SetRegFileClass("Clonk4.Binary", "c4b", "Clonk 4 Binary", szEnginePath, 9, "application/octet-stream")) return FALSE; if (!SetRegFileClass("Clonk4.Video", "c4v", "Clonk 4 Video", szEnginePath, 10, "video/avi")) return FALSE; if (!SetRegFileClass("Clonk4.Weblink", "c4l", "Clonk 4 Weblink", szEnginePath, 11, C4FileClassContentType)) return FALSE; if (!SetRegFileClass("Clonk4.Key", "c4k", "Clonk 4 Key", szEnginePath, 12, "application/octet-stream")) return FALSE; if (!SetRegFileClass("Clonk4.Update", "c4u", "Clonk 4 Update", szEnginePath, 13, C4FileClassContentType)) return FALSE; if (!SetProtocol("clonk", "%s %%1 /Fullscreen", szEnginePath)) return FALSE; char strCommand[2048]; // c4k installation: send to engine sprintf(strCommand, "\"%s\" \"%%1\"", szEnginePath); if (!SetRegShell("Clonk4.Key", "Register", "Register", strCommand)) return FALSE; // c4u application: send to engine sprintf(strCommand, "\"%s\" \"%%1\"", szEnginePath); if (!SetRegShell("Clonk4.Update", "Update", "Update", strCommand, TRUE)) return FALSE; // kill old App Paths registration DeleteRegistryKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\Clonk.exe"); return TRUE; }
// Deletes a registry key, including its subkeys void LRegistry::DeleteRegistryKey(HKEY hkMain, LPCTSTR szKey) { DWORD dwIndex = 0; HKEY hKey = NULL; LONG res = RegOpenKeyEx(hkMain, szKey, 0, KEY_ALL_ACCESS, &hKey); while (res == ERROR_SUCCESS) { TCHAR szSubKey[1024]; DWORD dwSubKeySize = 1024; FILETIME fileTime; res = RegEnumKeyEx(hKey, dwIndex, szSubKey, &dwSubKeySize, NULL, NULL, NULL, &fileTime); if (res == ERROR_SUCCESS) { DeleteRegistryKey(hKey, szSubKey); } // dwIndex remains 0, as we have deleted the first sub key } if (hKey) RegCloseKey(hKey); RegDeleteKey(hkMain, szKey); }
bool RemoveRegShell(const char *szClassName, const char *szShellName) { wchar_t strKey[256+1]; _snwprintf(strKey, 256, L"%s\\Shell\\%s", GetWideChar(szClassName).p, GetWideChar(szShellName).p); if (!DeleteRegistryKey(HKEY_CLASSES_ROOT, strKey)) return false; return true; }
// Moves one registry key to another position, including subkeys bool LRegistry::MoveRegistryKey(HKEY hkFrom, LPCTSTR szFrom, HKEY hkTo, LPCTSTR szTo) { bool success = CopyRegistryKey(hkFrom, szFrom, hkTo, szTo); if (success) DeleteRegistryKey(hkFrom, szFrom); return success; }
bool StdCompilerConfigWrite::Default(const char *szName) { // Open parent CreateKey(); // Remove key/value (failsafe) DeleteRegistryKey(pKey->Handle, GetWideChar(szName)); RegDeleteValueW(pKey->Handle, GetWideChar(szName)); // Handled return true; }
int Uninstall(HWND hWnd) { // Remove all installed files DeleteDirectory(g_sInstallPath); DeleteShortcut(hWnd); DeleteRegistryKey(); if (!g_bRunFromSetupDll) RunSystemUninstall(); // TODO: May probably handle errors during deletion (such as locked directories) // and notify user. Right now just return OK. return ErrOK; }
static bool DeleteRegistryKey(HKEY hKey, const wchar_t *szSubKey) { HKEY ckey; // Open the key if (RegOpenKeyExW(hKey, szSubKey, 0, KEY_ALL_ACCESS, &ckey) != ERROR_SUCCESS) return false; // Delete all subkeys wchar_t strChild[1024 + 1]; while (RegEnumKeyW(ckey, 0, strChild, 1024) == ERROR_SUCCESS) if (!DeleteRegistryKey(ckey, strChild)) return false; // Close the key RegCloseKey(ckey); // Delete the key if (RegDeleteKeyW(hKey, szSubKey) != ERROR_SUCCESS) return false; // Success return true; }
//--------------------------------------------------------------------------- HRESULT DeleteRegistryKey(HKEY RootKey,const wchar_t * KeyName) { HKEY hkey; HRESULT hr; if( (hr = RegOpenKeyExW(RootKey,KeyName,0,KEY_ENUMERATE_SUB_KEYS | KEY_READ | KEY_WRITE,&hkey)) == ERROR_SUCCESS ){ DWORD SubKeyNameLen, SubKeyNameLen2; wchar_t * SubKeyName, * SubKeyName2; for(;;){ LONG a; SubKeyName = (wchar_t *) malloc(sizeof(wchar_t)); SubKeyName[0] = L'\0'; SubKeyNameLen = 1; for(;;){ SubKeyNameLen2 = SubKeyNameLen; a = RegEnumKeyExW(hkey,0,SubKeyName,&SubKeyNameLen2,NULL,NULL,NULL,NULL); if( a != ERROR_MORE_DATA ) break; SubKeyName2 = (wchar_t *) realloc(SubKeyName,sizeof(wchar_t) * (SubKeyNameLen << 1)); if( SubKeyName2 == NULL ) break; SubKeyName = SubKeyName2; SubKeyNameLen <<= 1; } if( a != ERROR_SUCCESS || a == ERROR_NO_MORE_ITEMS ){ free(SubKeyName); break; } hr = RegDeleteKeyW(hkey,SubKeyName); if( FAILED(hr) ){ SubKeyName2 = (wchar_t *) malloc((wcslen(KeyName) + 1 + wcslen(SubKeyName) + 1) * sizeof(wchar_t)); if( SubKeyName2 != NULL ){ wcscpy_s(SubKeyName2,~rsize_t(0),KeyName); wcscat_s(SubKeyName2,~rsize_t(0),L"\\"); wcscat_s(SubKeyName2,~rsize_t(0),SubKeyName); hr = DeleteRegistryKey(RootKey,SubKeyName2); free(SubKeyName2); } } free(SubKeyName); if( FAILED(hr) ) break; } RegCloseKey(hkey); if( SUCCEEDED(hr) ) hr = RegDeleteKeyW(RootKey,KeyName); } return hr; }
// Process registry task int ProcessRegistryTask(tstring szName, tstring szValue, bool bAddValue, bool bSystemValue, bool bPrepend) { // Get the size of the current registry value RegKey obKey; DWORD dwSize = obKey.GetSizeOfValue(((bSystemValue) ? szREG_SYSTEM : szREG_USER), szName.c_str(), ((bSystemValue) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER)); DWORD iTotalLength = dwSize + (DWORD) szValue.size() + 1; TCHAR* pszRegkey = new TCHAR[iTotalLength]; memset(pszRegkey, 0, iTotalLength * sizeof(TCHAR)); bool bExpandedString = false; // Pre-process the string (i.e. is it expanded or dynamic) // Is this an expanded string ? if (szValue[0] == '%') { szValue = szValue.substr(1, szValue.length() - 1); // Remove the % character bExpandedString = true; } // If this is a dynamic string, i.e. contains "~PATH~", then replace ALL '~' chars with '%') szValue = DynamicCheckAndReplace(szValue, bExpandedString); // Does the value already exist ? bool bExists = obKey.GetKeyValue(((bSystemValue) ? szREG_SYSTEM : szREG_USER), szName.c_str(), pszRegkey, iTotalLength, false, ((bSystemValue) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER)); if (bExists) { // Already exists, so we need to modify it or delete it // First, check the type of value, i.e. REG_SZ or REG_EXPAND_SZ as we need to process them differently if (obKey.OpenKey(((bSystemValue) ? szREG_SYSTEM : szREG_USER), false, ((bSystemValue) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER))) { DWORD dwLen = 0; DWORD dwType = REG_SZ; if (obKey.QueryValue(szName.c_str(), dwLen, dwType)) { if (dwType == REG_EXPAND_SZ) bExpandedString = true; } obKey.CloseKey(); } if (bAddValue) // Add the value to the registry { if (bExpandedString) { tstring szOriginalValue = pszRegkey; // Does it already contain our new value ? size_t iValueIndex = 0; tstring::size_type index = FindValueInExpandedString(szOriginalValue, szValue, iValueIndex); if (index == tstring::npos) // Did we find it ? { tstring szNewValue; // No, so add it if (szOriginalValue.length() > 0) { // We may need to add a semi colon seperator if the last character of the original value is not a semi colon if (bPrepend) szNewValue = szValue + _T(";") + szOriginalValue; // Add semi colon else { if (szOriginalValue[szOriginalValue.length() - 1] != _T(';')) szNewValue = szOriginalValue + _T(";") + szValue; // Add semi colon else szNewValue = szOriginalValue + szValue; // Do not add the semi colon } } else szNewValue = szValue; if (!SetRegistryKey(obKey, bSystemValue, true, szName, szNewValue)) { DisplaySysError(_T("Set Value"), obKey.GetLastErrorCode()); // Failed delete[] pszRegkey; pszRegkey = NULL; return (obKey.GetLastErrorCode() == 0x05) ? ERR_ACCESS_DENIED : ERR_ERROR; } } } else // Just overwrite it { if (!SetRegistryKey(obKey, bSystemValue, bExpandedString, szName, szValue)) { DisplaySysError(_T("Set Value"), obKey.GetLastErrorCode()); // Failed delete[] pszRegkey; pszRegkey = NULL; return (obKey.GetLastErrorCode() == 0x05) ? ERR_ACCESS_DENIED : ERR_ERROR; } } } else // Delete the entry { if (szValue.length() == 0 || !bExpandedString) // Delete the whole key { if (!obKey.DeleteKeyValue(((bSystemValue) ? szREG_SYSTEM : szREG_USER), szName.c_str(), false, ((bSystemValue) ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER))) { DisplaySysError(_T("Delete Value"), obKey.GetLastErrorCode()); // Failed delete[] pszRegkey; pszRegkey = NULL; return (obKey.GetLastErrorCode() == 0x05) ? ERR_ACCESS_DENIED : ERR_ERROR; } } else if (bExpandedString) // We need to delete our bit only and leave the rest of the value alone { tstring szOriginalValue = pszRegkey; size_t iValueIndex = 0; tstring::size_type index = FindValueInExpandedString(szOriginalValue, szValue, iValueIndex); if (index != tstring::npos) // Did we find it ? { // We should only delete our value if it actually exists in the environment variable tstring szNewValue; if (szOriginalValue[index + szValue.length()] == _T(';')) // Any terminating ; to remove ? szNewValue = szOriginalValue.replace(index, szValue.length() + 1, _T("")); else szNewValue = szOriginalValue.replace(index, szValue.length(), _T("")); if (szNewValue[szNewValue.length() - 1] == _T(';')) szNewValue.replace(szNewValue.length() - 1, 1, _T("")); if (szNewValue.empty()) DeleteRegistryKey(obKey, bSystemValue, szName); else if (!SetRegistryKey(obKey, bSystemValue, true, szName, szNewValue)) { DisplaySysError(_T("Delete Value"), obKey.GetLastErrorCode()); // Failed delete[] pszRegkey; pszRegkey = NULL; return (obKey.GetLastErrorCode() == 0x05) ? ERR_ACCESS_DENIED : ERR_ERROR; } } } } } else // Value does NOT exist yet so create it { if (bAddValue) { bool bOK = SetRegistryKey(obKey, bSystemValue, bExpandedString, szName, szValue); if (!bOK) { DisplaySysError(_T("Set Value"), obKey.GetLastErrorCode()); // Failed delete[] pszRegkey; pszRegkey = NULL; return (obKey.GetLastErrorCode() == 0x05) ? ERR_ACCESS_DENIED : ERR_ERROR; } } } // Tell the system that we have changed a setting (can take 0.5 second to return) SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) _T("Environment"), SMTO_ABORTIFHUNG, 500, 0); delete[] pszRegkey; pszRegkey = NULL; return ERR_SUCCESS; }
bool SharedUtil::RemoveRegistryKey ( const SString& strPath ) { return DeleteRegistryKey ( HKEY_LOCAL_MACHINE, MakeVersionRegistryPath ( GetMajorVersionString (), strPath ) ); }