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