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
)
96 DestroyIconList(pIconContext
->hDlgCtrl
, pIconContext
);
97 SendMessageW(pIconContext
->hDlgCtrl
, LB_RESETCONTENT
, 0, 0);
98 delete[] pIconContext
->phIcons
;
101 if (pIconContext
->szPath
!= pszFile
)
102 StringCchCopyW(pIconContext
->szPath
, _countof(pIconContext
->szPath
), pszFile
);
103 ExpandEnvironmentStringsW(pszFile
, pIconContext
->szExpandedPath
, _countof(pIconContext
->szExpandedPath
));
105 // load DLL if possible
106 HMODULE hLibrary
= LoadLibraryExW(pIconContext
->szExpandedPath
, NULL
, LOAD_LIBRARY_AS_DATAFILE
);
107 if (pIconContext
->hLibrary
)
108 FreeLibrary(pIconContext
->hLibrary
);
109 pIconContext
->hLibrary
= hLibrary
;
111 if (pIconContext
->hLibrary
)
113 // load icons from DLL
114 pIconContext
->nIcons
= ExtractIconExW(pIconContext
->szExpandedPath
, -1, NULL
, NULL
, 0);
115 pIconContext
->phIcons
= new HICON
[pIconContext
->nIcons
];
117 if (ExtractIconExW(pIconContext
->szExpandedPath
, 0, pIconContext
->phIcons
, NULL
, pIconContext
->nIcons
))
119 EnumResourceNamesW(pIconContext
->hLibrary
, RT_GROUP_ICON
, EnumPickIconResourceProc
, (LPARAM
)pIconContext
);
123 pIconContext
->nIcons
= 0;
129 pIconContext
->nIcons
= 1;
130 pIconContext
->phIcons
= new HICON
[1];
132 if (ExtractIconExW(pIconContext
->szExpandedPath
, 0, pIconContext
->phIcons
, NULL
, pIconContext
->nIcons
))
134 SendMessageW(pIconContext
->hDlgCtrl
, LB_ADDSTRING
, 0, 0);
138 pIconContext
->nIcons
= 0;
143 SetDlgItemTextW(hwndDlg
, IDC_EDIT_PATH
, pIconContext
->szPath
);
145 if (pIconContext
->nIcons
== 0)
147 delete[] pIconContext
->phIcons
;
148 pIconContext
->phIcons
= NULL
;
151 return pIconContext
->nIcons
> 0;
154 static const LPCWSTR s_pszDefaultPath
= L
"%SystemRoot%\\system32\\shell32.dll";
156 static void NoIconsInFile(HWND hwndDlg
, PICK_ICON_CONTEXT
*pIconContext
)
159 CStringW strText
, strTitle(MAKEINTRESOURCEW(IDS_PICK_ICON_TITLE
));
160 strText
.Format(IDS_NO_ICONS
, pIconContext
->szPath
);
161 MessageBoxW(hwndDlg
, strText
, strTitle
, MB_ICONWARNING
);
163 // load default icons
164 DoLoadIcons(hwndDlg
, pIconContext
, s_pszDefaultPath
);
168 #define CX_ICON GetSystemMetrics(SM_CXICON)
169 #define CY_ICON GetSystemMetrics(SM_CYICON)
172 #define CX_ITEM (CX_ICON + 4)
173 #define CY_ITEM (CY_ICON + 12)
175 INT_PTR CALLBACK
PickIconProc(HWND hwndDlg
,
181 LPMEASUREITEMSTRUCT lpmis
;
182 LPDRAWITEMSTRUCT lpdis
;
185 WCHAR szText
[MAX_PATH
], szFilter
[100];
189 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
))
203 NoIconsInFile(hwndDlg
, pIconContext
);
207 count
= SendMessageW(pIconContext
->hDlgCtrl
, LB_GETCOUNT
, 0, 0);
210 if (pIconContext
->Index
< 0)
212 // A negative value will be interpreted as a negated resource ID.
213 LPARAM lParam
= -pIconContext
->Index
;
214 pIconContext
->Index
= (INT
)SendMessageW(pIconContext
->hDlgCtrl
, LB_FINDSTRINGEXACT
, -1, lParam
);
217 if (pIconContext
->Index
< 0 || count
<= pIconContext
->Index
)
218 pIconContext
->Index
= 0;
220 SendMessageW(pIconContext
->hDlgCtrl
, LB_SETCURSEL
, pIconContext
->Index
, 0);
221 SendMessageW(pIconContext
->hDlgCtrl
, LB_SETTOPINDEX
, pIconContext
->Index
, 0);
226 switch(LOWORD(wParam
))
229 index
= SendMessageW(pIconContext
->hDlgCtrl
, LB_GETCURSEL
, 0, 0);
230 pIconContext
->Index
= index
;
231 GetDlgItemTextW(hwndDlg
, IDC_EDIT_PATH
, pIconContext
->szPath
, MAX_PATH
);
232 ExpandEnvironmentStringsW(pIconContext
->szPath
, pIconContext
->szExpandedPath
, _countof(pIconContext
->szExpandedPath
));
233 DestroyIconList(pIconContext
->hDlgCtrl
, pIconContext
);
234 delete[] pIconContext
->phIcons
;
235 EndDialog(hwndDlg
, 1);
239 DestroyIconList(pIconContext
->hDlgCtrl
, pIconContext
);
240 delete[] pIconContext
->phIcons
;
241 EndDialog(hwndDlg
, 0);
244 case IDC_PICKICON_LIST
:
245 switch (HIWORD(wParam
))
248 InvalidateRect((HWND
)lParam
, NULL
, TRUE
);
252 SendMessage(hwndDlg
, WM_COMMAND
, MAKEWPARAM(IDOK
, 0), 0);
257 case IDC_BUTTON_PATH
:
261 ZeroMemory(&ofn
, sizeof(ofn
));
262 ofn
.lStructSize
= sizeof(ofn
);
263 ofn
.hwndOwner
= hwndDlg
;
264 ofn
.lpstrFile
= szText
;
265 ofn
.nMaxFile
= MAX_PATH
;
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
))
276 NoIconsInFile(hwndDlg
, pIconContext
);
280 SendMessageW(pIconContext
->hDlgCtrl
, LB_SETCURSEL
, 0, 0);
286 lpmis
= (LPMEASUREITEMSTRUCT
) lParam
;
287 lpmis
->itemHeight
= CY_ITEM
;
291 lpdis
= (LPDRAWITEMSTRUCT
) lParam
;
292 if (lpdis
->itemID
== (UINT
)-1)
296 switch (lpdis
->itemAction
)
301 index
= SendMessageW(pIconContext
->hDlgCtrl
, LB_GETCURSEL
, 0, 0);
302 hIcon
= pIconContext
->phIcons
[lpdis
->itemID
];
304 if (lpdis
->itemID
== (UINT
)index
)
305 FillRect(lpdis
->hDC
, &lpdis
->rcItem
, (HBRUSH
)(COLOR_HIGHLIGHT
+ 1));
307 FillRect(lpdis
->hDC
, &lpdis
->rcItem
, (HBRUSH
)(COLOR_WINDOW
+ 1));
310 INT x
= lpdis
->rcItem
.left
+ (CX_ITEM
- CX_ICON
) / 2;
311 INT y
= lpdis
->rcItem
.top
+ (CY_ITEM
- CY_ICON
) / 2;
313 DrawIconEx(lpdis
->hDC
, x
, y
, hIcon
, 0, 0, 0, NULL
, DI_NORMAL
);
323 BOOL WINAPI
PickIconDlg(
332 PICK_ICON_CONTEXT IconContext
= { NULL
};
333 IconContext
.Index
= *lpdwIconIndex
;
334 StringCchCopyW(IconContext
.szPath
, _countof(IconContext
.szPath
), lpstrFile
);
335 ExpandEnvironmentStringsW(lpstrFile
, IconContext
.szExpandedPath
, _countof(IconContext
.szExpandedPath
));
337 if (!IconContext
.szExpandedPath
[0] ||
338 GetFileAttributesW(IconContext
.szExpandedPath
) == INVALID_FILE_ATTRIBUTES
)
340 if (IconContext
.szExpandedPath
[0])
343 CStringW strText
, strTitle(MAKEINTRESOURCEW(IDS_PICK_ICON_TITLE
));
344 strText
.Format(IDS_FILE_NOT_FOUND
, lpstrFile
);
345 MessageBoxW(hWndOwner
, strText
, strTitle
, MB_ICONWARNING
);
349 StringCchCopyW(IconContext
.szPath
, _countof(IconContext
.szPath
), s_pszDefaultPath
);
350 ExpandEnvironmentStringsW(s_pszDefaultPath
, IconContext
.szPath
, _countof(IconContext
.szPath
));
354 res
= DialogBoxParamW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_PICK_ICON
), hWndOwner
, PickIconProc
, (LPARAM
)&IconContext
);
358 StringCchCopyW(lpstrFile
, nMaxFile
, IconContext
.szExpandedPath
);
359 *lpdwIconIndex
= IconContext
.Index
;
362 if (IconContext
.hLibrary
)
363 FreeLibrary(IconContext
.hLibrary
);
367 /*************************************************************************
368 * RunFileDlg [internal]
370 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
372 void WINAPI
RunFileDlg(
375 LPCWSTR lpstrDirectory
,
377 LPCWSTR lpstrDescription
,
382 RUNFILEDLGPARAMS rfdp
;
383 rfdp
.hwndOwner
= hWndOwner
;
385 rfdp
.lpstrDirectory
= lpstrDirectory
;
386 rfdp
.lpstrTitle
= lpstrTitle
;
387 rfdp
.lpstrDescription
= lpstrDescription
;
388 rfdp
.uFlags
= uFlags
;
390 DialogBoxParamW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_RUN
), hWndOwner
, RunDlgProc
, (LPARAM
)&rfdp
);
394 /* find the directory that contains the file being run */
395 static LPWSTR
RunDlg_GetParentDir(LPCWSTR cmdline
)
398 WCHAR
*dest
, *result
, *result_end
=NULL
;
399 static const WCHAR dotexeW
[] = L
".exe";
401 result
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
)*(strlenW(cmdline
)+5));
405 TRACE("HeapAlloc couldn't allocate %d bytes\n", sizeof(WCHAR
)*(strlenW(cmdline
)+5));
415 while (*src
&& *src
!= '"')
428 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesW(result
))
430 strcatW(dest
, dotexeW
);
431 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesW(result
))
434 else if (*src
== '\\')
447 HeapFree(GetProcessHeap(), 0, result
);
452 static void EnableOkButtonFromEditContents(HWND hwnd
)
456 HWND Edit
= GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
);
457 Length
= GetWindowTextLengthW(Edit
);
460 PWCHAR psz
= (PWCHAR
)HeapAlloc(GetProcessHeap(), 0, (Length
+ 1) * sizeof(WCHAR
));
463 GetWindowTextW(Edit
, psz
, Length
+ 1);
464 for (n
= 0; n
< Length
&& !Enable
; ++n
)
465 Enable
= psz
[n
] != ' ';
466 HeapFree(GetProcessHeap(), 0, psz
);
471 EnableWindow(GetDlgItem(hwnd
, IDOK
), Enable
);
474 /* Dialog procedure for RunFileDlg */
475 static INT_PTR CALLBACK
RunDlgProc(HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
477 RUNFILEDLGPARAMS
*prfdp
= (RUNFILEDLGPARAMS
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
482 prfdp
= (RUNFILEDLGPARAMS
*)lParam
;
483 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LONG_PTR
)prfdp
);
485 if (prfdp
->lpstrTitle
)
486 SetWindowTextW(hwnd
, prfdp
->lpstrTitle
);
487 if (prfdp
->lpstrDescription
)
488 SetWindowTextW(GetDlgItem(hwnd
, IDC_RUNDLG_DESCRIPTION
), prfdp
->lpstrDescription
);
489 if (prfdp
->uFlags
& RFF_NOBROWSE
)
491 HWND browse
= GetDlgItem(hwnd
, IDC_RUNDLG_BROWSE
);
492 ShowWindow(browse
, SW_HIDE
);
493 EnableWindow(browse
, FALSE
);
495 if (prfdp
->uFlags
& RFF_NOLABEL
)
496 ShowWindow(GetDlgItem(hwnd
, IDC_RUNDLG_LABEL
), SW_HIDE
);
497 if (prfdp
->uFlags
& RFF_NOSEPARATEMEM
)
499 FIXME("RFF_NOSEPARATEMEM not supported\n");
502 /* Use the default Shell Run icon if no one is specified */
503 if (prfdp
->hIcon
== NULL
)
504 prfdp
->hIcon
= LoadIconW(shell32_hInstance
, MAKEINTRESOURCEW(IDI_SHELL_RUN
));
506 * NOTE: Starting Windows Vista, the "Run File" dialog gets a
507 * title icon that remains the same as the default one, even if
508 * the user specifies a custom icon.
509 * Since we currently imitate Windows 2003, therefore do not show
512 // SendMessageW(hwnd, WM_SETICON, ICON_BIG, (LPARAM)prfdp->hIcon);
513 // SendMessageW(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)prfdp->hIcon);
514 SendMessageW(GetDlgItem(hwnd
, IDC_RUNDLG_ICON
), STM_SETICON
, (WPARAM
)prfdp
->hIcon
, 0);
516 FillList(GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
), NULL
, 0, (prfdp
->uFlags
& RFF_NODEFAULT
) == 0);
517 EnableOkButtonFromEditContents(hwnd
);
518 SetFocus(GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
));
522 switch (LOWORD(wParam
))
527 HWND htxt
= GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
);
529 WCHAR
*psz
, *parent
= NULL
;
530 SHELLEXECUTEINFOW sei
;
533 ic
= GetWindowTextLengthW(htxt
);
536 EndDialog(hwnd
, IDCANCEL
);
540 ZeroMemory(&sei
, sizeof(sei
));
541 sei
.cbSize
= sizeof(sei
);
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);
557 sei
.nShow
= SW_SHOWNORMAL
;
561 * The precedence is the following: first the user-given
562 * current directory is used; if there is none, a current
563 * directory is computed if the RFF_CALCDIRECTORY is set,
564 * otherwise no current directory is defined.
566 if (prfdp
->lpstrDirectory
)
567 sei
.lpDirectory
= prfdp
->lpstrDirectory
;
568 else if (prfdp
->uFlags
& RFF_CALCDIRECTORY
)
569 sei
.lpDirectory
= parent
= RunDlg_GetParentDir(sei
.lpFile
);
571 sei
.lpDirectory
= NULL
;
573 /* Hide the dialog for now on, we will show it up in case of retry */
574 ShowWindow(hwnd
, SW_HIDE
);
577 * As shown by manual tests on Windows, modifying the contents
578 * of the notification structure will not modify what the
579 * Run-Dialog will use for the nShow parameter. However the
580 * lpFile and lpDirectory pointers are set to the buffers used
581 * by the Run-Dialog, as a consequence they can be modified by
582 * the notification receiver, as long as it respects the lengths
583 * of the buffers (to avoid buffer overflows).
585 nmrfd
.hdr
.code
= RFN_VALIDATE
;
586 nmrfd
.hdr
.hwndFrom
= hwnd
;
587 nmrfd
.hdr
.idFrom
= 0;
588 nmrfd
.lpFile
= sei
.lpFile
;
589 nmrfd
.lpDirectory
= sei
.lpDirectory
;
590 nmrfd
.nShow
= sei
.nShow
;
592 lRet
= SendMessageW(prfdp
->hwndOwner
, WM_NOTIFY
, 0, (LPARAM
)&nmrfd
.hdr
);
597 EndDialog(hwnd
, IDCANCEL
);
601 if (ShellExecuteExW(&sei
))
603 /* Call again GetWindowText in case the contents of the edit box has changed? */
604 GetWindowTextW(htxt
, psz
, ic
+ 1);
605 FillList(htxt
, psz
, ic
+ 2 + 1, FALSE
);
606 EndDialog(hwnd
, IDOK
);
613 SendMessageW(htxt
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1));
614 /* Show back the dialog */
615 ShowWindow(hwnd
, SW_SHOW
);
619 HeapFree(GetProcessHeap(), 0, parent
);
620 HeapFree(GetProcessHeap(), 0, psz
);
625 EndDialog(hwnd
, IDCANCEL
);
628 case IDC_RUNDLG_BROWSE
:
630 HMODULE hComdlg
= NULL
;
631 LPFNOFN ofnProc
= NULL
;
632 WCHAR szFName
[1024] = {0};
633 WCHAR filter
[MAX_PATH
], szCaption
[MAX_PATH
];
636 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_FILTER
, filter
, MAX_PATH
);
637 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_CAPTION
, szCaption
, MAX_PATH
);
639 ZeroMemory(&ofn
, sizeof(ofn
));
640 ofn
.lStructSize
= sizeof(ofn
);
641 ofn
.hwndOwner
= hwnd
;
642 ofn
.lpstrFilter
= filter
;
643 ofn
.lpstrFile
= szFName
;
644 ofn
.nMaxFile
= _countof(szFName
) - 1;
645 ofn
.lpstrTitle
= szCaption
;
646 ofn
.Flags
= OFN_ENABLESIZING
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
;
647 ofn
.lpstrInitialDir
= prfdp
->lpstrDirectory
;
649 if (NULL
== (hComdlg
= LoadLibraryExW(L
"comdlg32", NULL
, 0)) ||
650 NULL
== (ofnProc
= (LPFNOFN
)GetProcAddress(hComdlg
, "GetOpenFileNameW")))
652 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg
, ofnProc
);
653 ShellMessageBoxW(shell32_hInstance
, hwnd
, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR
), NULL
, MB_OK
| MB_ICONERROR
);
659 SetFocus(GetDlgItem(hwnd
, IDOK
));
660 SetWindowTextW(GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
), szFName
);
661 SendMessageW(GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
), CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
662 EnableOkButtonFromEditContents(hwnd
);
663 SetFocus(GetDlgItem(hwnd
, IDOK
));
666 FreeLibrary(hComdlg
);
670 case IDC_RUNDLG_EDITPATH
:
672 if (HIWORD(wParam
) == CBN_EDITCHANGE
)
674 EnableOkButtonFromEditContents(hwnd
);
685 * This function grabs the MRU list from the registry and fills the combo-list
686 * for the "Run" dialog above. fShowDefault is ignored if pszLatest != NULL.
688 // FIXME: Part of this code should be part of some MRUList API,
689 // that is scattered amongst shell32, comctl32 (?!) and comdlg32.
690 static void FillList(HWND hCb
, LPWSTR pszLatest
, UINT cchStr
, BOOL fShowDefault
)
693 WCHAR
*pszList
= NULL
, *pszCmd
= NULL
, *pszTmp
= NULL
, cMatch
= 0, cMax
= 0x60;
694 WCHAR szIndex
[2] = L
"-";
696 DWORD dwType
, icList
= 0, icCmd
= 0;
701 * Retrieve the string length of pszLatest and check whether its buffer size
702 * (cchStr in number of characters) is large enough to add the terminating "\\1"
703 * (and the NULL character).
707 cchLatest
= wcslen(pszLatest
);
708 if (cchStr
< cchLatest
+ 2 + 1)
710 TRACE("pszLatest buffer is not large enough (%d) to hold the MRU terminator.\n", cchStr
);
719 SendMessageW(hCb
, CB_RESETCONTENT
, 0, 0);
721 lRet
= RegCreateKeyExW(HKEY_CURRENT_USER
,
722 L
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
723 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hkey
, NULL
);
724 if (lRet
!= ERROR_SUCCESS
)
726 TRACE("Unable to open or create the RunMRU key, error %d\n", GetLastError());
730 lRet
= RegQueryValueExW(hkey
, L
"MRUList", NULL
, &dwType
, NULL
, &icList
);
731 if (lRet
== ERROR_SUCCESS
&& dwType
== REG_SZ
&& icList
> sizeof(WCHAR
))
733 pszList
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, icList
);
736 TRACE("HeapAlloc failed to allocate %d bytes\n", icList
);
741 lRet
= RegQueryValueExW(hkey
, L
"MRUList", NULL
, NULL
, (LPBYTE
)pszList
, &icList
);
742 if (lRet
!= ERROR_SUCCESS
)
744 TRACE("Unable to grab MRUList, error %d\n", GetLastError());
751 icList
= sizeof(WCHAR
);
752 pszList
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, icList
);
755 TRACE("HeapAlloc failed to allocate %d bytes\n", icList
);
762 /* Convert the number of bytes from MRUList into number of characters (== number of indices) */
763 icList
/= sizeof(WCHAR
);
765 for (Nix
= 0; Nix
< icList
- 1; Nix
++)
767 if (pszList
[Nix
] > cMax
)
770 szIndex
[0] = pszList
[Nix
];
772 lRet
= RegQueryValueExW(hkey
, szIndex
, NULL
, &dwType
, NULL
, &icCmd
);
773 if (lRet
!= ERROR_SUCCESS
|| dwType
!= REG_SZ
)
775 TRACE("Unable to grab size of index, error %d\n", GetLastError());
781 pszTmp
= (WCHAR
*)HeapReAlloc(GetProcessHeap(), 0, pszCmd
, icCmd
);
784 TRACE("HeapReAlloc failed to reallocate %d bytes\n", icCmd
);
791 pszCmd
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, icCmd
);
794 TRACE("HeapAlloc failed to allocate %d bytes\n", icCmd
);
799 lRet
= RegQueryValueExW(hkey
, szIndex
, NULL
, NULL
, (LPBYTE
)pszCmd
, &icCmd
);
800 if (lRet
!= ERROR_SUCCESS
)
802 TRACE("Unable to grab index, error %d\n", GetLastError());
807 * Generally the command string will end up with "\\1".
808 * Find the last backslash in the string and NULL-terminate.
809 * Windows does not seem to check for what comes next, so that
810 * a command of the form:
811 * c:\\my_dir\\myfile.exe
812 * will be cut just after "my_dir", whereas a command of the form:
813 * c:\\my_dir\\myfile.exe\\1
814 * will be cut just after "myfile.exe".
816 pszTmp
= wcsrchr(pszCmd
, L
'\\');
821 * In the following we try to add pszLatest to the MRU list.
822 * We suppose that our caller has already correctly allocated
823 * the string with enough space for us to append a "\\1".
825 * FIXME: TODO! (At the moment we don't append it!)
830 if (wcsicmp(pszCmd
, pszLatest
) == 0)
832 SendMessageW(hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszCmd
);
833 SetWindowTextW(hCb
, pszCmd
);
834 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
836 cMatch
= pszList
[Nix
];
837 memmove(&pszList
[1], pszList
, Nix
* sizeof(WCHAR
));
843 if (icList
- 1 != 26 || icList
- 2 != Nix
|| cMatch
|| pszLatest
== NULL
)
845 SendMessageW(hCb
, CB_ADDSTRING
, 0, (LPARAM
)pszCmd
);
846 if (!Nix
&& fShowDefault
)
848 SetWindowTextW(hCb
, pszCmd
);
849 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
854 SendMessageW(hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
);
855 SetWindowTextW(hCb
, pszLatest
);
856 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
858 cMatch
= pszList
[Nix
];
859 memmove(&pszList
[1], pszList
, Nix
* sizeof(WCHAR
));
863 wcscpy(&pszLatest
[cchLatest
], L
"\\1");
864 RegSetValueExW(hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, (cchLatest
+ 2 + 1) * sizeof(WCHAR
));
865 pszLatest
[cchLatest
] = L
'\0';
869 if (!cMatch
&& pszLatest
!= NULL
)
871 SendMessageW(hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
);
872 SetWindowTextW(hCb
, pszLatest
);
873 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1));
879 pszTmp
= (WCHAR
*)HeapReAlloc(GetProcessHeap(), 0, pszList
, (++icList
) * sizeof(WCHAR
));
882 TRACE("HeapReAlloc failed to reallocate enough bytes\n");
889 pszList
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, (++icList
) * sizeof(WCHAR
));
892 TRACE("HeapAlloc failed to allocate enough bytes\n");
897 memmove(&pszList
[1], pszList
, (icList
- 1) * sizeof(WCHAR
));
901 wcscpy(&pszLatest
[cchLatest
], L
"\\1");
902 RegSetValueExW(hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, (cchLatest
+ 2 + 1) * sizeof(WCHAR
));
903 pszLatest
[cchLatest
] = L
'\0';
907 RegSetValueExW(hkey
, L
"MRUList", 0, REG_SZ
, (LPBYTE
)pszList
, (wcslen(pszList
) + 1) * sizeof(WCHAR
));
909 HeapFree(GetProcessHeap(), 0, pszCmd
);
910 HeapFree(GetProcessHeap(), 0, pszList
);
916 /*************************************************************************
917 * ConfirmDialog [internal]
919 * Put up a confirm box, return TRUE if the user confirmed
921 static BOOL
ConfirmDialog(HWND hWndOwner
, UINT PromptId
, UINT TitleId
)
926 LoadStringW(shell32_hInstance
, PromptId
, Prompt
, _countof(Prompt
));
927 LoadStringW(shell32_hInstance
, TitleId
, Title
, _countof(Title
));
928 return MessageBoxW(hWndOwner
, Prompt
, Title
, MB_YESNO
| MB_ICONQUESTION
) == IDYES
;
931 typedef HRESULT (WINAPI
*tShellDimScreen
)(IUnknown
** Unknown
, HWND
* hWindow
);
934 CallShellDimScreen(IUnknown
** pUnknown
, HWND
* hWindow
)
936 static tShellDimScreen ShellDimScreen
;
937 static BOOL Initialized
= FALSE
;
940 HMODULE mod
= LoadLibraryW(L
"msgina.dll");
941 ShellDimScreen
= (tShellDimScreen
)GetProcAddress(mod
, (LPCSTR
)16);
947 hr
= ShellDimScreen(pUnknown
, hWindow
);
948 return SUCCEEDED(hr
);
952 /* Used to get the shutdown privilege */
954 EnablePrivilege(LPCWSTR lpszPrivilegeName
, BOOL bEnablePrivilege
)
960 Success
= OpenProcessToken(GetCurrentProcess(),
961 TOKEN_ADJUST_PRIVILEGES
,
963 if (!Success
) return Success
;
965 Success
= LookupPrivilegeValueW(NULL
,
967 &tp
.Privileges
[0].Luid
);
968 if (!Success
) goto Quit
;
970 tp
.PrivilegeCount
= 1;
971 tp
.Privileges
[0].Attributes
= (bEnablePrivilege
? SE_PRIVILEGE_ENABLED
: 0);
973 Success
= AdjustTokenPrivileges(hToken
, FALSE
, &tp
, 0, NULL
, NULL
);
980 /*************************************************************************
981 * RestartDialogEx [SHELL32.730]
984 int WINAPI
RestartDialogEx(HWND hWndOwner
, LPCWSTR lpwstrReason
, DWORD uFlags
, DWORD uReason
)
986 TRACE("(%p)\n", hWndOwner
);
988 CComPtr
<IUnknown
> fadeHandler
;
991 if (!CallShellDimScreen(&fadeHandler
, &parent
))
994 /* FIXME: use lpwstrReason */
995 if (ConfirmDialog(parent
, IDS_RESTART_PROMPT
, IDS_RESTART_TITLE
))
997 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
998 ExitWindowsEx(EWX_REBOOT
, uReason
);
999 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1005 /*************************************************************************
1008 * NOTES: Used to make the Log Off dialog work
1010 INT_PTR CALLBACK
LogOffDialogProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1018 EndDialog(hwnd
, IDCANCEL
);
1024 if (LOWORD(wParam
) == WA_INACTIVE
)
1031 switch (LOWORD(wParam
))
1034 ExitWindowsEx(EWX_LOGOFF
, 0);
1038 EndDialog(hwnd
, IDCANCEL
);
1049 /*************************************************************************
1050 * LogoffWindowsDialog [SHELL32.54]
1053 EXTERN_C
int WINAPI
LogoffWindowsDialog(HWND hWndOwner
)
1055 CComPtr
<IUnknown
> fadeHandler
;
1058 if (!CallShellDimScreen(&fadeHandler
, &parent
))
1061 DialogBoxW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_LOG_OFF
), parent
, LogOffDialogProc
);
1065 /*************************************************************************
1066 * RestartDialog [SHELL32.59]
1069 int WINAPI
RestartDialog(HWND hWndOwner
, LPCWSTR lpstrReason
, DWORD uFlags
)
1071 return RestartDialogEx(hWndOwner
, lpstrReason
, uFlags
, 0);
1074 /*************************************************************************
1075 * ExitWindowsDialog_backup
1078 * Used as a backup solution to shutdown the OS in case msgina.dll
1079 * somehow cannot be found.
1081 VOID
ExitWindowsDialog_backup(HWND hWndOwner
)
1083 TRACE("(%p)\n", hWndOwner
);
1085 if (ConfirmDialog(hWndOwner
, IDS_SHUTDOWN_PROMPT
, IDS_SHUTDOWN_TITLE
))
1087 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1088 ExitWindowsEx(EWX_SHUTDOWN
, 0);
1089 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1093 /*************************************************************************
1094 * ExitWindowsDialog [SHELL32.60]
1097 * exported by ordinal
1101 * - Implement the ability to show either the Welcome Screen or the classic dialog boxes based upon the
1102 * registry value: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType.
1104 void WINAPI
ExitWindowsDialog(HWND hWndOwner
)
1106 typedef DWORD (WINAPI
*ShellShFunc
)(HWND hParent
, WCHAR
*Username
, BOOL bHideLogoff
);
1107 HINSTANCE msginaDll
= LoadLibraryW(L
"msgina.dll");
1109 TRACE("(%p)\n", hWndOwner
);
1111 CComPtr
<IUnknown
> fadeHandler
;
1113 if (!CallShellDimScreen(&fadeHandler
, &parent
))
1116 /* If the DLL cannot be found for any reason, then it simply uses a
1117 dialog box to ask if the user wants to shut down the computer. */
1120 TRACE("Unable to load msgina.dll.\n");
1121 ExitWindowsDialog_backup(parent
);
1125 ShellShFunc pShellShutdownDialog
= (ShellShFunc
)GetProcAddress(msginaDll
, "ShellShutdownDialog");
1127 if (pShellShutdownDialog
)
1129 /* Actually call the function */
1130 DWORD returnValue
= pShellShutdownDialog(parent
, NULL
, FALSE
);
1132 switch (returnValue
)
1134 case 0x01: /* Log off user */
1136 ExitWindowsEx(EWX_LOGOFF
, 0);
1139 case 0x02: /* Shut down */
1141 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1142 ExitWindowsEx(EWX_SHUTDOWN
, 0);
1143 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1146 case 0x03: /* Install Updates/Shutdown (?) */
1150 case 0x04: /* Reboot */
1152 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1153 ExitWindowsEx(EWX_REBOOT
, 0);
1154 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1157 case 0x10: /* Sleep */
1159 if (IsPwrSuspendAllowed())
1161 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1162 SetSuspendState(FALSE
, FALSE
, FALSE
);
1163 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1167 case 0x40: /* Hibernate */
1169 if (IsPwrHibernateAllowed())
1171 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1172 SetSuspendState(TRUE
, FALSE
, TRUE
);
1173 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1177 /* If the option is any other value */
1184 /* If the function cannot be found, then revert to using the backup solution */
1185 TRACE("Unable to find the 'ShellShutdownDialog' function");
1186 ExitWindowsDialog_backup(parent
);
1189 FreeLibrary(msginaDll
);