[CMAKE]
[reactos.git] / base / applications / regedit / treeview.c
index cfbd401..5e0727f 100644 (file)
@@ -540,15 +540,16 @@ BOOL CreateNewKey(HWND hwndTV, HTREEITEM hItem)
     TCHAR szNewKey[128];
     LPCTSTR pszKeyPath;
     int iIndex = 1;
-    HKEY hRootKey;
-    HKEY hKey = NULL;
-    HKEY hNewKey = NULL;
+    LONG nResult;
+    HKEY hRootKey = NULL, hKey = NULL, hNewKey = NULL;
     BOOL bSuccess = FALSE;
     DWORD dwDisposition;
     HTREEITEM hNewItem;
 
-    pszKeyPath = GetItemPath(g_pChildWnd->hTreeWnd, hItem, &hRootKey);
-    if (RegOpenKey(hRootKey, pszKeyPath, &hKey) != ERROR_SUCCESS)
+    pszKeyPath = GetItemPath(hwndTV, hItem, &hRootKey);
+    if (pszKeyPath[0] == TEXT('\0'))
+        hKey = hRootKey;
+    else if (RegOpenKey(hRootKey, pszKeyPath, &hKey) != ERROR_SUCCESS)
         goto done;
 
     if (LoadString(hInst, IDS_NEW_KEY, szNewKeyFormat, sizeof(szNewKeyFormat) / sizeof(szNewKeyFormat[0])) <= 0)
@@ -557,13 +558,20 @@ BOOL CreateNewKey(HWND hwndTV, HTREEITEM hItem)
     /* Need to create a new key with a unique name */
     do
     {
-        _sntprintf(szNewKey, sizeof(szNewKey) / sizeof(szNewKey[0]), szNewKeyFormat, iIndex++);
-        RegCreateKeyEx(hKey, szNewKey, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hNewKey, &dwDisposition);
-        if (hNewKey && (dwDisposition == REG_OPENED_EXISTING_KEY))
+        wsprintf(szNewKey, szNewKeyFormat, iIndex++);
+        nResult = RegCreateKeyEx(hKey, szNewKey, 0, NULL, 0, KEY_WRITE, NULL, &hNewKey, &dwDisposition);
+        if (hNewKey && dwDisposition == REG_OPENED_EXISTING_KEY)
         {
             RegCloseKey(hNewKey);
             hNewKey = NULL;
         }
+        else if (!hNewKey)
+        {
+            TCHAR sz[256];
+            wsprintf(sz, TEXT("Cannot create new key!\n\nError Code: %d"), nResult);
+            MessageBox(hFrameWnd, sz, NULL, MB_ICONERROR);
+            goto done;
+        }
     }
     while(!hNewKey);
 
@@ -578,7 +586,7 @@ BOOL CreateNewKey(HWND hwndTV, HTREEITEM hItem)
     bSuccess = TRUE;
 
 done:
-    if (hKey)
+    if (hKey != hRootKey && hKey)
         RegCloseKey(hKey);
     if (hNewKey)
         RegCloseKey(hNewKey);
@@ -625,7 +633,7 @@ BOOL SelectNode(HWND hwndTV, LPCTSTR keyPath)
        TVITEM tvi;
 
     /* Total no-good hack */
-    if (!_tcsncmp(keyPath, _T("My Computer\\"), 12))
+    if (!_tcsnicmp(keyPath, _T("My Computer\\"), 12))
         keyPath += 12;
 
        hRoot = TreeView_GetRoot(hwndTV);
@@ -639,17 +647,17 @@ BOOL SelectNode(HWND hwndTV, LPCTSTR keyPath)
                /* Special case for root to expand root key abbreviations */
                if (hItem == hRoot)
                {
-                       if (!_tcscmp(szPathPart, TEXT("HKCR")))
+                       if (!_tcsicmp(szPathPart, TEXT("HKCR")))
                                _tcscpy(szPathPart, TEXT("HKEY_CLASSES_ROOT"));
-                       else if (!_tcscmp(szPathPart, TEXT("HKCU")))
+                       else if (!_tcsicmp(szPathPart, TEXT("HKCU")))
                                _tcscpy(szPathPart, TEXT("HKEY_CURRENT_USER"));
-                       else if (!_tcscmp(szPathPart, TEXT("HKLM")))
+                       else if (!_tcsicmp(szPathPart, TEXT("HKLM")))
                                _tcscpy(szPathPart, TEXT("HKEY_LOCAL_MACHINE"));
-                       else if (!_tcscmp(szPathPart, TEXT("HKU")))
+                       else if (!_tcsicmp(szPathPart, TEXT("HKU")))
                                _tcscpy(szPathPart, TEXT("HKEY_USERS"));
-                       else if (!_tcscmp(szPathPart, TEXT("HKCC")))
+                       else if (!_tcsicmp(szPathPart, TEXT("HKCC")))
                                _tcscpy(szPathPart, TEXT("HKEY_CURRENT_CONFIG"));
-                       else if (!_tcscmp(szPathPart, TEXT("HKDD")))
+                       else if (!_tcsicmp(szPathPart, TEXT("HKDD")))
                                _tcscpy(szPathPart, TEXT("HKEY_DYN_DATA"));
                }
 
@@ -664,7 +672,7 @@ BOOL SelectNode(HWND hwndTV, LPCTSTR keyPath)
 
                        (void)TreeView_GetItem(hwndTV, &tvi);
 
-                       if (!_tcscmp(szBuffer, szPathPart))
+                       if (!_tcsicmp(szBuffer, szPathPart))
                                break;
                }