4 * Copyright 2000 Juergen Schmied
5 * Copyright 2018 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 LPCWSTR lpstrDirectory
;
30 LPCWSTR lpstrDescription
;
34 typedef BOOL (WINAPI
* LPFNOFN
) (OPENFILENAMEW
*);
36 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
37 static INT_PTR CALLBACK
RunDlgProc(HWND
, UINT
, WPARAM
, LPARAM
);
38 static void FillList(HWND
, LPWSTR
, UINT
, BOOL
);
41 /*************************************************************************
42 * PickIconDlg [SHELL32.62]
50 WCHAR szPath
[MAX_PATH
];
51 WCHAR szExpandedPath
[MAX_PATH
];
55 } PICK_ICON_CONTEXT
, *PPICK_ICON_CONTEXT
;
57 BOOL CALLBACK
EnumPickIconResourceProc(HMODULE hModule
,
62 PPICK_ICON_CONTEXT pIconContext
= PPICK_ICON_CONTEXT(lParam
);
63 HWND hDlgCtrl
= pIconContext
->hDlgCtrl
;
65 if (IS_INTRESOURCE(lpszName
))
66 lParam
= LOWORD(lpszName
);
70 SendMessageW(hDlgCtrl
, LB_ADDSTRING
, 0, lParam
);
76 DestroyIconList(HWND hDlgCtrl
, PPICK_ICON_CONTEXT pIconContext
)
81 count
= SendMessageW(hDlgCtrl
, LB_GETCOUNT
, 0, 0);
85 for(index
= 0; index
< count
; index
++)
87 DestroyIcon(pIconContext
->phIcons
[index
]);
88 pIconContext
->phIcons
[index
] = NULL
;
93 DoLoadIcons(HWND hwndDlg
, PICK_ICON_CONTEXT
*pIconContext
, LPCWSTR pszFile
)
95 // Destroy previous icons
96 DestroyIconList(pIconContext
->hDlgCtrl
, pIconContext
);
97 SendMessageW(pIconContext
->hDlgCtrl
, LB_RESETCONTENT
, 0, 0);
98 delete[] pIconContext
->phIcons
;
101 StringCchCopyW(pIconContext
->szPath
, _countof(pIconContext
->szPath
), pszFile
);
102 ExpandEnvironmentStringsW(pszFile
, pIconContext
->szExpandedPath
, _countof(pIconContext
->szExpandedPath
));
104 // Load the module if possible
105 HMODULE hLibrary
= LoadLibraryExW(pIconContext
->szExpandedPath
, NULL
, LOAD_LIBRARY_AS_DATAFILE
);
106 if (pIconContext
->hLibrary
)
107 FreeLibrary(pIconContext
->hLibrary
);
108 pIconContext
->hLibrary
= hLibrary
;
110 if (pIconContext
->hLibrary
)
112 // Load the icons from the module
113 pIconContext
->nIcons
= ExtractIconExW(pIconContext
->szExpandedPath
, -1, NULL
, NULL
, 0);
114 pIconContext
->phIcons
= new HICON
[pIconContext
->nIcons
];
116 if (ExtractIconExW(pIconContext
->szExpandedPath
, 0, pIconContext
->phIcons
, NULL
, pIconContext
->nIcons
))
118 EnumResourceNamesW(pIconContext
->hLibrary
, RT_GROUP_ICON
, EnumPickIconResourceProc
, (LPARAM
)pIconContext
);
122 pIconContext
->nIcons
= 0;
128 pIconContext
->nIcons
= 1;
129 pIconContext
->phIcons
= new HICON
[1];
131 if (ExtractIconExW(pIconContext
->szExpandedPath
, 0, pIconContext
->phIcons
, NULL
, pIconContext
->nIcons
))
133 SendMessageW(pIconContext
->hDlgCtrl
, LB_ADDSTRING
, 0, 0);
137 pIconContext
->nIcons
= 0;
142 SetDlgItemTextW(hwndDlg
, IDC_EDIT_PATH
, pIconContext
->szPath
);
144 if (pIconContext
->nIcons
== 0)
146 delete[] pIconContext
->phIcons
;
147 pIconContext
->phIcons
= NULL
;
150 return pIconContext
->nIcons
> 0;
153 static const LPCWSTR s_pszDefaultPath
= L
"%SystemRoot%\\system32\\shell32.dll";
155 static void NoIconsInFile(HWND hwndDlg
, PICK_ICON_CONTEXT
*pIconContext
)
157 // Show an error message
158 CStringW strText
, strTitle(MAKEINTRESOURCEW(IDS_PICK_ICON_TITLE
));
159 strText
.Format(IDS_NO_ICONS
, pIconContext
->szPath
);
160 MessageBoxW(hwndDlg
, strText
, strTitle
, MB_ICONWARNING
);
162 // Load the default icons
163 DoLoadIcons(hwndDlg
, pIconContext
, s_pszDefaultPath
);
167 #define CX_ICON GetSystemMetrics(SM_CXICON)
168 #define CY_ICON GetSystemMetrics(SM_CYICON)
171 #define CX_ITEM (CX_ICON + 4)
172 #define CY_ITEM (CY_ICON + 12)
174 INT_PTR CALLBACK
PickIconProc(
180 LPMEASUREITEMSTRUCT lpmis
;
181 LPDRAWITEMSTRUCT lpdis
;
184 WCHAR szText
[MAX_PATH
], szFilter
[100];
188 PPICK_ICON_CONTEXT pIconContext
= (PPICK_ICON_CONTEXT
)GetWindowLongPtr(hwndDlg
, DWLP_USER
);
194 pIconContext
= (PPICK_ICON_CONTEXT
)lParam
;
195 SetWindowLongPtr(hwndDlg
, DWLP_USER
, (LONG_PTR
)pIconContext
);
196 pIconContext
->hDlgCtrl
= GetDlgItem(hwndDlg
, IDC_PICKICON_LIST
);
198 SendMessageW(pIconContext
->hDlgCtrl
, LB_SETCOLUMNWIDTH
, CX_ITEM
, 0);
201 if (!DoLoadIcons(hwndDlg
, pIconContext
, pIconContext
->szPath
))
202 NoIconsInFile(hwndDlg
, pIconContext
);
205 count
= SendMessageW(pIconContext
->hDlgCtrl
, LB_GETCOUNT
, 0, 0);
208 if (pIconContext
->Index
< 0)
210 // A negative value will be interpreted as a negated resource ID.
211 LPARAM lParam
= -pIconContext
->Index
;
212 pIconContext
->Index
= (INT
)SendMessageW(pIconContext
->hDlgCtrl
, LB_FINDSTRINGEXACT
, -1, lParam
);
215 if (pIconContext
->Index
< 0 || count
<= pIconContext
->Index
)
216 pIconContext
->Index
= 0;
218 SendMessageW(pIconContext
->hDlgCtrl
, LB_SETCURSEL
, pIconContext
->Index
, 0);
219 SendMessageW(pIconContext
->hDlgCtrl
, LB_SETTOPINDEX
, pIconContext
->Index
, 0);
225 switch(LOWORD(wParam
))
228 index
= SendMessageW(pIconContext
->hDlgCtrl
, LB_GETCURSEL
, 0, 0);
229 pIconContext
->Index
= index
;
230 GetDlgItemTextW(hwndDlg
, IDC_EDIT_PATH
, pIconContext
->szPath
, _countof(pIconContext
->szPath
));
231 ExpandEnvironmentStringsW(pIconContext
->szPath
, pIconContext
->szExpandedPath
, _countof(pIconContext
->szExpandedPath
));
232 DestroyIconList(pIconContext
->hDlgCtrl
, pIconContext
);
233 delete[] pIconContext
->phIcons
;
234 EndDialog(hwndDlg
, 1);
238 DestroyIconList(pIconContext
->hDlgCtrl
, pIconContext
);
239 delete[] pIconContext
->phIcons
;
240 EndDialog(hwndDlg
, 0);
243 case IDC_PICKICON_LIST
:
244 switch (HIWORD(wParam
))
247 InvalidateRect((HWND
)lParam
, NULL
, TRUE
);
251 SendMessage(hwndDlg
, WM_COMMAND
, MAKEWPARAM(IDOK
, 0), 0);
256 case IDC_BUTTON_PATH
:
258 // Choose the module path
261 ZeroMemory(&ofn
, sizeof(ofn
));
262 ofn
.lStructSize
= sizeof(ofn
);
263 ofn
.hwndOwner
= hwndDlg
;
264 ofn
.lpstrFile
= szText
;
265 ofn
.nMaxFile
= _countof(szText
);
266 strTitle
.LoadString(IDS_PICK_ICON_TITLE
);
267 ofn
.lpstrTitle
= strTitle
;
268 LoadStringW(shell32_hInstance
, IDS_PICK_ICON_FILTER
, szFilter
, _countof(szFilter
));
269 ofn
.lpstrFilter
= szFilter
;
270 if (!GetOpenFileNameW(&ofn
))
274 if (!DoLoadIcons(hwndDlg
, pIconContext
, szText
))
275 NoIconsInFile(hwndDlg
, pIconContext
);
278 SendMessageW(pIconContext
->hDlgCtrl
, LB_SETCURSEL
, 0, 0);
288 lpmis
= (LPMEASUREITEMSTRUCT
)lParam
;
289 lpmis
->itemHeight
= CY_ITEM
;
294 lpdis
= (LPDRAWITEMSTRUCT
)lParam
;
295 if (lpdis
->itemID
== (UINT
)-1)
299 switch (lpdis
->itemAction
)
304 index
= SendMessageW(pIconContext
->hDlgCtrl
, LB_GETCURSEL
, 0, 0);
305 hIcon
= pIconContext
->phIcons
[lpdis
->itemID
];
307 if (lpdis
->itemID
== (UINT
)index
)
308 FillRect(lpdis
->hDC
, &lpdis
->rcItem
, (HBRUSH
)(COLOR_HIGHLIGHT
+ 1));
310 FillRect(lpdis
->hDC
, &lpdis
->rcItem
, (HBRUSH
)(COLOR_WINDOW
+ 1));
313 INT x
= lpdis
->rcItem
.left
+ (CX_ITEM
- CX_ICON
) / 2;
314 INT y
= lpdis
->rcItem
.top
+ (CY_ITEM
- CY_ICON
) / 2;
316 DrawIconEx(lpdis
->hDC
, x
, y
, hIcon
, 0, 0, 0, NULL
, DI_NORMAL
);
327 BOOL WINAPI
PickIconDlg(
335 // Initialize the dialog
336 PICK_ICON_CONTEXT IconContext
= { NULL
};
337 IconContext
.Index
= *lpdwIconIndex
;
338 StringCchCopyW(IconContext
.szPath
, _countof(IconContext
.szPath
), lpstrFile
);
339 ExpandEnvironmentStringsW(lpstrFile
, IconContext
.szExpandedPath
, _countof(IconContext
.szExpandedPath
));
341 if (!IconContext
.szExpandedPath
[0] ||
342 GetFileAttributesW(IconContext
.szExpandedPath
) == INVALID_FILE_ATTRIBUTES
)
344 if (IconContext
.szExpandedPath
[0])
347 CStringW strText
, strTitle(MAKEINTRESOURCEW(IDS_PICK_ICON_TITLE
));
348 strText
.Format(IDS_FILE_NOT_FOUND
, lpstrFile
);
349 MessageBoxW(hWndOwner
, strText
, strTitle
, MB_ICONWARNING
);
352 // Set the default value
353 StringCchCopyW(IconContext
.szPath
, _countof(IconContext
.szPath
), s_pszDefaultPath
);
354 ExpandEnvironmentStringsW(s_pszDefaultPath
, IconContext
.szPath
, _countof(IconContext
.szPath
));
358 res
= DialogBoxParamW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_PICK_ICON
), hWndOwner
, PickIconProc
, (LPARAM
)&IconContext
);
361 // Store the selected icon
362 StringCchCopyW(lpstrFile
, nMaxFile
, IconContext
.szExpandedPath
);
363 *lpdwIconIndex
= IconContext
.Index
;
366 if (IconContext
.hLibrary
)
367 FreeLibrary(IconContext
.hLibrary
);
371 /*************************************************************************
372 * RunFileDlg [internal]
374 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
376 void WINAPI
RunFileDlg(
379 LPCWSTR lpstrDirectory
,
381 LPCWSTR lpstrDescription
,
386 RUNFILEDLGPARAMS rfdp
;
387 rfdp
.hwndOwner
= hWndOwner
;
389 rfdp
.lpstrDirectory
= lpstrDirectory
;
390 rfdp
.lpstrTitle
= lpstrTitle
;
391 rfdp
.lpstrDescription
= lpstrDescription
;
392 rfdp
.uFlags
= uFlags
;
394 DialogBoxParamW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_RUN
), hWndOwner
, RunDlgProc
, (LPARAM
)&rfdp
);
398 /* find the directory that contains the file being run */
399 static LPWSTR
RunDlg_GetParentDir(LPCWSTR cmdline
)
402 WCHAR
*dest
, *result
, *result_end
=NULL
;
403 static const WCHAR dotexeW
[] = L
".exe";
405 result
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
)*(strlenW(cmdline
)+5));
409 TRACE("HeapAlloc couldn't allocate %d bytes\n", sizeof(WCHAR
)*(strlenW(cmdline
)+5));
419 while (*src
&& *src
!= '"')
432 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesW(result
))
434 strcatW(dest
, dotexeW
);
435 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesW(result
))
438 else if (*src
== '\\')
451 HeapFree(GetProcessHeap(), 0, result
);
456 static void EnableOkButtonFromEditContents(HWND hwnd
)
460 HWND Edit
= GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
);
461 Length
= GetWindowTextLengthW(Edit
);
464 PWCHAR psz
= (PWCHAR
)HeapAlloc(GetProcessHeap(), 0, (Length
+ 1) * sizeof(WCHAR
));
467 GetWindowTextW(Edit
, psz
, Length
+ 1);
468 for (n
= 0; n
< Length
&& !Enable
; ++n
)
469 Enable
= psz
[n
] != ' ';
470 HeapFree(GetProcessHeap(), 0, psz
);
475 EnableWindow(GetDlgItem(hwnd
, IDOK
), Enable
);
478 /* Dialog procedure for RunFileDlg */
479 static INT_PTR CALLBACK
RunDlgProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
481 RUNFILEDLGPARAMS
*prfdp
= (RUNFILEDLGPARAMS
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
486 prfdp
= (RUNFILEDLGPARAMS
*)lParam
;
487 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LONG_PTR
)prfdp
);
489 if (prfdp
->lpstrTitle
)
490 SetWindowTextW(hwnd
, prfdp
->lpstrTitle
);
491 if (prfdp
->lpstrDescription
)
492 SetWindowTextW(GetDlgItem(hwnd
, IDC_RUNDLG_DESCRIPTION
), prfdp
->lpstrDescription
);
493 if (prfdp
->uFlags
& RFF_NOBROWSE
)
495 HWND browse
= GetDlgItem(hwnd
, IDC_RUNDLG_BROWSE
);
496 ShowWindow(browse
, SW_HIDE
);
497 EnableWindow(browse
, FALSE
);
499 if (prfdp
->uFlags
& RFF_NOLABEL
)
500 ShowWindow(GetDlgItem(hwnd
, IDC_RUNDLG_LABEL
), SW_HIDE
);
501 if (prfdp
->uFlags
& RFF_NOSEPARATEMEM
)
503 FIXME("RFF_NOSEPARATEMEM not supported\n");
506 /* Use the default Shell Run icon if no one is specified */
507 if (prfdp
->hIcon
== NULL
)
508 prfdp
->hIcon
= LoadIconW(shell32_hInstance
, MAKEINTRESOURCEW(IDI_SHELL_RUN
));
510 * NOTE: Starting Windows Vista, the "Run File" dialog gets a
511 * title icon that remains the same as the default one, even if
512 * the user specifies a custom icon.
513 * Since we currently imitate Windows 2003, therefore do not show
516 // SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
517 // SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
518 SendMessageW(GetDlgItem(hwnd
, IDC_RUNDLG_ICON
), STM_SETICON
, (WPARAM
)prfdp
->hIcon
, 0);
520 FillList(GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
), NULL
, 0, (prfdp
->uFlags
& RFF_NODEFAULT
) == 0);
521 EnableOkButtonFromEditContents(hwnd
);
522 SetFocus(GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
));
526 switch (LOWORD(wParam
))
531 HWND htxt
= GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
);
533 WCHAR
*psz
, *parent
= NULL
;
536 ic
= GetWindowTextLengthW(htxt
);
539 EndDialog(hwnd
, IDCANCEL
);
544 * Allocate a new MRU entry, we need to add two characters
545 * for the terminating "\\1" part, then the NULL character.
547 psz
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, (ic
+ 2 + 1)*sizeof(WCHAR
));
550 EndDialog(hwnd
, IDCANCEL
);
554 GetWindowTextW(htxt
, psz
, ic
+ 1);
555 StrTrimW(psz
, L
" \t");
558 * The precedence is the following: first the user-given
559 * current directory is used; if there is none, a current
560 * directory is computed if the RFF_CALCDIRECTORY is set,
561 * otherwise no current directory is defined.
564 if (prfdp
->lpstrDirectory
)
565 pszStartDir
= prfdp
->lpstrDirectory
;
566 else if (prfdp
->uFlags
& RFF_CALCDIRECTORY
)
567 pszStartDir
= parent
= RunDlg_GetParentDir(psz
);
571 /* Hide the dialog for now on, we will show it up in case of retry */
572 ShowWindow(hwnd
, SW_HIDE
);
575 * As shown by manual tests on Windows, modifying the contents
576 * of the notification structure will not modify what the
577 * Run-Dialog will use for the nShow parameter. However the
578 * lpFile and lpDirectory pointers are set to the buffers used
579 * by the Run-Dialog, as a consequence they can be modified by
580 * the notification receiver, as long as it respects the lengths
581 * of the buffers (to avoid buffer overflows).
583 nmrfd
.hdr
.code
= RFN_VALIDATE
;
584 nmrfd
.hdr
.hwndFrom
= hwnd
;
585 nmrfd
.hdr
.idFrom
= 0;
587 nmrfd
.lpDirectory
= pszStartDir
;
588 nmrfd
.nShow
= SW_SHOWNORMAL
;
590 lRet
= SendMessageW(prfdp
->hwndOwner
, WM_NOTIFY
, 0, (LPARAM
)&nmrfd
.hdr
);
595 EndDialog(hwnd
, IDCANCEL
);
599 if (SUCCEEDED(ShellExecCmdLine(hwnd
, psz
, pszStartDir
, SW_SHOWNORMAL
, NULL
,
602 /* Call again GetWindowText in case the contents of the edit box has changed? */
603 GetWindowTextW(htxt
, psz
, ic
+ 1);
604 FillList(htxt
, psz
, ic
+ 2 + 1, FALSE
);
605 EndDialog(hwnd
, IDOK
);
612 SendMessageW(htxt
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1));
613 /* Show back the dialog */
614 ShowWindow(hwnd
, SW_SHOW
);
618 HeapFree(GetProcessHeap(), 0, parent
);
619 HeapFree(GetProcessHeap(), 0, psz
);
624 EndDialog(hwnd
, IDCANCEL
);
627 case IDC_RUNDLG_BROWSE
:
629 HMODULE hComdlg
= NULL
;
630 LPFNOFN ofnProc
= NULL
;
631 WCHAR szFName
[1024] = {0};
632 WCHAR filter
[MAX_PATH
], szCaption
[MAX_PATH
];
635 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_FILTER
, filter
, _countof(filter
));
636 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_CAPTION
, szCaption
, _countof(szCaption
));
638 ZeroMemory(&ofn
, sizeof(ofn
));
639 ofn
.lStructSize
= sizeof(ofn
);
640 ofn
.hwndOwner
= hwnd
;
641 ofn
.lpstrFilter
= filter
;
642 ofn
.lpstrFile
= szFName
;
643 ofn
.nMaxFile
= _countof(szFName
) - 1;
644 ofn
.lpstrTitle
= szCaption
;
645 ofn
.Flags
= OFN_ENABLESIZING
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
;
646 ofn
.lpstrInitialDir
= prfdp
->lpstrDirectory
;
648 if (NULL
== (hComdlg
= LoadLibraryExW(L
"comdlg32", NULL
, 0)) ||
649 NULL
== (ofnProc
= (LPFNOFN
)GetProcAddress(hComdlg
, "GetOpenFileNameW")))
651 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg
, ofnProc
);
652 ShellMessageBoxW(shell32_hInstance
, hwnd
, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR
), NULL
, MB_OK
| MB_ICONERROR
);
658 SetFocus(GetDlgItem(hwnd
, IDOK
));
659 SetWindowTextW(GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
), szFName
);
660 SendMessageW(GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
), CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
661 EnableOkButtonFromEditContents(hwnd
);
662 SetFocus(GetDlgItem(hwnd
, IDOK
));
665 FreeLibrary(hComdlg
);
669 case IDC_RUNDLG_EDITPATH
:
671 if (HIWORD(wParam
) == CBN_EDITCHANGE
)
673 EnableOkButtonFromEditContents(hwnd
);
684 * This function grabs the MRU list from the registry and fills the combo-list
685 * for the "Run" dialog above. fShowDefault is ignored if pszLatest != NULL.
687 // FIXME: Part of this code should be part of some MRUList API,
688 // that is scattered amongst shell32, comctl32 (?!) and comdlg32.
689 static void FillList(HWND hCb
, LPWSTR pszLatest
, UINT cchStr
, BOOL fShowDefault
)
692 WCHAR
*pszList
= NULL
, *pszCmd
= NULL
, *pszTmp
= NULL
, cMatch
= 0, cMax
= 0x60;
693 WCHAR szIndex
[2] = L
"-";
695 DWORD dwType
, icList
= 0, icCmd
= 0;
700 * Retrieve the string length of pszLatest and check whether its buffer size
701 * (cchStr in number of characters) is large enough to add the terminating "\\1"
702 * (and the NULL character).
706 cchLatest
= wcslen(pszLatest
);
707 if (cchStr
< cchLatest
+ 2 + 1)
709 TRACE("pszLatest buffer is not large enough (%d) to hold the MRU terminator.\n", cchStr
);
718 SendMessageW(hCb
, CB_RESETCONTENT
, 0, 0);
720 lRet
= RegCreateKeyExW(HKEY_CURRENT_USER
,
721 L
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
722 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hkey
, NULL
);
723 if (lRet
!= ERROR_SUCCESS
)
725 TRACE("Unable to open or create the RunMRU key, error %d\n", GetLastError());
729 lRet
= RegQueryValueExW(hkey
, L
"MRUList", NULL
, &dwType
, NULL
, &icList
);
730 if (lRet
== ERROR_SUCCESS
&& dwType
== REG_SZ
&& icList
> sizeof(WCHAR
))
732 pszList
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, icList
);
735 TRACE("HeapAlloc failed to allocate %d bytes\n", icList
);
740 lRet
= RegQueryValueExW(hkey
, L
"MRUList", NULL
, NULL
, (LPBYTE
)pszList
, &icList
);
741 if (lRet
!= ERROR_SUCCESS
)
743 TRACE("Unable to grab MRUList, error %d\n", GetLastError());
750 icList
= sizeof(WCHAR
);
751 pszList
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, icList
);
754 TRACE("HeapAlloc failed to allocate %d bytes\n", icList
);
761 /* Convert the number of bytes from MRUList into number of characters (== number of indices) */
762 icList
/= sizeof(WCHAR
);
764 for (Nix
= 0; Nix
< icList
- 1; Nix
++)
766 if (pszList
[Nix
] > cMax
)
769 szIndex
[0] = pszList
[Nix
];
771 lRet
= RegQueryValueExW(hkey
, szIndex
, NULL
, &dwType
, NULL
, &icCmd
);
772 if (lRet
!= ERROR_SUCCESS
|| dwType
!= REG_SZ
)
774 TRACE("Unable to grab size of index, error %d\n", GetLastError());
780 pszTmp
= (WCHAR
*)HeapReAlloc(GetProcessHeap(), 0, pszCmd
, icCmd
);
783 TRACE("HeapReAlloc failed to reallocate %d bytes\n", icCmd
);
790 pszCmd
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, icCmd
);
793 TRACE("HeapAlloc failed to allocate %d bytes\n", icCmd
);
798 lRet
= RegQueryValueExW(hkey
, szIndex
, NULL
, NULL
, (LPBYTE
)pszCmd
, &icCmd
);
799 if (lRet
!= ERROR_SUCCESS
)
801 TRACE("Unable to grab index, error %d\n", GetLastError());
806 * Generally the command string will end up with "\\1".
807 * Find the last backslash in the string and NULL-terminate.
808 * Windows does not seem to check for what comes next, so that
809 * a command of the form:
810 * c:\\my_dir\\myfile.exe
811 * will be cut just after "my_dir", whereas a command of the form:
812 * c:\\my_dir\\myfile.exe\\1
813 * will be cut just after "myfile.exe".
815 pszTmp
= wcsrchr(pszCmd
, L
'\\');
820 * In the following we try to add pszLatest to the MRU list.
821 * We suppose that our caller has already correctly allocated
822 * the string with enough space for us to append a "\\1".
824 * FIXME: TODO! (At the moment we don't append it!)
829 if (wcsicmp(pszCmd
, pszLatest
) == 0)
831 SendMessageW(hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszCmd
);
832 SetWindowTextW(hCb
, pszCmd
);
833 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
835 cMatch
= pszList
[Nix
];
836 memmove(&pszList
[1], pszList
, Nix
* sizeof(WCHAR
));
842 if (icList
- 1 != 26 || icList
- 2 != Nix
|| cMatch
|| pszLatest
== NULL
)
844 SendMessageW(hCb
, CB_ADDSTRING
, 0, (LPARAM
)pszCmd
);
845 if (!Nix
&& fShowDefault
)
847 SetWindowTextW(hCb
, pszCmd
);
848 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
853 SendMessageW(hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
);
854 SetWindowTextW(hCb
, pszLatest
);
855 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
857 cMatch
= pszList
[Nix
];
858 memmove(&pszList
[1], pszList
, Nix
* sizeof(WCHAR
));
862 wcscpy(&pszLatest
[cchLatest
], L
"\\1");
863 RegSetValueExW(hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, (cchLatest
+ 2 + 1) * sizeof(WCHAR
));
864 pszLatest
[cchLatest
] = L
'\0';
868 if (!cMatch
&& pszLatest
!= NULL
)
870 SendMessageW(hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
);
871 SetWindowTextW(hCb
, pszLatest
);
872 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1));
878 pszTmp
= (WCHAR
*)HeapReAlloc(GetProcessHeap(), 0, pszList
, (++icList
) * sizeof(WCHAR
));
881 TRACE("HeapReAlloc failed to reallocate enough bytes\n");
888 pszList
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, (++icList
) * sizeof(WCHAR
));
891 TRACE("HeapAlloc failed to allocate enough bytes\n");
896 memmove(&pszList
[1], pszList
, (icList
- 1) * sizeof(WCHAR
));
900 wcscpy(&pszLatest
[cchLatest
], L
"\\1");
901 RegSetValueExW(hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, (cchLatest
+ 2 + 1) * sizeof(WCHAR
));
902 pszLatest
[cchLatest
] = L
'\0';
906 RegSetValueExW(hkey
, L
"MRUList", 0, REG_SZ
, (LPBYTE
)pszList
, (wcslen(pszList
) + 1) * sizeof(WCHAR
));
908 HeapFree(GetProcessHeap(), 0, pszCmd
);
909 HeapFree(GetProcessHeap(), 0, pszList
);
915 /*************************************************************************
916 * ConfirmDialog [internal]
918 * Put up a confirm box, return TRUE if the user confirmed
920 static BOOL
ConfirmDialog(HWND hWndOwner
, UINT PromptId
, UINT TitleId
)
925 LoadStringW(shell32_hInstance
, PromptId
, Prompt
, _countof(Prompt
));
926 LoadStringW(shell32_hInstance
, TitleId
, Title
, _countof(Title
));
927 return MessageBoxW(hWndOwner
, Prompt
, Title
, MB_YESNO
| MB_ICONQUESTION
) == IDYES
;
930 typedef HRESULT (WINAPI
*tShellDimScreen
)(IUnknown
** Unknown
, HWND
* hWindow
);
933 CallShellDimScreen(IUnknown
** pUnknown
, HWND
* hWindow
)
935 static tShellDimScreen ShellDimScreen
;
936 static BOOL Initialized
= FALSE
;
939 HMODULE mod
= LoadLibraryW(L
"msgina.dll");
940 ShellDimScreen
= (tShellDimScreen
)GetProcAddress(mod
, (LPCSTR
)16);
946 hr
= ShellDimScreen(pUnknown
, hWindow
);
947 return SUCCEEDED(hr
);
951 /* Used to get the shutdown privilege */
953 EnablePrivilege(LPCWSTR lpszPrivilegeName
, BOOL bEnablePrivilege
)
959 Success
= OpenProcessToken(GetCurrentProcess(),
960 TOKEN_ADJUST_PRIVILEGES
,
962 if (!Success
) return Success
;
964 Success
= LookupPrivilegeValueW(NULL
,
966 &tp
.Privileges
[0].Luid
);
967 if (!Success
) goto Quit
;
969 tp
.PrivilegeCount
= 1;
970 tp
.Privileges
[0].Attributes
= (bEnablePrivilege
? SE_PRIVILEGE_ENABLED
: 0);
972 Success
= AdjustTokenPrivileges(hToken
, FALSE
, &tp
, 0, NULL
, NULL
);
979 /*************************************************************************
980 * RestartDialogEx [SHELL32.730]
983 int WINAPI
RestartDialogEx(HWND hWndOwner
, LPCWSTR lpwstrReason
, DWORD uFlags
, DWORD uReason
)
985 TRACE("(%p)\n", hWndOwner
);
987 CComPtr
<IUnknown
> fadeHandler
;
990 if (!CallShellDimScreen(&fadeHandler
, &parent
))
993 /* FIXME: use lpwstrReason */
994 if (ConfirmDialog(parent
, IDS_RESTART_PROMPT
, IDS_RESTART_TITLE
))
996 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
997 ExitWindowsEx(EWX_REBOOT
, uReason
);
998 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1004 /*************************************************************************
1007 * NOTES: Used to make the Log Off dialog work
1009 INT_PTR CALLBACK
LogOffDialogProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1017 EndDialog(hwnd
, IDCANCEL
);
1023 if (LOWORD(wParam
) == WA_INACTIVE
)
1030 switch (LOWORD(wParam
))
1033 ExitWindowsEx(EWX_LOGOFF
, 0);
1037 EndDialog(hwnd
, IDCANCEL
);
1048 /*************************************************************************
1049 * LogoffWindowsDialog [SHELL32.54]
1052 EXTERN_C
int WINAPI
LogoffWindowsDialog(HWND hWndOwner
)
1054 CComPtr
<IUnknown
> fadeHandler
;
1057 if (!CallShellDimScreen(&fadeHandler
, &parent
))
1060 DialogBoxW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_LOG_OFF
), parent
, LogOffDialogProc
);
1064 /*************************************************************************
1065 * RestartDialog [SHELL32.59]
1068 int WINAPI
RestartDialog(HWND hWndOwner
, LPCWSTR lpstrReason
, DWORD uFlags
)
1070 return RestartDialogEx(hWndOwner
, lpstrReason
, uFlags
, 0);
1073 /*************************************************************************
1074 * ExitWindowsDialog_backup
1077 * Used as a backup solution to shutdown the OS in case msgina.dll
1078 * somehow cannot be found.
1080 VOID
ExitWindowsDialog_backup(HWND hWndOwner
)
1082 TRACE("(%p)\n", hWndOwner
);
1084 if (ConfirmDialog(hWndOwner
, IDS_SHUTDOWN_PROMPT
, IDS_SHUTDOWN_TITLE
))
1086 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1087 ExitWindowsEx(EWX_SHUTDOWN
, 0);
1088 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1092 /*************************************************************************
1093 * ExitWindowsDialog [SHELL32.60]
1096 * exported by ordinal
1100 * - Implement the ability to show either the Welcome Screen or the classic dialog boxes based upon the
1101 * registry value: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType.
1103 void WINAPI
ExitWindowsDialog(HWND hWndOwner
)
1105 typedef DWORD (WINAPI
*ShellShFunc
)(HWND hParent
, WCHAR
*Username
, BOOL bHideLogoff
);
1106 HINSTANCE msginaDll
= LoadLibraryW(L
"msgina.dll");
1108 TRACE("(%p)\n", hWndOwner
);
1110 CComPtr
<IUnknown
> fadeHandler
;
1112 if (!CallShellDimScreen(&fadeHandler
, &parent
))
1115 /* If the DLL cannot be found for any reason, then it simply uses a
1116 dialog box to ask if the user wants to shut down the computer. */
1119 TRACE("Unable to load msgina.dll.\n");
1120 ExitWindowsDialog_backup(parent
);
1124 ShellShFunc pShellShutdownDialog
= (ShellShFunc
)GetProcAddress(msginaDll
, "ShellShutdownDialog");
1126 if (pShellShutdownDialog
)
1128 /* Actually call the function */
1129 DWORD returnValue
= pShellShutdownDialog(parent
, NULL
, FALSE
);
1131 switch (returnValue
)
1133 case 0x01: /* Log off user */
1135 ExitWindowsEx(EWX_LOGOFF
, 0);
1138 case 0x02: /* Shut down */
1140 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1141 ExitWindowsEx(EWX_SHUTDOWN
, 0);
1142 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1145 case 0x03: /* Install Updates/Shutdown (?) */
1149 case 0x04: /* Reboot */
1151 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1152 ExitWindowsEx(EWX_REBOOT
, 0);
1153 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1156 case 0x10: /* Sleep */
1158 if (IsPwrSuspendAllowed())
1160 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1161 SetSuspendState(FALSE
, FALSE
, FALSE
);
1162 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1166 case 0x40: /* Hibernate */
1168 if (IsPwrHibernateAllowed())
1170 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1171 SetSuspendState(TRUE
, FALSE
, TRUE
);
1172 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1176 /* If the option is any other value */
1183 /* If the function cannot be found, then revert to using the backup solution */
1184 TRACE("Unable to find the 'ShellShutdownDialog' function");
1185 ExitWindowsDialog_backup(parent
);
1188 FreeLibrary(msginaDll
);