[SHELL32] Fixes and improvements for PickIconDlg().
[reactos.git] / dll / win32 / shell32 / dialogs / dialogs.cpp
index eb947be..47b6f04 100644 (file)
@@ -2,6 +2,7 @@
  *    common shell dialogs
  *
  * Copyright 2000 Juergen Schmied
+ * Copyright 2018 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 
 typedef struct
 {
-    HWND hwndOwner ;
-    HICON hIcon ;
-    LPCWSTR lpstrDirectory ;
-    LPCWSTR lpstrTitle ;
-    LPCWSTR lpstrDescription ;
-    UINT uFlags ;
-} RUNFILEDLGPARAMS ;
+    HWND hwndOwner;
+    HICON hIcon;
+    LPCWSTR lpstrDirectory;
+    LPCWSTR lpstrTitle;
+    LPCWSTR lpstrDescription;
+    UINT uFlags;
+} RUNFILEDLGPARAMS;
 
-typedef BOOL (WINAPI * LPFNOFN) (OPENFILENAMEW *) ;
+typedef BOOL (WINAPI * LPFNOFN) (OPENFILENAMEW *);
 
 WINE_DEFAULT_DEBUG_CHANNEL(shell);
-static INT_PTR CALLBACK RunDlgProc (HWND, UINT, WPARAM, LPARAM) ;
-static void FillList (HWND, char *, BOOL) ;
+static INT_PTR CALLBACK RunDlgProc(HWND, UINT, WPARAM, LPARAM);
+static void FillList(HWND, LPWSTR, UINT, BOOL);
 
 
 /*************************************************************************
@@ -46,217 +47,345 @@ typedef struct
 {
     HMODULE hLibrary;
     HWND hDlgCtrl;
-    WCHAR szName[MAX_PATH];
+    WCHAR szPath[MAX_PATH];
     INT Index;
+    INT nIcons;
+    HICON *phIcons;
 } PICK_ICON_CONTEXT, *PPICK_ICON_CONTEXT;
 
 BOOL CALLBACK EnumPickIconResourceProc(HMODULE hModule,
     LPCWSTR lpszType,
     LPWSTR lpszName,
-    LONG_PTR lParam
-)
+    LONG_PTR lParam)
 {
-    WCHAR szName[100];
-    int index;
-    HICON  hIcon;
-    PPICK_ICON_CONTEXT pIconContext = (PPICK_ICON_CONTEXT)lParam;
+    PPICK_ICON_CONTEXT pIconContext = PPICK_ICON_CONTEXT(lParam);
+    HWND hDlgCtrl = pIconContext->hDlgCtrl;
 
     if (IS_INTRESOURCE(lpszName))
-        swprintf(szName, L"%u", (DWORD)lpszName);
+        lParam = LOWORD(lpszName);
     else
-        wcscpy(szName, (WCHAR*)lpszName);
-
-
-    hIcon = LoadIconW(pIconContext->hLibrary, (LPCWSTR)lpszName);
-    if (hIcon == NULL)
-        return TRUE;
+        lParam = -1;
 
-    index = SendMessageW(pIconContext->hDlgCtrl, LB_ADDSTRING, 0, (LPARAM)szName);
-    if (index != LB_ERR)
-        SendMessageW(pIconContext->hDlgCtrl, LB_SETITEMDATA, index, (LPARAM)hIcon);
+    SendMessageW(hDlgCtrl, LB_ADDSTRING, 0, lParam);
 
     return TRUE;
 }
 
 static void
-DestroyIconList(HWND hDlgCtrl)
+DestroyIconList(HWND hDlgCtrl, PPICK_ICON_CONTEXT pIconContext)
 {
     int count;
     int index;
 
-    count = SendMessage(hDlgCtrl, LB_GETCOUNT, 0, 0);
+    count = SendMessageW(hDlgCtrl, LB_GETCOUNT, 0, 0);
     if (count == LB_ERR)
         return;
 
     for(index = 0; index < count; index++)
     {
-        HICON hIcon = (HICON)SendMessageW(hDlgCtrl, LB_GETITEMDATA, index, 0);
-        DestroyIcon(hIcon);
+        DestroyIcon(pIconContext->phIcons[index]);
+        pIconContext->phIcons[index] = NULL;
+    }
+}
+
+static BOOL
+DoLoadIcons(HWND hwndDlg, PPICK_ICON_CONTEXT pIconContext, LPCWSTR pszFile)
+{
+    WCHAR szExpandedPath[MAX_PATH];
+
+    // Destroy previous icons
+    DestroyIconList(pIconContext->hDlgCtrl, pIconContext);
+    SendMessageW(pIconContext->hDlgCtrl, LB_RESETCONTENT, 0, 0);
+    delete[] pIconContext->phIcons;
+
+    // Store the path
+    StringCchCopyW(pIconContext->szPath, _countof(pIconContext->szPath), pszFile);
+    ExpandEnvironmentStringsW(pszFile, szExpandedPath, _countof(szExpandedPath));
+
+    // Load the module if possible
+    HMODULE hLibrary = LoadLibraryExW(szExpandedPath, NULL, LOAD_LIBRARY_AS_DATAFILE);
+    if (pIconContext->hLibrary)
+        FreeLibrary(pIconContext->hLibrary);
+    pIconContext->hLibrary = hLibrary;
+
+    if (pIconContext->hLibrary)
+    {
+        // Load the icons from the module
+        pIconContext->nIcons = ExtractIconExW(szExpandedPath, -1, NULL, NULL, 0);
+        pIconContext->phIcons = new HICON[pIconContext->nIcons];
+
+        if (ExtractIconExW(szExpandedPath, 0, pIconContext->phIcons, NULL, pIconContext->nIcons))
+        {
+            EnumResourceNamesW(pIconContext->hLibrary, RT_GROUP_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext);
+        }
+        else
+        {
+            pIconContext->nIcons = 0;
+        }
+    }
+    else
+    {
+        // .ico file
+        pIconContext->nIcons = 1;
+        pIconContext->phIcons = new HICON[1];
+
+        if (ExtractIconExW(szExpandedPath, 0, pIconContext->phIcons, NULL, pIconContext->nIcons))
+        {
+            SendMessageW(pIconContext->hDlgCtrl, LB_ADDSTRING, 0, 0);
+        }
+        else
+        {
+            pIconContext->nIcons = 0;
+        }
+    }
+
+    // Set the text and reset the edit control's modification flag
+    SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szPath);
+    SendDlgItemMessage(hwndDlg, IDC_EDIT_PATH, EM_SETMODIFY, FALSE, 0);
+
+    if (pIconContext->nIcons == 0)
+    {
+        delete[] pIconContext->phIcons;
+        pIconContext->phIcons = NULL;
     }
+
+    return (pIconContext->nIcons > 0);
+}
+
+static const LPCWSTR s_pszDefaultPath = L"%SystemRoot%\\system32\\shell32.dll";
+
+static void NoIconsInFile(HWND hwndDlg, PPICK_ICON_CONTEXT pIconContext)
+{
+    // Show an error message
+    CStringW strText, strTitle(MAKEINTRESOURCEW(IDS_PICK_ICON_TITLE));
+    strText.Format(IDS_NO_ICONS, pIconContext->szPath);
+    MessageBoxW(hwndDlg, strText, strTitle, MB_ICONWARNING);
+
+    // Load the default icons
+    DoLoadIcons(hwndDlg, pIconContext, s_pszDefaultPath);
 }
 
-INT_PTR CALLBACK PickIconProc(HWND hwndDlg,
+// Icon size
+#define CX_ICON     GetSystemMetrics(SM_CXICON)
+#define CY_ICON     GetSystemMetrics(SM_CYICON)
+
+// Item size
+#define CX_ITEM     (CX_ICON + 4)
+#define CY_ITEM     (CY_ICON + 12)
+
+INT_PTR CALLBACK PickIconProc(
+    HWND hwndDlg,
     UINT uMsg,
     WPARAM wParam,
-    LPARAM lParam
-)
+    LPARAM lParam)
 {
-    LPMEASUREITEMSTRUCT lpmis; 
-    LPDRAWITEMSTRUCT lpdis; 
+    LPMEASUREITEMSTRUCT lpmis;
+    LPDRAWITEMSTRUCT lpdis;
     HICON hIcon;
     INT index, count;
-    WCHAR szText[MAX_PATH], szTitle[100], szFilter[100];
-    OPENFILENAMEW ofn = {0};
+    WCHAR szText[MAX_PATH], szFilter[100];
+    CStringW strTitle;
+    OPENFILENAMEW ofn;
 
     PPICK_ICON_CONTEXT pIconContext = (PPICK_ICON_CONTEXT)GetWindowLongPtr(hwndDlg, DWLP_USER);
 
     switch(uMsg)
     {
-    case WM_INITDIALOG:
-        pIconContext = (PPICK_ICON_CONTEXT)lParam;
-        SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG)pIconContext);
-        pIconContext->hDlgCtrl = GetDlgItem(hwndDlg, IDC_PICKICON_LIST);
-        SendMessageW(pIconContext->hDlgCtrl, LB_SETCOLUMNWIDTH, 32, 0);
-        EnumResourceNamesW(pIconContext->hLibrary, RT_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext);
-        if (PathUnExpandEnvStringsW(pIconContext->szName, szText, MAX_PATH))
-            SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, szText);
-        else
-            SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName);
-
-        count = SendMessage(pIconContext->hDlgCtrl, LB_GETCOUNT, 0, 0);
-        if (count != LB_ERR)
+        case WM_INITDIALOG:
         {
-            if (count > pIconContext->Index)
+            pIconContext = (PPICK_ICON_CONTEXT)lParam;
+            SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pIconContext);
+            pIconContext->hDlgCtrl = GetDlgItem(hwndDlg, IDC_PICKICON_LIST);
+
+            SendMessageW(pIconContext->hDlgCtrl, LB_SETCOLUMNWIDTH, CX_ITEM, 0);
+
+            // Load the icons
+            if (!DoLoadIcons(hwndDlg, pIconContext, pIconContext->szPath))
+                NoIconsInFile(hwndDlg, pIconContext);
+
+            // Set the selection
+            count = SendMessageW(pIconContext->hDlgCtrl, LB_GETCOUNT, 0, 0);
+            if (count != LB_ERR)
+            {
+                if (pIconContext->Index < 0)
+                {
+                    // A negative value will be interpreted as a negated resource ID.
+                    LPARAM lParam = -pIconContext->Index;
+                    pIconContext->Index = (INT)SendMessageW(pIconContext->hDlgCtrl, LB_FINDSTRINGEXACT, -1, lParam);
+                }
+
+                if (pIconContext->Index < 0 || count <= pIconContext->Index)
+                    pIconContext->Index = 0;
+
                 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, pIconContext->Index, 0);
-            else
-                SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, 0, 0);
+                SendMessageW(pIconContext->hDlgCtrl, LB_SETTOPINDEX, pIconContext->Index, 0);
+            }
+            return TRUE;
         }
-        return TRUE;
-    case WM_COMMAND:
-        switch(LOWORD(wParam))
+
+        case WM_DESTROY:
         {
-        case IDOK:
-            index = SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
-            pIconContext->Index = index;
-            GetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName, MAX_PATH);
-            DestroyIconList(pIconContext->hDlgCtrl);
-            EndDialog(hwndDlg, 1);
-            break;
-        case IDCANCEL:
-            DestroyIconList(pIconContext->hDlgCtrl);
-            EndDialog(hwndDlg, 0);
-            break;
-        case IDC_PICKICON_LIST:
-            if (HIWORD(wParam) == LBN_SELCHANGE)
-                InvalidateRect((HWND)lParam, NULL, TRUE); // FIXME USE UPDATE RECT
+            DestroyIconList(pIconContext->hDlgCtrl, pIconContext);
+            delete[] pIconContext->phIcons;
+
+            if (pIconContext->hLibrary)
+                FreeLibrary(pIconContext->hLibrary);
             break;
-        case IDC_BUTTON_PATH:
-            szText[0] = 0;
-            szTitle[0] = 0;
-            szFilter[0] = 0;
-            ofn.lStructSize = sizeof(ofn);
-            ofn.hwndOwner = hwndDlg;
-            ofn.lpstrFile = szText;
-            ofn.nMaxFile = MAX_PATH;
-            LoadStringW(shell32_hInstance, IDS_PICK_ICON_TITLE, szTitle, sizeof(szTitle) / sizeof(WCHAR));
-            ofn.lpstrTitle = szTitle;
-            LoadStringW(shell32_hInstance, IDS_PICK_ICON_FILTER, szFilter, sizeof(szFilter) / sizeof(WCHAR));
-            ofn.lpstrFilter = szFilter;
-            if (GetOpenFileNameW(&ofn))
+        }
+
+        case WM_COMMAND:
+            switch(LOWORD(wParam))
             {
-                HMODULE hLibrary;
+            case IDOK:
+            {
+                /* Check whether the path edit control has been modified; if so load the icons instead of validating */
+                if (SendDlgItemMessage(hwndDlg, IDC_EDIT_PATH, EM_GETMODIFY, 0, 0))
+                {
+                    /* Reset the edit control's modification flag and retrieve the text */
+                    SendDlgItemMessage(hwndDlg, IDC_EDIT_PATH, EM_SETMODIFY, FALSE, 0);
+                    GetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, szText, _countof(szText));
 
-                if (!wcsicmp(pIconContext->szName, szText))
+                    // Load the icons
+                    if (!DoLoadIcons(hwndDlg, pIconContext, szText))
+                        NoIconsInFile(hwndDlg, pIconContext);
+
+                    // Set the selection
+                    SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, 0, 0);
                     break;
+                }
+
+                /* The path edit control has not been modified, return the selection */
+                pIconContext->Index = (INT)SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
+                GetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szPath, _countof(pIconContext->szPath));
+                EndDialog(hwndDlg, 1);
+                break;
+            }
+
+            case IDCANCEL:
+                EndDialog(hwndDlg, 0);
+                break;
+
+            case IDC_PICKICON_LIST:
+                switch (HIWORD(wParam))
+                {
+                    case LBN_SELCHANGE:
+                        InvalidateRect((HWND)lParam, NULL, TRUE);
+                        break;
 
-                DestroyIconList(pIconContext->hDlgCtrl);
+                    case LBN_DBLCLK:
+                        SendMessage(hwndDlg, WM_COMMAND, MAKEWPARAM(IDOK, 0), 0);
+                        break;
+                }
+                break;
 
-                hLibrary = LoadLibraryExW(szText, NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
-                if (hLibrary == NULL)
+            case IDC_BUTTON_PATH:
+            {
+                // Choose the module path
+                szText[0] = 0;
+                szFilter[0] = 0;
+                ZeroMemory(&ofn, sizeof(ofn));
+                ofn.lStructSize = sizeof(ofn);
+                ofn.hwndOwner = hwndDlg;
+                ofn.lpstrFile = szText;
+                ofn.nMaxFile = _countof(szText);
+                strTitle.LoadString(IDS_PICK_ICON_TITLE);
+                ofn.lpstrTitle = strTitle;
+                LoadStringW(shell32_hInstance, IDS_PICK_ICON_FILTER, szFilter, _countof(szFilter));
+                ofn.lpstrFilter = szFilter;
+                if (!GetOpenFileNameW(&ofn))
                     break;
-                FreeLibrary(pIconContext->hLibrary);
-                pIconContext->hLibrary = hLibrary;
-                wcscpy(pIconContext->szName, szText);
-                EnumResourceNamesW(pIconContext->hLibrary, RT_ICON, EnumPickIconResourceProc, (LPARAM)pIconContext);
-                if (PathUnExpandEnvStringsW(pIconContext->szName, szText, MAX_PATH))
-                    SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, szText);
-                else
-                    SetDlgItemTextW(hwndDlg, IDC_EDIT_PATH, pIconContext->szName);
 
+                // Load the icons
+                if (!DoLoadIcons(hwndDlg, pIconContext, szText))
+                    NoIconsInFile(hwndDlg, pIconContext);
+
+                // Set the selection
                 SendMessageW(pIconContext->hDlgCtrl, LB_SETCURSEL, 0, 0);
+                break;
+            }
+
+            default:
+                break;
             }
             break;
-        }
-        break;
+
         case WM_MEASUREITEM:
-            lpmis = (LPMEASUREITEMSTRUCT) lParam; 
-            lpmis->itemHeight = 32;
-            lpmis->itemWidth = 64;
-            return TRUE; 
-        case WM_DRAWITEM: 
-            lpdis = (LPDRAWITEMSTRUCT) lParam; 
-           if (lpdis->itemID == (UINT)-1) 
-            { 
-                break; 
-            } 
-            switch (lpdis->itemAction) 
-            { 
-                case ODA_SELECT: 
+            lpmis = (LPMEASUREITEMSTRUCT)lParam;
+            lpmis->itemHeight = CY_ITEM;
+            return TRUE;
+
+        case WM_DRAWITEM:
+        {
+            lpdis = (LPDRAWITEMSTRUCT)lParam;
+            if (lpdis->itemID == (UINT)-1)
+                break;
+            switch (lpdis->itemAction)
+            {
+                case ODA_SELECT:
                 case ODA_DRAWENTIRE:
+                {
                     index = SendMessageW(pIconContext->hDlgCtrl, LB_GETCURSEL, 0, 0);
-                    hIcon =(HICON)SendMessage(lpdis->hwndItem, LB_GETITEMDATA, lpdis->itemID, (LPARAM) 0);
+                    hIcon = pIconContext->phIcons[lpdis->itemID];
 
                     if (lpdis->itemID == (UINT)index)
-                    {
-                        HBRUSH hBrush;
-                        hBrush = CreateSolidBrush(RGB(0, 0, 255));
-                        FillRect(lpdis->hDC, &lpdis->rcItem, hBrush);
-                        DeleteObject(hBrush);
-                    }
+                        FillRect(lpdis->hDC, &lpdis->rcItem, (HBRUSH)(COLOR_HIGHLIGHT + 1));
                     else
-                    {
-                        HBRUSH hBrush;
-                        hBrush = CreateSolidBrush(RGB(255, 255, 255));
-                        FillRect(lpdis->hDC, &lpdis->rcItem, hBrush);
-                        DeleteObject(hBrush);
-                    }
-                    DrawIconEx(lpdis->hDC, lpdis->rcItem.left,lpdis->rcItem.top, hIcon, 
-                                0,
-                                0,
-                                0,
-                                NULL,
-                                DI_NORMAL);
+                        FillRect(lpdis->hDC, &lpdis->rcItem, (HBRUSH)(COLOR_WINDOW + 1));
+
+                    // Centering
+                    INT x = lpdis->rcItem.left + (CX_ITEM - CX_ICON) / 2;
+                    INT y = lpdis->rcItem.top + (CY_ITEM - CY_ICON) / 2;
+
+                    DrawIconEx(lpdis->hDC, x, y, hIcon, 0, 0, 0, NULL, DI_NORMAL);
                     break;
+                }
             }
-            break;
+            return TRUE;
+        }
     }
 
     return FALSE;
 }
 
 BOOL WINAPI PickIconDlg(
-    HWND hwndOwner,
+    HWND hWndOwner,
     LPWSTR lpstrFile,
     UINT nMaxFile,
     INT* lpdwIconIndex)
 {
-    HMODULE hLibrary;
     int res;
-    PICK_ICON_CONTEXT IconContext;
+    WCHAR szExpandedPath[MAX_PATH];
 
-    hLibrary = LoadLibraryExW(lpstrFile, NULL, LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
-    IconContext.hLibrary = hLibrary;
+    // Initialize the dialog
+    PICK_ICON_CONTEXT IconContext = { NULL };
     IconContext.Index = *lpdwIconIndex;
-    wcscpy(IconContext.szName, lpstrFile);
+    StringCchCopyW(IconContext.szPath, _countof(IconContext.szPath), lpstrFile);
+    ExpandEnvironmentStringsW(lpstrFile, szExpandedPath, _countof(szExpandedPath));
+
+    if (!szExpandedPath[0] ||
+        GetFileAttributesW(szExpandedPath) == INVALID_FILE_ATTRIBUTES)
+    {
+        if (szExpandedPath[0])
+        {
+            // No such file
+            CStringW strText, strTitle(MAKEINTRESOURCEW(IDS_PICK_ICON_TITLE));
+            strText.Format(IDS_FILE_NOT_FOUND, lpstrFile);
+            MessageBoxW(hWndOwner, strText, strTitle, MB_ICONWARNING);
+        }
+
+        // Set the default value
+        StringCchCopyW(IconContext.szPath, _countof(IconContext.szPath), s_pszDefaultPath);
+    }
 
-    res = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_PICK_ICON), hwndOwner, PickIconProc, (LPARAM)&IconContext);
+    // Show the dialog
+    res = DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_PICK_ICON), hWndOwner, PickIconProc, (LPARAM)&IconContext);
     if (res)
     {
-        wcscpy(lpstrFile, IconContext.szName);
+        // Store the selected icon
+        StringCchCopyW(lpstrFile, nMaxFile, IconContext.szPath);
         *lpdwIconIndex = IconContext.Index;
     }
 
-    FreeLibrary(hLibrary);
     return res;
 }
 
@@ -266,7 +395,7 @@ BOOL WINAPI PickIconDlg(
  * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
  */
 void WINAPI RunFileDlg(
-    HWND hwndOwner,
+    HWND hWndOwner,
     HICON hIcon,
     LPCWSTR lpstrDirectory,
     LPCWSTR lpstrTitle,
@@ -276,15 +405,14 @@ void WINAPI RunFileDlg(
     TRACE("\n");
 
     RUNFILEDLGPARAMS rfdp;
-    rfdp.hwndOwner        = hwndOwner;
+    rfdp.hwndOwner        = hWndOwner;
     rfdp.hIcon            = hIcon;
     rfdp.lpstrDirectory   = lpstrDirectory;
     rfdp.lpstrTitle       = lpstrTitle;
     rfdp.lpstrDescription = lpstrDescription;
     rfdp.uFlags           = uFlags;
 
-    DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_RUN), hwndOwner, RunDlgProc, (LPARAM)&rfdp);
-
+    DialogBoxParamW(shell32_hInstance, MAKEINTRESOURCEW(IDD_RUN), hWndOwner, RunDlgProc, (LPARAM)&rfdp);
 }
 
 
@@ -346,6 +474,27 @@ static LPWSTR RunDlg_GetParentDir(LPCWSTR cmdline)
     }
 }
 
+static void EnableOkButtonFromEditContents(HWND hwnd)
+{
+    BOOL Enable = FALSE;
+    INT Length, n;
+    HWND Edit = GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH);
+    Length = GetWindowTextLengthW(Edit);
+    if (Length > 0)
+    {
+        PWCHAR psz = (PWCHAR)HeapAlloc(GetProcessHeap(), 0, (Length + 1) * sizeof(WCHAR));
+        if (psz)
+        {
+            GetWindowTextW(Edit, psz, Length + 1);
+            for (n = 0; n < Length && !Enable; ++n)
+                Enable = psz[n] != ' ';
+            HeapFree(GetProcessHeap(), 0, psz);
+        }
+        else
+            Enable = TRUE;
+    }
+    EnableWindow(GetDlgItem(hwnd, IDOK), Enable);
+}
 
 /* Dialog procedure for RunFileDlg */
 static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
@@ -355,7 +504,7 @@ static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARA
     switch (message)
     {
         case WM_INITDIALOG:
-            prfdp = (RUNFILEDLGPARAMS *)lParam ;
+            prfdp = (RUNFILEDLGPARAMS *)lParam;
             SetWindowLongPtrW(hwnd, DWLP_USER, (LONG_PTR)prfdp);
 
             if (prfdp->lpstrTitle)
@@ -370,98 +519,155 @@ static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARA
             }
             if (prfdp->uFlags & RFF_NOLABEL)
                 ShowWindow(GetDlgItem(hwnd, IDC_RUNDLG_LABEL), SW_HIDE);
-            if (prfdp->uFlags & RFF_CALCDIRECTORY)
-                FIXME("RFF_CALCDIRECTORY not supported\n");
+            if (prfdp->uFlags & RFF_NOSEPARATEMEM)
+            {
+                FIXME("RFF_NOSEPARATEMEM not supported\n");
+            }
 
+            /* Use the default Shell Run icon if no one is specified */
             if (prfdp->hIcon == NULL)
-                prfdp->hIcon = LoadIconW(NULL, (LPCWSTR)IDI_WINLOGO);
-            SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
-            SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
+                prfdp->hIcon = LoadIconW(shell32_hInstance, MAKEINTRESOURCEW(IDI_SHELL_RUN));
+            /*
+             * NOTE: Starting Windows Vista, the "Run File" dialog gets a
+             * title icon that remains the same as the default one, even if
+             * the user specifies a custom icon.
+             * Since we currently imitate Windows 2003, therefore do not show
+             * any title icon.
+             */
+            // SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
+            // SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
             SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_ICON), STM_SETICON, (WPARAM)prfdp->hIcon, 0);
 
-            FillList (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), NULL, (prfdp->uFlags & RFF_NODEFAULT) == 0);
-            SetFocus (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH));
+            FillList(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), NULL, 0, (prfdp->uFlags & RFF_NODEFAULT) == 0);
+            EnableOkButtonFromEditContents(hwnd);
+            SetFocus(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH));
             return TRUE;
 
         case WM_COMMAND:
-            switch (LOWORD (wParam))
+            switch (LOWORD(wParam))
             {
                 case IDOK:
                 {
-                    int ic;
-                    HWND htxt = GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH);
-                    if ((ic = GetWindowTextLengthW (htxt)))
+                    LRESULT lRet;
+                    HWND htxt = GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH);
+                    INT ic;
+                    WCHAR *psz, *parent = NULL;
+                    NMRUNFILEDLGW nmrfd;
+
+                    ic = GetWindowTextLengthW(htxt);
+                    if (ic == 0)
                     {
-                        WCHAR *psz, *parent = NULL;
-                        SHELLEXECUTEINFOW sei;
-
-                        ZeroMemory (&sei, sizeof(sei));
-                        sei.cbSize = sizeof(sei);
-                        psz = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, (ic + 1)*sizeof(WCHAR));
-
-                        if (psz)
-                        {
-                            GetWindowTextW(htxt, psz, ic + 1);
-
-                            /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
-                             * WM_NOTIFY before execution */
+                        EndDialog(hwnd, IDCANCEL);
+                        return TRUE;
+                    }
 
-                            sei.hwnd = hwnd;
-                            sei.nShow = SW_SHOWNORMAL;
-                            sei.lpFile = psz;
+                    /*
+                     * Allocate a new MRU entry, we need to add two characters
+                     * for the terminating "\\1" part, then the NULL character.
+                     */
+                    psz = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, (ic + 2 + 1)*sizeof(WCHAR));
+                    if (!psz)
+                    {
+                        EndDialog(hwnd, IDCANCEL);
+                        return TRUE;
+                    }
 
-                            if (prfdp->lpstrDirectory)
-                                sei.lpDirectory = prfdp->lpstrDirectory;
-                            else
-                                sei.lpDirectory = parent = RunDlg_GetParentDir(sei.lpFile);
+                    GetWindowTextW(htxt, psz, ic + 1);
+                    StrTrimW(psz, L" \t");
+
+                    /*
+                     * The precedence is the following: first the user-given
+                     * current directory is used; if there is none, a current
+                     * directory is computed if the RFF_CALCDIRECTORY is set,
+                     * otherwise no current directory is defined.
+                     */
+                    LPCWSTR pszStartDir;
+                    if (prfdp->lpstrDirectory)
+                        pszStartDir = prfdp->lpstrDirectory;
+                    else if (prfdp->uFlags & RFF_CALCDIRECTORY)
+                        pszStartDir = parent = RunDlg_GetParentDir(psz);
+                    else
+                        pszStartDir = NULL;
+
+                    /* Hide the dialog for now on, we will show it up in case of retry */
+                    ShowWindow(hwnd, SW_HIDE);
+
+                    /*
+                     * As shown by manual tests on Windows, modifying the contents
+                     * of the notification structure will not modify what the
+                     * Run-Dialog will use for the nShow parameter. However the
+                     * lpFile and lpDirectory pointers are set to the buffers used
+                     * by the Run-Dialog, as a consequence they can be modified by
+                     * the notification receiver, as long as it respects the lengths
+                     * of the buffers (to avoid buffer overflows).
+                     */
+                    nmrfd.hdr.code = RFN_VALIDATE;
+                    nmrfd.hdr.hwndFrom = hwnd;
+                    nmrfd.hdr.idFrom = 0;
+                    nmrfd.lpFile = psz;
+                    nmrfd.lpDirectory = pszStartDir;
+                    nmrfd.nShow = SW_SHOWNORMAL;
+
+                    lRet = SendMessageW(prfdp->hwndOwner, WM_NOTIFY, 0, (LPARAM)&nmrfd.hdr);
+
+                    switch (lRet)
+                    {
+                        case RF_CANCEL:
+                            EndDialog(hwnd, IDCANCEL);
+                            break;
 
-                            if (!ShellExecuteExW(&sei))
+                        case RF_OK:
+                            if (SUCCEEDED(ShellExecCmdLine(hwnd, psz, pszStartDir, SW_SHOWNORMAL, NULL,
+                                                           SECL_ALLOW_NONEXE)))
                             {
-                                HeapFree(GetProcessHeap(), 0, psz);
-                                HeapFree(GetProcessHeap(), 0, parent);
-                                SendMessageA (htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
-                                return TRUE;
+                                /* Call again GetWindowText in case the contents of the edit box has changed? */
+                                GetWindowTextW(htxt, psz, ic + 1);
+                                FillList(htxt, psz, ic + 2 + 1, FALSE);
+                                EndDialog(hwnd, IDOK);
+                                break;
                             }
 
-                            /* FillList is still ANSI */
-                            GetWindowTextA (htxt, (LPSTR)psz, ic + 1);
-                            FillList (htxt, (LPSTR)psz, FALSE);
-
-                            HeapFree(GetProcessHeap(), 0, psz);
-                            HeapFree(GetProcessHeap(), 0, parent);
-                            EndDialog (hwnd, 0);
-                        }
+                        /* Fall-back */
+                        case RF_RETRY:
+                        default:
+                            SendMessageW(htxt, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
+                            /* Show back the dialog */
+                            ShowWindow(hwnd, SW_SHOW);
+                            break;
                     }
+
+                    HeapFree(GetProcessHeap(), 0, parent);
+                    HeapFree(GetProcessHeap(), 0, psz);
+                    return TRUE;
                 }
 
                 case IDCANCEL:
-                    EndDialog (hwnd, 0);
+                    EndDialog(hwnd, IDCANCEL);
                     return TRUE;
 
                 case IDC_RUNDLG_BROWSE:
                 {
                     HMODULE hComdlg = NULL;
                     LPFNOFN ofnProc = NULL;
-                    static const WCHAR comdlg32W[] = L"comdlg32";
                     WCHAR szFName[1024] = {0};
                     WCHAR filter[MAX_PATH], szCaption[MAX_PATH];
                     OPENFILENAMEW ofn;
 
-                    LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER, filter, MAX_PATH);
-                    LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, MAX_PATH);
+                    LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_FILTER, filter, _countof(filter));
+                    LoadStringW(shell32_hInstance, IDS_RUNDLG_BROWSE_CAPTION, szCaption, _countof(szCaption));
 
                     ZeroMemory(&ofn, sizeof(ofn));
-                    ofn.lStructSize = sizeof(OPENFILENAMEW);
+                    ofn.lStructSize = sizeof(ofn);
                     ofn.hwndOwner = hwnd;
                     ofn.lpstrFilter = filter;
                     ofn.lpstrFile = szFName;
-                    ofn.nMaxFile = 1023;
+                    ofn.nMaxFile = _countof(szFName) - 1;
                     ofn.lpstrTitle = szCaption;
                     ofn.Flags = OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
                     ofn.lpstrInitialDir = prfdp->lpstrDirectory;
 
-                    if (NULL == (hComdlg = LoadLibraryExW (comdlg32W, NULL, 0)) ||
-                        NULL == (ofnProc = (LPFNOFN)GetProcAddress (hComdlg, "GetOpenFileNameW")))
+                    if (NULL == (hComdlg = LoadLibraryExW(L"comdlg32", NULL, 0)) ||
+                        NULL == (ofnProc = (LPFNOFN)GetProcAddress(hComdlg, "GetOpenFileNameW")))
                     {
                         ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg, ofnProc);
                         ShellMessageBoxW(shell32_hInstance, hwnd, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR), NULL, MB_OK | MB_ICONERROR);
@@ -470,161 +676,260 @@ static INT_PTR CALLBACK RunDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARA
 
                     if (ofnProc(&ofn))
                     {
-                        SetFocus (GetDlgItem (hwnd, IDOK));
-                        SetWindowTextW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), szFName);
-                        SendMessageW (GetDlgItem (hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
-                        SetFocus (GetDlgItem (hwnd, IDOK));
+                        SetFocus(GetDlgItem(hwnd, IDOK));
+                        SetWindowTextW(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), szFName);
+                        SendMessageW(GetDlgItem(hwnd, IDC_RUNDLG_EDITPATH), CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
+                        EnableOkButtonFromEditContents(hwnd);
+                        SetFocus(GetDlgItem(hwnd, IDOK));
                     }
 
-                    FreeLibrary (hComdlg);
+                    FreeLibrary(hComdlg);
 
                     return TRUE;
                 }
+                case IDC_RUNDLG_EDITPATH:
+                {
+                    if (HIWORD(wParam) == CBN_EDITCHANGE)
+                    {
+                        EnableOkButtonFromEditContents(hwnd);
+                    }
+                    return TRUE;
+                }
             }
             return TRUE;
     }
     return FALSE;
 }
 
-/* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
-/* fShowDefault ignored if pszLatest != NULL */
-static void FillList(HWND hCb, char *pszLatest, BOOL fShowDefault)
+/*
+ * This function grabs the MRU list from the registry and fills the combo-list
+ * for the "Run" dialog above. fShowDefault is ignored if pszLatest != NULL.
+ */
+// FIXME: Part of this code should be part of some MRUList API,
+// that is scattered amongst shell32, comctl32 (?!) and comdlg32.
+static void FillList(HWND hCb, LPWSTR pszLatest, UINT cchStr, BOOL fShowDefault)
 {
-    HKEY hkey ;
-/*    char szDbgMsg[256] = "" ; */
-    char *pszList = NULL, *pszCmd = NULL, cMatch = 0, cMax = 0x60, szIndex[2] = "-" ;
-    DWORD icList = 0, icCmd = 0 ;
-    UINT Nix ;
-
-    SendMessageA (hCb, CB_RESETCONTENT, 0, 0) ;
-
-    if (ERROR_SUCCESS != RegCreateKeyExA (
-        HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
-        0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL))
-        MessageBoxA (hCb, "Unable to open registry key !", "Nix", MB_OK) ;
+    HKEY hkey;
+    WCHAR *pszList = NULL, *pszCmd = NULL, *pszTmp = NULL, cMatch = 0, cMax = 0x60;
+    WCHAR szIndex[2] = L"-";
+    UINT cchLatest;
+    DWORD dwType, icList = 0, icCmd = 0;
+    LRESULT lRet;
+    UINT Nix;
+
+    /*
+     * Retrieve the string length of pszLatest and check whether its buffer size
+     * (cchStr in number of characters) is large enough to add the terminating "\\1"
+     * (and the NULL character).
+     */
+    if (pszLatest)
+    {
+        cchLatest = wcslen(pszLatest);
+        if (cchStr < cchLatest + 2 + 1)
+        {
+            TRACE("pszLatest buffer is not large enough (%d) to hold the MRU terminator.\n", cchStr);
+            return;
+        }
+    }
+    else
+    {
+        cchStr = 0;
+    }
 
-    RegQueryValueExA (hkey, "MRUList", NULL, NULL, NULL, &icList) ;
+    SendMessageW(hCb, CB_RESETCONTENT, 0, 0);
 
-    if (icList > 0)
+    lRet = RegCreateKeyExW(HKEY_CURRENT_USER,
+                           L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
+                           0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL);
+    if (lRet != ERROR_SUCCESS)
     {
-        pszList = (char *)HeapAlloc( GetProcessHeap(), 0, icList) ;
+        TRACE("Unable to open or create the RunMRU key, error %d\n", GetLastError());
+        return;
+    }
 
-        if (pszList)
+    lRet = RegQueryValueExW(hkey, L"MRUList", NULL, &dwType, NULL, &icList);
+    if (lRet == ERROR_SUCCESS && dwType == REG_SZ && icList > sizeof(WCHAR))
+    {
+        pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icList);
+        if (!pszList)
         {
-            if (ERROR_SUCCESS != RegQueryValueExA (hkey, "MRUList", NULL, NULL, (LPBYTE)pszList, &icList))
-                MessageBoxA (hCb, "Unable to grab MRUList !", "Nix", MB_OK);
+            TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
+            goto Continue;
         }
-        else
+        pszList[0] = L'\0';
+
+        lRet = RegQueryValueExW(hkey, L"MRUList", NULL, NULL, (LPBYTE)pszList, &icList);
+        if (lRet != ERROR_SUCCESS)
         {
-            TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
+            TRACE("Unable to grab MRUList, error %d\n", GetLastError());
+            pszList[0] = L'\0';
         }
     }
     else
     {
-        icList = 1 ;
-        pszList = (char *)HeapAlloc( GetProcessHeap(), 0, icList) ;
-        pszList[0] = 0 ;
+Continue:
+        icList = sizeof(WCHAR);
+        pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icList);
+        if (!pszList)
+        {
+            TRACE("HeapAlloc failed to allocate %d bytes\n", icList);
+            RegCloseKey(hkey);
+            return;
+        }
+        pszList[0] = L'\0';
     }
 
-    for (Nix = 0 ; Nix < icList - 1 ; Nix++)
+    /* Convert the number of bytes from MRUList into number of characters (== number of indices) */
+    icList /= sizeof(WCHAR);
+
+    for (Nix = 0; Nix < icList - 1; Nix++)
     {
         if (pszList[Nix] > cMax)
-            cMax = pszList[Nix] ;
+            cMax = pszList[Nix];
+
+        szIndex[0] = pszList[Nix];
 
-        szIndex[0] = pszList[Nix] ;
+        lRet = RegQueryValueExW(hkey, szIndex, NULL, &dwType, NULL, &icCmd);
+        if (lRet != ERROR_SUCCESS || dwType != REG_SZ)
+        {
+            TRACE("Unable to grab size of index, error %d\n", GetLastError());
+            continue;
+        }
 
-        if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, NULL, &icCmd))
-            MessageBoxA (hCb, "Unable to grab size of index", "Nix", MB_OK) ;
-        if( pszCmd )
-            pszCmd = (char *)HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd) ;
+        if (pszCmd)
+        {
+            pszTmp = (WCHAR*)HeapReAlloc(GetProcessHeap(), 0, pszCmd, icCmd);
+            if (!pszTmp)
+            {
+                TRACE("HeapReAlloc failed to reallocate %d bytes\n", icCmd);
+                continue;
+            }
+            pszCmd = pszTmp;
+        }
         else
-            pszCmd = (char *)HeapAlloc(GetProcessHeap(), 0, icCmd) ;
-        if (ERROR_SUCCESS != RegQueryValueExA (hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd))
-            MessageBoxA (hCb, "Unable to grab index", "Nix", MB_OK) ;
+        {
+            pszCmd = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, icCmd);
+            if (!pszCmd)
+            {
+                TRACE("HeapAlloc failed to allocate %d bytes\n", icCmd);
+                continue;
+            }
+        }
+
+        lRet = RegQueryValueExW(hkey, szIndex, NULL, NULL, (LPBYTE)pszCmd, &icCmd);
+        if (lRet != ERROR_SUCCESS)
+        {
+            TRACE("Unable to grab index, error %d\n", GetLastError());
+            continue;
+        }
+
+        /*
+         * Generally the command string will end up with "\\1".
+         * Find the last backslash in the string and NULL-terminate.
+         * Windows does not seem to check for what comes next, so that
+         * a command of the form:
+         *     c:\\my_dir\\myfile.exe
+         * will be cut just after "my_dir", whereas a command of the form:
+         *     c:\\my_dir\\myfile.exe\\1
+         * will be cut just after "myfile.exe".
+         */
+        pszTmp = wcsrchr(pszCmd, L'\\');
+        if (pszTmp)
+            *pszTmp = L'\0';
 
-        if (NULL != pszLatest)
+        /*
+         * In the following we try to add pszLatest to the MRU list.
+         * We suppose that our caller has already correctly allocated
+         * the string with enough space for us to append a "\\1".
+         *
+         * FIXME: TODO! (At the moment we don't append it!)
+         */
+
+        if (pszLatest)
         {
-            if (!lstrcmpiA(pszCmd, pszLatest))
+            if (wcsicmp(pszCmd, pszLatest) == 0)
             {
-                /*
-                sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
-                MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
-                */
-                SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd) ;
-                SetWindowTextA (hCb, pszCmd) ;
-                SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
-
-                cMatch = pszList[Nix] ;
-                memmove (&pszList[1], pszList, Nix) ;
-                pszList[0] = cMatch ;
-                continue ;
+                SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszCmd);
+                SetWindowTextW(hCb, pszCmd);
+                SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
+
+                cMatch = pszList[Nix];
+                memmove(&pszList[1], pszList, Nix * sizeof(WCHAR));
+                pszList[0] = cMatch;
+                continue;
             }
         }
 
-        if (26 != icList - 1 || icList - 2 != Nix || cMatch || NULL == pszLatest)
+        if (icList - 1 != 26 || icList - 2 != Nix || cMatch || pszLatest == NULL)
         {
-            /*
-            sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
-            MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
-            */
-            SendMessageA (hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd) ;
+            SendMessageW(hCb, CB_ADDSTRING, 0, (LPARAM)pszCmd);
             if (!Nix && fShowDefault)
             {
-                SetWindowTextA (hCb, pszCmd) ;
-                SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
+                SetWindowTextW(hCb, pszCmd);
+                SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
             }
         }
         else
         {
-            /*
-            sprintf (szDbgMsg, "Doing loop thing.\n") ;
-            MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
-            */
-            SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
-            SetWindowTextA (hCb, pszLatest) ;
-            SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
-
-            cMatch = pszList[Nix] ;
-            memmove (&pszList[1], pszList, Nix) ;
-            pszList[0] = cMatch ;
-            szIndex[0] = cMatch ;
-            RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
+            SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest);
+            SetWindowTextW(hCb, pszLatest);
+            SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM(0, -1));
+
+            cMatch = pszList[Nix];
+            memmove(&pszList[1], pszList, Nix * sizeof(WCHAR));
+            pszList[0] = cMatch;
+            szIndex[0] = cMatch;
+
+            wcscpy(&pszLatest[cchLatest], L"\\1");
+            RegSetValueExW(hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, (cchLatest + 2 + 1) * sizeof(WCHAR));
+            pszLatest[cchLatest] = L'\0';
         }
     }
 
-    if (!cMatch && NULL != pszLatest)
+    if (!cMatch && pszLatest != NULL)
     {
-        /*
-        sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
-        MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
-        */
-        SendMessageA (hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest) ;
-        SetWindowTextA (hCb, pszLatest) ;
-        SendMessageA (hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1)) ;
-
-        cMatch = ++cMax ;
-        if (pszList)
-            pszList = (char *)HeapReAlloc(GetProcessHeap(), 0, pszList, ++icList) ;
-        else
-            pszList = (char *)HeapAlloc(GetProcessHeap(), 0, ++icList) ;
+        SendMessageW(hCb, CB_INSERTSTRING, 0, (LPARAM)pszLatest);
+        SetWindowTextW(hCb, pszLatest);
+        SendMessageW(hCb, CB_SETEDITSEL, 0, MAKELPARAM (0, -1));
+
+        cMatch = ++cMax;
 
         if (pszList)
         {
-            memmove (&pszList[1], pszList, icList - 1) ;
-            pszList[0] = cMatch ;
-            szIndex[0] = cMatch ;
-            RegSetValueExA (hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, strlen (pszLatest) + 1) ;
+            pszTmp = (WCHAR*)HeapReAlloc(GetProcessHeap(), 0, pszList, (++icList) * sizeof(WCHAR));
+            if (!pszTmp)
+            {
+                TRACE("HeapReAlloc failed to reallocate enough bytes\n");
+                goto Cleanup;
+            }
+            pszList = pszTmp;
         }
         else
         {
-            TRACE("HeapAlloc or HeapReAlloc failed to allocate enough bytes\n");
+            pszList = (WCHAR*)HeapAlloc(GetProcessHeap(), 0, (++icList) * sizeof(WCHAR));
+            if (!pszList)
+            {
+                TRACE("HeapAlloc failed to allocate enough bytes\n");
+                goto Cleanup;
+            }
         }
+
+        memmove(&pszList[1], pszList, (icList - 1) * sizeof(WCHAR));
+        pszList[0] = cMatch;
+        szIndex[0] = cMatch;
+
+        wcscpy(&pszLatest[cchLatest], L"\\1");
+        RegSetValueExW(hkey, szIndex, 0, REG_SZ, (LPBYTE)pszLatest, (cchLatest + 2 + 1) * sizeof(WCHAR));
+        pszLatest[cchLatest] = L'\0';
     }
 
-    RegSetValueExA (hkey, "MRUList", 0, REG_SZ, (LPBYTE)pszList, strlen (pszList) + 1) ;
+Cleanup:
+    RegSetValueExW(hkey, L"MRUList", 0, REG_SZ, (LPBYTE)pszList, (wcslen(pszList) + 1) * sizeof(WCHAR));
 
-    HeapFree( GetProcessHeap(), 0, pszCmd) ;
-    HeapFree( GetProcessHeap(), 0, pszList) ;
+    HeapFree(GetProcessHeap(), 0, pszCmd);
+    HeapFree(GetProcessHeap(), 0, pszList);
+
+    RegCloseKey(hkey);
 }
 
 
@@ -635,14 +940,62 @@ static void FillList(HWND hCb, char *pszLatest, BOOL fShowDefault)
  */
 static BOOL ConfirmDialog(HWND hWndOwner, UINT PromptId, UINT TitleId)
 {
-  WCHAR Prompt[256];
-  WCHAR Title[256];
+    WCHAR Prompt[256];
+    WCHAR Title[256];
 
-  LoadStringW(shell32_hInstance, PromptId, Prompt, sizeof(Prompt) / sizeof(WCHAR));
-  LoadStringW(shell32_hInstance, TitleId, Title, sizeof(Title) / sizeof(WCHAR));
-  return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO|MB_ICONQUESTION) == IDYES;
+    LoadStringW(shell32_hInstance, PromptId, Prompt, _countof(Prompt));
+    LoadStringW(shell32_hInstance, TitleId, Title, _countof(Title));
+    return MessageBoxW(hWndOwner, Prompt, Title, MB_YESNO | MB_ICONQUESTION) == IDYES;
 }
 
+typedef HRESULT (WINAPI *tShellDimScreen)(IUnknown** Unknown, HWND* hWindow);
+
+BOOL
+CallShellDimScreen(IUnknown** pUnknown, HWND* hWindow)
+{
+    static tShellDimScreen ShellDimScreen;
+    static BOOL Initialized = FALSE;
+    if (!Initialized)
+    {
+        HMODULE mod = LoadLibraryW(L"msgina.dll");
+        ShellDimScreen = (tShellDimScreen)GetProcAddress(mod, (LPCSTR)16);
+        Initialized = TRUE;
+    }
+
+    HRESULT hr = E_FAIL;
+    if (ShellDimScreen)
+        hr = ShellDimScreen(pUnknown, hWindow);
+    return SUCCEEDED(hr);
+}
+
+
+/* Used to get the shutdown privilege */
+static BOOL
+EnablePrivilege(LPCWSTR lpszPrivilegeName, BOOL bEnablePrivilege)
+{
+    BOOL   Success;
+    HANDLE hToken;
+    TOKEN_PRIVILEGES tp;
+
+    Success = OpenProcessToken(GetCurrentProcess(),
+                               TOKEN_ADJUST_PRIVILEGES,
+                               &hToken);
+    if (!Success) return Success;
+
+    Success = LookupPrivilegeValueW(NULL,
+                                    lpszPrivilegeName,
+                                    &tp.Privileges[0].Luid);
+    if (!Success) goto Quit;
+
+    tp.PrivilegeCount = 1;
+    tp.Privileges[0].Attributes = (bEnablePrivilege ? SE_PRIVILEGE_ENABLED : 0);
+
+    Success = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
+
+Quit:
+    CloseHandle(hToken);
+    return Success;
+}
 
 /*************************************************************************
  * RestartDialogEx                [SHELL32.730]
@@ -652,27 +1005,66 @@ int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, D
 {
     TRACE("(%p)\n", hWndOwner);
 
+    CComPtr<IUnknown> fadeHandler;
+    HWND parent;
+
+    if (!CallShellDimScreen(&fadeHandler, &parent))
+        parent = hWndOwner;
+
     /* FIXME: use lpwstrReason */
-    if (ConfirmDialog(hWndOwner, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
+    if (ConfirmDialog(parent, IDS_RESTART_PROMPT, IDS_RESTART_TITLE))
     {
-        HANDLE hToken;
-        TOKEN_PRIVILEGES npr;
-
-        /* enable the shutdown privilege for the current process */
-        if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
-        {
-            LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
-            npr.PrivilegeCount = 1;
-            npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
-            AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
-            CloseHandle(hToken);
-        }
+        EnablePrivilege(L"SeShutdownPrivilege", TRUE);
         ExitWindowsEx(EWX_REBOOT, uReason);
+        EnablePrivilege(L"SeShutdownPrivilege", FALSE);
     }
 
     return 0;
 }
 
+/*************************************************************************
+ * LogOffDialogProc
+ *
+ * NOTES: Used to make the Log Off dialog work
+ */
+INT_PTR CALLBACK LogOffDialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
+{
+    switch (uMsg)
+    {
+        case WM_INITDIALOG:
+            return TRUE;
+
+        case WM_CLOSE:
+            EndDialog(hwnd, IDCANCEL);
+            break;
+
+#if 0
+        case WM_ACTIVATE:
+        {
+            if (LOWORD(wParam) == WA_INACTIVE)
+                EndDialog(hwnd, 0);
+            return FALSE;
+        }
+#endif
+
+        case WM_COMMAND:
+            switch (LOWORD(wParam))
+            {
+                case IDOK:
+                    ExitWindowsEx(EWX_LOGOFF, 0);
+                    break;
+
+                case IDCANCEL:
+                    EndDialog(hwnd, IDCANCEL);
+                    break;
+            }
+            break;
+
+        default:
+            break;
+    }
+    return FALSE;
+}
 
 /*************************************************************************
  * LogoffWindowsDialog  [SHELL32.54]
@@ -680,13 +1072,16 @@ int WINAPI RestartDialogEx(HWND hWndOwner, LPCWSTR lpwstrReason, DWORD uFlags, D
 
 EXTERN_C int WINAPI LogoffWindowsDialog(HWND hWndOwner)
 {
-    if (ConfirmDialog(hWndOwner, IDS_LOGOFF_PROMPT, IDS_LOGOFF_TITLE))
-        ExitWindowsEx(EWX_LOGOFF, 0);
+    CComPtr<IUnknown> fadeHandler;
+    HWND parent;
+
+    if (!CallShellDimScreen(&fadeHandler, &parent))
+        parent = hWndOwner;
 
+    DialogBoxW(shell32_hInstance, MAKEINTRESOURCEW(IDD_LOG_OFF), parent, LogOffDialogProc);
     return 0;
 }
 
-
 /*************************************************************************
  * RestartDialog                [SHELL32.59]
  */
@@ -696,33 +1091,12 @@ int WINAPI RestartDialog(HWND hWndOwner, LPCWSTR lpstrReason, DWORD uFlags)
     return RestartDialogEx(hWndOwner, lpstrReason, uFlags, 0);
 }
 
- /*************************************************************************
- * Used to get the shutdown privilege
- */
-VOID ExitWindows_GetShutdownPrivilege(VOID)
-{
-    HANDLE hToken;
-    TOKEN_PRIVILEGES npr;
-
-    /* enable shut down privilege for current process */
-    if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
-    {
-        LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr.Privileges[0].Luid);
-
-        npr.PrivilegeCount = 1;
-        npr.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
-        AdjustTokenPrivileges(hToken, FALSE, &npr, 0, 0, 0);
-
-        CloseHandle(hToken);
-    }
-}
-
 /*************************************************************************
  * ExitWindowsDialog_backup
  *
  * NOTES
- *     used as a backup solution to shutdown the OS in case msgina.dll somehow
- *     cannot be found.
+ *     Used as a backup solution to shutdown the OS in case msgina.dll
+ *     somehow cannot be found.
  */
 VOID ExitWindowsDialog_backup(HWND hWndOwner)
 {
@@ -730,8 +1104,9 @@ VOID ExitWindowsDialog_backup(HWND hWndOwner)
 
     if (ConfirmDialog(hWndOwner, IDS_SHUTDOWN_PROMPT, IDS_SHUTDOWN_TITLE))
     {
-        ExitWindows_GetShutdownPrivilege();
+        EnablePrivilege(L"SeShutdownPrivilege", TRUE);
         ExitWindowsEx(EWX_SHUTDOWN, 0);
+        EnablePrivilege(L"SeShutdownPrivilege", FALSE);
     }
 }
 
@@ -742,34 +1117,39 @@ VOID ExitWindowsDialog_backup(HWND hWndOwner)
  *     exported by ordinal
  */
 /*
- * TODO: 
+ * TODO:
  * - Implement the ability to show either the Welcome Screen or the classic dialog boxes based upon the
  *   registry value: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType.
  */
 void WINAPI ExitWindowsDialog(HWND hWndOwner)
 {
     typedef DWORD (WINAPI *ShellShFunc)(HWND hParent, WCHAR *Username, BOOL bHideLogoff);
-    HINSTANCE msginaDll = LoadLibraryA("msgina.dll");
+    HINSTANCE msginaDll = LoadLibraryW(L"msgina.dll");
 
     TRACE("(%p)\n", hWndOwner);
 
+    CComPtr<IUnknown> fadeHandler;
+    HWND parent;
+    if (!CallShellDimScreen(&fadeHandler, &parent))
+        parent = hWndOwner;
+
     /* If the DLL cannot be found for any reason, then it simply uses a
        dialog box to ask if the user wants to shut down the computer. */
-    if(!msginaDll)
+    if (!msginaDll)
     {
         TRACE("Unable to load msgina.dll.\n");
-        ExitWindowsDialog_backup(hWndOwner);
+        ExitWindowsDialog_backup(parent);
         return;
     }
 
-    ShellShFunc pShellShutdownDialog = (ShellShFunc) GetProcAddress(msginaDll, "ShellShutdownDialog");
+    ShellShFunc pShellShutdownDialog = (ShellShFunc)GetProcAddress(msginaDll, "ShellShutdownDialog");
 
-    if(pShellShutdownDialog)
+    if (pShellShutdownDialog)
     {
         /* Actually call the function */
-        DWORD returnValue = pShellShutdownDialog(hWndOwner, NULL, FALSE);
+        DWORD returnValue = pShellShutdownDialog(parent, NULL, FALSE);
 
-        switch(returnValue)
+        switch (returnValue)
         {
         case 0x01: /* Log off user */
         {
@@ -778,8 +1158,9 @@ void WINAPI ExitWindowsDialog(HWND hWndOwner)
         }
         case 0x02: /* Shut down */
         {
-            ExitWindows_GetShutdownPrivilege();
+            EnablePrivilege(L"SeShutdownPrivilege", TRUE);
             ExitWindowsEx(EWX_SHUTDOWN, 0);
+            EnablePrivilege(L"SeShutdownPrivilege", FALSE);
             break;
         }
         case 0x03: /* Install Updates/Shutdown (?) */
@@ -788,25 +1169,28 @@ void WINAPI ExitWindowsDialog(HWND hWndOwner)
         }
         case 0x04: /* Reboot */
         {
-            ExitWindows_GetShutdownPrivilege();
+            EnablePrivilege(L"SeShutdownPrivilege", TRUE);
             ExitWindowsEx(EWX_REBOOT, 0);
+            EnablePrivilege(L"SeShutdownPrivilege", FALSE);
             break;
         }
         case 0x10: /* Sleep */
         {
-            if(IsPwrSuspendAllowed())
+            if (IsPwrSuspendAllowed())
             {
-                ExitWindows_GetShutdownPrivilege();
+                EnablePrivilege(L"SeShutdownPrivilege", TRUE);
                 SetSuspendState(FALSE, FALSE, FALSE);
+                EnablePrivilege(L"SeShutdownPrivilege", FALSE);
             }
             break;
         }
         case 0x40: /* Hibernate */
         {
-            if(IsPwrHibernateAllowed())
+            if (IsPwrHibernateAllowed())
             {
-                ExitWindows_GetShutdownPrivilege();
+                EnablePrivilege(L"SeShutdownPrivilege", TRUE);
                 SetSuspendState(TRUE, FALSE, TRUE);
+                EnablePrivilege(L"SeShutdownPrivilege", FALSE);
             }
             break;
         }
@@ -819,7 +1203,8 @@ void WINAPI ExitWindowsDialog(HWND hWndOwner)
     {
         /* If the function cannot be found, then revert to using the backup solution */
         TRACE("Unable to find the 'ShellShutdownDialog' function");
-        FreeLibrary(msginaDll);
-        ExitWindowsDialog_backup(hWndOwner);
+        ExitWindowsDialog_backup(parent);
     }
+
+    FreeLibrary(msginaDll);
 }