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