4 * Copyright 2000 Juergen Schmied
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
34 #include "wine/debug.h"
38 #include "shell32_main.h"
40 #include "undocshell.h"
46 LPCWSTR lpstrDirectory
;
48 LPCWSTR lpstrDescription
;
52 typedef BOOL (WINAPI
* LPFNOFN
) (OPENFILENAMEW
*) ;
54 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
55 static INT_PTR CALLBACK
RunDlgProc (HWND
, UINT
, WPARAM
, LPARAM
) ;
56 static void FillList (HWND
, char *, BOOL
) ;
59 /*************************************************************************
60 * PickIconDlg [SHELL32.62]
63 INT WINAPI
PickIconDlg(
67 LPDWORD lpdwIconIndex
)
69 FIXME("(%p,%s,%08x,%p):stub.\n",
70 hwndOwner
, lpstrFile
, nMaxFile
,lpdwIconIndex
);
74 /*************************************************************************
75 * RunFileDlgW [internal]
77 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
82 static void RunFileDlgW(
85 LPCWSTR lpstrDirectory
,
87 LPCWSTR lpstrDescription
,
90 static const WCHAR resnameW
[] = {'S','H','E','L','L','_','R','U','N','_','D','L','G',0};
91 RUNFILEDLGPARAMS rfdp
;
96 rfdp
.hwndOwner
= hwndOwner
;
98 rfdp
.lpstrDirectory
= lpstrDirectory
;
99 rfdp
.lpstrTitle
= lpstrTitle
;
100 rfdp
.lpstrDescription
= lpstrDescription
;
101 rfdp
.uFlags
= uFlags
;
103 if (!(hRes
= FindResourceW(shell32_hInstance
, resnameW
, (LPWSTR
)RT_DIALOG
)) ||
104 !(template = LoadResource(shell32_hInstance
, hRes
)))
106 ERR("Couldn't load SHELL_RUN_DLG resource\n");
107 ShellMessageBoxW(shell32_hInstance
, hwndOwner
, MAKEINTRESOURCEW(IDS_RUNDLG_ERROR
), NULL
, MB_OK
| MB_ICONERROR
);
111 DialogBoxIndirectParamW(shell32_hInstance
,
112 template, hwndOwner
, RunDlgProc
, (LPARAM
)&rfdp
);
116 /* find the directory that contains the file being run */
117 static LPWSTR
RunDlg_GetParentDir(LPCWSTR cmdline
)
120 WCHAR
*dest
, *result
, *result_end
=NULL
;
121 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
123 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
)*(strlenW(cmdline
)+5));
131 while (*src
&& *src
!= '"')
144 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesW(result
))
146 strcatW(dest
, dotexeW
);
147 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesW(result
))
150 else if (*src
== '\\')
163 HeapFree(GetProcessHeap(), 0, result
);
168 /* Dialog procedure for RunFileDlg */
169 static INT_PTR CALLBACK
RunDlgProc (HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
171 RUNFILEDLGPARAMS
*prfdp
= (RUNFILEDLGPARAMS
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
176 prfdp
= (RUNFILEDLGPARAMS
*)lParam
;
177 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LONG_PTR
)prfdp
);
179 if (prfdp
->lpstrTitle
)
180 SetWindowTextW(hwnd
, prfdp
->lpstrTitle
);
181 if (prfdp
->lpstrDescription
)
182 SetWindowTextW(GetDlgItem(hwnd
, IDC_RUNDLG_DESCRIPTION
), prfdp
->lpstrDescription
);
183 if (prfdp
->uFlags
& RFF_NOBROWSE
)
185 HWND browse
= GetDlgItem(hwnd
, IDC_RUNDLG_BROWSE
);
186 ShowWindow(browse
, SW_HIDE
);
187 EnableWindow(browse
, FALSE
);
189 if (prfdp
->uFlags
& RFF_NOLABEL
)
190 ShowWindow(GetDlgItem(hwnd
, IDC_RUNDLG_LABEL
), SW_HIDE
);
191 if (prfdp
->uFlags
& RFF_CALCDIRECTORY
)
192 FIXME("RFF_CALCDIRECTORY not supported\n");
194 if (prfdp
->hIcon
== NULL
)
195 prfdp
->hIcon
= LoadIconW(NULL
, (LPCWSTR
)IDI_WINLOGO
);
196 SendMessageW(hwnd
, WM_SETICON
, ICON_BIG
, (LPARAM
)prfdp
->hIcon
);
197 SendMessageW(hwnd
, WM_SETICON
, ICON_SMALL
, (LPARAM
)prfdp
->hIcon
);
198 SendMessageW(GetDlgItem(hwnd
, IDC_RUNDLG_ICON
), STM_SETICON
, (WPARAM
)prfdp
->hIcon
, 0);
200 FillList (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
), NULL
, (prfdp
->uFlags
& RFF_NODEFAULT
) == 0) ;
201 SetFocus (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
)) ;
205 switch (LOWORD (wParam
))
210 HWND htxt
= GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
);
211 if ((ic
= GetWindowTextLengthW (htxt
)))
213 WCHAR
*psz
, *parent
=NULL
;
214 SHELLEXECUTEINFOW sei
;
216 ZeroMemory (&sei
, sizeof(sei
)) ;
217 sei
.cbSize
= sizeof(sei
) ;
218 psz
= HeapAlloc( GetProcessHeap(), 0, (ic
+ 1)*sizeof(WCHAR
) );
219 GetWindowTextW (htxt
, psz
, ic
+ 1) ;
221 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
222 * WM_NOTIFY before execution */
225 sei
.nShow
= SW_SHOWNORMAL
;
228 if (prfdp
->lpstrDirectory
)
229 sei
.lpDirectory
= prfdp
->lpstrDirectory
;
231 sei
.lpDirectory
= parent
= RunDlg_GetParentDir(sei
.lpFile
);
233 if (!ShellExecuteExW( &sei
))
235 HeapFree(GetProcessHeap(), 0, psz
);
236 HeapFree(GetProcessHeap(), 0, parent
);
237 SendMessageA (htxt
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
241 /* FillList is still ANSI */
242 GetWindowTextA (htxt
, (LPSTR
)psz
, ic
+ 1) ;
243 FillList (htxt
, (LPSTR
)psz
, FALSE
) ;
245 HeapFree(GetProcessHeap(), 0, psz
);
246 HeapFree(GetProcessHeap(), 0, parent
);
252 EndDialog (hwnd
, 0) ;
255 case IDC_RUNDLG_BROWSE
:
257 static const WCHAR filterW
[] = {'%','s','%','c','*','.','e','x','e','%','c','%','s','%','c','*','.','*','%','c',0};
258 HMODULE hComdlg
= NULL
;
259 LPFNOFN ofnProc
= NULL
;
260 static const WCHAR comdlg32W
[] = {'c','o','m','d','l','g','3','2',0};
261 WCHAR szFName
[1024] = {0};
262 WCHAR filter_exe
[256], filter_all
[256], filter
[MAX_PATH
], szCaption
[MAX_PATH
];
265 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_FILTER_EXE
, filter_exe
, 256);
266 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_FILTER_ALL
, filter_all
, 256);
267 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_CAPTION
, szCaption
, MAX_PATH
);
268 snprintfW( filter
, MAX_PATH
, filterW
, filter_exe
, 0, 0, filter_all
, 0 );
270 ZeroMemory(&ofn
, sizeof(ofn
));
271 ofn
.lStructSize
= sizeof(OPENFILENAMEW
);
272 ofn
.hwndOwner
= hwnd
;
273 ofn
.lpstrFilter
= filter
;
274 ofn
.lpstrFile
= szFName
;
276 ofn
.lpstrTitle
= szCaption
;
277 ofn
.Flags
= OFN_ENABLESIZING
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
;
278 ofn
.lpstrInitialDir
= prfdp
->lpstrDirectory
;
280 if (NULL
== (hComdlg
= LoadLibraryExW (comdlg32W
, NULL
, 0)) ||
281 NULL
== (ofnProc
= (LPFNOFN
)GetProcAddress (hComdlg
, "GetOpenFileNameW")))
283 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg
, ofnProc
);
284 ShellMessageBoxW(shell32_hInstance
, hwnd
, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR
), NULL
, MB_OK
| MB_ICONERROR
);
290 SetFocus (GetDlgItem (hwnd
, IDOK
)) ;
291 SetWindowTextW (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
), szFName
) ;
292 SendMessageW (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
), CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
293 SetFocus (GetDlgItem (hwnd
, IDOK
)) ;
296 FreeLibrary (hComdlg
) ;
306 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
307 /* fShowDefault ignored if pszLatest != NULL */
308 static void FillList (HWND hCb
, char *pszLatest
, BOOL fShowDefault
)
311 /* char szDbgMsg[256] = "" ; */
312 char *pszList
= NULL
, *pszCmd
= NULL
, cMatch
= 0, cMax
= 0x60, szIndex
[2] = "-" ;
313 DWORD icList
= 0, icCmd
= 0 ;
316 SendMessageA (hCb
, CB_RESETCONTENT
, 0, 0) ;
318 if (ERROR_SUCCESS
!= RegCreateKeyExA (
319 HKEY_CURRENT_USER
, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
320 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hkey
, NULL
))
321 MessageBoxA (hCb
, "Unable to open registry key !", "Nix", MB_OK
) ;
323 RegQueryValueExA (hkey
, "MRUList", NULL
, NULL
, NULL
, &icList
) ;
327 pszList
= HeapAlloc( GetProcessHeap(), 0, icList
) ;
328 if (ERROR_SUCCESS
!= RegQueryValueExA (hkey
, "MRUList", NULL
, NULL
, (LPBYTE
)pszList
, &icList
))
329 MessageBoxA (hCb
, "Unable to grab MRUList !", "Nix", MB_OK
) ;
334 pszList
= HeapAlloc( GetProcessHeap(), 0, icList
) ;
338 for (Nix
= 0 ; Nix
< icList
- 1 ; Nix
++)
340 if (pszList
[Nix
] > cMax
)
341 cMax
= pszList
[Nix
] ;
343 szIndex
[0] = pszList
[Nix
] ;
345 if (ERROR_SUCCESS
!= RegQueryValueExA (hkey
, szIndex
, NULL
, NULL
, NULL
, &icCmd
))
346 MessageBoxA (hCb
, "Unable to grab size of index", "Nix", MB_OK
) ;
348 pszCmd
= HeapReAlloc(GetProcessHeap(), 0, pszCmd
, icCmd
) ;
350 pszCmd
= HeapAlloc(GetProcessHeap(), 0, icCmd
) ;
351 if (ERROR_SUCCESS
!= RegQueryValueExA (hkey
, szIndex
, NULL
, NULL
, (LPBYTE
)pszCmd
, &icCmd
))
352 MessageBoxA (hCb
, "Unable to grab index", "Nix", MB_OK
) ;
354 if (NULL
!= pszLatest
)
356 if (!lstrcmpiA(pszCmd
, pszLatest
))
359 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
360 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
362 SendMessageA (hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszCmd
) ;
363 SetWindowTextA (hCb
, pszCmd
) ;
364 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
366 cMatch
= pszList
[Nix
] ;
367 memmove (&pszList
[1], pszList
, Nix
) ;
368 pszList
[0] = cMatch
;
373 if (26 != icList
- 1 || icList
- 2 != Nix
|| cMatch
|| NULL
== pszLatest
)
376 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
377 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
379 SendMessageA (hCb
, CB_ADDSTRING
, 0, (LPARAM
)pszCmd
) ;
380 if (!Nix
&& fShowDefault
)
382 SetWindowTextA (hCb
, pszCmd
) ;
383 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
390 sprintf (szDbgMsg, "Doing loop thing.\n") ;
391 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
393 SendMessageA (hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
) ;
394 SetWindowTextA (hCb
, pszLatest
) ;
395 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
397 cMatch
= pszList
[Nix
] ;
398 memmove (&pszList
[1], pszList
, Nix
) ;
399 pszList
[0] = cMatch
;
400 szIndex
[0] = cMatch
;
401 RegSetValueExA (hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, strlen (pszLatest
) + 1) ;
405 if (!cMatch
&& NULL
!= pszLatest
)
408 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
409 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
411 SendMessageA (hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
) ;
412 SetWindowTextA (hCb
, pszLatest
) ;
413 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
417 pszList
= HeapReAlloc(GetProcessHeap(), 0, pszList
, ++icList
) ;
419 pszList
= HeapAlloc(GetProcessHeap(), 0, ++icList
) ;
420 memmove (&pszList
[1], pszList
, icList
- 1) ;
421 pszList
[0] = cMatch
;
422 szIndex
[0] = cMatch
;
423 RegSetValueExA (hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, strlen (pszLatest
) + 1) ;
426 RegSetValueExA (hkey
, "MRUList", 0, REG_SZ
, (LPBYTE
)pszList
, strlen (pszList
) + 1) ;
428 HeapFree( GetProcessHeap(), 0, pszCmd
) ;
429 HeapFree( GetProcessHeap(), 0, pszList
) ;
432 /*************************************************************************
433 * RunFileDlgA [internal]
435 * The ANSI function that is available as ordinal 61 on Windows 9x/Me
440 static void RunFileDlgA(
443 LPCSTR lpstrDirectory
,
445 LPCSTR lpstrDescription
,
448 WCHAR title
[MAX_PATH
]; /* longer string wouldn't be visible in the dialog anyway */
449 WCHAR description
[MAX_PATH
];
450 WCHAR directory
[MAX_PATH
];
452 MultiByteToWideChar(CP_ACP
, 0, lpstrTitle
, -1, title
, MAX_PATH
);
453 title
[MAX_PATH
- 1] = 0;
454 MultiByteToWideChar(CP_ACP
, 0, lpstrDescription
, -1, description
, MAX_PATH
);
455 description
[MAX_PATH
- 1] = 0;
456 if (!MultiByteToWideChar(CP_ACP
, 0, lpstrDirectory
, -1, directory
, MAX_PATH
))
459 RunFileDlgW(hwndOwner
, hIcon
,
460 lpstrDirectory
? directory
: NULL
,
461 lpstrTitle
? title
: NULL
,
462 lpstrDescription
? description
: NULL
,
466 /*************************************************************************
467 * RunFileDlgAW [SHELL32.61]
469 * An undocumented way to open the Run File dialog. A documented way is to use
470 * CLSID_Shell, IID_IShellDispatch (as of Wine 1.0, not implemented under Wine)
472 * Exported by ordinal. ANSI on Windows 9x and Unicode on Windows NT/2000/XP/etc
475 void WINAPI
RunFileDlgAW(
478 LPCVOID lpstrDirectory
,
480 LPCVOID lpstrDescription
,
483 if (SHELL_OsIsUnicode())
484 RunFileDlgW(hwndOwner
, hIcon
, lpstrDirectory
, lpstrTitle
, lpstrDescription
, uFlags
);
486 RunFileDlgA(hwndOwner
, hIcon
, lpstrDirectory
, lpstrTitle
, lpstrDescription
, uFlags
);
490 /*************************************************************************
491 * ConfirmDialog [internal]
493 * Put up a confirm box, return TRUE if the user confirmed
495 static BOOL
ConfirmDialog(HWND hWndOwner
, UINT PromptId
, UINT TitleId
)
500 LoadStringW(shell32_hInstance
, PromptId
, Prompt
, sizeof(Prompt
) / sizeof(WCHAR
));
501 LoadStringW(shell32_hInstance
, TitleId
, Title
, sizeof(Title
) / sizeof(WCHAR
));
502 return MessageBoxW(hWndOwner
, Prompt
, Title
, MB_YESNO
|MB_ICONQUESTION
) == IDYES
;
506 /*************************************************************************
507 * RestartDialogEx [SHELL32.730]
510 int WINAPI
RestartDialogEx(HWND hWndOwner
, LPCWSTR lpwstrReason
, DWORD uFlags
, DWORD uReason
)
512 TRACE("(%p)\n", hWndOwner
);
514 /* FIXME: use lpwstrReason */
515 if (ConfirmDialog(hWndOwner
, IDS_RESTART_PROMPT
, IDS_RESTART_TITLE
))
518 TOKEN_PRIVILEGES npr
;
520 /* enable the shutdown privilege for the current process */
521 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &hToken
))
523 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr
.Privileges
[0].Luid
);
524 npr
.PrivilegeCount
= 1;
525 npr
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
526 AdjustTokenPrivileges(hToken
, FALSE
, &npr
, 0, 0, 0);
529 ExitWindowsEx(EWX_REBOOT
, uReason
);
536 /*************************************************************************
537 * RestartDialog [SHELL32.59]
540 int WINAPI
RestartDialog(HWND hWndOwner
, LPCWSTR lpstrReason
, DWORD uFlags
)
542 return RestartDialogEx(hWndOwner
, lpstrReason
, uFlags
, 0);
546 /*************************************************************************
547 * ExitWindowsDialog [SHELL32.60]
550 * exported by ordinal
552 void WINAPI
ExitWindowsDialog (HWND hWndOwner
)
554 TRACE("(%p)\n", hWndOwner
);
556 if (ConfirmDialog(hWndOwner
, IDS_SHUTDOWN_PROMPT
, IDS_SHUTDOWN_TITLE
))
559 TOKEN_PRIVILEGES npr
;
561 /* enable shutdown privilege for current process */
562 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &hToken
))
564 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr
.Privileges
[0].Luid
);
565 npr
.PrivilegeCount
= 1;
566 npr
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
567 AdjustTokenPrivileges(hToken
, FALSE
, &npr
, 0, 0, 0);
570 ExitWindowsEx(EWX_SHUTDOWN
, 0);