Sync up with trunk r61578.
[reactos.git] / dll / win32 / shell32 / shlexec.cpp
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "precomp.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(exec);
25
26 static const WCHAR wszOpen[] = L"open";
27 static const WCHAR wszExe[] = L".exe";
28
29 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
30
31 static void ParseNoTildeEffect(PWSTR &res, LPCWSTR &args, DWORD &len, DWORD &used, int argNum)
32 {
33 bool firstCharQuote = false;
34 bool quotes_opened = false;
35 bool backslash_encountered = false;
36
37 for (int curArg = 0; curArg <= argNum && *args; ++curArg)
38 {
39 firstCharQuote = false;
40 if (*args == '"')
41 {
42 quotes_opened = true;
43 firstCharQuote = true;
44 args++;
45 }
46
47 while(*args)
48 {
49 if (*args == '\\')
50 {
51 // if we found a backslash then flip the variable
52 backslash_encountered = !backslash_encountered;
53 }
54 else if (*args == '"')
55 {
56 if (quotes_opened)
57 {
58 if (*(args + 1) != '"')
59 {
60 quotes_opened = false;
61 args++;
62 break;
63 }
64 else
65 {
66 args++;
67 }
68 }
69 else
70 {
71 quotes_opened = true;
72 }
73
74 backslash_encountered = false;
75 }
76 else
77 {
78 backslash_encountered = false;
79 if (*args == ' ' && !firstCharQuote)
80 break;
81 }
82
83 if (curArg == argNum)
84 {
85 used++;
86 if (used < len)
87 *res++ = *args;
88 }
89
90 args++;
91 }
92
93 while(*args == ' ')
94 ++args;
95 }
96 }
97
98 static void ParseTildeEffect(PWSTR &res, LPCWSTR &args, DWORD &len, DWORD &used, int argNum)
99 {
100 bool quotes_opened = false;
101 bool backslash_encountered = false;
102
103 for (int curArg = 0; curArg <= argNum && *args; ++curArg)
104 {
105 while(*args)
106 {
107 if (*args == '\\')
108 {
109 // if we found a backslash then flip the variable
110 backslash_encountered = !backslash_encountered;
111 }
112 else if (*args == '"')
113 {
114 if (quotes_opened)
115 {
116 if (*(args + 1) != '"')
117 {
118 quotes_opened = false;
119 }
120 else
121 {
122 args++;
123 }
124 }
125 else
126 {
127 quotes_opened = true;
128 }
129
130 backslash_encountered = false;
131 }
132 else
133 {
134 backslash_encountered = false;
135 if (*args == ' ' && !quotes_opened && curArg != argNum)
136 break;
137 }
138
139 if (curArg == argNum)
140 {
141 used++;
142 if (used < len)
143 *res++ = *args;
144 }
145
146 args++;
147 }
148 }
149 }
150
151 /***********************************************************************
152 * SHELL_ArgifyW [Internal]
153 *
154 * this function is supposed to expand the escape sequences found in the registry
155 * some diving reported that the following were used:
156 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
157 * %1 file
158 * %2 printer
159 * %3 driver
160 * %4 port
161 * %I address of a global item ID (explorer switch /idlist)
162 * %L seems to be %1 as long filename followed by the 8+3 variation
163 * %S ???
164 * %* all following parameters (see batfile)
165 *
166 * The way we parse the command line arguments was determined through extensive
167 * testing and can be summed up by the following rules"
168 *
169 * - %2
170 * - if first letter is " break on first non literal " and include any white spaces
171 * - if first letter is NOT " break on first " or white space
172 * - if " is opened any pair of consecutive " results in ONE literal "
173 *
174 * - %~2
175 * - use rules from here http://www.autohotkey.net/~deleyd/parameters/parameters.htm
176 */
177
178 static BOOL SHELL_ArgifyW(WCHAR* out, DWORD len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args, DWORD* out_len)
179 {
180 WCHAR xlpFile[1024];
181 BOOL done = FALSE;
182 BOOL found_p1 = FALSE;
183 PWSTR res = out;
184 PCWSTR cmd;
185 DWORD used = 0;
186 bool tildeEffect = false;
187
188 TRACE("Before parsing: %p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
189 debugstr_w(lpFile), pidl, args);
190
191 while (*fmt)
192 {
193 if (*fmt == '%')
194 {
195 switch (*++fmt)
196 {
197 case '\0':
198 case '%':
199 {
200 used++;
201 if (used < len)
202 *res++ = '%';
203 };
204 break;
205
206 case '*':
207 {
208 if (args)
209 {
210 if (*fmt == '*')
211 {
212 used++;
213 while(*args)
214 {
215 used++;
216 if (used < len)
217 *res++ = *args++;
218 else
219 args++;
220 }
221 used++;
222 break;
223 }
224 }
225 };
226 break;
227
228 case '~':
229
230 case '2':
231 case '3':
232 case '4':
233 case '5':
234 case '6':
235 case '7':
236 case '8':
237 case '9':
238 //case '0':
239 {
240 if (*fmt == '~')
241 {
242 fmt++;
243 tildeEffect = true;
244 }
245
246 if (args)
247 {
248 if (tildeEffect)
249 {
250 ParseTildeEffect(res, args, len, used, *fmt - '2');
251 tildeEffect = false;
252 }
253 else
254 {
255 ParseNoTildeEffect(res, args, len, used, *fmt - '2');
256 }
257 }
258 };
259 break;
260
261 case '1':
262 if (!done || (*fmt == '1'))
263 {
264 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
265 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile) / sizeof(WCHAR), xlpFile, NULL))
266 cmd = xlpFile;
267 else
268 cmd = lpFile;
269
270 used += wcslen(cmd);
271 if (used < len)
272 {
273 wcscpy(res, cmd);
274 res += wcslen(cmd);
275 }
276 }
277 found_p1 = TRUE;
278 break;
279
280 /*
281 * IE uses this a lot for activating things such as windows media
282 * player. This is not verified to be fully correct but it appears
283 * to work just fine.
284 */
285 case 'l':
286 case 'L':
287 if (lpFile)
288 {
289 used += wcslen(lpFile);
290 if (used < len)
291 {
292 wcscpy(res, lpFile);
293 res += wcslen(lpFile);
294 }
295 }
296 found_p1 = TRUE;
297 break;
298
299 case 'i':
300 case 'I':
301 if (pidl)
302 {
303 DWORD chars = 0;
304 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
305 * allowing a buffer of 100 should more than exceed all needs */
306 WCHAR buf[100];
307 LPVOID pv;
308 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
309 pv = SHLockShared(hmem, 0);
310 chars = swprintf(buf, L":%p", pv);
311
312 if (chars >= sizeof(buf) / sizeof(WCHAR))
313 ERR("pidl format buffer too small!\n");
314
315 used += chars;
316
317 if (used < len)
318 {
319 wcscpy(res, buf);
320 res += chars;
321 }
322 SHUnlockShared(pv);
323 }
324 found_p1 = TRUE;
325 break;
326
327 default:
328 /*
329 * Check if this is an env-variable here...
330 */
331
332 /* Make sure that we have at least one more %.*/
333 if (strchrW(fmt, '%'))
334 {
335 WCHAR tmpBuffer[1024];
336 PWSTR tmpB = tmpBuffer;
337 WCHAR tmpEnvBuff[MAX_PATH];
338 DWORD envRet;
339
340 while (*fmt != '%')
341 *tmpB++ = *fmt++;
342 *tmpB++ = 0;
343
344 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
345
346 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
347 if (envRet == 0 || envRet > MAX_PATH)
348 {
349 used += wcslen(tmpBuffer);
350 if (used < len)
351 {
352 wcscpy( res, tmpBuffer );
353 res += wcslen(tmpBuffer);
354 }
355 }
356 else
357 {
358 used += wcslen(tmpEnvBuff);
359 if (used < len)
360 {
361 wcscpy( res, tmpEnvBuff );
362 res += wcslen(tmpEnvBuff);
363 }
364 }
365 }
366 done = TRUE;
367 break;
368 }
369 /* Don't skip past terminator (catch a single '%' at the end) */
370 if (*fmt != '\0')
371 {
372 fmt++;
373 }
374 }
375 else
376 {
377 used ++;
378 if (used < len)
379 *res++ = *fmt++;
380 else
381 fmt++;
382 }
383 }
384
385 *res = '\0';
386 TRACE("used %i of %i space\n", used, len);
387 if (out_len)
388 *out_len = used;
389
390 TRACE("After parsing: %p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
391 debugstr_w(lpFile), pidl, args);
392
393 return found_p1;
394 }
395
396 static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
397 {
398 STRRET strret;
399 IShellFolder *desktop;
400
401 HRESULT hr = SHGetDesktopFolder(&desktop);
402
403 if (SUCCEEDED(hr))
404 {
405 hr = desktop->GetDisplayNameOf(pidl, SHGDN_FORPARSING, &strret);
406
407 if (SUCCEEDED(hr))
408 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
409
410 desktop->Release();
411 }
412
413 return hr;
414 }
415
416 /*************************************************************************
417 * SHELL_ExecuteW [Internal]
418 *
419 */
420 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
421 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
422 {
423 STARTUPINFOW startup;
424 PROCESS_INFORMATION info;
425 UINT_PTR retval = SE_ERR_NOASSOC;
426 UINT gcdret = 0;
427 WCHAR curdir[MAX_PATH];
428 DWORD dwCreationFlags;
429 const WCHAR *lpDirectory = NULL;
430
431 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
432
433 /* make sure we don't fail the CreateProcess if the calling app passes in
434 * a bad working directory */
435 if (psei->lpDirectory && psei->lpDirectory[0])
436 {
437 DWORD attr = GetFileAttributesW(psei->lpDirectory);
438 if (attr != INVALID_FILE_ATTRIBUTES && attr & FILE_ATTRIBUTE_DIRECTORY)
439 lpDirectory = psei->lpDirectory;
440 }
441
442 /* ShellExecute specifies the command from psei->lpDirectory
443 * if present. Not from the current dir as CreateProcess does */
444 if (lpDirectory)
445 if ((gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
446 if (!SetCurrentDirectoryW( lpDirectory))
447 ERR("cannot set directory %s\n", debugstr_w(lpDirectory));
448
449 ZeroMemory(&startup, sizeof(STARTUPINFOW));
450 startup.cb = sizeof(STARTUPINFOW);
451 startup.dwFlags = STARTF_USESHOWWINDOW;
452 startup.wShowWindow = psei->nShow;
453 startup.lpTitle = (LPWSTR)(psei->fMask & (SEE_MASK_HASLINKNAME | SEE_MASK_HASTITLE) ? psei->lpClass : NULL);
454
455 if (psei->fMask & SEE_MASK_HASLINKNAME)
456 startup.dwFlags |= STARTF_TITLEISLINKNAME;
457
458 dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
459
460 if (psei->fMask & SEE_MASK_NO_CONSOLE)
461 dwCreationFlags |= CREATE_NEW_CONSOLE;
462
463 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
464 lpDirectory, &startup, &info))
465 {
466 /* Give 30 seconds to the app to come up, if desired. Probably only needed
467 when starting app immediately before making a DDE connection. */
468 if (shWait)
469 if (WaitForInputIdle(info.hProcess, 30000) == WAIT_FAILED)
470 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
471 retval = 33;
472
473 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
474 psei_out->hProcess = info.hProcess;
475 else
476 CloseHandle( info.hProcess );
477 CloseHandle( info.hThread );
478 }
479 else if ((retval = GetLastError()) >= 32)
480 {
481 WARN("CreateProcess returned error %ld\n", retval);
482 retval = ERROR_BAD_FORMAT;
483 }
484
485 TRACE("returning %lu\n", retval);
486
487 psei_out->hInstApp = (HINSTANCE)retval;
488
489 if (gcdret)
490 if (!SetCurrentDirectoryW(curdir))
491 ERR("cannot return to directory %s\n", debugstr_w(curdir));
492
493 return retval;
494 }
495
496
497 /***********************************************************************
498 * SHELL_BuildEnvW [Internal]
499 *
500 * Build the environment for the new process, adding the specified
501 * path to the PATH variable. Returned pointer must be freed by caller.
502 */
503 static LPWSTR SHELL_BuildEnvW( const WCHAR *path )
504 {
505 static const WCHAR wPath[] = L"PATH=";
506 WCHAR *strings, *new_env;
507 WCHAR *p, *p2;
508 int total = wcslen(path) + 1;
509 BOOL got_path = FALSE;
510
511 if (!(strings = GetEnvironmentStringsW())) return NULL;
512 p = strings;
513 while (*p)
514 {
515 int len = wcslen(p) + 1;
516 if (!_wcsnicmp( p, wPath, 5 )) got_path = TRUE;
517 total += len;
518 p += len;
519 }
520 if (!got_path) total += 5; /* we need to create PATH */
521 total++; /* terminating null */
522
523 if (!(new_env = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, total * sizeof(WCHAR))))
524 {
525 FreeEnvironmentStringsW(strings);
526 return NULL;
527 }
528 p = strings;
529 p2 = new_env;
530 while (*p)
531 {
532 int len = wcslen(p) + 1;
533 memcpy(p2, p, len * sizeof(WCHAR));
534 if (!_wcsnicmp( p, wPath, 5 ))
535 {
536 p2[len - 1] = ';';
537 wcscpy( p2 + len, path );
538 p2 += wcslen(path) + 1;
539 }
540 p += len;
541 p2 += len;
542 }
543 if (!got_path)
544 {
545 wcscpy(p2, wPath);
546 wcscat(p2, path);
547 p2 += wcslen(p2) + 1;
548 }
549 *p2 = 0;
550 FreeEnvironmentStringsW(strings);
551 return new_env;
552 }
553
554
555 /***********************************************************************
556 * SHELL_TryAppPathW [Internal]
557 *
558 * Helper function for SHELL_FindExecutable
559 * @param lpResult - pointer to a buffer of size MAX_PATH
560 * On entry: szName is a filename (probably without path separators).
561 * On exit: if szName found in "App Path", place full path in lpResult, and return true
562 */
563 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
564 {
565 HKEY hkApp = 0;
566 WCHAR buffer[1024];
567 LONG len;
568 LONG res;
569 BOOL found = FALSE;
570
571 if (env) *env = NULL;
572 wcscpy(buffer, L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
573 wcscat(buffer, szName);
574 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
575 if (res) goto end;
576
577 len = MAX_PATH * sizeof(WCHAR);
578 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
579 if (res) goto end;
580 found = TRUE;
581
582 if (env)
583 {
584 DWORD count = sizeof(buffer);
585 if (!RegQueryValueExW(hkApp, L"Path", NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
586 *env = SHELL_BuildEnvW(buffer);
587 }
588
589 end:
590 if (hkApp) RegCloseKey(hkApp);
591 return found;
592 }
593
594 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
595 {
596 static const WCHAR wCommand[] = L"\\command";
597 HKEY hkeyClass;
598 WCHAR verb[MAX_PATH];
599
600 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, filetype, 0, 0x02000000, &hkeyClass))
601 return SE_ERR_NOASSOC;
602 if (!HCR_GetDefaultVerbW(hkeyClass, lpOperation, verb, sizeof(verb) / sizeof(verb[0])))
603 return SE_ERR_NOASSOC;
604 RegCloseKey(hkeyClass);
605
606 /* Looking for ...buffer\shell\<verb>\command */
607 wcscat(filetype, L"\\shell\\");
608 wcscat(filetype, verb);
609 wcscat(filetype, wCommand);
610
611 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
612 &commandlen) == ERROR_SUCCESS)
613 {
614 commandlen /= sizeof(WCHAR);
615 if (key) wcscpy(key, filetype);
616 #if 0
617 LPWSTR tmp;
618 WCHAR param[256];
619 LONG paramlen = sizeof(param);
620 static const WCHAR wSpace[] = {' ', 0};
621
622 /* FIXME: it seems all Windows version don't behave the same here.
623 * the doc states that this ddeexec information can be found after
624 * the exec names.
625 * on Win98, it doesn't appear, but I think it does on Win2k
626 */
627 /* Get the parameters needed by the application
628 from the associated ddeexec key */
629 tmp = strstrW(filetype, wCommand);
630 tmp[0] = '\0';
631 wcscat(filetype, wDdeexec);
632 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
633 &paramlen) == ERROR_SUCCESS)
634 {
635 paramlen /= sizeof(WCHAR);
636 wcscat(command, wSpace);
637 wcscat(command, param);
638 commandlen += paramlen;
639 }
640 #endif
641
642 command[commandlen] = '\0';
643
644 return 33; /* FIXME see SHELL_FindExecutable() */
645 }
646
647 return SE_ERR_NOASSOC;
648 }
649
650 /*************************************************************************
651 * SHELL_FindExecutable [Internal]
652 *
653 * Utility for code sharing between FindExecutable and ShellExecute
654 * in:
655 * lpFile the name of a file
656 * lpOperation the operation on it (open)
657 * out:
658 * lpResult a buffer, big enough :-(, to store the command to do the
659 * operation on the file
660 * key a buffer, big enough, to get the key name to do actually the
661 * command (it'll be used afterwards for more information
662 * on the operation)
663 */
664 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
665 LPWSTR lpResult, DWORD resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
666 {
667 WCHAR *extension = NULL; /* pointer to file extension */
668 WCHAR filetype[256]; /* registry name for this filetype */
669 LONG filetypelen = sizeof(filetype); /* length of above */
670 WCHAR command[1024]; /* command from registry */
671 WCHAR wBuffer[256]; /* Used to GetProfileString */
672 UINT retval = SE_ERR_NOASSOC;
673 WCHAR *tok; /* token pointer */
674 WCHAR xlpFile[256]; /* result of SearchPath */
675 DWORD attribs; /* file attributes */
676
677 TRACE("%s\n", debugstr_w(lpFile));
678
679 if (!lpResult)
680 return ERROR_INVALID_PARAMETER;
681
682 xlpFile[0] = '\0';
683 lpResult[0] = '\0'; /* Start off with an empty return string */
684 if (key) *key = '\0';
685
686 /* trap NULL parameters on entry */
687 if (!lpFile)
688 {
689 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
690 debugstr_w(lpFile), debugstr_w(lpResult));
691 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
692 }
693
694 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
695 {
696 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
697 return 33;
698 }
699
700 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile) / sizeof(WCHAR), xlpFile, NULL))
701 {
702 TRACE("SearchPathW returned non-zero\n");
703 lpFile = xlpFile;
704 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
705 }
706
707 attribs = GetFileAttributesW(lpFile);
708 if (attribs != INVALID_FILE_ATTRIBUTES && (attribs & FILE_ATTRIBUTE_DIRECTORY))
709 {
710 wcscpy(filetype, L"Folder");
711 filetypelen = 6; /* strlen("Folder") */
712 }
713 else
714 {
715 /* Did we get something? Anything? */
716 if (xlpFile[0] == 0)
717 {
718 TRACE("Returning SE_ERR_FNF\n");
719 return SE_ERR_FNF;
720 }
721 /* First thing we need is the file's extension */
722 extension = wcsrchr(xlpFile, '.'); /* Assume last "." is the one; */
723 /* File->Run in progman uses */
724 /* .\FILE.EXE :( */
725 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
726
727 if (extension == NULL || extension[1] == 0)
728 {
729 WARN("Returning SE_ERR_NOASSOC\n");
730 return SE_ERR_NOASSOC;
731 }
732
733 /* Three places to check: */
734 /* 1. win.ini, [windows], programs (NB no leading '.') */
735 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
736 /* 3. win.ini, [extensions], extension (NB no leading '.' */
737 /* All I know of the order is that registry is checked before */
738 /* extensions; however, it'd make sense to check the programs */
739 /* section first, so that's what happens here. */
740
741 /* See if it's a program - if GetProfileString fails, we skip this
742 * section. Actually, if GetProfileString fails, we've probably
743 * got a lot more to worry about than running a program... */
744 if (GetProfileStringW(L"windows", L"programs", L"exe pif bat cmd com", wBuffer, sizeof(wBuffer) / sizeof(WCHAR)) > 0)
745 {
746 CharLowerW(wBuffer);
747 tok = wBuffer;
748 while (*tok)
749 {
750 WCHAR *p = tok;
751 while (*p && *p != ' ' && *p != '\t') p++;
752 if (*p)
753 {
754 *p++ = 0;
755 while (*p == ' ' || *p == '\t') p++;
756 }
757
758 if (wcsicmp(tok, &extension[1]) == 0) /* have to skip the leading "." */
759 {
760 wcscpy(lpResult, xlpFile);
761 /* Need to perhaps check that the file has a path
762 * attached */
763 TRACE("found %s\n", debugstr_w(lpResult));
764 return 33;
765 /* Greater than 32 to indicate success */
766 }
767 tok = p;
768 }
769 }
770
771 /* Check registry */
772 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
773 &filetypelen) == ERROR_SUCCESS)
774 {
775 filetypelen /= sizeof(WCHAR);
776 if (filetypelen == sizeof(filetype) / sizeof(WCHAR))
777 filetypelen--;
778
779 filetype[filetypelen] = '\0';
780 TRACE("File type: %s\n", debugstr_w(filetype));
781 }
782 else
783 {
784 *filetype = '\0';
785 filetypelen = 0;
786 }
787 }
788
789 if (*filetype)
790 {
791 /* pass the operation string to SHELL_FindExecutableByOperation() */
792 filetype[filetypelen] = '\0';
793 retval = SHELL_FindExecutableByOperation(lpOperation, key, filetype, command, sizeof(command));
794
795 if (retval > 32)
796 {
797 DWORD finishedLen;
798 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
799 if (finishedLen > resultLen)
800 ERR("Argify buffer not large enough.. truncated\n");
801 /* Remove double quotation marks and command line arguments */
802 if (*lpResult == '"')
803 {
804 WCHAR *p = lpResult;
805 while (*(p + 1) != '"')
806 {
807 *p = *(p + 1);
808 p++;
809 }
810 *p = '\0';
811 }
812 else
813 {
814 /* Truncate on first space */
815 WCHAR *p = lpResult;
816 while (*p != ' ' && *p != '\0')
817 p++;
818 *p = '\0';
819 }
820 }
821 }
822 else /* Check win.ini */
823 {
824 /* Toss the leading dot */
825 extension++;
826 if (GetProfileStringW(L"extensions", extension, L"", command, sizeof(command) / sizeof(WCHAR)) > 0)
827 {
828 if (wcslen(command) != 0)
829 {
830 wcscpy(lpResult, command);
831 tok = wcschr(lpResult, '^'); /* should be ^.extension? */
832 if (tok != NULL)
833 {
834 tok[0] = '\0';
835 wcscat(lpResult, xlpFile); /* what if no dir in xlpFile? */
836 tok = wcschr(command, '^'); /* see above */
837 if ((tok != NULL) && (wcslen(tok) > 5))
838 {
839 wcscat(lpResult, &tok[5]);
840 }
841 }
842 retval = 33; /* FIXME - see above */
843 }
844 }
845 }
846
847 TRACE("returning %s\n", debugstr_w(lpResult));
848 return retval;
849 }
850
851 /******************************************************************
852 * dde_cb
853 *
854 * callback for the DDE connection. not really useful
855 */
856 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
857 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
858 ULONG_PTR dwData1, ULONG_PTR dwData2)
859 {
860 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
861 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
862 return NULL;
863 }
864
865 /******************************************************************
866 * dde_connect
867 *
868 * ShellExecute helper. Used to do an operation with a DDE connection
869 *
870 * Handles both the direct connection (try #1), and if it fails,
871 * launching an application and trying (#2) to connect to it
872 *
873 */
874 static unsigned dde_connect(const WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
875 const WCHAR* lpFile, WCHAR *env,
876 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
877 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
878 {
879 WCHAR regkey[256];
880 WCHAR * endkey = regkey + wcslen(key);
881 WCHAR app[256], topic[256], ifexec[256], res[256];
882 LONG applen, topiclen, ifexeclen;
883 WCHAR * exec;
884 DWORD ddeInst = 0;
885 DWORD tid;
886 DWORD resultLen;
887 HSZ hszApp, hszTopic;
888 HCONV hConv;
889 HDDEDATA hDdeData;
890 unsigned ret = SE_ERR_NOASSOC;
891 BOOL unicode = !(GetVersion() & 0x80000000);
892
893 wcscpy(regkey, key);
894 wcscpy(endkey, L"\\application");
895 applen = sizeof(app);
896 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
897 {
898 WCHAR command[1024], fullpath[MAX_PATH];
899 static const WCHAR wSo[] = L".so";
900 DWORD sizeSo = sizeof(wSo) / sizeof(WCHAR);
901 LPWSTR ptr = NULL;
902 DWORD ret = 0;
903
904 /* Get application command from start string and find filename of application */
905 if (*start == '"')
906 {
907 wcscpy(command, start + 1);
908 if ((ptr = wcschr(command, '"')))
909 * ptr = 0;
910 ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath) / sizeof(WCHAR), fullpath, &ptr);
911 }
912 else
913 {
914 LPWSTR p, space;
915 for (p = (LPWSTR)start; (space = const_cast<LPWSTR>(strchrW(p, ' '))); p = space + 1)
916 {
917 int idx = space - start;
918 memcpy(command, start, idx * sizeof(WCHAR));
919 command[idx] = '\0';
920 if ((ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath) / sizeof(WCHAR), fullpath, &ptr)))
921 break;
922 }
923 if (!ret)
924 ret = SearchPathW(NULL, start, wszExe, sizeof(fullpath) / sizeof(WCHAR), fullpath, &ptr);
925 }
926
927 if (!ret)
928 {
929 ERR("Unable to find application path for command %s\n", debugstr_w(start));
930 return ERROR_ACCESS_DENIED;
931 }
932 wcscpy(app, ptr);
933
934 /* Remove extensions (including .so) */
935 ptr = app + wcslen(app) - (sizeSo - 1);
936 if (wcslen(app) >= sizeSo &&
937 !wcscmp(ptr, wSo))
938 *ptr = 0;
939
940 ptr = const_cast<LPWSTR>(strrchrW(app, '.'));
941 assert(ptr);
942 *ptr = 0;
943 }
944
945 wcscpy(endkey, L"\\topic");
946 topiclen = sizeof(topic);
947 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
948 {
949 wcscpy(topic, L"System");
950 }
951
952 if (unicode)
953 {
954 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
955 return 2;
956 }
957 else
958 {
959 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
960 return 2;
961 }
962
963 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
964 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
965
966 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
967 exec = ddeexec;
968 if (!hConv)
969 {
970 TRACE("Launching %s\n", debugstr_w(start));
971 ret = execfunc(start, env, TRUE, psei, psei_out);
972 if (ret <= 32)
973 {
974 TRACE("Couldn't launch\n");
975 goto error;
976 }
977 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
978 if (!hConv)
979 {
980 TRACE("Couldn't connect. ret=%d\n", ret);
981 DdeUninitialize(ddeInst);
982 SetLastError(ERROR_DDE_FAIL);
983 return 30; /* whatever */
984 }
985 strcpyW(endkey, L"\\ifexec");
986 ifexeclen = sizeof(ifexec);
987 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
988 {
989 exec = ifexec;
990 }
991 }
992
993 SHELL_ArgifyW(res, sizeof(res) / sizeof(WCHAR), exec, lpFile, pidl, szCommandline, &resultLen);
994 if (resultLen > sizeof(res) / sizeof(WCHAR))
995 ERR("Argify buffer not large enough, truncated\n");
996 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
997
998 /* It's documented in the KB 330337 that IE has a bug and returns
999 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
1000 */
1001 if (unicode)
1002 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0, XTYP_EXECUTE, 30000, &tid);
1003 else
1004 {
1005 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
1006 char *resA = (LPSTR)HeapAlloc(GetProcessHeap(), 0, lenA);
1007 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
1008 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
1009 XTYP_EXECUTE, 10000, &tid );
1010 HeapFree(GetProcessHeap(), 0, resA);
1011 }
1012 if (hDdeData)
1013 DdeFreeDataHandle(hDdeData);
1014 else
1015 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
1016 ret = 33;
1017
1018 DdeDisconnect(hConv);
1019
1020 error:
1021 DdeUninitialize(ddeInst);
1022
1023 return ret;
1024 }
1025
1026 /*************************************************************************
1027 * execute_from_key [Internal]
1028 */
1029 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env,
1030 LPCWSTR szCommandline, LPCWSTR executable_name,
1031 SHELL_ExecuteW32 execfunc,
1032 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
1033 {
1034 WCHAR cmd[256], param[1024], ddeexec[256];
1035 DWORD cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
1036 UINT_PTR retval = SE_ERR_NOASSOC;
1037 DWORD resultLen;
1038 LPWSTR tmp;
1039
1040 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
1041 debugstr_w(szCommandline), debugstr_w(executable_name));
1042
1043 cmd[0] = '\0';
1044 param[0] = '\0';
1045
1046 /* Get the application from the registry */
1047 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, (LONG *)&cmdlen) == ERROR_SUCCESS)
1048 {
1049 TRACE("got cmd: %s\n", debugstr_w(cmd));
1050
1051 /* Is there a replace() function anywhere? */
1052 cmdlen /= sizeof(WCHAR);
1053 if (cmdlen >= sizeof(cmd) / sizeof(WCHAR))
1054 cmdlen = sizeof(cmd) / sizeof(WCHAR) - 1;
1055 cmd[cmdlen] = '\0';
1056 SHELL_ArgifyW(param, sizeof(param) / sizeof(WCHAR), cmd, lpFile, (LPITEMIDLIST)psei->lpIDList, szCommandline, &resultLen);
1057 if (resultLen > sizeof(param) / sizeof(WCHAR))
1058 ERR("Argify buffer not large enough, truncating\n");
1059 }
1060
1061 /* Get the parameters needed by the application
1062 from the associated ddeexec key */
1063 tmp = const_cast<LPWSTR>(strstrW(key, L"command"));
1064 assert(tmp);
1065 wcscpy(tmp, L"ddeexec");
1066
1067 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, (LONG *)&ddeexeclen) == ERROR_SUCCESS)
1068 {
1069 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
1070 if (!param[0]) strcpyW(param, executable_name);
1071 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, (LPITEMIDLIST)psei->lpIDList, execfunc, psei, psei_out);
1072 }
1073 else if (param[0])
1074 {
1075 TRACE("executing: %s\n", debugstr_w(param));
1076 retval = execfunc(param, env, FALSE, psei, psei_out);
1077 }
1078 else
1079 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
1080
1081 return retval;
1082 }
1083
1084 /*************************************************************************
1085 * FindExecutableA [SHELL32.@]
1086 */
1087 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
1088 {
1089 HINSTANCE retval;
1090 WCHAR *wFile = NULL, *wDirectory = NULL;
1091 WCHAR wResult[MAX_PATH];
1092
1093 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
1094 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
1095
1096 retval = FindExecutableW(wFile, wDirectory, wResult);
1097 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
1098 SHFree(wFile);
1099 SHFree(wDirectory);
1100
1101 TRACE("returning %s\n", lpResult);
1102 return retval;
1103 }
1104
1105 /*************************************************************************
1106 * FindExecutableW [SHELL32.@]
1107 *
1108 * This function returns the executable associated with the specified file
1109 * for the default verb.
1110 *
1111 * PARAMS
1112 * lpFile [I] The file to find the association for. This must refer to
1113 * an existing file otherwise FindExecutable fails and returns
1114 * SE_ERR_FNF.
1115 * lpResult [O] Points to a buffer into which the executable path is
1116 * copied. This parameter must not be NULL otherwise
1117 * FindExecutable() segfaults. The buffer must be of size at
1118 * least MAX_PATH characters.
1119 *
1120 * RETURNS
1121 * A value greater than 32 on success, less than or equal to 32 otherwise.
1122 * See the SE_ERR_* constants.
1123 *
1124 * NOTES
1125 * On Windows XP and 2003, FindExecutable() seems to first convert the
1126 * filename into 8.3 format, thus taking into account only the first three
1127 * characters of the extension, and expects to find an association for those.
1128 * However other Windows versions behave sanely.
1129 */
1130 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1131 {
1132 UINT_PTR retval = SE_ERR_NOASSOC;
1133 WCHAR old_dir[1024];
1134
1135 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1136
1137 lpResult[0] = '\0'; /* Start off with an empty return string */
1138 if (lpFile == NULL)
1139 return (HINSTANCE)SE_ERR_FNF;
1140
1141 if (lpDirectory)
1142 {
1143 GetCurrentDirectoryW(sizeof(old_dir) / sizeof(WCHAR), old_dir);
1144 SetCurrentDirectoryW(lpDirectory);
1145 }
1146
1147 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
1148
1149 TRACE("returning %s\n", debugstr_w(lpResult));
1150 if (lpDirectory)
1151 SetCurrentDirectoryW(old_dir);
1152 return (HINSTANCE)retval;
1153 }
1154
1155 /* FIXME: is this already implemented somewhere else? */
1156 static HKEY ShellExecute_GetClassKey(const SHELLEXECUTEINFOW *sei)
1157 {
1158 LPCWSTR ext = NULL, lpClass = NULL;
1159 LPWSTR cls = NULL;
1160 DWORD type = 0, sz = 0;
1161 HKEY hkey = 0;
1162 LONG r;
1163
1164 if (sei->fMask & SEE_MASK_CLASSALL)
1165 return sei->hkeyClass;
1166
1167 if (sei->fMask & SEE_MASK_CLASSNAME)
1168 lpClass = sei->lpClass;
1169 else
1170 {
1171 ext = PathFindExtensionW(sei->lpFile);
1172 TRACE("ext = %s\n", debugstr_w(ext));
1173 if (!ext)
1174 return hkey;
1175
1176 r = RegOpenKeyW(HKEY_CLASSES_ROOT, ext, &hkey);
1177 if (r != ERROR_SUCCESS)
1178 return hkey;
1179
1180 r = RegQueryValueExW(hkey, NULL, 0, &type, NULL, &sz);
1181 if (r == ERROR_SUCCESS && type == REG_SZ)
1182 {
1183 sz += sizeof (WCHAR);
1184 cls = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sz);
1185 cls[0] = 0;
1186 RegQueryValueExW(hkey, NULL, 0, &type, (LPBYTE) cls, &sz);
1187 }
1188
1189 RegCloseKey( hkey );
1190 lpClass = cls;
1191 }
1192
1193 TRACE("class = %s\n", debugstr_w(lpClass));
1194
1195 hkey = 0;
1196 if (lpClass)
1197 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey);
1198
1199 HeapFree(GetProcessHeap(), 0, cls);
1200
1201 return hkey;
1202 }
1203
1204 static IDataObject *shellex_get_dataobj( LPSHELLEXECUTEINFOW sei )
1205 {
1206 LPCITEMIDLIST pidllast = NULL;
1207 IDataObject *dataobj = NULL;
1208 IShellFolder *shf = NULL;
1209 LPITEMIDLIST pidl = NULL;
1210 HRESULT r;
1211
1212 if (sei->fMask & SEE_MASK_CLASSALL)
1213 pidl = (LPITEMIDLIST)sei->lpIDList;
1214 else
1215 {
1216 WCHAR fullpath[MAX_PATH];
1217 BOOL ret;
1218
1219 fullpath[0] = 0;
1220 ret = GetFullPathNameW(sei->lpFile, MAX_PATH, fullpath, NULL);
1221 if (!ret)
1222 goto end;
1223
1224 pidl = ILCreateFromPathW(fullpath);
1225 }
1226
1227 r = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &shf), &pidllast);
1228 if (FAILED(r))
1229 goto end;
1230
1231 shf->GetUIObjectOf(NULL, 1, &pidllast,
1232 IID_IDataObject, NULL, (LPVOID*) &dataobj);
1233
1234 end:
1235 if (pidl != sei->lpIDList)
1236 ILFree(pidl);
1237 if (shf)
1238 shf->Release();
1239 return dataobj;
1240 }
1241
1242 static HRESULT shellex_run_context_menu_default(IShellExtInit *obj,
1243 LPSHELLEXECUTEINFOW sei)
1244 {
1245 IContextMenu *cm = NULL;
1246 CMINVOKECOMMANDINFOEX ici;
1247 MENUITEMINFOW info;
1248 WCHAR string[0x80];
1249 INT i, n, def = -1;
1250 HMENU hmenu = 0;
1251 HRESULT r;
1252
1253 TRACE("%p %p\n", obj, sei);
1254
1255 r = obj->QueryInterface(IID_IContextMenu, (LPVOID*) &cm);
1256 if (FAILED(r))
1257 return r;
1258
1259 hmenu = CreateMenu();
1260 if (!hmenu)
1261 goto end;
1262
1263 /* the number of the last menu added is returned in r */
1264 r = cm->QueryContextMenu(hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY);
1265 if (FAILED(r))
1266 goto end;
1267
1268 n = GetMenuItemCount(hmenu);
1269 for (i = 0; i < n; i++)
1270 {
1271 memset(&info, 0, sizeof(info));
1272 info.cbSize = sizeof info;
1273 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1274 info.dwTypeData = string;
1275 info.cch = sizeof string;
1276 string[0] = 0;
1277 GetMenuItemInfoW(hmenu, i, TRUE, &info);
1278
1279 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1280 info.fState, info.dwItemData, info.fType, info.wID);
1281 if ((!sei->lpVerb && (info.fState & MFS_DEFAULT)) ||
1282 (sei->lpVerb && !lstrcmpiW(sei->lpVerb, string)))
1283 {
1284 def = i;
1285 break;
1286 }
1287 }
1288
1289 r = E_FAIL;
1290 if (def == -1)
1291 goto end;
1292
1293 memset(&ici, 0, sizeof ici);
1294 ici.cbSize = sizeof ici;
1295 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NOASYNC | SEE_MASK_ASYNCOK | SEE_MASK_FLAG_NO_UI));
1296 ici.nShow = sei->nShow;
1297 ici.lpVerb = MAKEINTRESOURCEA(def);
1298 ici.hwnd = sei->hwnd;
1299 ici.lpParametersW = sei->lpParameters;
1300
1301 r = cm->InvokeCommand((LPCMINVOKECOMMANDINFO)&ici);
1302
1303 TRACE("invoke command returned %08x\n", r);
1304
1305 end:
1306 if (hmenu)
1307 DestroyMenu( hmenu );
1308 if (cm)
1309 cm->Release();
1310 return r;
1311 }
1312
1313 static HRESULT shellex_load_object_and_run(HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei)
1314 {
1315 IDataObject *dataobj = NULL;
1316 IObjectWithSite *ows = NULL;
1317 IShellExtInit *obj = NULL;
1318 HRESULT r;
1319
1320 TRACE("%p %s %p\n", hkey, debugstr_guid(guid), sei);
1321
1322 r = CoInitialize(NULL);
1323 if (FAILED(r))
1324 goto end;
1325
1326 r = CoCreateInstance(*guid, NULL, CLSCTX_INPROC_SERVER,
1327 IID_PPV_ARG(IShellExtInit, &obj));
1328 if (FAILED(r))
1329 {
1330 ERR("failed %08x\n", r);
1331 goto end;
1332 }
1333
1334 dataobj = shellex_get_dataobj(sei);
1335 if (!dataobj)
1336 {
1337 ERR("failed to get data object\n");
1338 goto end;
1339 }
1340
1341 r = obj->Initialize(NULL, dataobj, hkey);
1342 if (FAILED(r))
1343 goto end;
1344
1345 r = obj->QueryInterface(IID_IObjectWithSite, (LPVOID*) &ows);
1346 if (FAILED(r))
1347 goto end;
1348
1349 ows->SetSite(NULL);
1350
1351 r = shellex_run_context_menu_default(obj, sei);
1352
1353 end:
1354 if (ows)
1355 ows->Release();
1356 if (dataobj)
1357 dataobj->Release();
1358 if (obj)
1359 obj->Release();
1360 CoUninitialize();
1361 return r;
1362 }
1363
1364
1365 /*************************************************************************
1366 * ShellExecute_FromContextMenu [Internal]
1367 */
1368 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1369 {
1370 HKEY hkey, hkeycm = 0;
1371 WCHAR szguid[39];
1372 HRESULT hr;
1373 GUID guid;
1374 DWORD i;
1375 LONG r;
1376
1377 TRACE("%s\n", debugstr_w(sei->lpFile));
1378
1379 hkey = ShellExecute_GetClassKey(sei);
1380 if (!hkey)
1381 return ERROR_FUNCTION_FAILED;
1382
1383 r = RegOpenKeyW(hkey, L"shellex\\ContextMenuHandlers", &hkeycm);
1384 if (r == ERROR_SUCCESS)
1385 {
1386 i = 0;
1387 while (1)
1388 {
1389 r = RegEnumKeyW(hkeycm, i++, szguid, sizeof(szguid) / sizeof(szguid[0]));
1390 if (r != ERROR_SUCCESS)
1391 break;
1392
1393 hr = CLSIDFromString(szguid, &guid);
1394 if (SUCCEEDED(hr))
1395 {
1396 /* stop at the first one that succeeds in running */
1397 hr = shellex_load_object_and_run(hkey, &guid, sei);
1398 if (SUCCEEDED(hr))
1399 break;
1400 }
1401 }
1402 RegCloseKey(hkeycm);
1403 }
1404
1405 if (hkey != sei->hkeyClass)
1406 RegCloseKey(hkey);
1407 return r;
1408 }
1409
1410 static UINT_PTR SHELL_execute_class(LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc)
1411 {
1412 WCHAR execCmd[1024], wcmd[1024];
1413 /* launch a document by fileclass like 'WordPad.Document.1' */
1414 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1415 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1416 ULONG cmask = (psei->fMask & SEE_MASK_CLASSALL);
1417 DWORD resultLen;
1418 BOOL done;
1419
1420 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1421 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass : NULL,
1422 psei->lpVerb,
1423 execCmd, sizeof(execCmd));
1424
1425 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1426 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1427
1428 wcmd[0] = '\0';
1429 done = SHELL_ArgifyW(wcmd, sizeof(wcmd) / sizeof(WCHAR), execCmd, wszApplicationName, (LPITEMIDLIST)psei->lpIDList, NULL, &resultLen);
1430 if (!done && wszApplicationName[0])
1431 {
1432 strcatW(wcmd, L" ");
1433 strcatW(wcmd, wszApplicationName);
1434 }
1435 if (resultLen > sizeof(wcmd) / sizeof(WCHAR))
1436 ERR("Argify buffer not large enough... truncating\n");
1437 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1438 }
1439
1440 static BOOL SHELL_translate_idlist(LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen)
1441 {
1442 static const WCHAR wExplorer[] = L"explorer.exe";
1443 WCHAR buffer[MAX_PATH];
1444 BOOL appKnownSingular = FALSE;
1445
1446 /* last chance to translate IDList: now also allow CLSID paths */
1447 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST)sei->lpIDList, buffer, sizeof(buffer)))) {
1448 if (buffer[0] == ':' && buffer[1] == ':') {
1449 /* open shell folder for the specified class GUID */
1450 if (strlenW(buffer) + 1 > parametersLen)
1451 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1452 lstrlenW(buffer) + 1, parametersLen);
1453 lstrcpynW(wszParameters, buffer, parametersLen);
1454 if (strlenW(wExplorer) > dwApplicationNameLen)
1455 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1456 lstrlenW(wExplorer) + 1, dwApplicationNameLen);
1457 lstrcpynW(wszApplicationName, wExplorer, dwApplicationNameLen);
1458 appKnownSingular = TRUE;
1459
1460 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1461 } else {
1462 WCHAR target[MAX_PATH];
1463 DWORD attribs;
1464 DWORD resultLen;
1465 /* Check if we're executing a directory and if so use the
1466 handler for the Folder class */
1467 strcpyW(target, buffer);
1468 attribs = GetFileAttributesW(buffer);
1469 if (attribs != INVALID_FILE_ATTRIBUTES &&
1470 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1471 HCR_GetExecuteCommandW(0, L"Folder",
1472 sei->lpVerb,
1473 buffer, sizeof(buffer))) {
1474 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1475 buffer, target, (LPITEMIDLIST)sei->lpIDList, NULL, &resultLen);
1476 if (resultLen > dwApplicationNameLen)
1477 ERR("Argify buffer not large enough... truncating\n");
1478 appKnownSingular = FALSE;
1479 }
1480 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1481 }
1482 }
1483 return appKnownSingular;
1484 }
1485
1486 static UINT_PTR SHELL_quote_and_execute(LPCWSTR wcmd, LPCWSTR wszParameters, LPCWSTR lpstrProtocol, LPCWSTR wszApplicationName, LPWSTR env, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc)
1487 {
1488 UINT_PTR retval;
1489 DWORD len;
1490 WCHAR *wszQuotedCmd;
1491
1492 /* Length of quotes plus length of command plus NULL terminator */
1493 len = 2 + lstrlenW(wcmd) + 1;
1494 if (wszParameters[0])
1495 {
1496 /* Length of space plus length of parameters */
1497 len += 1 + lstrlenW(wszParameters);
1498 }
1499 wszQuotedCmd = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1500 /* Must quote to handle case where cmd contains spaces,
1501 * else security hole if malicious user creates executable file "C:\\Program"
1502 */
1503 strcpyW(wszQuotedCmd, L"\"");
1504 strcatW(wszQuotedCmd, wcmd);
1505 strcatW(wszQuotedCmd, L"\"");
1506 if (wszParameters[0])
1507 {
1508 strcatW(wszQuotedCmd, L" ");
1509 strcatW(wszQuotedCmd, wszParameters);
1510 }
1511
1512 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1513
1514 if (*lpstrProtocol)
1515 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1516 else
1517 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1518 HeapFree(GetProcessHeap(), 0, wszQuotedCmd);
1519 return retval;
1520 }
1521
1522 static UINT_PTR SHELL_execute_url(LPCWSTR lpFile, LPCWSTR wFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc)
1523 {
1524 static const WCHAR wShell[] = L"\\shell\\";
1525 static const WCHAR wCommand[] = L"\\command";
1526 UINT_PTR retval;
1527 WCHAR *lpstrProtocol;
1528 LPCWSTR lpstrRes;
1529 INT iSize;
1530 DWORD len;
1531
1532 lpstrRes = strchrW(lpFile, ':');
1533 if (lpstrRes)
1534 iSize = lpstrRes - lpFile;
1535 else
1536 iSize = strlenW(lpFile);
1537
1538 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1539 /* Looking for ...protocol\shell\lpOperation\command */
1540 len = iSize + lstrlenW(wShell) + lstrlenW(wCommand) + 1;
1541 if (psei->lpVerb)
1542 len += lstrlenW(psei->lpVerb);
1543 else
1544 len += lstrlenW(wszOpen);
1545 lpstrProtocol = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1546 memcpy(lpstrProtocol, lpFile, iSize * sizeof(WCHAR));
1547 lpstrProtocol[iSize] = '\0';
1548 strcatW(lpstrProtocol, wShell);
1549 strcatW(lpstrProtocol, psei->lpVerb ? psei->lpVerb : wszOpen);
1550 strcatW(lpstrProtocol, wCommand);
1551
1552 /* Remove File Protocol from lpFile */
1553 /* In the case file://path/file */
1554 if (!strncmpiW(lpFile, wFile, iSize))
1555 {
1556 lpFile += iSize;
1557 while (*lpFile == ':') lpFile++;
1558 }
1559 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1560 wcmd, execfunc, psei, psei_out);
1561 HeapFree(GetProcessHeap(), 0, lpstrProtocol);
1562 return retval;
1563 }
1564
1565 void do_error_dialog(UINT_PTR retval, HWND hwnd, WCHAR* filename)
1566 {
1567 WCHAR msg[2048];
1568 DWORD_PTR msgArguments[3] = { (DWORD_PTR)filename, 0, 0 };
1569 DWORD error_code;
1570
1571 error_code = GetLastError();
1572
1573 if (retval == SE_ERR_NOASSOC)
1574 LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, sizeof(msg) / sizeof(WCHAR));
1575 else
1576 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
1577 NULL,
1578 error_code,
1579 LANG_USER_DEFAULT,
1580 msg,
1581 sizeof(msg) / sizeof(WCHAR),
1582 (va_list*)msgArguments);
1583
1584 MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1585 }
1586
1587 /*************************************************************************
1588 * SHELL_execute [Internal]
1589 */
1590 BOOL SHELL_execute(LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
1591 {
1592 static const DWORD unsupportedFlags =
1593 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1594 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1595 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1596
1597 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1598 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd;
1599 DWORD dwApplicationNameLen = MAX_PATH + 2;
1600 DWORD parametersLen = sizeof(parametersBuffer) / sizeof(WCHAR);
1601 DWORD dirLen = sizeof(dirBuffer) / sizeof(WCHAR);
1602 DWORD wcmdLen = sizeof(wcmdBuffer) / sizeof(WCHAR);
1603 DWORD len;
1604 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1605 WCHAR wfileName[MAX_PATH];
1606 WCHAR *env;
1607 WCHAR lpstrProtocol[256];
1608 LPCWSTR lpFile;
1609 UINT_PTR retval = SE_ERR_NOASSOC;
1610 BOOL appKnownSingular = FALSE;
1611
1612 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1613 sei_tmp = *sei;
1614
1615 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1616 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1617 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1618 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1619 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1620 debugstr_w(sei_tmp.lpClass) : "not used");
1621
1622 sei->hProcess = NULL;
1623
1624 /* make copies of all path/command strings */
1625 if (!sei_tmp.lpFile)
1626 {
1627 wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen * sizeof(WCHAR));
1628 *wszApplicationName = '\0';
1629 }
1630 else if (*sei_tmp.lpFile == '\"')
1631 {
1632 DWORD l = strlenW(sei_tmp.lpFile + 1);
1633 if(l >= dwApplicationNameLen)
1634 dwApplicationNameLen = l + 1;
1635
1636 wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen * sizeof(WCHAR));
1637 memcpy(wszApplicationName, sei_tmp.lpFile + 1, (l + 1)*sizeof(WCHAR));
1638
1639 if (wszApplicationName[l-1] == L'\"')
1640 wszApplicationName[l-1] = L'\0';
1641 appKnownSingular = TRUE;
1642
1643 TRACE("wszApplicationName=%s\n", debugstr_w(wszApplicationName));
1644 }
1645 else
1646 {
1647 DWORD l = strlenW(sei_tmp.lpFile) + 1;
1648 if(l > dwApplicationNameLen) dwApplicationNameLen = l + 1;
1649 wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen * sizeof(WCHAR));
1650 memcpy(wszApplicationName, sei_tmp.lpFile, l * sizeof(WCHAR));
1651 }
1652
1653 wszParameters = parametersBuffer;
1654 if (sei_tmp.lpParameters)
1655 {
1656 len = lstrlenW(sei_tmp.lpParameters) + 1;
1657 if (len > parametersLen)
1658 {
1659 wszParameters = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1660 parametersLen = len;
1661 }
1662 strcpyW(wszParameters, sei_tmp.lpParameters);
1663 }
1664 else
1665 *wszParameters = L'\0';
1666
1667 wszDir = dirBuffer;
1668 if (sei_tmp.lpDirectory)
1669 {
1670 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1671 if (len > dirLen)
1672 {
1673 wszDir = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1674 dirLen = len;
1675 }
1676 strcpyW(wszDir, sei_tmp.lpDirectory);
1677 }
1678 else
1679 *wszDir = L'\0';
1680
1681 /* adjust string pointers to point to the new buffers */
1682 sei_tmp.lpFile = wszApplicationName;
1683 sei_tmp.lpParameters = wszParameters;
1684 sei_tmp.lpDirectory = wszDir;
1685
1686 if (sei_tmp.fMask & unsupportedFlags)
1687 {
1688 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1689 }
1690
1691 /* process the IDList */
1692 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1693 {
1694 IShellExecuteHookW* pSEH;
1695
1696 HRESULT hr = SHBindToParent((LPCITEMIDLIST)sei_tmp.lpIDList, IID_PPV_ARG(IShellExecuteHookW, &pSEH), NULL);
1697
1698 if (SUCCEEDED(hr))
1699 {
1700 hr = pSEH->Execute(&sei_tmp);
1701
1702 pSEH->Release();
1703
1704 if (hr == S_OK)
1705 {
1706 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1707 if (wszParameters != parametersBuffer)
1708 HeapFree(GetProcessHeap(), 0, wszParameters);
1709 if (wszDir != dirBuffer)
1710 HeapFree(GetProcessHeap(), 0, wszDir);
1711 return TRUE;
1712 }
1713 }
1714
1715 SHGetPathFromIDListW((LPCITEMIDLIST)sei_tmp.lpIDList, wszApplicationName);
1716 appKnownSingular = TRUE;
1717 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1718 }
1719
1720 if (ERROR_SUCCESS == ShellExecute_FromContextMenu(&sei_tmp))
1721 {
1722 sei->hInstApp = (HINSTANCE) 33;
1723 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1724 if (wszParameters != parametersBuffer)
1725 HeapFree(GetProcessHeap(), 0, wszParameters);
1726 if (wszDir != dirBuffer)
1727 HeapFree(GetProcessHeap(), 0, wszDir);
1728 return TRUE;
1729 }
1730
1731 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1732 {
1733 retval = SHELL_execute_class(wszApplicationName, &sei_tmp, sei, execfunc);
1734 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1735 {
1736 OPENASINFO Info;
1737
1738 //FIXME
1739 // need full path
1740
1741 Info.pcszFile = wszApplicationName;
1742 Info.pcszClass = NULL;
1743 Info.oaifInFlags = OAIF_ALLOW_REGISTRATION | OAIF_EXEC;
1744
1745 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1746 do_error_dialog(retval, sei_tmp.hwnd, wszApplicationName);
1747 }
1748 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1749 if (wszParameters != parametersBuffer)
1750 HeapFree(GetProcessHeap(), 0, wszParameters);
1751 if (wszDir != dirBuffer)
1752 HeapFree(GetProcessHeap(), 0, wszDir);
1753 return retval > 32;
1754 }
1755
1756 /* Has the IDList not yet been translated? */
1757 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1758 {
1759 appKnownSingular = SHELL_translate_idlist( &sei_tmp, wszParameters,
1760 parametersLen,
1761 wszApplicationName,
1762 dwApplicationNameLen );
1763 }
1764
1765 /* expand environment strings */
1766 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1767 if (len > 0)
1768 {
1769 LPWSTR buf;
1770 buf = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1771
1772 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len + 1);
1773 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1774 dwApplicationNameLen = len + 1;
1775 wszApplicationName = buf;
1776 /* appKnownSingular unmodified */
1777
1778 sei_tmp.lpFile = wszApplicationName;
1779 }
1780
1781 if (*sei_tmp.lpParameters)
1782 {
1783 len = ExpandEnvironmentStringsW(sei_tmp.lpParameters, NULL, 0);
1784 if (len > 0)
1785 {
1786 LPWSTR buf;
1787 len++;
1788 buf = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1789 ExpandEnvironmentStringsW(sei_tmp.lpParameters, buf, len);
1790 if (wszParameters != parametersBuffer)
1791 HeapFree(GetProcessHeap(), 0, wszParameters);
1792 wszParameters = buf;
1793 parametersLen = len;
1794 sei_tmp.lpParameters = wszParameters;
1795 }
1796 }
1797
1798 if (*sei_tmp.lpDirectory)
1799 {
1800 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1801 if (len > 0)
1802 {
1803 LPWSTR buf;
1804 len++;
1805 buf = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1806 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1807 if (wszDir != dirBuffer)
1808 HeapFree(GetProcessHeap(), 0, wszDir);
1809 wszDir = buf;
1810 sei_tmp.lpDirectory = wszDir;
1811 }
1812 }
1813
1814 /* Else, try to execute the filename */
1815 TRACE("execute: %s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1816
1817 /* separate out command line arguments from executable file name */
1818 if (!*sei_tmp.lpParameters && !appKnownSingular)
1819 {
1820 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1821 if (sei_tmp.lpFile[0] == L'"')
1822 {
1823 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1824 LPWSTR dst = wfileName;
1825 LPWSTR end;
1826
1827 /* copy the unquoted executable path to 'wfileName' */
1828 while(*src && *src != L'"')
1829 *dst++ = *src++;
1830
1831 *dst = L'\0';
1832
1833 if (*src == L'"')
1834 {
1835 end = ++src;
1836
1837 while(isspace(*src))
1838 ++src;
1839 }
1840 else
1841 end = src;
1842
1843 /* copy the parameter string to 'wszParameters' */
1844 strcpyW(wszParameters, src);
1845
1846 /* terminate previous command string after the quote character */
1847 *end = L'\0';
1848 }
1849 else
1850 {
1851 /* If the executable name is not quoted, we have to use this search loop here,
1852 that in CreateProcess() is not sufficient because it does not handle shell links. */
1853 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1854 LPWSTR space, s;
1855
1856 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1857 for(s = beg; (space = const_cast<LPWSTR>(strchrW(s, L' '))); s = space + 1)
1858 {
1859 int idx = space - sei_tmp.lpFile;
1860 memcpy(buffer, sei_tmp.lpFile, idx * sizeof(WCHAR));
1861 buffer[idx] = '\0';
1862
1863 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1864 if (SearchPathW(*sei_tmp.lpDirectory ? sei_tmp.lpDirectory : NULL, buffer, wszExe, sizeof(xlpFile) / sizeof(xlpFile[0]), xlpFile, NULL))
1865 {
1866 /* separate out command from parameter string */
1867 LPCWSTR p = space + 1;
1868
1869 while(isspaceW(*p))
1870 ++p;
1871
1872 strcpyW(wszParameters, p);
1873 *space = L'\0';
1874
1875 break;
1876 }
1877 }
1878
1879 lstrcpynW(wfileName, sei_tmp.lpFile, sizeof(wfileName) / sizeof(WCHAR));
1880 }
1881 }
1882 else
1883 lstrcpynW(wfileName, sei_tmp.lpFile, sizeof(wfileName) / sizeof(WCHAR));
1884
1885 lpFile = wfileName;
1886
1887 wcmd = wcmdBuffer;
1888 len = lstrlenW(wszApplicationName) + 3;
1889 if (sei_tmp.lpParameters[0])
1890 len += 1 + lstrlenW(wszParameters);
1891 if (len > wcmdLen)
1892 {
1893 wcmd = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1894 wcmdLen = len;
1895 }
1896 swprintf(wcmd, L"\"%s\"", wszApplicationName);
1897 if (sei_tmp.lpParameters[0])
1898 {
1899 strcatW(wcmd, L" ");
1900 strcatW(wcmd, wszParameters);
1901 }
1902
1903 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1904 if (retval > 32)
1905 {
1906 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1907 if (wszParameters != parametersBuffer)
1908 HeapFree(GetProcessHeap(), 0, wszParameters);
1909 if (wszDir != dirBuffer)
1910 HeapFree(GetProcessHeap(), 0, wszDir);
1911 if (wcmd != wcmdBuffer)
1912 HeapFree(GetProcessHeap(), 0, wcmd);
1913 return TRUE;
1914 }
1915
1916 /* Else, try to find the executable */
1917 wcmd[0] = L'\0';
1918 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, lpstrProtocol, &env, (LPITEMIDLIST)sei_tmp.lpIDList, sei_tmp.lpParameters);
1919 if (retval > 32) /* Found */
1920 {
1921 retval = SHELL_quote_and_execute(wcmd, wszParameters, lpstrProtocol,
1922 wszApplicationName, env, &sei_tmp,
1923 sei, execfunc);
1924 HeapFree(GetProcessHeap(), 0, env);
1925 }
1926 else if (PathIsDirectoryW(lpFile))
1927 {
1928 WCHAR wExec[MAX_PATH];
1929 WCHAR * lpQuotedFile = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (strlenW(lpFile) + 3));
1930
1931 if (lpQuotedFile)
1932 {
1933 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, L"explorer",
1934 wszOpen, wExec, MAX_PATH,
1935 NULL, &env, NULL, NULL);
1936 if (retval > 32)
1937 {
1938 swprintf(lpQuotedFile, L"\"%s\"", lpFile);
1939 retval = SHELL_quote_and_execute(wExec, lpQuotedFile,
1940 lpstrProtocol,
1941 wszApplicationName, env,
1942 &sei_tmp, sei, execfunc);
1943 HeapFree(GetProcessHeap(), 0, env);
1944 }
1945 HeapFree(GetProcessHeap(), 0, lpQuotedFile);
1946 }
1947 else
1948 retval = 0; /* Out of memory */
1949 }
1950 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1951 {
1952 retval = SHELL_execute_url(lpFile, L"file", wcmd, &sei_tmp, sei, execfunc );
1953 }
1954 /* Check if file specified is in the form www.??????.*** */
1955 else if (!strncmpiW(lpFile, L"www", 3))
1956 {
1957 /* if so, append lpFile http:// and call ShellExecute */
1958 WCHAR lpstrTmpFile[256];
1959 strcpyW(lpstrTmpFile, L"http://");
1960 strcatW(lpstrTmpFile, lpFile);
1961 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1962 }
1963
1964 TRACE("retval %lu\n", retval);
1965
1966 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1967 {
1968 OPENASINFO Info;
1969
1970 //FIXME
1971 // need full path
1972
1973 Info.pcszFile = wszApplicationName;
1974 Info.pcszClass = NULL;
1975 Info.oaifInFlags = OAIF_ALLOW_REGISTRATION | OAIF_EXEC;
1976
1977 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1978 do_error_dialog(retval, sei_tmp.hwnd, wszApplicationName);
1979 }
1980
1981 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1982 if (wszParameters != parametersBuffer)
1983 HeapFree(GetProcessHeap(), 0, wszParameters);
1984 if (wszDir != dirBuffer)
1985 HeapFree(GetProcessHeap(), 0, wszDir);
1986 if (wcmd != wcmdBuffer)
1987 HeapFree(GetProcessHeap(), 0, wcmd);
1988
1989 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1990
1991 return retval > 32;
1992 }
1993
1994 /*************************************************************************
1995 * ShellExecuteA [SHELL32.290]
1996 */
1997 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile,
1998 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
1999 {
2000 SHELLEXECUTEINFOA sei;
2001
2002 TRACE("%p,%s,%s,%s,%s,%d\n",
2003 hWnd, debugstr_a(lpOperation), debugstr_a(lpFile),
2004 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
2005
2006 sei.cbSize = sizeof(sei);
2007 sei.fMask = SEE_MASK_FLAG_NO_UI;
2008 sei.hwnd = hWnd;
2009 sei.lpVerb = lpOperation;
2010 sei.lpFile = lpFile;
2011 sei.lpParameters = lpParameters;
2012 sei.lpDirectory = lpDirectory;
2013 sei.nShow = iShowCmd;
2014 sei.lpIDList = 0;
2015 sei.lpClass = 0;
2016 sei.hkeyClass = 0;
2017 sei.dwHotKey = 0;
2018 sei.hProcess = 0;
2019
2020 ShellExecuteExA(&sei);
2021 return sei.hInstApp;
2022 }
2023
2024 /*************************************************************************
2025 * ShellExecuteExA [SHELL32.292]
2026 *
2027 */
2028 BOOL WINAPI ShellExecuteExA(LPSHELLEXECUTEINFOA sei)
2029 {
2030 SHELLEXECUTEINFOW seiW;
2031 BOOL ret;
2032 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
2033
2034 TRACE("%p\n", sei);
2035
2036 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
2037
2038 if (sei->lpVerb)
2039 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
2040
2041 if (sei->lpFile)
2042 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
2043
2044 if (sei->lpParameters)
2045 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
2046
2047 if (sei->lpDirectory)
2048 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
2049
2050 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
2051 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
2052 else
2053 seiW.lpClass = NULL;
2054
2055 ret = SHELL_execute(&seiW, SHELL_ExecuteW);
2056
2057 sei->hInstApp = seiW.hInstApp;
2058
2059 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
2060 sei->hProcess = seiW.hProcess;
2061
2062 SHFree(wVerb);
2063 SHFree(wFile);
2064 SHFree(wParameters);
2065 SHFree(wDirectory);
2066 SHFree(wClass);
2067
2068 return ret;
2069 }
2070
2071 /*************************************************************************
2072 * ShellExecuteExW [SHELL32.293]
2073 *
2074 */
2075 BOOL WINAPI ShellExecuteExW(LPSHELLEXECUTEINFOW sei)
2076 {
2077 return SHELL_execute(sei, SHELL_ExecuteW);
2078 }
2079
2080 /*************************************************************************
2081 * ShellExecuteW [SHELL32.294]
2082 * from shellapi.h
2083 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
2084 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
2085 */
2086 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
2087 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
2088 {
2089 SHELLEXECUTEINFOW sei;
2090
2091 TRACE("\n");
2092 sei.cbSize = sizeof(sei);
2093 sei.fMask = SEE_MASK_FLAG_NO_UI;
2094 sei.hwnd = hwnd;
2095 sei.lpVerb = lpOperation;
2096 sei.lpFile = lpFile;
2097 sei.lpParameters = lpParameters;
2098 sei.lpDirectory = lpDirectory;
2099 sei.nShow = nShowCmd;
2100 sei.lpIDList = 0;
2101 sei.lpClass = 0;
2102 sei.hkeyClass = 0;
2103 sei.dwHotKey = 0;
2104 sei.hProcess = 0;
2105
2106 SHELL_execute(&sei, SHELL_ExecuteW);
2107 return sei.hInstApp;
2108 }
2109
2110 /*************************************************************************
2111 * WOWShellExecute [SHELL32.@]
2112 *
2113 * FIXME: the callback function most likely doesn't work the same way on Windows.
2114 */
2115 EXTERN_C HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile,
2116 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd, void *callback)
2117 {
2118 SHELLEXECUTEINFOW seiW;
2119 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
2120 HANDLE hProcess = 0;
2121
2122 seiW.lpVerb = lpOperation ? __SHCloneStrAtoW(&wVerb, lpOperation) : NULL;
2123 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
2124 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
2125 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
2126
2127 seiW.cbSize = sizeof(seiW);
2128 seiW.fMask = 0;
2129 seiW.hwnd = hWnd;
2130 seiW.nShow = iShowCmd;
2131 seiW.lpIDList = 0;
2132 seiW.lpClass = 0;
2133 seiW.hkeyClass = 0;
2134 seiW.dwHotKey = 0;
2135 seiW.hProcess = hProcess;
2136
2137 SHELL_execute(&seiW, (SHELL_ExecuteW32)callback);
2138
2139 SHFree(wVerb);
2140 SHFree(wFile);
2141 SHFree(wParameters);
2142 SHFree(wDirectory);
2143 return seiW.hInstApp;
2144 }
2145
2146 /*************************************************************************
2147 * OpenAs_RunDLLA [SHELL32.@]
2148 */
2149 EXTERN_C void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2150 {
2151 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2152 }
2153
2154 /*************************************************************************
2155 * OpenAs_RunDLLW [SHELL32.@]
2156 */
2157 EXTERN_C void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2158 {
2159 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);
2160 }