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