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