Merge Wine shell32 commit:
[reactos.git] / reactos / lib / shell32 / shlexec.c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <ctype.h>
33 #include <assert.h>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "winreg.h"
39 #include "wownt32.h"
40 #include "shellapi.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "shlobj.h"
44 #include "shlwapi.h"
45 #include "ddeml.h"
46
47 #include "wine/winbase16.h"
48 #include "shell32_main.h"
49 #include "undocshell.h"
50 #include "pidl.h"
51
52 #include "wine/debug.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(exec);
55
56 static const WCHAR wszOpen[] = {'o','p','e','n',0};
57 static const WCHAR wszExe[] = {'.','e','x','e',0};
58 static const WCHAR wszILPtr[] = {':','%','p',0};
59 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
60 static const WCHAR wszFolder[] = {'F','o','l','d','e','r',0};
61 static const WCHAR wszEmpty[] = {0};
62
63
64 /***********************************************************************
65 * SHELL_ArgifyW [Internal]
66 *
67 * this function is supposed to expand the escape sequences found in the registry
68 * some diving reported that the following were used:
69 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
70 * %1 file
71 * %2 printer
72 * %3 driver
73 * %4 port
74 * %I address of a global item ID (explorer switch /idlist)
75 * %L seems to be %1 as long filename followed by the 8+3 variation
76 * %S ???
77 * %* all following parameters (see batfile)
78 *
79 * FIXME: use 'len'
80 */
81 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args)
82 {
83 WCHAR xlpFile[1024];
84 BOOL done = FALSE;
85 PWSTR res = out;
86 PCWSTR cmd;
87 LPVOID pv;
88
89 while (*fmt)
90 {
91 if (*fmt == '%')
92 {
93 switch (*++fmt)
94 {
95 case '\0':
96 case '%':
97 *res++ = '%';
98 break;
99
100 case '2':
101 case '3':
102 case '4':
103 case '5':
104 case '6':
105 case '7':
106 case '8':
107 case '9':
108 case '0':
109 case '*':
110 if (args)
111 {
112 if (*fmt == '*')
113 {
114 *res++ = '"';
115 while(*args)
116 *res++ = *args++;
117 *res++ = '"';
118 }
119 else
120 {
121 while(*args && !isspace(*args))
122 *res++ = *args++;
123
124 while(isspace(*args))
125 ++args;
126 }
127 break;
128 }
129 /* else fall through */
130 case '1':
131 if (!done || (*fmt == '1'))
132 {
133 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
134 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
135 cmd = xlpFile;
136 else
137 cmd = lpFile;
138
139 /* Add double quotation marks unless we already have them (e.g.: "%1" %* for exefile) */
140 if (res==out || res[-1]!='"')
141 {
142 *res++ = '"';
143 strcpyW(res, cmd);
144 res += strlenW(cmd);
145 *res++ = '"';
146 }
147 else
148 {
149 strcpyW(res, cmd);
150 res += strlenW(cmd);
151 }
152 }
153 break;
154
155 /*
156 * IE uses this alot for activating things such as windows media
157 * player. This is not verified to be fully correct but it appears
158 * to work just fine.
159 */
160 case 'l':
161 case 'L':
162 if (lpFile) {
163 strcpyW(res, lpFile);
164 res += strlenW(lpFile);
165 }
166 break;
167
168 case 'i':
169 case 'I':
170 if (pidl) {
171 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
172 pv = SHLockShared(hmem, 0);
173 res += sprintfW(res, wszILPtr, pv);
174 SHUnlockShared(pv);
175 }
176 break;
177
178 default: FIXME("Unknown escape sequence %%%c\n", *fmt);
179 }
180
181 fmt++;
182 done = TRUE;
183 }
184 else
185 *res++ = *fmt++;
186 }
187
188 *res = '\0';
189
190 return done;
191 }
192
193 HRESULT SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl, LPSTR pszPath, UINT uOutSize)
194 {
195 STRRET strret;
196 IShellFolder* desktop;
197
198 HRESULT hr = SHGetDesktopFolder(&desktop);
199
200 if (SUCCEEDED(hr)) {
201 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
202
203 if (SUCCEEDED(hr))
204 StrRetToStrNA(pszPath, uOutSize, &strret, pidl);
205
206 IShellFolder_Release(desktop);
207 }
208
209 return hr;
210 }
211
212 HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
213 {
214 STRRET strret;
215 IShellFolder* desktop;
216
217 HRESULT hr = SHGetDesktopFolder(&desktop);
218
219 if (SUCCEEDED(hr)) {
220 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
221
222 if (SUCCEEDED(hr))
223 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
224
225 IShellFolder_Release(desktop);
226 }
227
228 return hr;
229 }
230
231 /*************************************************************************
232 * SHELL_ResolveShortCutW [Internal]
233 * read shortcut file at 'wcmd'
234 */
235 static HRESULT SHELL_ResolveShortCutW(LPWSTR wcmd, LPWSTR wargs, LPWSTR wdir, HWND hwnd, LPCWSTR lpVerb, int* pshowcmd, LPITEMIDLIST* ppidl)
236 {
237 IShellFolder* psf;
238
239 HRESULT hr = SHGetDesktopFolder(&psf);
240
241 *ppidl = NULL;
242
243 if (SUCCEEDED(hr)) {
244 LPITEMIDLIST pidl;
245 ULONG l;
246
247 hr = IShellFolder_ParseDisplayName(psf, 0, 0, wcmd, &l, &pidl, 0);
248
249 if (SUCCEEDED(hr)) {
250 IShellLinkW* psl;
251
252 hr = IShellFolder_GetUIObjectOf(psf, NULL, 1, (LPCITEMIDLIST*)&pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
253
254 if (SUCCEEDED(hr)) {
255 hr = IShellLinkW_Resolve(psl, hwnd, 0);
256
257 if (SUCCEEDED(hr)) {
258 hr = IShellLinkW_GetPath(psl, wcmd, MAX_PATH, NULL, SLGP_UNCPRIORITY);
259
260 if (SUCCEEDED(hr)) {
261 if (!*wcmd) {
262 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
263 hr = IShellLinkW_GetIDList(psl, ppidl);
264
265 if (SUCCEEDED(hr) && *ppidl) {
266 /* We got a PIDL instead of a file system path - try to translate it. */
267 if (SUCCEEDED(SHELL_GetPathFromIDListW(*ppidl, wcmd, MAX_PATH))) {
268 SHFree(*ppidl);
269 *ppidl = NULL;
270 }
271 }
272 }
273
274 if (SUCCEEDED(hr)) {
275 /* get command line arguments, working directory and display mode if available */
276 IShellLinkW_GetWorkingDirectory(psl, wdir, MAX_PATH);
277 IShellLinkW_GetArguments(psl, wargs, MAX_PATH);
278 IShellLinkW_GetShowCmd(psl, pshowcmd);
279 }
280 }
281 }
282
283 IShellLinkW_Release(psl);
284 }
285
286 SHFree(pidl);
287 }
288
289 IShellFolder_Release(psf);
290 }
291
292 return hr;
293 }
294
295 /*************************************************************************
296 * SHELL_ExecuteW [Internal]
297 *
298 */
299 static UINT SHELL_ExecuteW(const WCHAR *lpCmd, void *env, BOOL shWait,
300 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
301 {
302 STARTUPINFOW startup;
303 PROCESS_INFORMATION info;
304 UINT retval = 31;
305 UINT gcdret = 0;
306 WCHAR curdir[MAX_PATH];
307
308 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
309
310 /* ShellExecute specifies the command from psei->lpDirectory
311 * if present. Not from the current dir as CreateProcess does */
312 if( psei->lpDirectory && psei->lpDirectory[0] )
313 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
314 if( !SetCurrentDirectoryW( psei->lpDirectory))
315 ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory));
316
317 ZeroMemory(&startup, sizeof(startup));
318 startup.cb = sizeof(STARTUPINFOW);
319 startup.dwFlags = STARTF_USESHOWWINDOW;
320 startup.wShowWindow = psei->nShow;
321
322 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, 0,
323 env, *psei->lpDirectory? psei->lpDirectory: NULL, &startup, &info))
324 {
325 /* Give 30 seconds to the app to come up, if desired. Probably only needed
326 when starting app immediately before making a DDE connection. */
327 if (shWait)
328 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
329 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
330 retval = 33;
331
332 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
333 psei_out->hProcess = info.hProcess;
334 else
335 CloseHandle(info.hProcess);
336
337 CloseHandle(info.hThread);
338 }
339 else if ((retval = GetLastError()) >= 32)
340 {
341 FIXME("Strange error set by CreateProcess: %d\n", retval);
342 retval = ERROR_BAD_FORMAT;
343 }
344
345 TRACE("returning %u\n", retval);
346
347 psei_out->hInstApp = (HINSTANCE)retval;
348
349 if( gcdret )
350 if( !SetCurrentDirectoryW( curdir))
351 ERR("cannot return to directory %s\n", debugstr_w(curdir));
352
353 return retval;
354 }
355
356
357 /***********************************************************************
358 * SHELL_BuildEnvW [Internal]
359 *
360 * Build the environment for the new process, adding the specified
361 * path to the PATH variable. Returned pointer must be freed by caller.
362 */
363 static void *SHELL_BuildEnvW( const WCHAR *path )
364 {
365 static const WCHAR wPath[] = {'P','A','T','H','=',0};
366 WCHAR *strings, *new_env;
367 WCHAR *p, *p2;
368 int total = strlenW(path) + 1;
369 BOOL got_path = FALSE;
370
371 if (!(strings = GetEnvironmentStringsW())) return NULL;
372 p = strings;
373 while (*p)
374 {
375 int len = strlenW(p) + 1;
376 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
377 total += len;
378 p += len;
379 }
380 if (!got_path) total += 5; /* we need to create PATH */
381 total++; /* terminating null */
382
383 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
384 {
385 FreeEnvironmentStringsW( strings );
386 return NULL;
387 }
388 p = strings;
389 p2 = new_env;
390 while (*p)
391 {
392 int len = strlenW(p) + 1;
393 memcpy( p2, p, len * sizeof(WCHAR) );
394 if (!strncmpiW( p, wPath, 5 ))
395 {
396 p2[len - 1] = ';';
397 strcpyW( p2 + len, path );
398 p2 += strlenW(path) + 1;
399 }
400 p += len;
401 p2 += len;
402 }
403 if (!got_path)
404 {
405 strcpyW( p2, wPath );
406 strcatW( p2, path );
407 p2 += strlenW(p2) + 1;
408 }
409 *p2 = 0;
410 FreeEnvironmentStringsW( strings );
411 return new_env;
412 }
413
414
415 /***********************************************************************
416 * SHELL_TryAppPathW [Internal]
417 *
418 * Helper function for SHELL_FindExecutable
419 * @param lpResult - pointer to a buffer of size MAX_PATH
420 * On entry: szName is a filename (probably without path separators).
421 * On exit: if szName found in "App Path", place full path in lpResult, and return true
422 */
423 static BOOL SHELL_TryAppPathW(LPCWSTR szName, LPWSTR lpResult, void** env)
424 {
425 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',
426 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
427 static const WCHAR wPath[] = {'P','a','t','h',0};
428
429 HKEY hkApp = 0;
430 WCHAR buffer[1024];
431 LONG len;
432 LONG res;
433 BOOL found = FALSE;
434
435 if (env) *env = NULL;
436 strcpyW(buffer, wszKeyAppPaths);
437 strcatW(buffer, szName);
438 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
439 if (res) goto end;
440
441 len = MAX_PATH*sizeof(WCHAR);
442 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
443 if (res) goto end;
444 found = TRUE;
445
446 if (env)
447 {
448 DWORD count = sizeof(buffer);
449 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
450 *env = SHELL_BuildEnvW( buffer );
451 }
452
453 end:
454 if (hkApp) RegCloseKey(hkApp);
455 return found;
456 }
457
458 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
459 {
460 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
461
462 /* Looking for ...buffer\shell\<verb>\command */
463 strcatW(filetype, wszShell);
464 strcatW(filetype, lpOperation);
465 strcatW(filetype, wCommand);
466
467 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
468 &commandlen) == ERROR_SUCCESS)
469 {
470 commandlen /= sizeof(WCHAR);
471 if (key) strcpyW(key, filetype);
472 #if 0
473 LPWSTR tmp;
474 WCHAR param[256];
475 LONG paramlen = sizeof(param);
476 static const WCHAR wSpace[] = {' ',0};
477
478 /* FIXME: it seems all Windows version don't behave the same here.
479 * the doc states that this ddeexec information can be found after
480 * the exec names.
481 * on Win98, it doesn't appear, but I think it does on Win2k
482 */
483 /* Get the parameters needed by the application
484 from the associated ddeexec key */
485 tmp = strstrW(filetype, wCommand);
486 tmp[0] = '\0';
487 strcatW(filetype, wDdeexec);
488 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
489 &paramlen) == ERROR_SUCCESS)
490 {
491 paramlen /= sizeof(WCHAR);
492 strcatW(command, wSpace);
493 strcatW(command, param);
494 commandlen += paramlen;
495 }
496 #endif
497
498 command[commandlen] = '\0';
499
500 return 33; /* FIXME see SHELL_FindExecutable() */
501 }
502
503 return 31; /* default - 'No association was found' */
504 }
505
506 /*************************************************************************
507 * SHELL_FindExecutable [Internal]
508 *
509 * Utility for code sharing between FindExecutable and ShellExecute
510 * in:
511 * lpFile the name of a file
512 * lpOperation the operation on it (open)
513 * out:
514 * lpResult a buffer, big enough :-(, to store the command to do the
515 * operation on the file
516 * key a buffer, big enough, to get the key name to do actually the
517 * command (it'll be used afterwards for more information
518 * on the operation)
519 */
520 UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
521 LPWSTR lpResult, int resultLen, LPWSTR key, void **env, LPITEMIDLIST pidl, LPCWSTR args)
522 {
523 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
524 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
525 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
526
527 WCHAR *extension = NULL; /* pointer to file extension */
528 WCHAR filetype[256]; /* registry name for this filetype */
529 LONG filetypelen = sizeof(filetype); /* length of above */
530 WCHAR command[1024]; /* command from registry */
531 WCHAR wBuffer[256]; /* Used to GetProfileString */
532 UINT retval = 31; /* default - 'No association was found' */
533 WCHAR *tok; /* token pointer */
534 WCHAR xlpFile[256]; /* result of SearchPath */
535 DWORD attribs; /* file attributes */
536
537 TRACE("%s\n", (lpFile != NULL) ? debugstr_w(lpFile) : "-");
538
539 xlpFile[0] = '\0';
540 lpResult[0] = '\0'; /* Start off with an empty return string */
541 if (key) *key = '\0';
542
543 /* trap NULL parameters on entry */
544 if ((lpFile == NULL) || (lpResult == NULL))
545 {
546 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n", debugstr_w(lpFile), debugstr_w(lpResult));
547 return 2; /* File not found. Close enough, I guess. */
548 }
549
550 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
551 {
552 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
553 return 33;
554 }
555
556 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
557 {
558 TRACE("SearchPathW returned non-zero\n");
559 lpFile = xlpFile;
560 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
561 }
562
563 attribs = GetFileAttributesW(lpFile);
564 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
565 {
566 strcpyW(filetype, wszFolder);
567 filetypelen = 6; /* strlen("Folder") */
568 }
569 else
570 {
571 /* First thing we need is the file's extension */
572 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
573 /* File->Run in progman uses */
574 /* .\FILE.EXE :( */
575 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
576
577 if (extension == NULL || extension[1]==0)
578 {
579 WARN("Returning 31 - No association\n");
580 return 31; /* no association */
581 }
582
583 /* Three places to check: */
584 /* 1. win.ini, [windows], programs (NB no leading '.') */
585 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
586 /* 3. win.ini, [extensions], extension (NB no leading '.' */
587 /* All I know of the order is that registry is checked before */
588 /* extensions; however, it'd make sense to check the programs */
589 /* section first, so that's what happens here. */
590
591 /* See if it's a program - if GetProfileString fails, we skip this
592 * section. Actually, if GetProfileString fails, we've probably
593 * got a lot more to worry about than running a program... */
594 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
595 {
596 CharLowerW(wBuffer);
597 tok = wBuffer;
598 while (*tok)
599 {
600 WCHAR *p = tok;
601 while (*p && *p != ' ' && *p != '\t') p++;
602 if (*p)
603 {
604 *p++ = 0;
605 while (*p == ' ' || *p == '\t') p++;
606 }
607
608 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
609 {
610 strcpyW(lpResult, xlpFile);
611 /* Need to perhaps check that the file has a path
612 * attached */
613 TRACE("found %s\n", debugstr_w(lpResult));
614 return 33;
615
616 /* Greater than 32 to indicate success FIXME According to the
617 * docs, I should be returning a handle for the
618 * executable. Does this mean I'm supposed to open the
619 * executable file or something? More RTFM, I guess... */
620 }
621 tok = p;
622 }
623 }
624
625 /* Check registry */
626 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
627 &filetypelen) == ERROR_SUCCESS)
628 {
629 filetypelen /= sizeof(WCHAR);
630 filetype[filetypelen] = '\0';
631 TRACE("File type: %s\n", debugstr_w(filetype));
632 }
633 }
634
635 if (*filetype)
636 {
637 if (lpOperation)
638 {
639 /* pass the operation string to SHELL_FindExecutableByOperation() */
640 filetype[filetypelen] = '\0';
641 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, lpOperation, key, filetype, command, sizeof(command));
642 }
643 else
644 {
645 WCHAR operation[MAX_PATH];
646 HKEY hkey;
647
648 /* Looking for ...buffer\shell\<operation>\command */
649 strcatW(filetype, wszShell);
650
651 /* enumerate the operation subkeys in the registry and search for one with an associated command */
652 if (RegOpenKeyW(HKEY_CLASSES_ROOT, filetype, &hkey) == ERROR_SUCCESS)
653 {
654 int idx = 0;
655 for(;; ++idx)
656 {
657 if (RegEnumKeyW(hkey, idx, operation, MAX_PATH) != ERROR_SUCCESS)
658 break;
659
660 filetype[filetypelen] = '\0';
661 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, operation, key, filetype, command, sizeof(command));
662
663 if (retval > 32)
664 break;
665 }
666 RegCloseKey(hkey);
667 }
668 }
669
670 if (retval > 32)
671 {
672 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args);
673
674 /* Remove double quotation marks and command line arguments */
675 if (*lpResult == '"')
676 {
677 WCHAR *p = lpResult;
678 while (*(p + 1) != '"')
679 {
680 *p = *(p + 1);
681 p++;
682 }
683 *p = '\0';
684 }
685 }
686 }
687 else if (extension) /* Check win.ini */
688 {
689 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
690
691 /* Toss the leading dot */
692 extension++;
693 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
694 {
695 if (strlenW(command) != 0)
696 {
697 strcpyW(lpResult, command);
698 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
699
700 if (tok != NULL)
701 {
702 tok[0] = '\0';
703 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
704 tok = strchrW(command, '^'); /* see above */
705 if ((tok != NULL) && (strlenW(tok)>5))
706 {
707 strcatW(lpResult, &tok[5]);
708 }
709 }
710
711 retval = 33; /* FIXME - see above */
712 }
713 }
714 }
715
716 TRACE("returning %s\n", debugstr_w(lpResult));
717 return retval;
718 }
719
720 /******************************************************************
721 * dde_cb
722 *
723 * callback for the DDE connection. not really usefull
724 */
725 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
726 HSZ hsz1, HSZ hsz2,
727 HDDEDATA hData, DWORD dwData1, DWORD dwData2)
728 {
729 return NULL;
730 }
731
732 /******************************************************************
733 * dde_connect
734 *
735 * ShellExecute helper. Used to do an operation with a DDE connection
736 *
737 * Handles both the direct connection (try #1), and if it fails,
738 * launching an application and trying (#2) to connect to it
739 *
740 */
741 static unsigned dde_connect(WCHAR * key, WCHAR* start, WCHAR* ddeexec,
742 const WCHAR* lpFile, void *env,
743 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
744 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
745 {
746 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
747 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
748
749 WCHAR* endkey = key + strlenW(key);
750 WCHAR app[256], topic[256], ifexec[256];
751 WCHAR res[1024];
752 LONG applen, topiclen, ifexeclen;
753 WCHAR * exec;
754 DWORD ddeInst = 0;
755 DWORD tid;
756 HSZ hszApp, hszTopic;
757 HCONV hConv;
758 unsigned ret = 31;
759
760 strcpyW(endkey, wApplication);
761 applen = sizeof(app);
762 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
763 {
764 FIXME("default app name NIY %s\n", debugstr_w(key));
765 return 2;
766 }
767
768 strcpyW(endkey, wTopic);
769 topiclen = sizeof(topic);
770 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
771 {
772 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
773 strcpyW(topic, wSystem);
774 }
775
776 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
777 {
778 return 2;
779 }
780
781 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINANSI);
782 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINANSI);
783
784 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
785 exec = ddeexec;
786 if (!hConv)
787 {
788 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
789 TRACE("Launching '%s'\n", debugstr_w(start));
790 ret = execfunc(start, env, TRUE, psei, psei_out);
791 if (ret < 32)
792 {
793 TRACE("Couldn't launch\n");
794 goto error;
795 }
796 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
797 if (!hConv)
798 {
799 TRACE("Couldn't connect. ret=%d\n", ret);
800 DdeUninitialize(ddeInst);
801 SetLastError(ERROR_DDE_FAIL);
802 return 30; /* whatever */
803 }
804 strcpyW(endkey, wIfexec);
805 ifexeclen = sizeof(ifexec);
806 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
807 {
808 exec = ifexec;
809 }
810 }
811
812 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline);
813 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
814
815 ret = (DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
816 XTYP_EXECUTE, 10000, &tid) != DMLERR_NO_ERROR) ? 31 : 33;
817 DdeDisconnect(hConv);
818
819 error:
820 DdeUninitialize(ddeInst);
821
822 return ret;
823 }
824
825 /*************************************************************************
826 * execute_from_key [Internal]
827 */
828 static UINT execute_from_key(LPWSTR key, LPCWSTR lpFile, void *env, LPCWSTR szCommandline,
829 SHELL_ExecuteW32 execfunc,
830 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
831 {
832 WCHAR cmd[1024];
833 LONG cmdlen = sizeof(cmd);
834 UINT retval = 31;
835
836 cmd[0] = '\0';
837
838 /* Get the application for the registry */
839 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
840 {
841 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
842 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
843
844 LPWSTR tmp;
845 WCHAR param[256];
846 LONG paramlen = sizeof(param);
847
848 param[0] = '\0';
849
850 /* Get the parameters needed by the application
851 from the associated ddeexec key */
852 tmp = strstrW(key, wCommand);
853 assert(tmp);
854 strcpyW(tmp, wDdeexec);
855
856 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, param, &paramlen) == ERROR_SUCCESS)
857 {
858 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(param));
859 retval = dde_connect(key, cmd, param, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
860 }
861 else
862 {
863 /* Is there a replace() function anywhere? */
864 cmdlen /= sizeof(WCHAR);
865 cmd[cmdlen] = '\0';
866 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline);
867 retval = execfunc(param, env, FALSE, psei, psei_out);
868 }
869 }
870 else TRACE("ooch\n");
871
872 return retval;
873 }
874
875 /*************************************************************************
876 * FindExecutableA [SHELL32.@]
877 */
878 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
879 {
880 HINSTANCE retval;
881 WCHAR *wFile = NULL, *wDirectory = NULL;
882 WCHAR wResult[MAX_PATH];
883
884 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
885 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
886
887 retval = FindExecutableW(wFile, wDirectory, wResult);
888 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
889 if (wFile) SHFree( wFile );
890 if (wDirectory) SHFree( wDirectory );
891
892 TRACE("returning %s\n", lpResult);
893 return (HINSTANCE)retval;
894 }
895
896 /*************************************************************************
897 * FindExecutableW [SHELL32.@]
898 */
899 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
900 {
901 UINT retval = 31; /* default - 'No association was found' */
902 WCHAR old_dir[1024];
903
904 TRACE("File %s, Dir %s\n",
905 (lpFile != NULL ? debugstr_w(lpFile) : "-"), (lpDirectory != NULL ? debugstr_w(lpDirectory) : "-"));
906
907 lpResult[0] = '\0'; /* Start off with an empty return string */
908
909 /* trap NULL parameters on entry */
910 if ((lpFile == NULL) || (lpResult == NULL))
911 {
912 /* FIXME - should throw a warning, perhaps! */
913 return (HINSTANCE)2; /* File not found. Close enough, I guess. */
914 }
915
916 if (lpDirectory)
917 {
918 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
919 SetCurrentDirectoryW(lpDirectory);
920 }
921
922 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
923
924 TRACE("returning %s\n", debugstr_w(lpResult));
925 if (lpDirectory)
926 SetCurrentDirectoryW(old_dir);
927 return (HINSTANCE)retval;
928 }
929
930 /*************************************************************************
931 * ShellExecuteExW32 [Internal]
932 */
933 BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei, SHELL_ExecuteW32 execfunc)
934 {
935 static const WCHAR wQuote[] = {'"',0};
936 static const WCHAR wSpace[] = {' ',0};
937 static const WCHAR wWww[] = {'w','w','w',0};
938 static const WCHAR wFile[] = {'f','i','l','e',0};
939 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
940 static const WCHAR wExtLnk[] = {'.','l','n','k',0};
941 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
942
943 WCHAR wszApplicationName[MAX_PATH+2], wszParameters[1024], wszDir[MAX_PATH];
944 SHELLEXECUTEINFOW sei_tmp; /* modifyable copy of SHELLEXECUTEINFO struct */
945 WCHAR wfileName[MAX_PATH];
946 void *env;
947 WCHAR wszProtocol[256];
948 LPCWSTR lpFile;
949 UINT retval = 31;
950 WCHAR buffer[1024];
951 const WCHAR* ext;
952
953 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
954 memcpy(&sei_tmp, psei, sizeof(sei_tmp));
955
956 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
957 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
958 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
959 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
960 (sei_tmp.fMask & SEE_MASK_CLASSNAME) ? debugstr_w(sei_tmp.lpClass) : "not used");
961
962 psei->hProcess = NULL;
963
964 /* make copies of all path/command strings */
965 if (sei_tmp.lpFile)
966 strcpyW(wszApplicationName, sei_tmp.lpFile);
967 else
968 *wszApplicationName = '\0';
969
970 if (sei_tmp.lpParameters)
971 strcpyW(wszParameters, sei_tmp.lpParameters);
972 else
973 *wszParameters = '\0';
974
975 if (sei_tmp.lpDirectory)
976 strcpyW(wszDir, sei_tmp.lpDirectory);
977 else
978 *wszDir = '\0';
979
980 /* adjust string pointers to point to the new buffers */
981 sei_tmp.lpFile = wszApplicationName;
982 sei_tmp.lpParameters = wszParameters;
983 sei_tmp.lpDirectory = wszDir;
984
985 if (sei_tmp.fMask & (SEE_MASK_ICON | SEE_MASK_HOTKEY |
986 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
987 SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE |
988 SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR ))
989 {
990 FIXME("flags ignored: 0x%08lx\n", sei_tmp.fMask);
991 }
992
993 /* process the IDList */
994 if (sei_tmp.fMask & SEE_MASK_IDLIST)
995 {
996 IShellExecuteHookW* pSEH;
997
998 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
999
1000 if (SUCCEEDED(hr))
1001 {
1002 hr = IShellExecuteHookW_Execute(pSEH, psei);
1003
1004 IShellExecuteHookW_Release(pSEH);
1005
1006 if (hr == S_OK)
1007 return TRUE;
1008 }
1009
1010 /* try to translate PIDL directly into the corresponding file system path */
1011 if (SUCCEEDED(SHELL_GetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName/*sei_tmp.lpFile*/, sizeof(wszApplicationName)/sizeof(WCHAR))))
1012 {
1013 sei_tmp.lpIDList = NULL;
1014 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1015 }
1016
1017 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1018 }
1019
1020 if (sei_tmp.fMask & (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY))
1021 {
1022 /* launch a document by fileclass like 'WordPad.Document.1' */
1023 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1024 /* FIXME: wszParameters should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1025 HCR_GetExecuteCommandW((sei_tmp.fMask & SEE_MASK_CLASSKEY)? sei_tmp.hkeyClass: NULL,
1026 (sei_tmp.fMask & SEE_MASK_CLASSNAME)? sei_tmp.lpClass: NULL,
1027 (sei_tmp.lpVerb)? sei_tmp.lpVerb: wszOpen,
1028 wszParameters/*sei_tmp.lpParameters*/, sizeof(wszParameters)/sizeof(WCHAR));
1029
1030 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1031 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(sei_tmp.lpParameters), debugstr_w(sei_tmp.lpFile));
1032
1033 buffer[0] = '\0';
1034
1035 if (!SHELL_ArgifyW(buffer, sizeof(buffer)/sizeof(WCHAR), sei_tmp.lpParameters, sei_tmp.lpFile, sei_tmp.lpIDList, NULL) && sei_tmp.lpFile[0])
1036 {
1037 strcatW(buffer, wExtLnk);
1038 strcatW(buffer, sei_tmp.lpFile);
1039 }
1040
1041 retval = execfunc(buffer, NULL, FALSE, &sei_tmp, psei);
1042 if (retval > 32)
1043 return TRUE;
1044 else
1045 return FALSE;
1046 }
1047
1048
1049 /* resolve shell shortcuts */
1050 ext = PathFindExtensionW(sei_tmp.lpFile);
1051
1052 if (ext && !strcmpiW(ext, wExtLnk)) /* or check for: shell_attribs & SFGAO_LINK */
1053 {
1054 HRESULT hr;
1055
1056 /* expand paths before reading shell link */
1057 if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
1058 lstrcpyW(wszApplicationName/*sei_tmp.lpFile*/, buffer);
1059
1060 if (*sei_tmp.lpParameters)
1061 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1062 lstrcpyW(wszParameters/*sei_tmp.lpParameters*/, buffer);
1063
1064 hr = SHELL_ResolveShortCutW((LPWSTR)sei_tmp.lpFile, (LPWSTR)sei_tmp.lpParameters, (LPWSTR)sei_tmp.lpDirectory,
1065 sei_tmp.hwnd, sei_tmp.lpVerb?sei_tmp.lpVerb:wszEmpty, &sei_tmp.nShow, (LPITEMIDLIST*)&sei_tmp.lpIDList);
1066
1067 if (psei->lpIDList)
1068 psei->fMask |= SEE_MASK_IDLIST; //@@ nicht sei_tmp.fMask ?!
1069
1070 if (SUCCEEDED(hr))
1071 {
1072 /* repeat IDList processing if needed */
1073 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1074 {
1075 IShellExecuteHookW* pSEH;
1076
1077 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1078
1079 if (SUCCEEDED(hr))
1080 {
1081 hr = IShellExecuteHookW_Execute(pSEH, psei);
1082
1083 IShellExecuteHookW_Release(pSEH);
1084
1085 if (hr == S_OK)
1086 return TRUE;
1087 }
1088
1089 TRACE("-- idlist=%p (%s)\n", debugstr_w(sei_tmp.lpIDList), debugstr_w(sei_tmp.lpFile));
1090 }
1091 }
1092 }
1093
1094
1095 /* Has the IDList not yet been translated? */
1096 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1097 {
1098 /* last chance to translate IDList: now also allow CLSID paths */
1099 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp.lpIDList, buffer, sizeof(buffer)))) {
1100 if (buffer[0]==':' && buffer[1]==':') {
1101 /* open shell folder for the specified class GUID */
1102 strcpyW(wszParameters, buffer);
1103 strcpyW(wszApplicationName, wExplorer);
1104
1105 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1106 } else if (HCR_GetExecuteCommandW(0, wszFolder, sei_tmp.lpVerb?sei_tmp.lpVerb:wszOpen, buffer, sizeof(buffer))) {
1107 SHELL_ArgifyW(wszApplicationName, sizeof(wszApplicationName)/sizeof(WCHAR), buffer, NULL, sei_tmp.lpIDList, NULL);
1108
1109 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1110 }
1111 }
1112 }
1113
1114
1115 /* expand environment strings */
1116
1117 if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
1118 lstrcpyW(wszApplicationName/*sei_tmp.lpFile*/, buffer);
1119
1120 if (*sei_tmp.lpParameters)
1121 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1122 lstrcpyW(wszParameters/*sei_tmp.lpParameters*/, buffer);
1123
1124 if (*sei_tmp.lpDirectory)
1125 if (ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buffer, MAX_PATH))
1126 lstrcpyW(wszDir/*sei_tmp.lpDirectory*/, buffer);
1127
1128 /* Else, try to execute the filename */
1129 TRACE("execute:'%s','%s','%s'\n", debugstr_w(wszApplicationName), debugstr_w(wszCommandline), debugstr_w(wszDir));
1130
1131
1132 /* separate out command line arguments from executable file name */
1133 if (!*sei_tmp.lpParameters) {
1134 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1135 if (sei_tmp.lpFile[0] == '"') {
1136 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1137 LPWSTR dst = wfileName;
1138 LPWSTR end;
1139
1140 /* copy the unquoted executabe path to 'wfileName' */
1141 while(*src && *src!='"')
1142 *dst++ = *src++;
1143
1144 *dst = '\0';
1145
1146 if (*src == '"') {
1147 end = ++src;
1148
1149 while(isspace(*src))
1150 ++src;
1151 } else
1152 end = src;
1153
1154 /* copy the parameter string to 'wszParameters' */
1155 strcpyW(wszParameters, src);
1156
1157 /* terminate previous command string after the quote character */
1158 *end = '\0';
1159 }
1160 else
1161 {
1162 /* If the executable name is not quoted, we have to use this search loop here,
1163 that in CreateProcess() is not sufficient because it does not handle shell links. */
1164 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1165 LPWSTR space, s;
1166
1167 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1168 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1169 int idx = space-sei_tmp.lpFile;
1170 strncpyW(buffer, sei_tmp.lpFile, idx);
1171 buffer[idx] = '\0';
1172
1173 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1174 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile), xlpFile, NULL))
1175 {
1176 /* separate out command from parameter string */
1177 LPCWSTR p = space + 1;
1178
1179 while(isspaceW(*p))
1180 ++p;
1181
1182 strcpyW(wszParameters, p);
1183 *space = '\0';
1184
1185 break;
1186 }
1187 }
1188
1189 strcpyW(wfileName, sei_tmp.lpFile);
1190 }
1191 } else
1192 strcpyW(wfileName, sei_tmp.lpFile);
1193
1194 lpFile = wfileName;
1195
1196 if (sei_tmp.lpParameters[0]) {
1197 strcatW(wszApplicationName/*sei_tmp.lpFile*/, wSpace);
1198 strcatW(wszApplicationName/*sei_tmp.lpFile*/, sei_tmp.lpParameters);
1199 }
1200
1201 retval = execfunc(sei_tmp.lpFile, NULL, FALSE, &sei_tmp, psei);
1202 if (retval > 32)
1203 {
1204 /* Now, that we have successfully launched a process, we can free the PIDL.
1205 It may have been used before for %I command line options. */
1206 if (sei_tmp.lpIDList!=psei->lpIDList && sei_tmp.lpIDList)
1207 SHFree(sei_tmp.lpIDList);
1208
1209 TRACE("execfunc: retval=%d psei->hInstApp=%p\n", retval, psei->hInstApp);
1210 return TRUE;
1211 }
1212
1213 /* Else, try to find the executable */
1214 buffer[0] = '\0';
1215 retval = SHELL_FindExecutable(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, lpFile, sei_tmp.lpVerb, buffer, 1024, wszProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1216
1217 if (retval > 32) /* Found */
1218 {
1219 WCHAR wszQuotedCmd[MAX_PATH+2];
1220 /* Must quote to handle case where 'buffer' contains spaces,
1221 * else security hole if malicious user creates executable file "C:\\Program"
1222 *
1223 * FIXME: If we don't have set explicitly command line arguments, we must first
1224 * split executable path from optional command line arguments. Otherwise we would quote
1225 * the complete string with executable path _and_ arguments, which is not what we want.
1226 */
1227 strcpyW(wszQuotedCmd, wQuote);
1228 strcatW(wszQuotedCmd, buffer);
1229 strcatW(wszQuotedCmd, wQuote);
1230
1231 if (*sei_tmp.lpParameters) {
1232 strcatW(wszQuotedCmd, wSpace);
1233 strcatW(wszQuotedCmd, sei_tmp.lpParameters);
1234 }
1235
1236 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(buffer), debugstr_w(wszQuotedCmd), debugstr_w(wszProtocol));
1237
1238 if (*wszProtocol)
1239 retval = execute_from_key(wszProtocol, lpFile, env, sei_tmp.lpParameters, execfunc, &sei_tmp, psei);
1240 else
1241 retval = execfunc(wszQuotedCmd, env, FALSE, &sei_tmp, psei);
1242
1243 if (env) HeapFree( GetProcessHeap(), 0, env );
1244 }
1245 else if (PathIsURLW((LPWSTR)lpFile)) /* File not found, check for URL */
1246 {
1247 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
1248 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1249 LPWSTR lpstrRes;
1250 INT iSize;
1251
1252 lpstrRes = strchrW(lpFile, ':');
1253 if (lpstrRes)
1254 iSize = lpstrRes - lpFile;
1255 else
1256 iSize = strlenW(lpFile);
1257
1258 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1259 /* Looking for ...protocol\shell\<verb>\command */
1260 strncpyW(wszProtocol, lpFile, iSize);
1261 wszProtocol[iSize] = '\0';
1262 strcatW(wszProtocol, wszShell);
1263 strcatW(wszProtocol, sei_tmp.lpVerb? sei_tmp.lpVerb: wszOpen);
1264 strcatW(wszProtocol, wCommand);
1265
1266 /* Remove File Protocol from lpFile */
1267 /* In the case file://path/file */
1268 if (!strncmpiW(lpFile, wFile, iSize))
1269 {
1270 lpFile += iSize;
1271 while (*lpFile == ':') lpFile++;
1272 }
1273
1274 retval = execute_from_key(wszProtocol, lpFile, NULL, sei_tmp.lpParameters, execfunc, &sei_tmp, psei);
1275 }
1276 /* Check if file specified is in the form www.??????.*** */
1277 else if (!strncmpiW(lpFile, wWww, 3))
1278 {
1279 /* if so, append lpFile http:// and call ShellExecute */
1280 WCHAR lpstrTmpFile[256];
1281 strcpyW(lpstrTmpFile, wHttp);
1282 strcatW(lpstrTmpFile, lpFile);
1283 retval = (UINT)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1284 }
1285
1286 /* Now we can free the PIDL. It may have been used before for %I command line options. */
1287 if (sei_tmp.lpIDList!=psei->lpIDList && sei_tmp.lpIDList)
1288 SHFree(sei_tmp.lpIDList);
1289
1290 TRACE("retval %u\n", retval);
1291
1292 if (retval <= 32)
1293 {
1294 psei->hInstApp = (HINSTANCE)retval;
1295 return FALSE;
1296 }
1297
1298 psei->hInstApp = (HINSTANCE)33;
1299 return TRUE;
1300 }
1301
1302 /*************************************************************************
1303 * ShellExecuteA [SHELL32.290]
1304 */
1305 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation, LPCSTR lpFile,
1306 LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
1307 {
1308 SHELLEXECUTEINFOA sei;
1309 HANDLE hProcess = 0;
1310
1311 TRACE("\n");
1312 sei.cbSize = sizeof(sei);
1313 sei.fMask = 0;
1314 sei.hwnd = hWnd;
1315 sei.lpVerb = lpOperation;
1316 sei.lpFile = lpFile;
1317 sei.lpParameters = lpParameters;
1318 sei.lpDirectory = lpDirectory;
1319 sei.nShow = iShowCmd;
1320 sei.lpIDList = 0;
1321 sei.lpClass = 0;
1322 sei.hkeyClass = 0;
1323 sei.dwHotKey = 0;
1324 sei.hProcess = hProcess;
1325
1326 ShellExecuteExA (&sei);
1327 return sei.hInstApp;
1328 }
1329
1330 /*************************************************************************
1331 * ShellExecuteEx [SHELL32.291]
1332 *
1333 */
1334 BOOL WINAPI ShellExecuteExAW (LPVOID sei)
1335 {
1336 if (SHELL_OsIsUnicode())
1337 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1338 return ShellExecuteExA (sei);
1339 }
1340
1341 /*************************************************************************
1342 * ShellExecuteExA [SHELL32.292]
1343 *
1344 */
1345 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1346 {
1347 SHELLEXECUTEINFOW seiW;
1348 BOOL ret;
1349 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1350
1351 TRACE("%p\n", sei);
1352
1353 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1354
1355 if (sei->lpVerb)
1356 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1357
1358 if (sei->lpFile)
1359 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1360
1361 if (sei->lpParameters)
1362 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1363
1364 if (sei->lpDirectory)
1365 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1366
1367 if ((sei->fMask & SEE_MASK_CLASSNAME) && sei->lpClass)
1368 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1369 else
1370 seiW.lpClass = NULL;
1371
1372 ret = ShellExecuteExW32 (&seiW, SHELL_ExecuteW);
1373
1374 sei->hInstApp = seiW.hInstApp;
1375
1376 if (wVerb) SHFree(wVerb);
1377 if (wFile) SHFree(wFile);
1378 if (wParameters) SHFree(wParameters);
1379 if (wDirectory) SHFree(wDirectory);
1380 if (wClass) SHFree(wClass);
1381
1382 return ret;
1383 }
1384
1385 /*************************************************************************
1386 * ShellExecuteExW [SHELL32.293]
1387 *
1388 */
1389 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1390 {
1391 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1392 }
1393
1394 /*************************************************************************
1395 * ShellExecuteW [SHELL32.294]
1396 * from shellapi.h
1397 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1398 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1399 */
1400 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1401 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1402 {
1403 SHELLEXECUTEINFOW sei;
1404
1405 TRACE("\n");
1406 sei.cbSize = sizeof(sei);
1407 sei.fMask = 0;
1408 sei.hwnd = hwnd;
1409 sei.lpVerb = lpOperation;
1410 sei.lpFile = lpFile;
1411 sei.lpParameters = lpParameters;
1412 sei.lpDirectory = lpDirectory;
1413 sei.nShow = nShowCmd;
1414 sei.lpIDList = 0;
1415 sei.lpClass = 0;
1416 sei.hkeyClass = 0;
1417 sei.dwHotKey = 0;
1418 sei.hProcess = 0;
1419
1420 ShellExecuteExW32 (&sei, SHELL_ExecuteW);
1421 return sei.hInstApp;
1422 }