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 HRESULT WINAPI
SHOpenWithDialog(HWND parent
, const OPENASINFO
*info
)
80 /*************************************************************************
81 * RunFileDlgW [internal]
83 * The Unicode function that is available as ordinal 61 on Windows NT/2000/XP/...
88 static void RunFileDlgW(
91 LPCWSTR lpstrDirectory
,
93 LPCWSTR lpstrDescription
,
96 static const WCHAR resnameW
[] = {'S','H','E','L','L','_','R','U','N','_','D','L','G',0};
97 RUNFILEDLGPARAMS rfdp
;
102 rfdp
.hwndOwner
= hwndOwner
;
104 rfdp
.lpstrDirectory
= lpstrDirectory
;
105 rfdp
.lpstrTitle
= lpstrTitle
;
106 rfdp
.lpstrDescription
= lpstrDescription
;
107 rfdp
.uFlags
= uFlags
;
109 if (!(hRes
= FindResourceW(shell32_hInstance
, resnameW
, (LPWSTR
)RT_DIALOG
)) ||
110 !(template = LoadResource(shell32_hInstance
, hRes
)))
112 ERR("Couldn't load SHELL_RUN_DLG resource\n");
113 ShellMessageBoxW(shell32_hInstance
, hwndOwner
, MAKEINTRESOURCEW(IDS_RUNDLG_ERROR
), NULL
, MB_OK
| MB_ICONERROR
);
117 DialogBoxIndirectParamW(shell32_hInstance
,
118 template, hwndOwner
, RunDlgProc
, (LPARAM
)&rfdp
);
122 /* find the directory that contains the file being run */
123 static LPWSTR
RunDlg_GetParentDir(LPCWSTR cmdline
)
126 WCHAR
*dest
, *result
, *result_end
=NULL
;
127 static const WCHAR dotexeW
[] = {'.','e','x','e',0};
129 result
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
)*(strlenW(cmdline
)+5));
137 while (*src
&& *src
!= '"')
150 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesW(result
))
152 strcatW(dest
, dotexeW
);
153 if (INVALID_FILE_ATTRIBUTES
!= GetFileAttributesW(result
))
156 else if (*src
== '\\')
169 HeapFree(GetProcessHeap(), 0, result
);
174 /* Dialog procedure for RunFileDlg */
175 static INT_PTR CALLBACK
RunDlgProc (HWND hwnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
177 RUNFILEDLGPARAMS
*prfdp
= (RUNFILEDLGPARAMS
*)GetWindowLongPtrW(hwnd
, DWLP_USER
);
182 prfdp
= (RUNFILEDLGPARAMS
*)lParam
;
183 SetWindowLongPtrW(hwnd
, DWLP_USER
, (LONG_PTR
)prfdp
);
185 if (prfdp
->lpstrTitle
)
186 SetWindowTextW(hwnd
, prfdp
->lpstrTitle
);
187 if (prfdp
->lpstrDescription
)
188 SetWindowTextW(GetDlgItem(hwnd
, IDC_RUNDLG_DESCRIPTION
), prfdp
->lpstrDescription
);
189 if (prfdp
->uFlags
& RFF_NOBROWSE
)
191 HWND browse
= GetDlgItem(hwnd
, IDC_RUNDLG_BROWSE
);
192 ShowWindow(browse
, SW_HIDE
);
193 EnableWindow(browse
, FALSE
);
195 if (prfdp
->uFlags
& RFF_NOLABEL
)
196 ShowWindow(GetDlgItem(hwnd
, IDC_RUNDLG_LABEL
), SW_HIDE
);
197 if (prfdp
->uFlags
& RFF_CALCDIRECTORY
)
198 FIXME("RFF_CALCDIRECTORY not supported\n");
200 if (prfdp
->hIcon
== NULL
)
201 prfdp
->hIcon
= LoadIconW(NULL
, (LPCWSTR
)IDI_WINLOGO
);
202 SendMessageW(hwnd
, WM_SETICON
, ICON_BIG
, (LPARAM
)prfdp
->hIcon
);
203 SendMessageW(hwnd
, WM_SETICON
, ICON_SMALL
, (LPARAM
)prfdp
->hIcon
);
204 SendMessageW(GetDlgItem(hwnd
, IDC_RUNDLG_ICON
), STM_SETICON
, (WPARAM
)prfdp
->hIcon
, 0);
206 FillList (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
), NULL
, (prfdp
->uFlags
& RFF_NODEFAULT
) == 0) ;
207 SetFocus (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
)) ;
211 switch (LOWORD (wParam
))
216 HWND htxt
= GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
);
217 if ((ic
= GetWindowTextLengthW (htxt
)))
219 WCHAR
*psz
, *parent
=NULL
;
220 SHELLEXECUTEINFOW sei
;
222 ZeroMemory (&sei
, sizeof(sei
)) ;
223 sei
.cbSize
= sizeof(sei
) ;
224 psz
= HeapAlloc( GetProcessHeap(), 0, (ic
+ 1)*sizeof(WCHAR
) );
225 GetWindowTextW (htxt
, psz
, ic
+ 1) ;
227 /* according to http://www.codeproject.com/KB/shell/runfiledlg.aspx we should send a
228 * WM_NOTIFY before execution */
231 sei
.nShow
= SW_SHOWNORMAL
;
234 if (prfdp
->lpstrDirectory
)
235 sei
.lpDirectory
= prfdp
->lpstrDirectory
;
237 sei
.lpDirectory
= parent
= RunDlg_GetParentDir(sei
.lpFile
);
239 if (!ShellExecuteExW( &sei
))
241 HeapFree(GetProcessHeap(), 0, psz
);
242 HeapFree(GetProcessHeap(), 0, parent
);
243 SendMessageA (htxt
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
247 /* FillList is still ANSI */
248 GetWindowTextA (htxt
, (LPSTR
)psz
, ic
+ 1) ;
249 FillList (htxt
, (LPSTR
)psz
, FALSE
) ;
251 HeapFree(GetProcessHeap(), 0, psz
);
252 HeapFree(GetProcessHeap(), 0, parent
);
259 EndDialog (hwnd
, 0) ;
262 case IDC_RUNDLG_BROWSE
:
264 static const WCHAR filterW
[] = {'%','s','%','c','*','.','e','x','e','%','c','%','s','%','c','*','.','*','%','c',0};
265 HMODULE hComdlg
= NULL
;
266 LPFNOFN ofnProc
= NULL
;
267 static const WCHAR comdlg32W
[] = {'c','o','m','d','l','g','3','2',0};
268 WCHAR szFName
[1024] = {0};
269 WCHAR filter_exe
[256], filter_all
[256], filter
[MAX_PATH
], szCaption
[MAX_PATH
];
272 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_FILTER_EXE
, filter_exe
, 256);
273 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_FILTER_ALL
, filter_all
, 256);
274 LoadStringW(shell32_hInstance
, IDS_RUNDLG_BROWSE_CAPTION
, szCaption
, MAX_PATH
);
275 snprintfW( filter
, MAX_PATH
, filterW
, filter_exe
, 0, 0, filter_all
, 0, 0 );
277 ZeroMemory(&ofn
, sizeof(ofn
));
278 ofn
.lStructSize
= sizeof(OPENFILENAMEW
);
279 ofn
.hwndOwner
= hwnd
;
280 ofn
.lpstrFilter
= filter
;
281 ofn
.lpstrFile
= szFName
;
283 ofn
.lpstrTitle
= szCaption
;
284 ofn
.Flags
= OFN_ENABLESIZING
| OFN_FILEMUSTEXIST
| OFN_HIDEREADONLY
| OFN_PATHMUSTEXIST
;
285 ofn
.lpstrInitialDir
= prfdp
->lpstrDirectory
;
287 if (NULL
== (hComdlg
= LoadLibraryExW (comdlg32W
, NULL
, 0)) ||
288 NULL
== (ofnProc
= (LPFNOFN
)GetProcAddress (hComdlg
, "GetOpenFileNameW")))
290 ERR("Couldn't get GetOpenFileName function entry (lib=%p, proc=%p)\n", hComdlg
, ofnProc
);
291 ShellMessageBoxW(shell32_hInstance
, hwnd
, MAKEINTRESOURCEW(IDS_RUNDLG_BROWSE_ERROR
), NULL
, MB_OK
| MB_ICONERROR
);
297 SetFocus (GetDlgItem (hwnd
, IDOK
)) ;
298 SetWindowTextW (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
), szFName
) ;
299 SendMessageW (GetDlgItem (hwnd
, IDC_RUNDLG_EDITPATH
), CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
300 SetFocus (GetDlgItem (hwnd
, IDOK
)) ;
303 FreeLibrary (hComdlg
) ;
313 /* This grabs the MRU list from the registry and fills the combo for the "Run" dialog above */
314 /* fShowDefault ignored if pszLatest != NULL */
315 static void FillList (HWND hCb
, char *pszLatest
, BOOL fShowDefault
)
318 /* char szDbgMsg[256] = "" ; */
319 char *pszList
= NULL
, *pszCmd
= NULL
, cMatch
= 0, cMax
= 0x60, szIndex
[2] = "-" ;
320 DWORD icList
= 0, icCmd
= 0 ;
323 SendMessageA (hCb
, CB_RESETCONTENT
, 0, 0) ;
325 if (ERROR_SUCCESS
!= RegCreateKeyExA (
326 HKEY_CURRENT_USER
, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU",
327 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hkey
, NULL
))
328 MessageBoxA (hCb
, "Unable to open registry key !", "Nix", MB_OK
) ;
330 RegQueryValueExA (hkey
, "MRUList", NULL
, NULL
, NULL
, &icList
) ;
334 pszList
= HeapAlloc( GetProcessHeap(), 0, icList
) ;
335 if (ERROR_SUCCESS
!= RegQueryValueExA (hkey
, "MRUList", NULL
, NULL
, (LPBYTE
)pszList
, &icList
))
336 MessageBoxA (hCb
, "Unable to grab MRUList !", "Nix", MB_OK
) ;
341 pszList
= HeapAlloc( GetProcessHeap(), 0, icList
) ;
345 for (Nix
= 0 ; Nix
< icList
- 1 ; Nix
++)
347 if (pszList
[Nix
] > cMax
)
348 cMax
= pszList
[Nix
] ;
350 szIndex
[0] = pszList
[Nix
] ;
352 if (ERROR_SUCCESS
!= RegQueryValueExA (hkey
, szIndex
, NULL
, NULL
, NULL
, &icCmd
))
353 MessageBoxA (hCb
, "Unable to grab size of index", "Nix", MB_OK
) ;
355 pszCmd
= HeapReAlloc(GetProcessHeap(), 0, pszCmd
, icCmd
) ;
357 pszCmd
= HeapAlloc(GetProcessHeap(), 0, icCmd
) ;
358 if (ERROR_SUCCESS
!= RegQueryValueExA (hkey
, szIndex
, NULL
, NULL
, (LPBYTE
)pszCmd
, &icCmd
))
359 MessageBoxA (hCb
, "Unable to grab index", "Nix", MB_OK
) ;
361 if (NULL
!= pszLatest
)
363 if (!lstrcmpiA(pszCmd
, pszLatest
))
366 sprintf (szDbgMsg, "Found existing (%d).\n", Nix) ;
367 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
369 SendMessageA (hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszCmd
) ;
370 SetWindowTextA (hCb
, pszCmd
) ;
371 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
373 cMatch
= pszList
[Nix
] ;
374 memmove (&pszList
[1], pszList
, Nix
) ;
375 pszList
[0] = cMatch
;
380 if (26 != icList
- 1 || icList
- 2 != Nix
|| cMatch
|| NULL
== pszLatest
)
383 sprintf (szDbgMsg, "Happily appending (%d).\n", Nix) ;
384 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
386 SendMessageA (hCb
, CB_ADDSTRING
, 0, (LPARAM
)pszCmd
) ;
387 if (!Nix
&& fShowDefault
)
389 SetWindowTextA (hCb
, pszCmd
) ;
390 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
397 sprintf (szDbgMsg, "Doing loop thing.\n") ;
398 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
400 SendMessageA (hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
) ;
401 SetWindowTextA (hCb
, pszLatest
) ;
402 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
404 cMatch
= pszList
[Nix
] ;
405 memmove (&pszList
[1], pszList
, Nix
) ;
406 pszList
[0] = cMatch
;
407 szIndex
[0] = cMatch
;
408 RegSetValueExA (hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, strlen (pszLatest
) + 1) ;
412 if (!cMatch
&& NULL
!= pszLatest
)
415 sprintf (szDbgMsg, "Simply inserting (increasing list).\n") ;
416 MessageBoxA (hCb, szDbgMsg, "Nix", MB_OK) ;
418 SendMessageA (hCb
, CB_INSERTSTRING
, 0, (LPARAM
)pszLatest
) ;
419 SetWindowTextA (hCb
, pszLatest
) ;
420 SendMessageA (hCb
, CB_SETEDITSEL
, 0, MAKELPARAM (0, -1)) ;
424 pszList
= HeapReAlloc(GetProcessHeap(), 0, pszList
, ++icList
) ;
426 pszList
= HeapAlloc(GetProcessHeap(), 0, ++icList
) ;
427 memmove (&pszList
[1], pszList
, icList
- 1) ;
428 pszList
[0] = cMatch
;
429 szIndex
[0] = cMatch
;
430 RegSetValueExA (hkey
, szIndex
, 0, REG_SZ
, (LPBYTE
)pszLatest
, strlen (pszLatest
) + 1) ;
433 RegSetValueExA (hkey
, "MRUList", 0, REG_SZ
, (LPBYTE
)pszList
, strlen (pszList
) + 1) ;
435 HeapFree( GetProcessHeap(), 0, pszCmd
) ;
436 HeapFree( GetProcessHeap(), 0, pszList
) ;
439 /*************************************************************************
440 * RunFileDlgA [internal]
442 * The ANSI function that is available as ordinal 61 on Windows 9x/Me
447 static void RunFileDlgA(
450 LPCSTR lpstrDirectory
,
452 LPCSTR lpstrDescription
,
455 WCHAR title
[MAX_PATH
]; /* longer string wouldn't be visible in the dialog anyway */
456 WCHAR description
[MAX_PATH
];
457 WCHAR directory
[MAX_PATH
];
459 MultiByteToWideChar(CP_ACP
, 0, lpstrTitle
, -1, title
, MAX_PATH
);
460 title
[MAX_PATH
- 1] = 0;
461 MultiByteToWideChar(CP_ACP
, 0, lpstrDescription
, -1, description
, MAX_PATH
);
462 description
[MAX_PATH
- 1] = 0;
463 if (!MultiByteToWideChar(CP_ACP
, 0, lpstrDirectory
, -1, directory
, MAX_PATH
))
466 RunFileDlgW(hwndOwner
, hIcon
,
467 lpstrDirectory
? directory
: NULL
,
468 lpstrTitle
? title
: NULL
,
469 lpstrDescription
? description
: NULL
,
473 /*************************************************************************
474 * RunFileDlgAW [SHELL32.61]
476 * An undocumented way to open the Run File dialog. A documented way is to use
477 * CLSID_Shell, IID_IShellDispatch (as of Wine 1.0, not implemented under Wine)
479 * Exported by ordinal. ANSI on Windows 9x and Unicode on Windows NT/2000/XP/etc
482 void WINAPI
RunFileDlgAW(
485 LPCVOID lpstrDirectory
,
487 LPCVOID lpstrDescription
,
490 if (SHELL_OsIsUnicode())
491 RunFileDlgW(hwndOwner
, hIcon
, lpstrDirectory
, lpstrTitle
, lpstrDescription
, uFlags
);
493 RunFileDlgA(hwndOwner
, hIcon
, lpstrDirectory
, lpstrTitle
, lpstrDescription
, uFlags
);
497 /*************************************************************************
498 * ConfirmDialog [internal]
500 * Put up a confirm box, return TRUE if the user confirmed
502 static BOOL
ConfirmDialog(HWND hWndOwner
, UINT PromptId
, UINT TitleId
)
507 LoadStringW(shell32_hInstance
, PromptId
, Prompt
, sizeof(Prompt
) / sizeof(WCHAR
));
508 LoadStringW(shell32_hInstance
, TitleId
, Title
, sizeof(Title
) / sizeof(WCHAR
));
509 return MessageBoxW(hWndOwner
, Prompt
, Title
, MB_YESNO
|MB_ICONQUESTION
) == IDYES
;
513 /*************************************************************************
514 * RestartDialogEx [SHELL32.730]
517 int WINAPI
RestartDialogEx(HWND hWndOwner
, LPCWSTR lpwstrReason
, DWORD uFlags
, DWORD uReason
)
519 TRACE("(%p)\n", hWndOwner
);
521 /* FIXME: use lpwstrReason */
522 if (ConfirmDialog(hWndOwner
, IDS_RESTART_PROMPT
, IDS_RESTART_TITLE
))
525 TOKEN_PRIVILEGES npr
;
527 /* enable the shutdown privilege for the current process */
528 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &hToken
))
530 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr
.Privileges
[0].Luid
);
531 npr
.PrivilegeCount
= 1;
532 npr
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
533 AdjustTokenPrivileges(hToken
, FALSE
, &npr
, 0, 0, 0);
536 ExitWindowsEx(EWX_REBOOT
, uReason
);
543 /*************************************************************************
544 * RestartDialog [SHELL32.59]
547 int WINAPI
RestartDialog(HWND hWndOwner
, LPCWSTR lpstrReason
, DWORD uFlags
)
549 return RestartDialogEx(hWndOwner
, lpstrReason
, uFlags
, 0);
553 /*************************************************************************
554 * ExitWindowsDialog [SHELL32.60]
557 * exported by ordinal
559 void WINAPI
ExitWindowsDialog (HWND hWndOwner
)
561 TRACE("(%p)\n", hWndOwner
);
563 if (ConfirmDialog(hWndOwner
, IDS_SHUTDOWN_PROMPT
, IDS_SHUTDOWN_TITLE
))
566 TOKEN_PRIVILEGES npr
;
568 /* enable shutdown privilege for current process */
569 if (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
, &hToken
))
571 LookupPrivilegeValueA(0, "SeShutdownPrivilege", &npr
.Privileges
[0].Luid
);
572 npr
.PrivilegeCount
= 1;
573 npr
.Privileges
[0].Attributes
= SE_PRIVILEGE_ENABLED
;
574 AdjustTokenPrivileges(hToken
, FALSE
, &npr
, 0, 0, 0);
577 ExitWindowsEx(EWX_SHUTDOWN
, 0);