Merge Wine shell32 commits:
[reactos.git] / reactos / lib / shell32 / shlexec.c
1 /*
2 * Shell Library Functions
3 *
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2002 Eric Pouech
6 *
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.
11 *
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.
16 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <ctype.h>
33 #include <assert.h>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "winreg.h"
39 #include "wownt32.h"
40 #include "shellapi.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "shlobj.h"
44 #include "shlwapi.h"
45 #include "ddeml.h"
46
47 #include "wine/winbase16.h"
48 #include "shell32_main.h"
49 #include "undocshell.h"
50 #include "pidl.h"
51
52 #include "wine/debug.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(exec);
55
56 static const WCHAR wszOpen[] = {'o','p','e','n',0};
57 static const WCHAR wszExe[] = {'.','e','x','e',0};
58 static const WCHAR wszILPtr[] = {':','%','p',0};
59 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
60 static const WCHAR wszFolder[] = {'F','o','l','d','e','r',0};
61 static const WCHAR wszEmpty[] = {0};
62
63
64 /***********************************************************************
65 * SHELL_ArgifyW [Internal]
66 *
67 * this function is supposed to expand the escape sequences found in the registry
68 * some diving reported that the following were used:
69 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
70 * %1 file
71 * %2 printer
72 * %3 driver
73 * %4 port
74 * %I address of a global item ID (explorer switch /idlist)
75 * %L seems to be %1 as long filename followed by the 8+3 variation
76 * %S ???
77 * %* all following parameters (see batfile)
78 *
79 * FIXME: use 'len'
80 */
81 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args)
82 {
83 WCHAR xlpFile[1024];
84 BOOL done = FALSE;
85 PWSTR res = out;
86 PCWSTR cmd;
87 LPVOID pv;
88
89 while (*fmt)
90 {
91 if (*fmt == '%')
92 {
93 switch (*++fmt)
94 {
95 case '\0':
96 case '%':
97 *res++ = '%';
98 break;
99
100 case '2':
101 case '3':
102 case '4':
103 case '5':
104 case '6':
105 case '7':
106 case '8':
107 case '9':
108 case '0':
109 case '*':
110 if (args)
111 {
112 if (*fmt == '*')
113 {
114 *res++ = '"';
115 while(*args)
116 *res++ = *args++;
117 *res++ = '"';
118 }
119 else
120 {
121 while(*args && !isspace(*args))
122 *res++ = *args++;
123
124 while(isspace(*args))
125 ++args;
126 }
127 break;
128 }
129 /* else fall through */
130 case '1':
131 if (!done || (*fmt == '1'))
132 {
133 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
134 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
135 cmd = xlpFile;
136 else
137 cmd = lpFile;
138
139 /* Add double quotation marks unless we already have them (e.g.: "%1" %* for exefile) */
140 if (res==out || res[-1]!='"')
141 {
142 *res++ = '"';
143 strcpyW(res, cmd);
144 res += strlenW(cmd);
145 *res++ = '"';
146 }
147 else
148 {
149 strcpyW(res, cmd);
150 res += strlenW(cmd);
151 }
152 }
153 break;
154
155 /*
156 * IE uses this alot for activating things such as windows media
157 * player. This is not verified to be fully correct but it appears
158 * to work just fine.
159 */
160 case 'l':
161 case 'L':
162 if (lpFile) {
163 strcpyW(res, lpFile);
164 res += strlenW(lpFile);
165 }
166 break;
167
168 case 'i':
169 case 'I':
170 if (pidl) {
171 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
172 pv = SHLockShared(hmem, 0);
173 res += sprintfW(res, wszILPtr, pv);
174 SHUnlockShared(pv);
175 }
176 break;
177
178 default: FIXME("Unknown escape sequence %%%c\n", *fmt);
179 }
180
181 fmt++;
182 done = TRUE;
183 }
184 else
185 *res++ = *fmt++;
186 }
187
188 *res = '\0';
189
190 return done;
191 }
192
193 HRESULT SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl, LPSTR pszPath, UINT uOutSize)
194 {
195 STRRET strret;
196 IShellFolder* desktop;
197
198 HRESULT hr = SHGetDesktopFolder(&desktop);
199
200 if (SUCCEEDED(hr)) {
201 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
202
203 if (SUCCEEDED(hr))
204 StrRetToStrNA(pszPath, uOutSize, &strret, pidl);
205
206 IShellFolder_Release(desktop);
207 }
208
209 return hr;
210 }
211
212 HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
213 {
214 STRRET strret;
215 IShellFolder* desktop;
216
217 HRESULT hr = SHGetDesktopFolder(&desktop);
218
219 if (SUCCEEDED(hr)) {
220 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
221
222 if (SUCCEEDED(hr))
223 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
224
225 IShellFolder_Release(desktop);
226 }
227
228 return hr;
229 }
230
231 /*************************************************************************
232 * SHELL_ResolveShortCutW [Internal]
233 * read shortcut file at 'wcmd'
234 */
235 static HRESULT SHELL_ResolveShortCutW(LPWSTR wcmd, LPWSTR wargs, LPWSTR wdir, HWND hwnd, LPCWSTR lpVerb, int* pshowcmd, LPITEMIDLIST* ppidl)
236 {
237 IShellFolder* psf;
238
239 HRESULT hr = SHGetDesktopFolder(&psf);
240
241 *ppidl = NULL;
242
243 if (SUCCEEDED(hr)) {
244 LPITEMIDLIST pidl;
245 ULONG l;
246
247 hr = IShellFolder_ParseDisplayName(psf, 0, 0, wcmd, &l, &pidl, 0);
248
249 if (SUCCEEDED(hr)) {
250 IShellLinkW* psl;
251
252 hr = IShellFolder_GetUIObjectOf(psf, NULL, 1, (LPCITEMIDLIST*)&pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
253
254 if (SUCCEEDED(hr)) {
255 hr = IShellLinkW_Resolve(psl, hwnd, 0);
256
257 if (SUCCEEDED(hr)) {
258 hr = IShellLinkW_GetPath(psl, wcmd, MAX_PATH, NULL, SLGP_UNCPRIORITY);
259
260 if (SUCCEEDED(hr)) {
261 if (!*wcmd) {
262 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
263 hr = IShellLinkW_GetIDList(psl, ppidl);
264
265 if (SUCCEEDED(hr) && *ppidl) {
266 /* We got a PIDL instead of a file system path - try to translate it. */
267 if (SUCCEEDED(SHELL_GetPathFromIDListW(*ppidl, wcmd, MAX_PATH))) {
268 SHFree(*ppidl);
269 *ppidl = NULL;
270 }
271 }
272 }
273
274 if (SUCCEEDED(hr)) {
275 /* get command line arguments, working directory and display mode if available */
276 IShellLinkW_GetWorkingDirectory(psl, wdir, MAX_PATH);
277 IShellLinkW_GetArguments(psl, wargs, MAX_PATH);
278 IShellLinkW_GetShowCmd(psl, pshowcmd);
279 }
280 }
281 }
282
283 IShellLinkW_Release(psl);
284 }
285
286 SHFree(pidl);
287 }
288
289 IShellFolder_Release(psf);
290 }
291
292 return hr;
293 }
294
295 /*************************************************************************
296 * SHELL_ExecuteW [Internal]
297 *
298 */
299 static UINT SHELL_ExecuteW(const WCHAR *lpCmd, void *env, BOOL shWait,
300 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
301 {
302 STARTUPINFOW startup;
303 PROCESS_INFORMATION info;
304 UINT retval = 31;
305 UINT gcdret = 0;
306 WCHAR curdir[MAX_PATH];
307
308 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
309
310 /* ShellExecute specifies the command from psei->lpDirectory
311 * if present. Not from the current dir as CreateProcess does */
312 if( psei->lpDirectory && psei->lpDirectory[0] )
313 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
314 if( !SetCurrentDirectoryW( psei->lpDirectory))
315 ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory));
316
317 ZeroMemory(&startup, sizeof(startup));
318 startup.cb = sizeof(STARTUPINFOW);
319 startup.dwFlags = STARTF_USESHOWWINDOW;
320 startup.wShowWindow = psei->nShow;
321
322 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, 0,
323 env, *psei->lpDirectory? psei->lpDirectory: NULL, &startup, &info))
324 {
325 /* Give 30 seconds to the app to come up, if desired. Probably only needed
326 when starting app immediately before making a DDE connection. */
327 if (shWait)
328 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
329 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
330 retval = 33;
331
332 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
333 psei_out->hProcess = info.hProcess;
334 else
335 CloseHandle(info.hProcess);
336
337 CloseHandle(info.hThread);
338 }
339 else if ((retval = GetLastError()) >= 32)
340 {
341 FIXME("Strange error set by CreateProcess: %d\n", retval);
342 retval = ERROR_BAD_FORMAT;
343 }
344
345 TRACE("returning %u\n", retval);
346
347 psei_out->hInstApp = (HINSTANCE)retval;
348
349 if( gcdret )
350 if( !SetCurrentDirectoryW( curdir))
351 ERR("cannot return to directory %s\n", debugstr_w(curdir));
352
353 return retval;
354 }
355
356
357 /***********************************************************************
358 * SHELL_BuildEnvW [Internal]
359 *
360 * Build the environment for the new process, adding the specified
361 * path to the PATH variable. Returned pointer must be freed by caller.
362 */
363 static void *SHELL_BuildEnvW( const WCHAR *path )
364 {
365 static const WCHAR wPath[] = {'P','A','T','H','=',0};
366 WCHAR *strings, *new_env;
367 WCHAR *p, *p2;
368 int total = strlenW(path) + 1;
369 BOOL got_path = FALSE;
370
371 if (!(strings = GetEnvironmentStringsW())) return NULL;
372 p = strings;
373 while (*p)
374 {
375 int len = strlenW(p) + 1;
376 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
377 total += len;
378 p += len;
379 }
380 if (!got_path) total += 5; /* we need to create PATH */
381 total++; /* terminating null */
382
383 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
384 {
385 FreeEnvironmentStringsW( strings );
386 return NULL;
387 }
388 p = strings;
389 p2 = new_env;
390 while (*p)
391 {
392 int len = strlenW(p) + 1;
393 memcpy( p2, p, len * sizeof(WCHAR) );
394 if (!strncmpiW( p, wPath, 5 ))
395 {
396 p2[len - 1] = ';';
397 strcpyW( p2 + len, path );
398 p2 += strlenW(path) + 1;
399 }
400 p += len;
401 p2 += len;
402 }
403 if (!got_path)
404 {
405 strcpyW( p2, wPath );
406 strcatW( p2, path );
407 p2 += strlenW(p2) + 1;
408 }
409 *p2 = 0;
410 FreeEnvironmentStringsW( strings );
411 return new_env;
412 }
413
414
415 /***********************************************************************
416 * SHELL_TryAppPathW [Internal]
417 *
418 * Helper function for SHELL_FindExecutable
419 * @param lpResult - pointer to a buffer of size MAX_PATH
420 * On entry: szName is a filename (probably without path separators).
421 * On exit: if szName found in "App Path", place full path in lpResult, and return true
422 */
423 static BOOL SHELL_TryAppPathW(LPCWSTR szName, LPWSTR lpResult, void** env)
424 {
425 static const WCHAR wszKeyAppPaths[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',
426 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
427 static const WCHAR wPath[] = {'P','a','t','h',0};
428
429 HKEY hkApp = 0;
430 WCHAR buffer[1024];
431 LONG len;
432 LONG res;
433 BOOL found = FALSE;
434
435 if (env) *env = NULL;
436 strcpyW(buffer, wszKeyAppPaths);
437 strcatW(buffer, szName);
438 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
439 if (res) goto end;
440
441 len = MAX_PATH*sizeof(WCHAR);
442 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
443 if (res) goto end;
444 found = TRUE;
445
446 if (env)
447 {
448 DWORD count = sizeof(buffer);
449 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
450 *env = SHELL_BuildEnvW( buffer );
451 }
452
453 end:
454 if (hkApp) RegCloseKey(hkApp);
455 return found;
456 }
457
458 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
459 {
460 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
461
462 /* Looking for ...buffer\shell\<verb>\command */
463 strcatW(filetype, wszShell);
464 strcatW(filetype, lpOperation);
465 strcatW(filetype, wCommand);
466
467 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
468 &commandlen) == ERROR_SUCCESS)
469 {
470 commandlen /= sizeof(WCHAR);
471 if (key) strcpyW(key, filetype);
472 #if 0
473 LPWSTR tmp;
474 WCHAR param[256];
475 LONG paramlen = sizeof(param);
476 static const WCHAR wSpace[] = {' ',0};
477
478 /* FIXME: it seems all Windows version don't behave the same here.
479 * the doc states that this ddeexec information can be found after
480 * the exec names.
481 * on Win98, it doesn't appear, but I think it does on Win2k
482 */
483 /* Get the parameters needed by the application
484 from the associated ddeexec key */
485 tmp = strstrW(filetype, wCommand);
486 tmp[0] = '\0';
487 strcatW(filetype, wDdeexec);
488 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
489 &paramlen) == ERROR_SUCCESS)
490 {
491 paramlen /= sizeof(WCHAR);
492 strcatW(command, wSpace);
493 strcatW(command, param);
494 commandlen += paramlen;
495 }
496 #endif
497
498 command[commandlen] = '\0';
499
500 return 33; /* FIXME see SHELL_FindExecutable() */
501 }
502
503 return 31; /* default - 'No association was found' */
504 }
505
506 /*************************************************************************
507 * SHELL_FindExecutable [Internal]
508 *
509 * Utility for code sharing between FindExecutable and ShellExecute
510 * in:
511 * lpFile the name of a file
512 * lpOperation the operation on it (open)
513 * out:
514 * lpResult a buffer, big enough :-(, to store the command to do the
515 * operation on the file
516 * key a buffer, big enough, to get the key name to do actually the
517 * command (it'll be used afterwards for more information
518 * on the operation)
519 */
520 UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
521 LPWSTR lpResult, int resultLen, LPWSTR key, void **env, LPITEMIDLIST pidl, LPCWSTR args)
522 {
523 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
524 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
525 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
526
527 WCHAR *extension = NULL; /* pointer to file extension */
528 WCHAR wtmpext[5]; /* local copy to mung as we please */
529 WCHAR filetype[256]; /* registry name for this filetype */
530 LONG filetypelen = sizeof(filetype); /* length of above */
531 WCHAR command[256]; /* command from registry */
532 WCHAR wBuffer[256]; /* Used to GetProfileString */
533 UINT retval = 31; /* default - 'No association was found' */
534 WCHAR *tok; /* token pointer */
535 WCHAR xlpFile[256]; /* result of SearchPath */
536 DWORD attribs; /* file attributes */
537
538 TRACE("%s\n", (lpFile != NULL) ? debugstr_w(lpFile) : "-");
539
540 xlpFile[0] = '\0';
541 lpResult[0] = '\0'; /* Start off with an empty return string */
542 if (key) *key = '\0';
543
544 /* trap NULL parameters on entry */
545 if ((lpFile == NULL) || (lpResult == NULL))
546 {
547 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n", debugstr_w(lpFile), debugstr_w(lpResult));
548 return 2; /* File not found. Close enough, I guess. */
549 }
550
551 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
552 {
553 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
554 return 33;
555 }
556
557 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
558 {
559 TRACE("SearchPathW returned non-zero\n");
560 lpFile = xlpFile;
561 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
562 }
563
564 attribs = GetFileAttributesW(lpFile);
565 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
566 {
567 strcpyW(filetype, wszFolder);
568 filetypelen = 6; /* strlen("Folder") */
569 }
570 else
571 {
572 /* First thing we need is the file's extension */
573 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
574 /* File->Run in progman uses */
575 /* .\FILE.EXE :( */
576 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
577
578 if ((extension == NULL) || (extension == &xlpFile[strlenW(xlpFile)]))
579 {
580 WARN("Returning 31 - No association\n");
581 return 31; /* no association */
582 }
583 /* Make local copy & lowercase it for reg & 'programs=' lookup */
584 lstrcpynW(wtmpext, extension, 5);
585 CharLowerW(wtmpext);
586 TRACE("%s file\n", debugstr_w(wtmpext));
587
588 /* Three places to check: */
589 /* 1. win.ini, [windows], programs (NB no leading '.') */
590 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
591 /* 3. win.ini, [extensions], extension (NB no leading '.' */
592 /* All I know of the order is that registry is checked before */
593 /* extensions; however, it'd make sense to check the programs */
594 /* section first, so that's what happens here. */
595
596 /* See if it's a program - if GetProfileString fails, we skip this
597 * section. Actually, if GetProfileString fails, we've probably
598 * got a lot more to worry about than running a program... */
599 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
600 {
601 CharLowerW(wBuffer);
602 tok = wBuffer;
603 while (*tok)
604 {
605 WCHAR *p = tok;
606 while (*p && *p != ' ' && *p != '\t') p++;
607 if (*p)
608 {
609 *p++ = 0;
610 while (*p == ' ' || *p == '\t') p++;
611 }
612
613 if (strcmpW(tok, &wtmpext[1]) == 0) /* have to skip the leading "." */
614 {
615 strcpyW(lpResult, xlpFile);
616 /* Need to perhaps check that the file has a path
617 * attached */
618 TRACE("found %s\n", debugstr_w(lpResult));
619 return 33;
620
621 /* Greater than 32 to indicate success FIXME According to the
622 * docs, I should be returning a handle for the
623 * executable. Does this mean I'm supposed to open the
624 * executable file or something? More RTFM, I guess... */
625 }
626 tok = p;
627 }
628 }
629
630 /* Check registry */
631 if (RegQueryValueW(HKEY_CLASSES_ROOT, wtmpext, filetype,
632 &filetypelen) == ERROR_SUCCESS)
633 {
634 filetypelen /= sizeof(WCHAR);
635 filetype[filetypelen] = '\0';
636 TRACE("File type: %s\n", debugstr_w(filetype));
637 }
638 }
639
640 if (*filetype)
641 {
642 if (lpOperation)
643 {
644 /* pass the operation string to SHELL_FindExecutableByOperation() */
645 filetype[filetypelen] = '\0';
646 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, lpOperation, key, filetype, command, sizeof(command));
647 }
648 else
649 {
650 WCHAR operation[MAX_PATH];
651 HKEY hkey;
652
653 /* Looking for ...buffer\shell\<operation>\command */
654 strcatW(filetype, wszShell);
655
656 /* enumerate the operation subkeys in the registry and search for one with an associated command */
657 if (RegOpenKeyW(HKEY_CLASSES_ROOT, filetype, &hkey) == ERROR_SUCCESS)
658 {
659 int idx = 0;
660 for(;; ++idx)
661 {
662 if (RegEnumKeyW(hkey, idx, operation, MAX_PATH) != ERROR_SUCCESS)
663 break;
664
665 filetype[filetypelen] = '\0';
666 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, operation, key, filetype, command, sizeof(command));
667
668 if (retval > 32)
669 break;
670 }
671 RegCloseKey(hkey);
672 }
673 }
674
675 if (retval > 32)
676 {
677 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args);
678
679 /* Remove double quotation marks and command line arguments */
680 if (*lpResult == '"')
681 {
682 WCHAR *p = lpResult;
683 while (*(p + 1) != '"')
684 {
685 *p = *(p + 1);
686 p++;
687 }
688 *p = '\0';
689 }
690 }
691 }
692 else if (extension) /* Check win.ini */
693 {
694 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
695
696 /* Toss the leading dot */
697 extension++;
698 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
699 {
700 if (strlenW(command) != 0)
701 {
702 strcpyW(lpResult, command);
703 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
704
705 if (tok != NULL)
706 {
707 tok[0] = '\0';
708 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
709 tok = strchrW(command, '^'); /* see above */
710 if ((tok != NULL) && (strlenW(tok)>5))
711 {
712 strcatW(lpResult, &tok[5]);
713 }
714 }
715
716 retval = 33; /* FIXME - see above */
717 }
718 }
719 }
720
721 TRACE("returning %s\n", debugstr_w(lpResult));
722 return retval;
723 }
724
725 /******************************************************************
726 * dde_cb
727 *
728 * callback for the DDE connection. not really usefull
729 */
730 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
731 HSZ hsz1, HSZ hsz2,
732 HDDEDATA hData, DWORD dwData1, DWORD dwData2)
733 {
734 return NULL;
735 }
736
737 /******************************************************************
738 * dde_connect
739 *
740 * ShellExecute helper. Used to do an operation with a DDE connection
741 *
742 * Handles both the direct connection (try #1), and if it fails,
743 * launching an application and trying (#2) to connect to it
744 *
745 */
746 static unsigned dde_connect(WCHAR * key, WCHAR* start, WCHAR* ddeexec,
747 const WCHAR* lpFile, void *env,
748 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
749 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
750 {
751 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
752 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
753
754 WCHAR* endkey = key + strlenW(key);
755 WCHAR app[256], topic[256], ifexec[256];
756 WCHAR res[1024];
757 LONG applen, topiclen, ifexeclen;
758 WCHAR * exec;
759 DWORD ddeInst = 0;
760 DWORD tid;
761 HSZ hszApp, hszTopic;
762 HCONV hConv;
763 unsigned ret = 31;
764
765 strcpyW(endkey, wApplication);
766 applen = sizeof(app);
767 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
768 {
769 FIXME("default app name NIY %s\n", debugstr_w(key));
770 return 2;
771 }
772
773 strcpyW(endkey, wTopic);
774 topiclen = sizeof(topic);
775 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
776 {
777 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
778 strcpyW(topic, wSystem);
779 }
780
781 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
782 {
783 return 2;
784 }
785
786 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINANSI);
787 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINANSI);
788
789 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
790 exec = ddeexec;
791 if (!hConv)
792 {
793 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
794 TRACE("Launching '%s'\n", debugstr_w(start));
795 ret = execfunc(start, env, TRUE, psei, psei_out);
796 if (ret < 32)
797 {
798 TRACE("Couldn't launch\n");
799 goto error;
800 }
801 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
802 if (!hConv)
803 {
804 TRACE("Couldn't connect. ret=%d\n", ret);
805 DdeUninitialize(ddeInst);
806 SetLastError(ERROR_DDE_FAIL);
807 return 30; /* whatever */
808 }
809 strcpyW(endkey, wIfexec);
810 ifexeclen = sizeof(ifexec);
811 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
812 {
813 exec = ifexec;
814 }
815 }
816
817 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline);
818 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
819
820 ret = (DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
821 XTYP_EXECUTE, 10000, &tid) != DMLERR_NO_ERROR) ? 31 : 33;
822 DdeDisconnect(hConv);
823
824 error:
825 DdeUninitialize(ddeInst);
826
827 return ret;
828 }
829
830 /*************************************************************************
831 * execute_from_key [Internal]
832 */
833 static UINT execute_from_key(LPWSTR key, LPCWSTR lpFile, void *env, LPCWSTR szCommandline,
834 SHELL_ExecuteW32 execfunc,
835 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
836 {
837 WCHAR cmd[1024];
838 LONG cmdlen = sizeof(cmd);
839 UINT retval = 31;
840
841 cmd[0] = '\0';
842
843 /* Get the application for the registry */
844 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
845 {
846 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
847 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
848
849 LPWSTR tmp;
850 WCHAR param[256];
851 LONG paramlen = sizeof(param);
852
853 param[0] = '\0';
854
855 /* Get the parameters needed by the application
856 from the associated ddeexec key */
857 tmp = strstrW(key, wCommand);
858 assert(tmp);
859 strcpyW(tmp, wDdeexec);
860
861 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, param, &paramlen) == ERROR_SUCCESS)
862 {
863 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(param));
864 retval = dde_connect(key, cmd, param, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
865 }
866 else
867 {
868 /* Is there a replace() function anywhere? */
869 cmdlen /= sizeof(WCHAR);
870 cmd[cmdlen] = '\0';
871 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline);
872 retval = execfunc(param, env, FALSE, psei, psei_out);
873 }
874 }
875 else TRACE("ooch\n");
876
877 return retval;
878 }
879
880 /*************************************************************************
881 * FindExecutableA [SHELL32.@]
882 */
883 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
884 {
885 HINSTANCE retval;
886 WCHAR *wFile = NULL, *wDirectory = NULL;
887 WCHAR wResult[MAX_PATH];
888
889 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
890 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
891
892 retval = FindExecutableW(wFile, wDirectory, wResult);
893 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
894 if (wFile) SHFree( wFile );
895 if (wDirectory) SHFree( wDirectory );
896
897 TRACE("returning %s\n", lpResult);
898 return (HINSTANCE)retval;
899 }
900
901 /*************************************************************************
902 * FindExecutableW [SHELL32.@]
903 */
904 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
905 {
906 UINT retval = 31; /* default - 'No association was found' */
907 WCHAR old_dir[1024];
908
909 TRACE("File %s, Dir %s\n",
910 (lpFile != NULL ? debugstr_w(lpFile) : "-"), (lpDirectory != NULL ? debugstr_w(lpDirectory) : "-"));
911
912 lpResult[0] = '\0'; /* Start off with an empty return string */
913
914 /* trap NULL parameters on entry */
915 if ((lpFile == NULL) || (lpResult == NULL))
916 {
917 /* FIXME - should throw a warning, perhaps! */
918 return (HINSTANCE)2; /* File not found. Close enough, I guess. */
919 }
920
921 if (lpDirectory)
922 {
923 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
924 SetCurrentDirectoryW(lpDirectory);
925 }
926
927 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
928
929 TRACE("returning %s\n", debugstr_w(lpResult));
930 if (lpDirectory)
931 SetCurrentDirectoryW(old_dir);
932 return (HINSTANCE)retval;
933 }
934
935 /*************************************************************************
936 * ShellExecuteExW32 [Internal]
937 */
938 BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfunc)
939 {
940 static const WCHAR wQuote[] = {'"',0};
941 static const WCHAR wSpace[] = {' ',0};
942 static const WCHAR wWww[] = {'w','w','w',0};
943 static const WCHAR wFile[] = {'f','i','l','e',0};
944 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
945 static const WCHAR wExtLnk[] = {'.','l','n','k',0};
946 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
947
948 WCHAR wszApplicationName[MAX_PATH+2], wszParameters[1024], wszDir[MAX_PATH];
949 SHELLEXECUTEINFOW sei_tmp; /* modifyable copy of SHELLEXECUTEINFO struct */
950 WCHAR wfileName[MAX_PATH];
951 void *env;
952 WCHAR wszProtocol[256];
953 LPCWSTR lpFile;
954 UINT retval = 31;
955 WCHAR buffer[1024];
956 const WCHAR* ext;
957
958 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
959 memcpy(&sei_tmp, psei, sizeof(sei_tmp));
960
961 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
962 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
963 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
964 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
965 (sei_tmp.fMask & SEE_MASK_CLASSNAME) ? debugstr_w(sei_tmp.lpClass) : "not used");
966
967 psei->hProcess = NULL;
968
969 /* make copies of all path/command strings */
970 if (sei_tmp.lpFile)
971 strcpyW(wszApplicationName, sei_tmp.lpFile);
972 else
973 *wszApplicationName = '\0';
974
975 if (sei_tmp.lpParameters)
976 strcpyW(wszParameters, sei_tmp.lpParameters);
977 else
978 *wszParameters = '\0';
979
980 if (sei_tmp.lpDirectory)
981 strcpyW(wszDir, sei_tmp.lpDirectory);
982 else
983 *wszDir = '\0';
984
985 /* adjust string pointers to point to the new buffers */
986 sei_tmp.lpFile = wszApplicationName;
987 sei_tmp.lpParameters = wszParameters;
988 sei_tmp.lpDirectory = wszDir;
989
990 if (sei_tmp.fMask & (SEE_MASK_ICON | SEE_MASK_HOTKEY |
991 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
992 SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE |
993 SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR ))
994 {
995 FIXME("flags ignored: 0x%08lx\n", sei_tmp.fMask);
996 }
997
998 /* process the IDList */
999 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1000 {
1001 IShellExecuteHookW* pSEH;
1002
1003 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1004
1005 if (SUCCEEDED(hr))
1006 {
1007 hr = IShellExecuteHookW_Execute(pSEH, psei);
1008
1009 IShellExecuteHookW_Release(pSEH);
1010
1011 if (hr == S_OK)
1012 return TRUE;
1013 }
1014
1015 /* try to translate PIDL directly into the corresponding file system path */
1016 if (SUCCEEDED(SHELL_GetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName/*sei_tmp.lpFile*/, sizeof(wszApplicationName)/sizeof(WCHAR))))
1017 {
1018 sei_tmp.lpIDList = NULL;
1019 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1020 }
1021
1022 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1023 }
1024
1025 if (sei_tmp.fMask & (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY))
1026 {
1027 /* launch a document by fileclass like 'WordPad.Document.1' */
1028 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1029 /* FIXME: wszParameters should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1030 HCR_GetExecuteCommandW((sei_tmp.fMask & SEE_MASK_CLASSKEY)? sei_tmp.hkeyClass: NULL,
1031 (sei_tmp.fMask & SEE_MASK_CLASSNAME)? sei_tmp.lpClass: NULL,
1032 (sei_tmp.lpVerb)? sei_tmp.lpVerb: wszOpen,
1033 wszParameters/*sei_tmp.lpParameters*/, sizeof(wszParameters)/sizeof(WCHAR));
1034
1035 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1036 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(sei_tmp.lpParameters), debugstr_w(sei_tmp.lpFile));
1037
1038 buffer[0] = '\0';
1039
1040 if (!SHELL_ArgifyW(buffer, sizeof(buffer)/sizeof(WCHAR), sei_tmp.lpParameters, sei_tmp.lpFile, sei_tmp.lpIDList, NULL) && sei_tmp.lpFile[0])
1041 {
1042 strcatW(buffer, wExtLnk);
1043 strcatW(buffer, sei_tmp.lpFile);
1044 }
1045
1046 retval = execfunc(buffer, NULL, FALSE, &sei_tmp, psei);
1047 if (retval > 32)
1048 return TRUE;
1049 else
1050 return FALSE;
1051 }
1052
1053
1054 /* resolve shell shortcuts */
1055 ext = PathFindExtensionW(sei_tmp.lpFile);
1056
1057 if (ext && !strcmpiW(ext, wExtLnk)) /* or check for: shell_attribs & SFGAO_LINK */
1058 {
1059 HRESULT hr;
1060
1061 /* expand paths before reading shell link */
1062 if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
1063 lstrcpyW(wszApplicationName/*sei_tmp.lpFile*/, buffer);
1064
1065 if (*sei_tmp.lpParameters)
1066 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1067 lstrcpyW(wszParameters/*sei_tmp.lpParameters*/, buffer);
1068
1069 hr = SHELL_ResolveShortCutW((LPWSTR)sei_tmp.lpFile, (LPWSTR)sei_tmp.lpParameters, (LPWSTR)sei_tmp.lpDirectory,
1070 sei_tmp.hwnd, sei_tmp.lpVerb?sei_tmp.lpVerb:wszEmpty, &sei_tmp.nShow, (LPITEMIDLIST*)&sei_tmp.lpIDList);
1071
1072 if (psei->lpIDList)
1073 psei->fMask |= SEE_MASK_IDLIST; //@@ nicht sei_tmp.fMask ?!
1074
1075 if (SUCCEEDED(hr))
1076 {
1077 /* repeat IDList processing if needed */
1078 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1079 {
1080 IShellExecuteHookW* pSEH;
1081
1082 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1083
1084 if (SUCCEEDED(hr))
1085 {
1086 hr = IShellExecuteHookW_Execute(pSEH, psei);
1087
1088 IShellExecuteHookW_Release(pSEH);
1089
1090 if (hr == S_OK)
1091 return TRUE;
1092 }
1093
1094 TRACE("-- idlist=%p (%s)\n", debugstr_w(sei_tmp.lpIDList), debugstr_w(sei_tmp.lpFile));
1095 }
1096 }
1097 }
1098
1099
1100 /* Has the IDList not yet been translated? */
1101 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1102 {
1103 /* last chance to translate IDList: now also allow CLSID paths */
1104 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp.lpIDList, buffer, sizeof(buffer)))) {
1105 if (buffer[0]==':' && buffer[1]==':') {
1106 /* open shell folder for the specified class GUID */
1107 strcpyW(wszParameters, buffer);
1108 strcpyW(wszApplicationName, wExplorer);
1109
1110 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1111 } else if (HCR_GetExecuteCommandW(0, wszFolder, sei_tmp.lpVerb?sei_tmp.lpVerb:wszOpen, buffer, sizeof(buffer))) {
1112 SHELL_ArgifyW(wszApplicationName, sizeof(wszApplicationName)/sizeof(WCHAR), buffer, NULL, sei_tmp.lpIDList, NULL);
1113
1114 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1115 }
1116 }
1117 }
1118
1119
1120 /* expand environment strings */
1121
1122 if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
1123 lstrcpyW(wszApplicationName/*sei_tmp.lpFile*/, buffer);
1124
1125 if (*sei_tmp.lpParameters)
1126 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1127 lstrcpyW(wszParameters/*sei_tmp.lpParameters*/, buffer);
1128
1129 if (*sei_tmp.lpDirectory)
1130 if (ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buffer, MAX_PATH))
1131 lstrcpyW(wszDir/*sei_tmp.lpDirectory*/, buffer);
1132
1133 /* Else, try to execute the filename */
1134 TRACE("execute:'%s','%s','%s'\n", debugstr_w(wszApplicationName), debugstr_w(wszCommandline), debugstr_w(wszDir));
1135
1136
1137 /* separate out command line arguments from executable file name */
1138 if (!*sei_tmp.lpParameters) {
1139 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1140 if (sei_tmp.lpFile[0] == '"') {
1141 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1142 LPWSTR dst = wfileName;
1143 LPWSTR end;
1144
1145 /* copy the unquoted executabe path to 'wfileName' */
1146 while(*src && *src!='"')
1147 *dst++ = *src++;
1148
1149 *dst = '\0';
1150
1151 if (*src == '"') {
1152 end = ++src;
1153
1154 while(isspace(*src))
1155 ++src;
1156 } else
1157 end = src;
1158
1159 /* copy the parameter string to 'wszParameters' */
1160 strcpyW(wszParameters, src);
1161
1162 /* terminate previous command string after the quote character */
1163 *end = '\0';
1164 }
1165 else
1166 {
1167 /* If the executable name is not quoted, we have to use this search loop here,
1168 that in CreateProcess() is not sufficient because it does not handle shell links. */
1169 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1170 LPWSTR space, s;
1171
1172 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1173 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1174 int idx = space-sei_tmp.lpFile;
1175 strncpyW(buffer, sei_tmp.lpFile, idx);
1176 buffer[idx] = '\0';
1177
1178 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1179 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile), xlpFile, NULL))
1180 {
1181 /* separate out command from parameter string */
1182 LPCWSTR p = space + 1;
1183
1184 while(isspaceW(*p))
1185 ++p;
1186
1187 strcpyW(wszParameters, p);
1188 *space = '\0';
1189
1190 break;
1191 }
1192 }
1193
1194 strcpyW(wfileName, sei_tmp.lpFile);
1195 }
1196 } else
1197 strcpyW(wfileName, sei_tmp.lpFile);
1198
1199 lpFile = wfileName;
1200
1201 if (sei_tmp.lpParameters[0]) {
1202 strcatW(wszApplicationName/*sei_tmp.lpFile*/, wSpace);
1203 strcatW(wszApplicationName/*sei_tmp.lpFile*/, sei_tmp.lpParameters);
1204 }
1205
1206 retval = execfunc(sei_tmp.lpFile, NULL, FALSE, &sei_tmp, psei);
1207 if (retval > 32)
1208 {
1209 /* Now, that we have successfully launched a process, we can free the PIDL.
1210 It may have been used before for %I command line options. */
1211 if (sei_tmp.lpIDList!=psei->lpIDList && sei_tmp.lpIDList)
1212 SHFree(sei_tmp.lpIDList);
1213
1214 TRACE("execfunc: retval=%d psei->hInstApp=%p\n", retval, psei->hInstApp);
1215 return TRUE;
1216 }
1217
1218 /* Else, try to find the executable */
1219 buffer[0] = '\0';
1220 retval = SHELL_FindExecutable(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, lpFile, sei_tmp.lpVerb, buffer, 1024, wszProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1221
1222 if (retval > 32) /* Found */
1223 {
1224 WCHAR wszQuotedCmd[MAX_PATH+2];
1225 /* Must quote to handle case where 'buffer' contains spaces,
1226 * else security hole if malicious user creates executable file "C:\\Program"
1227 *
1228 * FIXME: If we don't have set explicitly command line arguments, we must first
1229 * split executable path from optional command line arguments. Otherwise we would quote
1230 * the complete string with executable path _and_ arguments, which is not what we want.
1231 */
1232 strcpyW(wszQuotedCmd, wQuote);
1233 strcatW(wszQuotedCmd, buffer);
1234 strcatW(wszQuotedCmd, wQuote);
1235
1236 if (*sei_tmp.lpParameters) {
1237 strcatW(wszQuotedCmd, wSpace);
1238 strcatW(wszQuotedCmd, sei_tmp.lpParameters);
1239 }
1240
1241 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(buffer), debugstr_w(wszQuotedCmd), debugstr_w(wszProtocol));
1242
1243 if (*wszProtocol)
1244 retval = execute_from_key(wszProtocol, lpFile, env, sei_tmp.lpParameters, execfunc, &sei_tmp, psei);
1245 else
1246 retval = execfunc(wszQuotedCmd, env, FALSE, &sei_tmp, psei);
1247
1248 if (env) HeapFree( GetProcessHeap(), 0, env );
1249 }
1250 else if (PathIsURLW((LPWSTR)lpFile)) /* File not found, check for URL */
1251 {
1252 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
1253 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1254 LPWSTR lpstrRes;
1255 INT iSize;
1256
1257 lpstrRes = strchrW(lpFile, ':');
1258 if (lpstrRes)
1259 iSize = lpstrRes - lpFile;
1260 else
1261 iSize = strlenW(lpFile);
1262
1263 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1264 /* Looking for ...protocol\shell\<verb>\command */
1265 strncpyW(wszProtocol, lpFile, iSize);
1266 wszProtocol[iSize] = '\0';
1267 strcatW(wszProtocol, wszShell);
1268 strcatW(wszProtocol, sei_tmp.lpVerb? sei_tmp.lpVerb: wszOpen);
1269 strcatW(wszProtocol, wCommand);
1270
1271 /* Remove File Protocol from lpFile */
1272 /* In the case file://path/file */
1273 if (!strncmpiW(lpFile, wFile, iSize))
1274 {
1275 lpFile += iSize;
1276 while (*lpFile == ':') lpFile++;
1277 }
1278
1279 retval = execute_from_key(wszProtocol, lpFile, NULL, sei_tmp.lpParameters, execfunc, &sei_tmp, psei);
1280 }
1281 /* Check if file specified is in the form www.??????.*** */
1282 else if (!strncmpiW(lpFile, wWww, 3))
1283 {
1284 /* if so, append lpFile http:// and call ShellExecute */
1285 WCHAR lpstrTmpFile[256];
1286 strcpyW(lpstrTmpFile, wHttp);
1287 strcatW(lpstrTmpFile, lpFile);
1288 retval = (UINT)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1289 }
1290
1291 /* Now we can free the PIDL. It may have been used before for %I command line options. */
1292 if (sei_tmp.lpIDList!=psei->lpIDList && sei_tmp.lpIDList)
1293 SHFree(sei_tmp.lpIDList);
1294
1295 TRACE("retval %u\n", retval);
1296
1297 if (retval <= 32)
1298 {
1299 psei->hInstApp = (HINSTANCE)retval;
1300 return FALSE;
1301 }
1302
1303 psei->hInstApp = (HINSTANCE)33;
1304 return TRUE;
1305 }
1306
1307 /*************************************************************************
1308 * ShellExecuteA [SHELL32.290]
1309 */
1310 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile,
1311 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
1312 {
1313 SHELLEXECUTEINFOA sei;
1314 HANDLE hProcess = 0;
1315
1316 TRACE("\n");
1317 sei.cbSize = sizeof(sei);
1318 sei.fMask = 0;
1319 sei.hwnd = hWnd;
1320 sei.lpVerb = lpOperation;
1321 sei.lpFile = lpFile;
1322 sei.lpParameters = lpParameters;
1323 sei.lpDirectory = lpDirectory;
1324 sei.nShow = iShowCmd;
1325 sei.lpIDList = 0;
1326 sei.lpClass = 0;
1327 sei.hkeyClass = 0;
1328 sei.dwHotKey = 0;
1329 sei.hProcess = hProcess;
1330
1331 ShellExecuteExA (&sei);
1332 return sei.hInstApp;
1333 }
1334
1335 /*************************************************************************
1336 * ShellExecuteEx [SHELL32.291]
1337 *
1338 */
1339 BOOL WINAPI ShellExecuteExAW (LPVOID sei)
1340 {
1341 if (SHELL_OsIsUnicode())
1342 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1343 return ShellExecuteExA (sei);
1344 }
1345
1346 /*************************************************************************
1347 * ShellExecuteExA [SHELL32.292]
1348 *
1349 */
1350 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1351 {
1352 SHELLEXECUTEINFOW seiW;
1353 BOOL ret;
1354 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1355
1356 TRACE("%p\n", sei);
1357
1358 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1359
1360 if (sei->lpVerb)
1361 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1362
1363 if (sei->lpFile)
1364 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1365
1366 if (sei->lpParameters)
1367 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1368
1369 if (sei->lpDirectory)
1370 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1371
1372 if ((sei->fMask & SEE_MASK_CLASSNAME) && sei->lpClass)
1373 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1374 else
1375 seiW.lpClass = NULL;
1376
1377 ret = ShellExecuteExW32 (&seiW, SHELL_ExecuteW);
1378
1379 sei->hInstApp = seiW.hInstApp;
1380
1381 if (wVerb) SHFree(wVerb);
1382 if (wFile) SHFree(wFile);
1383 if (wParameters) SHFree(wParameters);
1384 if (wDirectory) SHFree(wDirectory);
1385 if (wClass) SHFree(wClass);
1386
1387 return ret;
1388 }
1389
1390 /*************************************************************************
1391 * ShellExecuteExW [SHELL32.293]
1392 *
1393 */
1394 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1395 {
1396 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1397 }
1398
1399 /*************************************************************************
1400 * ShellExecuteW [SHELL32.294]
1401 * from shellapi.h
1402 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1403 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1404 */
1405 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1406 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1407 {
1408 SHELLEXECUTEINFOW sei;
1409
1410 TRACE("\n");
1411 sei.cbSize = sizeof(sei);
1412 sei.fMask = 0;
1413 sei.hwnd = hwnd;
1414 sei.lpVerb = lpOperation;
1415 sei.lpFile = lpFile;
1416 sei.lpParameters = lpParameters;
1417 sei.lpDirectory = lpDirectory;
1418 sei.nShow = nShowCmd;
1419 sei.lpIDList = 0;
1420 sei.lpClass = 0;
1421 sei.hkeyClass = 0;
1422 sei.dwHotKey = 0;
1423 sei.hProcess = 0;
1424
1425 ShellExecuteExW32 (&sei, SHELL_ExecuteW);
1426 return sei.hInstApp;
1427 }