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(HWND hwndOwner
, WCHAR
*path
, UINT path_len
, INT
*index
)
65 FIXME("(%p,%s,%u,%p):stub.\n", hwndOwner
, debugstr_w(path
), path_len
, index
);
69 HRESULT WINAPI
SHOpenWithDialog(HWND parent
, const OPENASINFO
*info
)
75 /*************************************************************************
76 * RunFileDlgW [internal]
78 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
83 static void RunFileDlgW(
86 LPCWSTR lpstrDirectory
,
88 LPCWSTR lpstrDescription
,
91 static const WCHAR resnameW
[] = {'S','H','E','L','L','_','R','U','N','_','D','L','G',0};
92 RUNFILEDLGPARAMS rfdp
;
97 rfdp
.hwndOwner
= hwndOwner
;
99 rfdp
.lpstrDirectory
= lpstrDirectory
;
100 rfdp
.lpstrTitle
= lpstrTitle
;
101 rfdp
.lpstrDescription
= lpstrDescription
;
102 rfdp
.uFlags
= uFlags
;
104 if (!(hRes
= FindResourceW(shell32_hInstance
, resnameW
, (LPWSTR
)RT_DIALOG
)) ||
105 !(template = LoadResource(shell32_hInstance
, hRes
)))
107 ERR("Couldn't load SHELL_RUN_DLG resource\n");
108 ShellMessageBoxW(shell32_hInstance
, hwndOwner
, MAKEINTRESOURCEW(IDS_RUNDLG_ERROR
), NULL
, MB_OK
| MB_ICONERROR
);
112 DialogBoxIndirectParamW(shell32_hInstance
,
113 template, hwndOwner
, RunDlgProc
, (LPARAM
)&rfdp
);
117 /* find the directory that contains the file being run */
118 static LPWSTR
RunDlg_GetParentDir(LPCWSTR cmdline
)
121 WCHAR
*dest
, *result
, *result_end
=NULL
;
122 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
124 result
= heap_alloc(sizeof(WCHAR
)*(strlenW(cmdline
)+5));
132 while (*src
&& *src
!= '"')
145 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesW(result
))
147 strcatW(dest
, dotexeW
);
148 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesW(result
))
151 else if (*src
== '\\')
169 /* Dialog procedure for RunFileDlg */
170 static INT_PTR CALLBACK
RunDlgProc (HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
172 RUNFILEDLGPARAMS
*prfdp
= (RUNFILEDLGPARAMS
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
177 prfdp
= (RUNFILEDLGPARAMS
*)lParam
;
178 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LONG_PTR
)prfdp
);
180 if (prfdp
->lpstrTitle
)
181 SetWindowTextW(hwnd
, prfdp
->lpstrTitle
);
182 if (prfdp
->lpstrDescription
)
183 SetWindowTextW(GetDlgItem(hwnd
, IDC_RUNDLG_DESCRIPTION
), prfdp
->lpstrDescription
);
184 if (prfdp
->uFlags
& RFF_NOBROWSE
)
186 HWND browse
= GetDlgItem(hwnd
, IDC_RUNDLG_BROWSE
);
187 ShowWindow(browse
, SW_HIDE
);
188 EnableWindow(browse
, FALSE
);
190 if (prfdp
->uFlags
& RFF_NOLABEL
)
191 ShowWindow(GetDlgItem(hwnd
, IDC_RUNDLG_LABEL
), SW_HIDE
);
192 if (prfdp
->uFlags
& RFF_CALCDIRECTORY
)
193 FIXME("RFF_CALCDIRECTORY not supported\n");
195 if (prfdp
->hIcon
== NULL
)
196 prfdp
->hIcon
= LoadIconW(NULL
, (LPCWSTR
)IDI_WINLOGO
);
197 SendMessageW(hwnd
, WM_SETICON
, ICON_BIG
, (LPARAM
)prfdp
->hIcon
);
198 SendMessageW(hwnd
, WM_SETICON
, ICON_SMALL
, (LPARAM
)prfdp
->hIcon
);
199 SendMessageW(GetDlgItem(hwnd
, IDC_RUNDLG_ICON
), STM_SETICON
, (WPARAM
)prfdp
->hIcon
, 0);
201 FillList (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
), NULL
, (prfdp
->uFlags
& RFF_NODEFAULT
) == 0) ;
202 SetFocus (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
)) ;
206 switch (LOWORD (wParam
))
211 HWND htxt
= GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
);
212 if ((ic
= GetWindowTextLengthW (htxt
)))
214 WCHAR
*psz
, *parent
=NULL
;
215 SHELLEXECUTEINFOW sei
;
217 ZeroMemory (&sei
, sizeof(sei
)) ;
218 sei
.cbSize
= sizeof(sei
) ;
219 psz
= heap_alloc( (ic
+ 1)*sizeof(WCHAR
) );
220 GetWindowTextW (htxt
, psz
, ic
+ 1) ;
222 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
223 * WM_NOTIFY before execution */
226 sei
.nShow
= SW_SHOWNORMAL
;
229 if (prfdp
->lpstrDirectory
)
230 sei
.lpDirectory
= prfdp
->lpstrDirectory
;
232 sei
.lpDirectory
= parent
= RunDlg_GetParentDir(sei
.lpFile
);
234 if (!ShellExecuteExW( &sei
))
238 SendMessageA (htxt
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
242 /* FillList is still ANSI */
243 GetWindowTextA (htxt
, (LPSTR
)psz
, ic
+ 1) ;
244 FillList (htxt
, (LPSTR
)psz
, FALSE
) ;
254 EndDialog (hwnd
, 0) ;
257 case IDC_RUNDLG_BROWSE
:
259 static const WCHAR filterW
[] = {'%','s','%','c','*','.','e','x','e','%','c','%','s','%','c','*','.','*','%','c',0};
260 HMODULE hComdlg
= NULL
;
261 LPFNOFN ofnProc
= NULL
;
262 static const WCHAR comdlg32W
[] = {'c','o','m','d','l','g','3','2',0};
263 WCHAR szFName
[1024] = {0};
264 WCHAR filter_exe
[256], filter_all
[256], filter
[MAX_PATH
], szCaption
[MAX_PATH
];
267 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_FILTER_EXE
, filter_exe
, 256);
268 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_FILTER_ALL
, filter_all
, 256);
269 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_CAPTION
, szCaption
, MAX_PATH
);
270 snprintfW( filter
, MAX_PATH
, filterW
, filter_exe
, 0, 0, filter_all
, 0, 0 );
272 ZeroMemory(&ofn
, sizeof(ofn
));
273 ofn
.lStructSize
= sizeof(OPENFILENAMEW
);
274 ofn
.hwndOwner
= hwnd
;
275 ofn
.lpstrFilter
= filter
;
276 ofn
.lpstrFile
= szFName
;
278 ofn
.lpstrTitle
= szCaption
;
279 ofn
.Flags
= OFN_ENABLESIZING
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
;
280 ofn
.lpstrInitialDir
= prfdp
->lpstrDirectory
;
282 if (NULL
== (hComdlg
= LoadLibraryExW (comdlg32W
, NULL
, 0)) ||
283 NULL
== (ofnProc
= (LPFNOFN
)GetProcAddress (hComdlg
, "GetOpenFileNameW")))
285 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg
, ofnProc
);
286 ShellMessageBoxW(shell32_hInstance
, hwnd
, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR
), NULL
, MB_OK
| MB_ICONERROR
);
292 SetFocus (GetDlgItem (hwnd
, IDOK
)) ;
293 SetWindowTextW (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
), szFName
) ;
294 SendMessageW (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
), CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
295 SetFocus (GetDlgItem (hwnd
, IDOK
)) ;
298 FreeLibrary (hComdlg
) ;
308 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
309 /* fShowDefault ignored if pszLatest != NULL */
310 static void FillList (HWND hCb
, char *pszLatest
, BOOL fShowDefault
)
313 /* char szDbgMsg[256] = "" ; */
314 char *pszList
= NULL
, *pszCmd
= NULL
, cMatch
= 0, cMax
= 0x60, szIndex
[2] = "-" ;
315 DWORD icList
= 0, icCmd
= 0 ;
318 SendMessageA (hCb
, CB_RESETCONTENT
, 0, 0) ;
320 if (ERROR_SUCCESS
!= RegCreateKeyExA (
321 HKEY_CURRENT_USER
, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
322 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hkey
, NULL
))
323 MessageBoxA (hCb
, "Unable to open registry key !", "Nix", MB_OK
) ;
325 RegQueryValueExA (hkey
, "MRUList", NULL
, NULL
, NULL
, &icList
) ;
329 pszList
= heap_alloc(icList
) ;
330 if (ERROR_SUCCESS
!= RegQueryValueExA (hkey
, "MRUList", NULL
, NULL
, (LPBYTE
)pszList
, &icList
))
331 MessageBoxA (hCb
, "Unable to grab MRUList !", "Nix", MB_OK
) ;
336 pszList
= heap_alloc(icList
) ;
340 for (Nix
= 0 ; Nix
< icList
- 1 ; Nix
++)
342 if (pszList
[Nix
] > cMax
)
343 cMax
= pszList
[Nix
] ;
345 szIndex
[0] = pszList
[Nix
] ;
347 if (ERROR_SUCCESS
!= RegQueryValueExA (hkey
, szIndex
, NULL
, NULL
, NULL
, &icCmd
))
348 MessageBoxA (hCb
, "Unable to grab size of index", "Nix", MB_OK
) ;
350 pszCmd
= heap_realloc(pszCmd
, icCmd
) ;
352 pszCmd
= heap_alloc(icCmd
) ;
353 if (ERROR_SUCCESS
!= RegQueryValueExA (hkey
, szIndex
, NULL
, NULL
, (LPBYTE
)pszCmd
, &icCmd
))
354 MessageBoxA (hCb
, "Unable to grab index", "Nix", MB_OK
) ;
356 if (NULL
!= pszLatest
)
358 if (!lstrcmpiA(pszCmd
, pszLatest
))
361 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
362 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
364 SendMessageA (hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszCmd
) ;
365 SetWindowTextA (hCb
, pszCmd
) ;
366 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
368 cMatch
= pszList
[Nix
] ;
369 memmove (&pszList
[1], pszList
, Nix
) ;
370 pszList
[0] = cMatch
;
375 if (26 != icList
- 1 || icList
- 2 != Nix
|| cMatch
|| NULL
== pszLatest
)
378 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
379 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
381 SendMessageA (hCb
, CB_ADDSTRING
, 0, (LPARAM
)pszCmd
) ;
382 if (!Nix
&& fShowDefault
)
384 SetWindowTextA (hCb
, pszCmd
) ;
385 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
392 sprintf (szDbgMsg, "Doing loop thing.\n") ;
393 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
395 SendMessageA (hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
) ;
396 SetWindowTextA (hCb
, pszLatest
) ;
397 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
399 cMatch
= pszList
[Nix
] ;
400 memmove (&pszList
[1], pszList
, Nix
) ;
401 pszList
[0] = cMatch
;
402 szIndex
[0] = cMatch
;
403 RegSetValueExA (hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, strlen (pszLatest
) + 1) ;
407 if (!cMatch
&& NULL
!= pszLatest
)
410 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
411 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
413 SendMessageA (hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
) ;
414 SetWindowTextA (hCb
, pszLatest
) ;
415 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
419 pszList
= heap_realloc(pszList
, ++icList
) ;
421 pszList
= heap_alloc(++icList
) ;
422 memmove (&pszList
[1], pszList
, icList
- 1) ;
423 pszList
[0] = cMatch
;
424 szIndex
[0] = cMatch
;
425 RegSetValueExA (hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, strlen (pszLatest
) + 1) ;
428 RegSetValueExA (hkey
, "MRUList", 0, REG_SZ
, (LPBYTE
)pszList
, strlen (pszList
) + 1) ;
434 /*************************************************************************
435 * RunFileDlgA [internal]
437 * The ANSI function that is available as ordinal 61 on Windows 9x/Me
442 static void RunFileDlgA(
445 LPCSTR lpstrDirectory
,
447 LPCSTR lpstrDescription
,
450 WCHAR title
[MAX_PATH
]; /* longer string wouldn't be visible in the dialog anyway */
451 WCHAR description
[MAX_PATH
];
452 WCHAR directory
[MAX_PATH
];
454 MultiByteToWideChar(CP_ACP
, 0, lpstrTitle
, -1, title
, MAX_PATH
);
455 title
[MAX_PATH
- 1] = 0;
456 MultiByteToWideChar(CP_ACP
, 0, lpstrDescription
, -1, description
, MAX_PATH
);
457 description
[MAX_PATH
- 1] = 0;
458 if (!MultiByteToWideChar(CP_ACP
, 0, lpstrDirectory
, -1, directory
, MAX_PATH
))
461 RunFileDlgW(hwndOwner
, hIcon
,
462 lpstrDirectory
? directory
: NULL
,
463 lpstrTitle
? title
: NULL
,
464 lpstrDescription
? description
: NULL
,
468 /*************************************************************************
469 * RunFileDlgAW [SHELL32.61]
471 * An undocumented way to open the Run File dialog. A documented way is to use
472 * CLSID_Shell, IID_IShellDispatch (as of Wine 1.0, not implemented under Wine)
474 * Exported by ordinal. ANSI on Windows 9x and Unicode on Windows NT/2000/XP/etc
477 void WINAPI
RunFileDlgAW(
480 LPCVOID lpstrDirectory
,
482 LPCVOID lpstrDescription
,
485 if (SHELL_OsIsUnicode())
486 RunFileDlgW(hwndOwner
, hIcon
, lpstrDirectory
, lpstrTitle
, lpstrDescription
, uFlags
);
488 RunFileDlgA(hwndOwner
, hIcon
, lpstrDirectory
, lpstrTitle
, lpstrDescription
, uFlags
);
492 /*************************************************************************
493 * ConfirmDialog [internal]
495 * Put up a confirm box, return TRUE if the user confirmed
497 static BOOL
ConfirmDialog(HWND hWndOwner
, UINT PromptId
, UINT TitleId
)
502 LoadStringW(shell32_hInstance
, PromptId
, Prompt
, ARRAY_SIZE(Prompt
));
503 LoadStringW(shell32_hInstance
, TitleId
, Title
, ARRAY_SIZE(Title
));
504 return MessageBoxW(hWndOwner
, Prompt
, Title
, MB_YESNO
|MB_ICONQUESTION
) == IDYES
;
508 /*************************************************************************
509 * RestartDialogEx [SHELL32.730]
512 int WINAPI
RestartDialogEx(HWND hWndOwner
, LPCWSTR lpwstrReason
, DWORD uFlags
, DWORD uReason
)
514 TRACE("(%p)\n", hWndOwner
);
516 /* FIXME: use lpwstrReason */
517 if (ConfirmDialog(hWndOwner
, IDS_RESTART_PROMPT
, IDS_RESTART_TITLE
))
520 TOKEN_PRIVILEGES npr
;
522 /* enable the shutdown privilege for the current process */
523 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &hToken
))
525 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr
.Privileges
[0].Luid
);
526 npr
.PrivilegeCount
= 1;
527 npr
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
528 AdjustTokenPrivileges(hToken
, FALSE
, &npr
, 0, 0, 0);
531 ExitWindowsEx(EWX_REBOOT
, uReason
);
538 /*************************************************************************
539 * RestartDialog [SHELL32.59]
542 int WINAPI
RestartDialog(HWND hWndOwner
, LPCWSTR lpstrReason
, DWORD uFlags
)
544 return RestartDialogEx(hWndOwner
, lpstrReason
, uFlags
, 0);
548 /*************************************************************************
549 * ExitWindowsDialog [SHELL32.60]
552 * exported by ordinal
554 void WINAPI
ExitWindowsDialog (HWND hWndOwner
)
556 TRACE("(%p)\n", hWndOwner
);
558 if (ConfirmDialog(hWndOwner
, IDS_SHUTDOWN_PROMPT
, IDS_SHUTDOWN_TITLE
))
561 TOKEN_PRIVILEGES npr
;
563 /* enable shutdown privilege for current process */
564 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &hToken
))
566 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr
.Privileges
[0].Luid
);
567 npr
.PrivilegeCount
= 1;
568 npr
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
569 AdjustTokenPrivileges(hToken
, FALSE
, &npr
, 0, 0, 0);
572 ExitWindowsEx(EWX_SHUTDOWN
, 0);