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