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