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