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