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