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
;
532 ic
= GetWindowTextLengthW(htxt
);
535 EndDialog(hwnd
, IDCANCEL
);
540 * Allocate a new MRU entry, we need to add two characters
541 * for the terminating "\\1" part, then the NULL character.
543 psz
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, (ic
+ 2 + 1)*sizeof(WCHAR
));
546 EndDialog(hwnd
, IDCANCEL
);
550 GetWindowTextW(htxt
, psz
, ic
+ 1);
551 StrTrimW(psz
, L
" \t");
554 * The precedence is the following: first the user-given
555 * current directory is used; if there is none, a current
556 * directory is computed if the RFF_CALCDIRECTORY is set,
557 * otherwise no current directory is defined.
560 if (prfdp
->lpstrDirectory
)
561 pszStartDir
= prfdp
->lpstrDirectory
;
562 else if (prfdp
->uFlags
& RFF_CALCDIRECTORY
)
563 pszStartDir
= parent
= RunDlg_GetParentDir(psz
);
567 /* Hide the dialog for now on, we will show it up in case of retry */
568 ShowWindow(hwnd
, SW_HIDE
);
571 * As shown by manual tests on Windows, modifying the contents
572 * of the notification structure will not modify what the
573 * Run-Dialog will use for the nShow parameter. However the
574 * lpFile and lpDirectory pointers are set to the buffers used
575 * by the Run-Dialog, as a consequence they can be modified by
576 * the notification receiver, as long as it respects the lengths
577 * of the buffers (to avoid buffer overflows).
579 nmrfd
.hdr
.code
= RFN_VALIDATE
;
580 nmrfd
.hdr
.hwndFrom
= hwnd
;
581 nmrfd
.hdr
.idFrom
= 0;
583 nmrfd
.lpDirectory
= pszStartDir
;
584 nmrfd
.nShow
= SW_SHOWNORMAL
;
586 lRet
= SendMessageW(prfdp
->hwndOwner
, WM_NOTIFY
, 0, (LPARAM
)&nmrfd
.hdr
);
591 EndDialog(hwnd
, IDCANCEL
);
595 if (SUCCEEDED(ShellExecCmdLine(hwnd
, psz
, pszStartDir
, SW_SHOWNORMAL
, NULL
,
598 /* Call again GetWindowText in case the contents of the edit box has changed? */
599 GetWindowTextW(htxt
, psz
, ic
+ 1);
600 FillList(htxt
, psz
, ic
+ 2 + 1, FALSE
);
601 EndDialog(hwnd
, IDOK
);
608 SendMessageW(htxt
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1));
609 /* Show back the dialog */
610 ShowWindow(hwnd
, SW_SHOW
);
614 HeapFree(GetProcessHeap(), 0, parent
);
615 HeapFree(GetProcessHeap(), 0, psz
);
620 EndDialog(hwnd
, IDCANCEL
);
623 case IDC_RUNDLG_BROWSE
:
625 HMODULE hComdlg
= NULL
;
626 LPFNOFN ofnProc
= NULL
;
627 WCHAR szFName
[1024] = {0};
628 WCHAR filter
[MAX_PATH
], szCaption
[MAX_PATH
];
631 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_FILTER
, filter
, MAX_PATH
);
632 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_CAPTION
, szCaption
, MAX_PATH
);
634 ZeroMemory(&ofn
, sizeof(ofn
));
635 ofn
.lStructSize
= sizeof(ofn
);
636 ofn
.hwndOwner
= hwnd
;
637 ofn
.lpstrFilter
= filter
;
638 ofn
.lpstrFile
= szFName
;
639 ofn
.nMaxFile
= _countof(szFName
) - 1;
640 ofn
.lpstrTitle
= szCaption
;
641 ofn
.Flags
= OFN_ENABLESIZING
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
;
642 ofn
.lpstrInitialDir
= prfdp
->lpstrDirectory
;
644 if (NULL
== (hComdlg
= LoadLibraryExW(L
"comdlg32", NULL
, 0)) ||
645 NULL
== (ofnProc
= (LPFNOFN
)GetProcAddress(hComdlg
, "GetOpenFileNameW")))
647 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg
, ofnProc
);
648 ShellMessageBoxW(shell32_hInstance
, hwnd
, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR
), NULL
, MB_OK
| MB_ICONERROR
);
654 SetFocus(GetDlgItem(hwnd
, IDOK
));
655 SetWindowTextW(GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
), szFName
);
656 SendMessageW(GetDlgItem(hwnd
, IDC_RUNDLG_EDITPATH
), CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
657 EnableOkButtonFromEditContents(hwnd
);
658 SetFocus(GetDlgItem(hwnd
, IDOK
));
661 FreeLibrary(hComdlg
);
665 case IDC_RUNDLG_EDITPATH
:
667 if (HIWORD(wParam
) == CBN_EDITCHANGE
)
669 EnableOkButtonFromEditContents(hwnd
);
680 * This function grabs the MRU list from the registry and fills the combo-list
681 * for the "Run" dialog above. fShowDefault is ignored if pszLatest != NULL.
683 // FIXME: Part of this code should be part of some MRUList API,
684 // that is scattered amongst shell32, comctl32 (?!) and comdlg32.
685 static void FillList(HWND hCb
, LPWSTR pszLatest
, UINT cchStr
, BOOL fShowDefault
)
688 WCHAR
*pszList
= NULL
, *pszCmd
= NULL
, *pszTmp
= NULL
, cMatch
= 0, cMax
= 0x60;
689 WCHAR szIndex
[2] = L
"-";
691 DWORD dwType
, icList
= 0, icCmd
= 0;
696 * Retrieve the string length of pszLatest and check whether its buffer size
697 * (cchStr in number of characters) is large enough to add the terminating "\\1"
698 * (and the NULL character).
702 cchLatest
= wcslen(pszLatest
);
703 if (cchStr
< cchLatest
+ 2 + 1)
705 TRACE("pszLatest buffer is not large enough (%d) to hold the MRU terminator.\n", cchStr
);
714 SendMessageW(hCb
, CB_RESETCONTENT
, 0, 0);
716 lRet
= RegCreateKeyExW(HKEY_CURRENT_USER
,
717 L
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
718 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hkey
, NULL
);
719 if (lRet
!= ERROR_SUCCESS
)
721 TRACE("Unable to open or create the RunMRU key, error %d\n", GetLastError());
725 lRet
= RegQueryValueExW(hkey
, L
"MRUList", NULL
, &dwType
, NULL
, &icList
);
726 if (lRet
== ERROR_SUCCESS
&& dwType
== REG_SZ
&& icList
> sizeof(WCHAR
))
728 pszList
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, icList
);
731 TRACE("HeapAlloc failed to allocate %d bytes\n", icList
);
736 lRet
= RegQueryValueExW(hkey
, L
"MRUList", NULL
, NULL
, (LPBYTE
)pszList
, &icList
);
737 if (lRet
!= ERROR_SUCCESS
)
739 TRACE("Unable to grab MRUList, error %d\n", GetLastError());
746 icList
= sizeof(WCHAR
);
747 pszList
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, icList
);
750 TRACE("HeapAlloc failed to allocate %d bytes\n", icList
);
757 /* Convert the number of bytes from MRUList into number of characters (== number of indices) */
758 icList
/= sizeof(WCHAR
);
760 for (Nix
= 0; Nix
< icList
- 1; Nix
++)
762 if (pszList
[Nix
] > cMax
)
765 szIndex
[0] = pszList
[Nix
];
767 lRet
= RegQueryValueExW(hkey
, szIndex
, NULL
, &dwType
, NULL
, &icCmd
);
768 if (lRet
!= ERROR_SUCCESS
|| dwType
!= REG_SZ
)
770 TRACE("Unable to grab size of index, error %d\n", GetLastError());
776 pszTmp
= (WCHAR
*)HeapReAlloc(GetProcessHeap(), 0, pszCmd
, icCmd
);
779 TRACE("HeapReAlloc failed to reallocate %d bytes\n", icCmd
);
786 pszCmd
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, icCmd
);
789 TRACE("HeapAlloc failed to allocate %d bytes\n", icCmd
);
794 lRet
= RegQueryValueExW(hkey
, szIndex
, NULL
, NULL
, (LPBYTE
)pszCmd
, &icCmd
);
795 if (lRet
!= ERROR_SUCCESS
)
797 TRACE("Unable to grab index, error %d\n", GetLastError());
802 * Generally the command string will end up with "\\1".
803 * Find the last backslash in the string and NULL-terminate.
804 * Windows does not seem to check for what comes next, so that
805 * a command of the form:
806 * c:\\my_dir\\myfile.exe
807 * will be cut just after "my_dir", whereas a command of the form:
808 * c:\\my_dir\\myfile.exe\\1
809 * will be cut just after "myfile.exe".
811 pszTmp
= wcsrchr(pszCmd
, L
'\\');
816 * In the following we try to add pszLatest to the MRU list.
817 * We suppose that our caller has already correctly allocated
818 * the string with enough space for us to append a "\\1".
820 * FIXME: TODO! (At the moment we don't append it!)
825 if (wcsicmp(pszCmd
, pszLatest
) == 0)
827 SendMessageW(hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszCmd
);
828 SetWindowTextW(hCb
, pszCmd
);
829 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
831 cMatch
= pszList
[Nix
];
832 memmove(&pszList
[1], pszList
, Nix
* sizeof(WCHAR
));
838 if (icList
- 1 != 26 || icList
- 2 != Nix
|| cMatch
|| pszLatest
== NULL
)
840 SendMessageW(hCb
, CB_ADDSTRING
, 0, (LPARAM
)pszCmd
);
841 if (!Nix
&& fShowDefault
)
843 SetWindowTextW(hCb
, pszCmd
);
844 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
849 SendMessageW(hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
);
850 SetWindowTextW(hCb
, pszLatest
);
851 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM(0, -1));
853 cMatch
= pszList
[Nix
];
854 memmove(&pszList
[1], pszList
, Nix
* sizeof(WCHAR
));
858 wcscpy(&pszLatest
[cchLatest
], L
"\\1");
859 RegSetValueExW(hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, (cchLatest
+ 2 + 1) * sizeof(WCHAR
));
860 pszLatest
[cchLatest
] = L
'\0';
864 if (!cMatch
&& pszLatest
!= NULL
)
866 SendMessageW(hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
);
867 SetWindowTextW(hCb
, pszLatest
);
868 SendMessageW(hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1));
874 pszTmp
= (WCHAR
*)HeapReAlloc(GetProcessHeap(), 0, pszList
, (++icList
) * sizeof(WCHAR
));
877 TRACE("HeapReAlloc failed to reallocate enough bytes\n");
884 pszList
= (WCHAR
*)HeapAlloc(GetProcessHeap(), 0, (++icList
) * sizeof(WCHAR
));
887 TRACE("HeapAlloc failed to allocate enough bytes\n");
892 memmove(&pszList
[1], pszList
, (icList
- 1) * sizeof(WCHAR
));
896 wcscpy(&pszLatest
[cchLatest
], L
"\\1");
897 RegSetValueExW(hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, (cchLatest
+ 2 + 1) * sizeof(WCHAR
));
898 pszLatest
[cchLatest
] = L
'\0';
902 RegSetValueExW(hkey
, L
"MRUList", 0, REG_SZ
, (LPBYTE
)pszList
, (wcslen(pszList
) + 1) * sizeof(WCHAR
));
904 HeapFree(GetProcessHeap(), 0, pszCmd
);
905 HeapFree(GetProcessHeap(), 0, pszList
);
911 /*************************************************************************
912 * ConfirmDialog [internal]
914 * Put up a confirm box, return TRUE if the user confirmed
916 static BOOL
ConfirmDialog(HWND hWndOwner
, UINT PromptId
, UINT TitleId
)
921 LoadStringW(shell32_hInstance
, PromptId
, Prompt
, _countof(Prompt
));
922 LoadStringW(shell32_hInstance
, TitleId
, Title
, _countof(Title
));
923 return MessageBoxW(hWndOwner
, Prompt
, Title
, MB_YESNO
| MB_ICONQUESTION
) == IDYES
;
926 typedef HRESULT (WINAPI
*tShellDimScreen
)(IUnknown
** Unknown
, HWND
* hWindow
);
929 CallShellDimScreen(IUnknown
** pUnknown
, HWND
* hWindow
)
931 static tShellDimScreen ShellDimScreen
;
932 static BOOL Initialized
= FALSE
;
935 HMODULE mod
= LoadLibraryW(L
"msgina.dll");
936 ShellDimScreen
= (tShellDimScreen
)GetProcAddress(mod
, (LPCSTR
)16);
942 hr
= ShellDimScreen(pUnknown
, hWindow
);
943 return SUCCEEDED(hr
);
947 /* Used to get the shutdown privilege */
949 EnablePrivilege(LPCWSTR lpszPrivilegeName
, BOOL bEnablePrivilege
)
955 Success
= OpenProcessToken(GetCurrentProcess(),
956 TOKEN_ADJUST_PRIVILEGES
,
958 if (!Success
) return Success
;
960 Success
= LookupPrivilegeValueW(NULL
,
962 &tp
.Privileges
[0].Luid
);
963 if (!Success
) goto Quit
;
965 tp
.PrivilegeCount
= 1;
966 tp
.Privileges
[0].Attributes
= (bEnablePrivilege
? SE_PRIVILEGE_ENABLED
: 0);
968 Success
= AdjustTokenPrivileges(hToken
, FALSE
, &tp
, 0, NULL
, NULL
);
975 /*************************************************************************
976 * RestartDialogEx [SHELL32.730]
979 int WINAPI
RestartDialogEx(HWND hWndOwner
, LPCWSTR lpwstrReason
, DWORD uFlags
, DWORD uReason
)
981 TRACE("(%p)\n", hWndOwner
);
983 CComPtr
<IUnknown
> fadeHandler
;
986 if (!CallShellDimScreen(&fadeHandler
, &parent
))
989 /* FIXME: use lpwstrReason */
990 if (ConfirmDialog(parent
, IDS_RESTART_PROMPT
, IDS_RESTART_TITLE
))
992 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
993 ExitWindowsEx(EWX_REBOOT
, uReason
);
994 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1000 /*************************************************************************
1003 * NOTES: Used to make the Log Off dialog work
1005 INT_PTR CALLBACK
LogOffDialogProc(HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
1013 EndDialog(hwnd
, IDCANCEL
);
1019 if (LOWORD(wParam
) == WA_INACTIVE
)
1026 switch (LOWORD(wParam
))
1029 ExitWindowsEx(EWX_LOGOFF
, 0);
1033 EndDialog(hwnd
, IDCANCEL
);
1044 /*************************************************************************
1045 * LogoffWindowsDialog [SHELL32.54]
1048 EXTERN_C
int WINAPI
LogoffWindowsDialog(HWND hWndOwner
)
1050 CComPtr
<IUnknown
> fadeHandler
;
1053 if (!CallShellDimScreen(&fadeHandler
, &parent
))
1056 DialogBoxW(shell32_hInstance
, MAKEINTRESOURCEW(IDD_LOG_OFF
), parent
, LogOffDialogProc
);
1060 /*************************************************************************
1061 * RestartDialog [SHELL32.59]
1064 int WINAPI
RestartDialog(HWND hWndOwner
, LPCWSTR lpstrReason
, DWORD uFlags
)
1066 return RestartDialogEx(hWndOwner
, lpstrReason
, uFlags
, 0);
1069 /*************************************************************************
1070 * ExitWindowsDialog_backup
1073 * Used as a backup solution to shutdown the OS in case msgina.dll
1074 * somehow cannot be found.
1076 VOID
ExitWindowsDialog_backup(HWND hWndOwner
)
1078 TRACE("(%p)\n", hWndOwner
);
1080 if (ConfirmDialog(hWndOwner
, IDS_SHUTDOWN_PROMPT
, IDS_SHUTDOWN_TITLE
))
1082 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1083 ExitWindowsEx(EWX_SHUTDOWN
, 0);
1084 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1088 /*************************************************************************
1089 * ExitWindowsDialog [SHELL32.60]
1092 * exported by ordinal
1096 * - Implement the ability to show either the Welcome Screen or the classic dialog boxes based upon the
1097 * registry value: SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\LogonType.
1099 void WINAPI
ExitWindowsDialog(HWND hWndOwner
)
1101 typedef DWORD (WINAPI
*ShellShFunc
)(HWND hParent
, WCHAR
*Username
, BOOL bHideLogoff
);
1102 HINSTANCE msginaDll
= LoadLibraryW(L
"msgina.dll");
1104 TRACE("(%p)\n", hWndOwner
);
1106 CComPtr
<IUnknown
> fadeHandler
;
1108 if (!CallShellDimScreen(&fadeHandler
, &parent
))
1111 /* If the DLL cannot be found for any reason, then it simply uses a
1112 dialog box to ask if the user wants to shut down the computer. */
1115 TRACE("Unable to load msgina.dll.\n");
1116 ExitWindowsDialog_backup(parent
);
1120 ShellShFunc pShellShutdownDialog
= (ShellShFunc
)GetProcAddress(msginaDll
, "ShellShutdownDialog");
1122 if (pShellShutdownDialog
)
1124 /* Actually call the function */
1125 DWORD returnValue
= pShellShutdownDialog(parent
, NULL
, FALSE
);
1127 switch (returnValue
)
1129 case 0x01: /* Log off user */
1131 ExitWindowsEx(EWX_LOGOFF
, 0);
1134 case 0x02: /* Shut down */
1136 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1137 ExitWindowsEx(EWX_SHUTDOWN
, 0);
1138 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1141 case 0x03: /* Install Updates/Shutdown (?) */
1145 case 0x04: /* Reboot */
1147 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1148 ExitWindowsEx(EWX_REBOOT
, 0);
1149 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1152 case 0x10: /* Sleep */
1154 if (IsPwrSuspendAllowed())
1156 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1157 SetSuspendState(FALSE
, FALSE
, FALSE
);
1158 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1162 case 0x40: /* Hibernate */
1164 if (IsPwrHibernateAllowed())
1166 EnablePrivilege(L
"SeShutdownPrivilege", TRUE
);
1167 SetSuspendState(TRUE
, FALSE
, TRUE
);
1168 EnablePrivilege(L
"SeShutdownPrivilege", FALSE
);
1172 /* If the option is any other value */
1179 /* If the function cannot be found, then revert to using the backup solution */
1180 TRACE("Unable to find the 'ShellShutdownDialog' function");
1181 ExitWindowsDialog_backup(parent
);
1184 FreeLibrary(msginaDll
);