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