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