Sync with trunk r63174.
[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, IID_NULL_PPV_ARG(IDataObject, &dataobj));
1232
1233 end:
1234 if (pidl != sei->lpIDList)
1235 ILFree(pidl);
1236 if (shf)
1237 shf->Release();
1238 return dataobj;
1239 }
1240
1241 static HRESULT shellex_run_context_menu_default(IShellExtInit *obj,
1242 LPSHELLEXECUTEINFOW sei)
1243 {
1244 IContextMenu *cm = NULL;
1245 CMINVOKECOMMANDINFOEX ici;
1246 MENUITEMINFOW info;
1247 WCHAR string[0x80];
1248 INT i, n, def = -1;
1249 HMENU hmenu = 0;
1250 HRESULT r;
1251
1252 TRACE("%p %p\n", obj, sei);
1253
1254 r = obj->QueryInterface(IID_PPV_ARG(IContextMenu, &cm));
1255 if (FAILED(r))
1256 return r;
1257
1258 hmenu = CreateMenu();
1259 if (!hmenu)
1260 goto end;
1261
1262 /* the number of the last menu added is returned in r */
1263 r = cm->QueryContextMenu(hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY);
1264 if (FAILED(r))
1265 goto end;
1266
1267 n = GetMenuItemCount(hmenu);
1268 for (i = 0; i < n; i++)
1269 {
1270 memset(&info, 0, sizeof(info));
1271 info.cbSize = sizeof info;
1272 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1273 info.dwTypeData = string;
1274 info.cch = sizeof string;
1275 string[0] = 0;
1276 GetMenuItemInfoW(hmenu, i, TRUE, &info);
1277
1278 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1279 info.fState, info.dwItemData, info.fType, info.wID);
1280 if ((!sei->lpVerb && (info.fState & MFS_DEFAULT)) ||
1281 (sei->lpVerb && !lstrcmpiW(sei->lpVerb, string)))
1282 {
1283 def = i;
1284 break;
1285 }
1286 }
1287
1288 r = E_FAIL;
1289 if (def == -1)
1290 goto end;
1291
1292 memset(&ici, 0, sizeof ici);
1293 ici.cbSize = sizeof ici;
1294 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NOASYNC | SEE_MASK_ASYNCOK | SEE_MASK_FLAG_NO_UI));
1295 ici.nShow = sei->nShow;
1296 ici.lpVerb = MAKEINTRESOURCEA(def);
1297 ici.hwnd = sei->hwnd;
1298 ici.lpParametersW = sei->lpParameters;
1299
1300 r = cm->InvokeCommand((LPCMINVOKECOMMANDINFO)&ici);
1301
1302 TRACE("invoke command returned %08x\n", r);
1303
1304 end:
1305 if (hmenu)
1306 DestroyMenu( hmenu );
1307 if (cm)
1308 cm->Release();
1309 return r;
1310 }
1311
1312 static HRESULT shellex_load_object_and_run(HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei)
1313 {
1314 IDataObject *dataobj = NULL;
1315 IObjectWithSite *ows = NULL;
1316 IShellExtInit *obj = NULL;
1317 HRESULT r;
1318
1319 TRACE("%p %s %p\n", hkey, debugstr_guid(guid), sei);
1320
1321 r = CoInitialize(NULL);
1322 if (FAILED(r))
1323 goto end;
1324
1325 r = CoCreateInstance(*guid, NULL, CLSCTX_INPROC_SERVER,
1326 IID_PPV_ARG(IShellExtInit, &obj));
1327 if (FAILED(r))
1328 {
1329 ERR("failed %08x\n", r);
1330 goto end;
1331 }
1332
1333 dataobj = shellex_get_dataobj(sei);
1334 if (!dataobj)
1335 {
1336 ERR("failed to get data object\n");
1337 goto end;
1338 }
1339
1340 r = obj->Initialize(NULL, dataobj, hkey);
1341 if (FAILED(r))
1342 goto end;
1343
1344 r = obj->QueryInterface(IID_PPV_ARG(IObjectWithSite, &ows));
1345 if (FAILED(r))
1346 goto end;
1347
1348 ows->SetSite(NULL);
1349
1350 r = shellex_run_context_menu_default(obj, sei);
1351
1352 end:
1353 if (ows)
1354 ows->Release();
1355 if (dataobj)
1356 dataobj->Release();
1357 if (obj)
1358 obj->Release();
1359 CoUninitialize();
1360 return r;
1361 }
1362
1363
1364 /*************************************************************************
1365 * ShellExecute_FromContextMenu [Internal]
1366 */
1367 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1368 {
1369 HKEY hkey, hkeycm = 0;
1370 WCHAR szguid[39];
1371 HRESULT hr;
1372 GUID guid;
1373 DWORD i;
1374 LONG r;
1375
1376 TRACE("%s\n", debugstr_w(sei->lpFile));
1377
1378 hkey = ShellExecute_GetClassKey(sei);
1379 if (!hkey)
1380 return ERROR_FUNCTION_FAILED;
1381
1382 r = RegOpenKeyW(hkey, L"shellex\\ContextMenuHandlers", &hkeycm);
1383 if (r == ERROR_SUCCESS)
1384 {
1385 i = 0;
1386 while (1)
1387 {
1388 r = RegEnumKeyW(hkeycm, i++, szguid, sizeof(szguid) / sizeof(szguid[0]));
1389 if (r != ERROR_SUCCESS)
1390 break;
1391
1392 hr = CLSIDFromString(szguid, &guid);
1393 if (SUCCEEDED(hr))
1394 {
1395 /* stop at the first one that succeeds in running */
1396 hr = shellex_load_object_and_run(hkey, &guid, sei);
1397 if (SUCCEEDED(hr))
1398 break;
1399 }
1400 }
1401 RegCloseKey(hkeycm);
1402 }
1403
1404 if (hkey != sei->hkeyClass)
1405 RegCloseKey(hkey);
1406 return r;
1407 }
1408
1409 static UINT_PTR SHELL_execute_class(LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc)
1410 {
1411 WCHAR execCmd[1024], wcmd[1024];
1412 /* launch a document by fileclass like 'WordPad.Document.1' */
1413 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1414 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1415 ULONG cmask = (psei->fMask & SEE_MASK_CLASSALL);
1416 DWORD resultLen;
1417 BOOL done;
1418
1419 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1420 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass : NULL,
1421 psei->lpVerb,
1422 execCmd, sizeof(execCmd));
1423
1424 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1425 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1426
1427 wcmd[0] = '\0';
1428 done = SHELL_ArgifyW(wcmd, sizeof(wcmd) / sizeof(WCHAR), execCmd, wszApplicationName, (LPITEMIDLIST)psei->lpIDList, NULL, &resultLen);
1429 if (!done && wszApplicationName[0])
1430 {
1431 strcatW(wcmd, L" ");
1432 strcatW(wcmd, wszApplicationName);
1433 }
1434 if (resultLen > sizeof(wcmd) / sizeof(WCHAR))
1435 ERR("Argify buffer not large enough... truncating\n");
1436 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1437 }
1438
1439 static BOOL SHELL_translate_idlist(LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen)
1440 {
1441 static const WCHAR wExplorer[] = L"explorer.exe";
1442 WCHAR buffer[MAX_PATH];
1443 BOOL appKnownSingular = FALSE;
1444
1445 /* last chance to translate IDList: now also allow CLSID paths */
1446 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST)sei->lpIDList, buffer, sizeof(buffer)))) {
1447 if (buffer[0] == ':' && buffer[1] == ':') {
1448 /* open shell folder for the specified class GUID */
1449 if (strlenW(buffer) + 1 > parametersLen)
1450 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1451 lstrlenW(buffer) + 1, parametersLen);
1452 lstrcpynW(wszParameters, buffer, parametersLen);
1453 if (strlenW(wExplorer) > dwApplicationNameLen)
1454 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1455 lstrlenW(wExplorer) + 1, dwApplicationNameLen);
1456 lstrcpynW(wszApplicationName, wExplorer, dwApplicationNameLen);
1457 appKnownSingular = TRUE;
1458
1459 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1460 } else {
1461 WCHAR target[MAX_PATH];
1462 DWORD attribs;
1463 DWORD resultLen;
1464 /* Check if we're executing a directory and if so use the
1465 handler for the Folder class */
1466 strcpyW(target, buffer);
1467 attribs = GetFileAttributesW(buffer);
1468 if (attribs != INVALID_FILE_ATTRIBUTES &&
1469 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1470 HCR_GetExecuteCommandW(0, L"Folder",
1471 sei->lpVerb,
1472 buffer, sizeof(buffer))) {
1473 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1474 buffer, target, (LPITEMIDLIST)sei->lpIDList, NULL, &resultLen);
1475 if (resultLen > dwApplicationNameLen)
1476 ERR("Argify buffer not large enough... truncating\n");
1477 appKnownSingular = FALSE;
1478 }
1479 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1480 }
1481 }
1482 return appKnownSingular;
1483 }
1484
1485 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)
1486 {
1487 UINT_PTR retval;
1488 DWORD len;
1489 WCHAR *wszQuotedCmd;
1490
1491 /* Length of quotes plus length of command plus NULL terminator */
1492 len = 2 + lstrlenW(wcmd) + 1;
1493 if (wszParameters[0])
1494 {
1495 /* Length of space plus length of parameters */
1496 len += 1 + lstrlenW(wszParameters);
1497 }
1498 wszQuotedCmd = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1499 /* Must quote to handle case where cmd contains spaces,
1500 * else security hole if malicious user creates executable file "C:\\Program"
1501 */
1502 strcpyW(wszQuotedCmd, L"\"");
1503 strcatW(wszQuotedCmd, wcmd);
1504 strcatW(wszQuotedCmd, L"\"");
1505 if (wszParameters[0])
1506 {
1507 strcatW(wszQuotedCmd, L" ");
1508 strcatW(wszQuotedCmd, wszParameters);
1509 }
1510
1511 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1512
1513 if (*lpstrProtocol)
1514 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1515 else
1516 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1517 HeapFree(GetProcessHeap(), 0, wszQuotedCmd);
1518 return retval;
1519 }
1520
1521 static UINT_PTR SHELL_execute_url(LPCWSTR lpFile, LPCWSTR wFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc)
1522 {
1523 static const WCHAR wShell[] = L"\\shell\\";
1524 static const WCHAR wCommand[] = L"\\command";
1525 UINT_PTR retval;
1526 WCHAR *lpstrProtocol;
1527 LPCWSTR lpstrRes;
1528 INT iSize;
1529 DWORD len;
1530
1531 lpstrRes = strchrW(lpFile, ':');
1532 if (lpstrRes)
1533 iSize = lpstrRes - lpFile;
1534 else
1535 iSize = strlenW(lpFile);
1536
1537 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1538 /* Looking for ...protocol\shell\lpOperation\command */
1539 len = iSize + lstrlenW(wShell) + lstrlenW(wCommand) + 1;
1540 if (psei->lpVerb)
1541 len += lstrlenW(psei->lpVerb);
1542 else
1543 len += lstrlenW(wszOpen);
1544 lpstrProtocol = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1545 memcpy(lpstrProtocol, lpFile, iSize * sizeof(WCHAR));
1546 lpstrProtocol[iSize] = '\0';
1547 strcatW(lpstrProtocol, wShell);
1548 strcatW(lpstrProtocol, psei->lpVerb ? psei->lpVerb : wszOpen);
1549 strcatW(lpstrProtocol, wCommand);
1550
1551 /* Remove File Protocol from lpFile */
1552 /* In the case file://path/file */
1553 if (!strncmpiW(lpFile, wFile, iSize))
1554 {
1555 lpFile += iSize;
1556 while (*lpFile == ':') lpFile++;
1557 }
1558 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1559 wcmd, execfunc, psei, psei_out);
1560 HeapFree(GetProcessHeap(), 0, lpstrProtocol);
1561 return retval;
1562 }
1563
1564 void do_error_dialog(UINT_PTR retval, HWND hwnd, WCHAR* filename)
1565 {
1566 WCHAR msg[2048];
1567 DWORD_PTR msgArguments[3] = { (DWORD_PTR)filename, 0, 0 };
1568 DWORD error_code;
1569
1570 error_code = GetLastError();
1571
1572 if (retval == SE_ERR_NOASSOC)
1573 LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, sizeof(msg) / sizeof(WCHAR));
1574 else
1575 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
1576 NULL,
1577 error_code,
1578 LANG_USER_DEFAULT,
1579 msg,
1580 sizeof(msg) / sizeof(WCHAR),
1581 (va_list*)msgArguments);
1582
1583 MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1584 }
1585
1586 /*************************************************************************
1587 * SHELL_execute [Internal]
1588 */
1589 BOOL SHELL_execute(LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
1590 {
1591 static const DWORD unsupportedFlags =
1592 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1593 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1594 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1595
1596 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1597 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd;
1598 DWORD dwApplicationNameLen = MAX_PATH + 2;
1599 DWORD parametersLen = sizeof(parametersBuffer) / sizeof(WCHAR);
1600 DWORD dirLen = sizeof(dirBuffer) / sizeof(WCHAR);
1601 DWORD wcmdLen = sizeof(wcmdBuffer) / sizeof(WCHAR);
1602 DWORD len;
1603 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1604 WCHAR wfileName[MAX_PATH];
1605 WCHAR *env;
1606 WCHAR lpstrProtocol[256];
1607 LPCWSTR lpFile;
1608 UINT_PTR retval = SE_ERR_NOASSOC;
1609 BOOL appKnownSingular = FALSE;
1610
1611 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1612 sei_tmp = *sei;
1613
1614 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1615 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1616 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1617 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1618 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1619 debugstr_w(sei_tmp.lpClass) : "not used");
1620
1621 sei->hProcess = NULL;
1622
1623 /* make copies of all path/command strings */
1624 if (!sei_tmp.lpFile)
1625 {
1626 wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen * sizeof(WCHAR));
1627 *wszApplicationName = '\0';
1628 }
1629 else if (*sei_tmp.lpFile == '\"')
1630 {
1631 DWORD l = strlenW(sei_tmp.lpFile + 1);
1632 if(l >= dwApplicationNameLen)
1633 dwApplicationNameLen = l + 1;
1634
1635 wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen * sizeof(WCHAR));
1636 memcpy(wszApplicationName, sei_tmp.lpFile + 1, (l + 1)*sizeof(WCHAR));
1637
1638 if (wszApplicationName[l-1] == L'\"')
1639 wszApplicationName[l-1] = L'\0';
1640 appKnownSingular = TRUE;
1641
1642 TRACE("wszApplicationName=%s\n", debugstr_w(wszApplicationName));
1643 }
1644 else
1645 {
1646 DWORD l = strlenW(sei_tmp.lpFile) + 1;
1647 if(l > dwApplicationNameLen) dwApplicationNameLen = l + 1;
1648 wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen * sizeof(WCHAR));
1649 memcpy(wszApplicationName, sei_tmp.lpFile, l * sizeof(WCHAR));
1650 }
1651
1652 wszParameters = parametersBuffer;
1653 if (sei_tmp.lpParameters)
1654 {
1655 len = lstrlenW(sei_tmp.lpParameters) + 1;
1656 if (len > parametersLen)
1657 {
1658 wszParameters = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1659 parametersLen = len;
1660 }
1661 strcpyW(wszParameters, sei_tmp.lpParameters);
1662 }
1663 else
1664 *wszParameters = L'\0';
1665
1666 wszDir = dirBuffer;
1667 if (sei_tmp.lpDirectory)
1668 {
1669 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1670 if (len > dirLen)
1671 {
1672 wszDir = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1673 dirLen = len;
1674 }
1675 strcpyW(wszDir, sei_tmp.lpDirectory);
1676 }
1677 else
1678 *wszDir = L'\0';
1679
1680 /* adjust string pointers to point to the new buffers */
1681 sei_tmp.lpFile = wszApplicationName;
1682 sei_tmp.lpParameters = wszParameters;
1683 sei_tmp.lpDirectory = wszDir;
1684
1685 if (sei_tmp.fMask & unsupportedFlags)
1686 {
1687 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1688 }
1689
1690 /* process the IDList */
1691 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1692 {
1693 IShellExecuteHookW* pSEH;
1694
1695 HRESULT hr = SHBindToParent((LPCITEMIDLIST)sei_tmp.lpIDList, IID_PPV_ARG(IShellExecuteHookW, &pSEH), NULL);
1696
1697 if (SUCCEEDED(hr))
1698 {
1699 hr = pSEH->Execute(&sei_tmp);
1700
1701 pSEH->Release();
1702
1703 if (hr == S_OK)
1704 {
1705 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1706 if (wszParameters != parametersBuffer)
1707 HeapFree(GetProcessHeap(), 0, wszParameters);
1708 if (wszDir != dirBuffer)
1709 HeapFree(GetProcessHeap(), 0, wszDir);
1710 return TRUE;
1711 }
1712 }
1713
1714 SHGetPathFromIDListW((LPCITEMIDLIST)sei_tmp.lpIDList, wszApplicationName);
1715 appKnownSingular = TRUE;
1716 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1717 }
1718
1719 if (ERROR_SUCCESS == ShellExecute_FromContextMenu(&sei_tmp))
1720 {
1721 sei->hInstApp = (HINSTANCE) 33;
1722 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1723 if (wszParameters != parametersBuffer)
1724 HeapFree(GetProcessHeap(), 0, wszParameters);
1725 if (wszDir != dirBuffer)
1726 HeapFree(GetProcessHeap(), 0, wszDir);
1727 return TRUE;
1728 }
1729
1730 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1731 {
1732 retval = SHELL_execute_class(wszApplicationName, &sei_tmp, sei, execfunc);
1733 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1734 {
1735 OPENASINFO Info;
1736
1737 //FIXME
1738 // need full path
1739
1740 Info.pcszFile = wszApplicationName;
1741 Info.pcszClass = NULL;
1742 Info.oaifInFlags = OAIF_ALLOW_REGISTRATION | OAIF_EXEC;
1743
1744 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1745 do_error_dialog(retval, sei_tmp.hwnd, wszApplicationName);
1746 }
1747 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1748 if (wszParameters != parametersBuffer)
1749 HeapFree(GetProcessHeap(), 0, wszParameters);
1750 if (wszDir != dirBuffer)
1751 HeapFree(GetProcessHeap(), 0, wszDir);
1752 return retval > 32;
1753 }
1754
1755 /* Has the IDList not yet been translated? */
1756 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1757 {
1758 appKnownSingular = SHELL_translate_idlist( &sei_tmp, wszParameters,
1759 parametersLen,
1760 wszApplicationName,
1761 dwApplicationNameLen );
1762 }
1763
1764 /* expand environment strings */
1765 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1766 if (len > 0)
1767 {
1768 LPWSTR buf;
1769 buf = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
1770
1771 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len + 1);
1772 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1773 dwApplicationNameLen = len + 1;
1774 wszApplicationName = buf;
1775 /* appKnownSingular unmodified */
1776
1777 sei_tmp.lpFile = wszApplicationName;
1778 }
1779
1780 if (*sei_tmp.lpParameters)
1781 {
1782 len = ExpandEnvironmentStringsW(sei_tmp.lpParameters, NULL, 0);
1783 if (len > 0)
1784 {
1785 LPWSTR buf;
1786 len++;
1787 buf = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1788 ExpandEnvironmentStringsW(sei_tmp.lpParameters, buf, len);
1789 if (wszParameters != parametersBuffer)
1790 HeapFree(GetProcessHeap(), 0, wszParameters);
1791 wszParameters = buf;
1792 parametersLen = len;
1793 sei_tmp.lpParameters = wszParameters;
1794 }
1795 }
1796
1797 if (*sei_tmp.lpDirectory)
1798 {
1799 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1800 if (len > 0)
1801 {
1802 LPWSTR buf;
1803 len++;
1804 buf = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1805 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1806 if (wszDir != dirBuffer)
1807 HeapFree(GetProcessHeap(), 0, wszDir);
1808 wszDir = buf;
1809 sei_tmp.lpDirectory = wszDir;
1810 }
1811 }
1812
1813 /* Else, try to execute the filename */
1814 TRACE("execute: %s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1815
1816 /* separate out command line arguments from executable file name */
1817 if (!*sei_tmp.lpParameters && !appKnownSingular)
1818 {
1819 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1820 if (sei_tmp.lpFile[0] == L'"')
1821 {
1822 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1823 LPWSTR dst = wfileName;
1824 LPWSTR end;
1825
1826 /* copy the unquoted executable path to 'wfileName' */
1827 while(*src && *src != L'"')
1828 *dst++ = *src++;
1829
1830 *dst = L'\0';
1831
1832 if (*src == L'"')
1833 {
1834 end = ++src;
1835
1836 while(isspace(*src))
1837 ++src;
1838 }
1839 else
1840 end = src;
1841
1842 /* copy the parameter string to 'wszParameters' */
1843 strcpyW(wszParameters, src);
1844
1845 /* terminate previous command string after the quote character */
1846 *end = L'\0';
1847 }
1848 else
1849 {
1850 /* If the executable name is not quoted, we have to use this search loop here,
1851 that in CreateProcess() is not sufficient because it does not handle shell links. */
1852 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1853 LPWSTR space, s;
1854
1855 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1856 for(s = beg; (space = const_cast<LPWSTR>(strchrW(s, L' '))); s = space + 1)
1857 {
1858 int idx = space - sei_tmp.lpFile;
1859 memcpy(buffer, sei_tmp.lpFile, idx * sizeof(WCHAR));
1860 buffer[idx] = '\0';
1861
1862 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1863 if (SearchPathW(*sei_tmp.lpDirectory ? sei_tmp.lpDirectory : NULL, buffer, wszExe, sizeof(xlpFile) / sizeof(xlpFile[0]), xlpFile, NULL))
1864 {
1865 /* separate out command from parameter string */
1866 LPCWSTR p = space + 1;
1867
1868 while(isspaceW(*p))
1869 ++p;
1870
1871 strcpyW(wszParameters, p);
1872 *space = L'\0';
1873
1874 break;
1875 }
1876 }
1877
1878 lstrcpynW(wfileName, sei_tmp.lpFile, sizeof(wfileName) / sizeof(WCHAR));
1879 }
1880 }
1881 else
1882 lstrcpynW(wfileName, sei_tmp.lpFile, sizeof(wfileName) / sizeof(WCHAR));
1883
1884 lpFile = wfileName;
1885
1886 wcmd = wcmdBuffer;
1887 len = lstrlenW(wszApplicationName) + 3;
1888 if (sei_tmp.lpParameters[0])
1889 len += 1 + lstrlenW(wszParameters);
1890 if (len > wcmdLen)
1891 {
1892 wcmd = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1893 wcmdLen = len;
1894 }
1895 swprintf(wcmd, L"\"%s\"", wszApplicationName);
1896 if (sei_tmp.lpParameters[0])
1897 {
1898 strcatW(wcmd, L" ");
1899 strcatW(wcmd, wszParameters);
1900 }
1901
1902 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1903 if (retval > 32)
1904 {
1905 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1906 if (wszParameters != parametersBuffer)
1907 HeapFree(GetProcessHeap(), 0, wszParameters);
1908 if (wszDir != dirBuffer)
1909 HeapFree(GetProcessHeap(), 0, wszDir);
1910 if (wcmd != wcmdBuffer)
1911 HeapFree(GetProcessHeap(), 0, wcmd);
1912 return TRUE;
1913 }
1914
1915 /* Else, try to find the executable */
1916 wcmd[0] = L'\0';
1917 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, lpstrProtocol, &env, (LPITEMIDLIST)sei_tmp.lpIDList, sei_tmp.lpParameters);
1918 if (retval > 32) /* Found */
1919 {
1920 retval = SHELL_quote_and_execute(wcmd, wszParameters, lpstrProtocol,
1921 wszApplicationName, env, &sei_tmp,
1922 sei, execfunc);
1923 HeapFree(GetProcessHeap(), 0, env);
1924 }
1925 else if (PathIsDirectoryW(lpFile))
1926 {
1927 WCHAR wExec[MAX_PATH];
1928 WCHAR * lpQuotedFile = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (strlenW(lpFile) + 3));
1929
1930 if (lpQuotedFile)
1931 {
1932 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, L"explorer",
1933 wszOpen, wExec, MAX_PATH,
1934 NULL, &env, NULL, NULL);
1935 if (retval > 32)
1936 {
1937 swprintf(lpQuotedFile, L"\"%s\"", lpFile);
1938 retval = SHELL_quote_and_execute(wExec, lpQuotedFile,
1939 lpstrProtocol,
1940 wszApplicationName, env,
1941 &sei_tmp, sei, execfunc);
1942 HeapFree(GetProcessHeap(), 0, env);
1943 }
1944 HeapFree(GetProcessHeap(), 0, lpQuotedFile);
1945 }
1946 else
1947 retval = 0; /* Out of memory */
1948 }
1949 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1950 {
1951 retval = SHELL_execute_url(lpFile, L"file", wcmd, &sei_tmp, sei, execfunc );
1952 }
1953 /* Check if file specified is in the form www.??????.*** */
1954 else if (!strncmpiW(lpFile, L"www", 3))
1955 {
1956 /* if so, append lpFile http:// and call ShellExecute */
1957 WCHAR lpstrTmpFile[256];
1958 strcpyW(lpstrTmpFile, L"http://");
1959 strcatW(lpstrTmpFile, lpFile);
1960 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1961 }
1962
1963 TRACE("retval %lu\n", retval);
1964
1965 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1966 {
1967 OPENASINFO Info;
1968
1969 //FIXME
1970 // need full path
1971
1972 Info.pcszFile = wszApplicationName;
1973 Info.pcszClass = NULL;
1974 Info.oaifInFlags = OAIF_ALLOW_REGISTRATION | OAIF_EXEC;
1975
1976 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1977 do_error_dialog(retval, sei_tmp.hwnd, wszApplicationName);
1978 }
1979
1980 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1981 if (wszParameters != parametersBuffer)
1982 HeapFree(GetProcessHeap(), 0, wszParameters);
1983 if (wszDir != dirBuffer)
1984 HeapFree(GetProcessHeap(), 0, wszDir);
1985 if (wcmd != wcmdBuffer)
1986 HeapFree(GetProcessHeap(), 0, wcmd);
1987
1988 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1989
1990 return retval > 32;
1991 }
1992
1993 /*************************************************************************
1994 * ShellExecuteA [SHELL32.290]
1995 */
1996 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile,
1997 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
1998 {
1999 SHELLEXECUTEINFOA sei;
2000
2001 TRACE("%p,%s,%s,%s,%s,%d\n",
2002 hWnd, debugstr_a(lpOperation), debugstr_a(lpFile),
2003 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
2004
2005 sei.cbSize = sizeof(sei);
2006 sei.fMask = SEE_MASK_FLAG_NO_UI;
2007 sei.hwnd = hWnd;
2008 sei.lpVerb = lpOperation;
2009 sei.lpFile = lpFile;
2010 sei.lpParameters = lpParameters;
2011 sei.lpDirectory = lpDirectory;
2012 sei.nShow = iShowCmd;
2013 sei.lpIDList = 0;
2014 sei.lpClass = 0;
2015 sei.hkeyClass = 0;
2016 sei.dwHotKey = 0;
2017 sei.hProcess = 0;
2018
2019 ShellExecuteExA(&sei);
2020 return sei.hInstApp;
2021 }
2022
2023 /*************************************************************************
2024 * ShellExecuteExA [SHELL32.292]
2025 *
2026 */
2027 BOOL
2028 WINAPI
2029 DECLSPEC_HOTPATCH
2030 ShellExecuteExA(LPSHELLEXECUTEINFOA sei)
2031 {
2032 SHELLEXECUTEINFOW seiW;
2033 BOOL ret;
2034 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
2035
2036 TRACE("%p\n", sei);
2037
2038 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
2039
2040 if (sei->lpVerb)
2041 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
2042
2043 if (sei->lpFile)
2044 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
2045
2046 if (sei->lpParameters)
2047 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
2048
2049 if (sei->lpDirectory)
2050 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
2051
2052 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
2053 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
2054 else
2055 seiW.lpClass = NULL;
2056
2057 ret = SHELL_execute(&seiW, SHELL_ExecuteW);
2058
2059 sei->hInstApp = seiW.hInstApp;
2060
2061 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
2062 sei->hProcess = seiW.hProcess;
2063
2064 SHFree(wVerb);
2065 SHFree(wFile);
2066 SHFree(wParameters);
2067 SHFree(wDirectory);
2068 SHFree(wClass);
2069
2070 return ret;
2071 }
2072
2073 /*************************************************************************
2074 * ShellExecuteExW [SHELL32.293]
2075 *
2076 */
2077 BOOL
2078 WINAPI
2079 DECLSPEC_HOTPATCH
2080 ShellExecuteExW(LPSHELLEXECUTEINFOW sei)
2081 {
2082 return SHELL_execute(sei, SHELL_ExecuteW);
2083 }
2084
2085 /*************************************************************************
2086 * ShellExecuteW [SHELL32.294]
2087 * from shellapi.h
2088 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
2089 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
2090 */
2091 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
2092 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
2093 {
2094 SHELLEXECUTEINFOW sei;
2095
2096 TRACE("\n");
2097 sei.cbSize = sizeof(sei);
2098 sei.fMask = SEE_MASK_FLAG_NO_UI;
2099 sei.hwnd = hwnd;
2100 sei.lpVerb = lpOperation;
2101 sei.lpFile = lpFile;
2102 sei.lpParameters = lpParameters;
2103 sei.lpDirectory = lpDirectory;
2104 sei.nShow = nShowCmd;
2105 sei.lpIDList = 0;
2106 sei.lpClass = 0;
2107 sei.hkeyClass = 0;
2108 sei.dwHotKey = 0;
2109 sei.hProcess = 0;
2110
2111 SHELL_execute(&sei, SHELL_ExecuteW);
2112 return sei.hInstApp;
2113 }
2114
2115 /*************************************************************************
2116 * WOWShellExecute [SHELL32.@]
2117 *
2118 * FIXME: the callback function most likely doesn't work the same way on Windows.
2119 */
2120 EXTERN_C HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile,
2121 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd, void *callback)
2122 {
2123 SHELLEXECUTEINFOW seiW;
2124 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
2125 HANDLE hProcess = 0;
2126
2127 seiW.lpVerb = lpOperation ? __SHCloneStrAtoW(&wVerb, lpOperation) : NULL;
2128 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
2129 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
2130 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
2131
2132 seiW.cbSize = sizeof(seiW);
2133 seiW.fMask = 0;
2134 seiW.hwnd = hWnd;
2135 seiW.nShow = iShowCmd;
2136 seiW.lpIDList = 0;
2137 seiW.lpClass = 0;
2138 seiW.hkeyClass = 0;
2139 seiW.dwHotKey = 0;
2140 seiW.hProcess = hProcess;
2141
2142 SHELL_execute(&seiW, (SHELL_ExecuteW32)callback);
2143
2144 SHFree(wVerb);
2145 SHFree(wFile);
2146 SHFree(wParameters);
2147 SHFree(wDirectory);
2148 return seiW.hInstApp;
2149 }
2150
2151 /*************************************************************************
2152 * OpenAs_RunDLLA [SHELL32.@]
2153 */
2154 EXTERN_C void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2155 {
2156 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2157 }
2158
2159 /*************************************************************************
2160 * OpenAs_RunDLLW [SHELL32.@]
2161 */
2162 EXTERN_C void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2163 {
2164 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);
2165 }