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