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