/* ------------------------------------------------------------------------- */ void associate(const std::basic_string<TCHAR>& key, const std::basic_string<TCHAR>& value, bool flag) { static const TCHAR* clsid_associate = _T("{F3DB85F4-4731-4e80-BC2E-754A7320D830}"); static const TCHAR* clsid_tooltip = _T("{00021500-0000-0000-C000-000000000046}"); HKEY subkey; LONG status = RegOpenKeyEx(HKEY_CLASSES_ROOT, ( key + _T( "\\shellex" ) ).c_str(), 0, KEY_ALL_ACCESS, &subkey ); if( !status ) { RegDeleteKeyNT( subkey, clsid_tooltip ); RegCloseKey( subkey ); } if (flag) { DWORD disposition = 0; LONG status = RegCreateKeyEx(HKEY_CLASSES_ROOT, key.c_str(), 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &subkey, &disposition); if (!status) { TCHAR buffer[32] = {}; DWORD type = 0; DWORD size = sizeof(buffer); if (RegQueryValueEx(subkey, _T(""), NULL, &type, (LPBYTE)buffer, &size) == ERROR_SUCCESS && std::basic_string<TCHAR>(buffer) != value) { RegSetValueEx(subkey, CUBEICE_REG_PREVARCHIVER, 0, REG_SZ, (CONST BYTE*)buffer, (_tcslen(buffer) + 1) * sizeof(TCHAR)); } RegSetValueEx(subkey, _T(""), 0, REG_SZ, (CONST BYTE*)value.c_str(), (value.size() + 1) * sizeof(TCHAR)); disposition = 0; status = RegCreateKeyEx(HKEY_CLASSES_ROOT, ( key + _T( "\\shellex\\") + clsid_tooltip ).c_str(), 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &subkey, &disposition); if (!status) { RegSetValueEx(subkey, _T(""), 0, REG_SZ, (CONST BYTE*)clsid_associate, (_tcslen(clsid_associate) + 1) * sizeof(TCHAR)); } } } else { LONG status = RegOpenKeyEx(HKEY_CLASSES_ROOT, key.c_str(), 0, KEY_ALL_ACCESS, &subkey); if (!status) { TCHAR buffer[32] = {}; DWORD type = 0; DWORD size = sizeof(buffer); if (RegQueryValueEx(subkey, _T(""), NULL, &type, (LPBYTE)buffer, &size) == ERROR_SUCCESS && std::basic_string<TCHAR>(buffer) == value) { TCHAR prev[32] = {}; type = 0; size = sizeof(prev); if (RegQueryValueEx(subkey, CUBEICE_REG_PREVARCHIVER, NULL, &type, (LPBYTE)prev, &size) == ERROR_SUCCESS) { RegSetValueEx(subkey, _T(""), 0, REG_SZ, (CONST BYTE*)prev, (_tcslen(prev) + 1) * sizeof(TCHAR)); } else { RegCloseKey(subkey); RegDeleteKeyNT(HKEY_CLASSES_ROOT, key.c_str()); } } } } }
void MCS_delete_registry(const char *key, MCExecPoint &dest) { HKEY hkey = NULL; /* get the root key string, it is at the begining of the key */ char *str = (char *)strchr(key, '\\'); if (!str) { MCresult->sets("no key"); dest.setboolean(False); return; } *str++ = '\0'; //str now pointing to the begining of subkey string /* find the matching key for the root key string */ MCString s = key; uint1 i; for (i = 0 ; i < ELEMENTS(Regkeys) ; i++) { if (s == Regkeys[i].token) break; } if (i >= ELEMENTS(Regkeys)) { MCresult->sets("bad key"); dest.setboolean(False); return; } errno = RegDeleteKeyNT(Regkeys[i].key, str); if (errno != ERROR_SUCCESS) { MCresult->sets("could not delete key"); dest.setboolean(False); } else { MCresult->clear(False); dest.setboolean(True); } }
/* ------------------------------------------------------------------------- */ inline DWORD RegDeleteKeyNT(HKEY hStartKey , LPCTSTR pKeyName ){ static const int max_keylen = 2048; DWORD dwRtn, dwSubKeyLength; LPTSTR pSubKey = NULL; TCHAR szSubKey[max_keylen] = {}; HKEY hKey; // Do not allow NULL or empty key name if ( pKeyName && lstrlen(pKeyName)) { if( (dwRtn=RegOpenKeyEx(hStartKey,pKeyName, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &hKey )) == ERROR_SUCCESS ) { while (dwRtn == ERROR_SUCCESS ) { dwSubKeyLength = max_keylen; dwRtn = RegEnumKeyEx( hKey, 0, // always index zero szSubKey, &dwSubKeyLength, NULL, NULL, NULL, NULL ); if(dwRtn == ERROR_NO_MORE_ITEMS) { dwRtn = RegDeleteKey(hStartKey, pKeyName); break; } else if(dwRtn == ERROR_SUCCESS) dwRtn=RegDeleteKeyNT(hKey, szSubKey); } RegCloseKey(hKey); // Do not save return code because error // has already occurred } } else dwRtn = ERROR_BADKEY; return dwRtn; }
//WINNT does not delete registry entry if there are subkeys..this functions fixes that. DWORD RegDeleteKeyNT(HKEY hStartKey, char *pKeyName) { DWORD dwRtn, dwSubKeyLength; char *pSubKey = NULL; char szSubKey[256]; HKEY hKey; // Do not allow NULL or empty key name if (pKeyName && lstrlenA(pKeyName)) { if ((dwRtn = RegOpenKeyExA(hStartKey, pKeyName, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &hKey)) == ERROR_SUCCESS) { while (dwRtn == ERROR_SUCCESS) { dwSubKeyLength = 256; dwRtn = RegEnumKeyExA(hKey, 0, szSubKey, &dwSubKeyLength, NULL, NULL, NULL, NULL); if (dwRtn == ERROR_NO_MORE_ITEMS) { dwRtn = RegDeleteKeyA(hStartKey, pKeyName); break; } else if (dwRtn == ERROR_SUCCESS) dwRtn = RegDeleteKeyNT(hKey, szSubKey); } RegCloseKey(hKey); // Do not save return code because error // has already occurred } } else dwRtn = ERROR_BADKEY; return dwRtn; }