xref: /reactos/dll/win32/shell32/shlexec.cpp (revision d0354521)
1 /*
2  *          Shell Library Functions
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 2002 Eric Pouech
6  * Copyright 2018-2019 Katayama Hirofumi MZ
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22 
23 #include "precomp.h"
24 #include <undocshell.h>
25 
26 WINE_DEFAULT_DEBUG_CHANNEL(exec);
27 
28 EXTERN_C BOOL PathIsExeW(LPCWSTR lpszPath);
29 
30 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
31 
32 typedef UINT_PTR (*SHELL_ExecuteW32)(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
33                 const SHELLEXECUTEINFOW *sei, LPSHELLEXECUTEINFOW sei_out);
34 
35 static void ParseNoTildeEffect(PWSTR &res, LPCWSTR &args, DWORD &len, DWORD &used, int argNum)
36 {
37     bool firstCharQuote = false;
38     bool quotes_opened = false;
39     bool backslash_encountered = false;
40 
41     for (int curArg = 0; curArg <= argNum && *args; ++curArg)
42     {
43         firstCharQuote = false;
44         if (*args == '"')
45         {
46             quotes_opened = true;
47             firstCharQuote = true;
48             args++;
49         }
50 
51         while(*args)
52         {
53             if (*args == '\\')
54             {
55                 // if we found a backslash then flip the variable
56                 backslash_encountered = !backslash_encountered;
57             }
58             else if (*args == '"')
59             {
60                 if (quotes_opened)
61                 {
62                     if (*(args + 1) != '"')
63                     {
64                         quotes_opened = false;
65                         args++;
66                         break;
67                     }
68                     else
69                     {
70                         args++;
71                     }
72                 }
73                 else
74                 {
75                     quotes_opened = true;
76                 }
77 
78                 backslash_encountered = false;
79             }
80             else
81             {
82                 backslash_encountered = false;
83                 if (*args == ' ' && !firstCharQuote)
84                     break;
85             }
86 
87             if (curArg == argNum)
88             {
89                 used++;
90                 if (used < len)
91                     *res++ = *args;
92             }
93 
94             args++;
95         }
96 
97         while(*args == ' ')
98             ++args;
99     }
100 }
101 
102 static void ParseTildeEffect(PWSTR &res, LPCWSTR &args, DWORD &len, DWORD &used, int argNum)
103 {
104     bool quotes_opened = false;
105     bool backslash_encountered = false;
106 
107     for (int curArg = 0; curArg <= argNum && *args; ++curArg)
108     {
109         while(*args)
110         {
111             if (*args == '\\')
112             {
113                 // if we found a backslash then flip the variable
114                 backslash_encountered = !backslash_encountered;
115             }
116             else if (*args == '"')
117             {
118                 if (quotes_opened)
119                 {
120                     if (*(args + 1) != '"')
121                     {
122                         quotes_opened = false;
123                     }
124                     else
125                     {
126                         args++;
127                     }
128                 }
129                 else
130                 {
131                     quotes_opened = true;
132                 }
133 
134                 backslash_encountered = false;
135             }
136             else
137             {
138                 backslash_encountered = false;
139                 if (*args == ' ' && !quotes_opened && curArg != argNum)
140                     break;
141             }
142 
143             if (curArg == argNum)
144             {
145                 used++;
146                 if (used < len)
147                     *res++ = *args;
148             }
149 
150             args++;
151         }
152     }
153 }
154 
155 /***********************************************************************
156  * SHELL_ArgifyW [Internal]
157  *
158  * this function is supposed to expand the escape sequences found in the registry
159  * some diving reported that the following were used:
160  * + %1, %2...  seem to report to parameter of index N in ShellExecute pmts
161  * %1 file
162  * %2 printer
163  * %3 driver
164  * %4 port
165  * %I address of a global item ID (explorer switch /idlist)
166  * %L seems to be %1 as long filename followed by the 8+3 variation
167  * %S ???
168  * %W Working directory
169  * %V Use either %L or %W
170  * %* all following parameters (see batfile)
171  *
172  * The way we parse the command line arguments was determined through extensive
173  * testing and can be summed up by the following rules"
174  *
175  * - %2
176  *     - if first letter is " break on first non literal " and include any white spaces
177  *     - if first letter is NOT " break on first " or white space
178  *     - if " is opened any pair of consecutive " results in ONE literal "
179  *
180  * - %~2
181  *     - use rules from here http://www.autohotkey.net/~deleyd/parameters/parameters.htm
182  */
183 
184 static BOOL SHELL_ArgifyW(WCHAR* out, DWORD len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args, DWORD* out_len, const WCHAR* lpDir)
185 {
186     WCHAR   xlpFile[1024];
187     BOOL    done = FALSE;
188     BOOL    found_p1 = FALSE;
189     PWSTR   res = out;
190     PCWSTR  cmd;
191     DWORD   used = 0;
192     bool    tildeEffect = false;
193 
194     TRACE("Before parsing: %p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
195           debugstr_w(lpFile), pidl, args);
196 
197     while (*fmt)
198     {
199         if (*fmt == '%')
200         {
201             switch (*++fmt)
202             {
203                 case '\0':
204                 case '%':
205                 {
206                     used++;
207                     if (used < len)
208                         *res++ = '%';
209                 };
210                 break;
211 
212                 case '*':
213                 {
214                     if (args)
215                     {
216                         if (*fmt == '*')
217                         {
218                             used++;
219                             while(*args)
220                             {
221                                 used++;
222                                 if (used < len)
223                                     *res++ = *args++;
224                                 else
225                                     args++;
226                             }
227                             used++;
228                             break;
229                         }
230                     }
231                 };
232                 break;
233 
234                 case '~':
235 
236                 case '2':
237                 case '3':
238                 case '4':
239                 case '5':
240                 case '6':
241                 case '7':
242                 case '8':
243                 case '9':
244                     //case '0':
245                 {
246                     if (*fmt == '~')
247                     {
248                         fmt++;
249                         tildeEffect = true;
250                     }
251 
252                     if (args)
253                     {
254                         if (tildeEffect)
255                         {
256                             ParseTildeEffect(res, args, len, used, *fmt - '2');
257                             tildeEffect = false;
258                         }
259                         else
260                         {
261                             ParseNoTildeEffect(res, args, len, used, *fmt - '2');
262                         }
263                     }
264                 };
265                 break;
266 
267                 case '1':
268                     if (!done || (*fmt == '1'))
269                     {
270                         /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
271                         if (SearchPathW(lpDir, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
272                             cmd = xlpFile;
273                         else
274                             cmd = lpFile;
275 
276                         used += wcslen(cmd);
277                         if (used < len)
278                         {
279                             wcscpy(res, cmd);
280                             res += wcslen(cmd);
281                         }
282                     }
283                     found_p1 = TRUE;
284                     break;
285 
286                     /*
287                      * IE uses this a lot for activating things such as windows media
288                      * player. This is not verified to be fully correct but it appears
289                      * to work just fine.
290                      */
291                 case 'l':
292                 case 'L':
293                     if (lpFile)
294                     {
295                         used += wcslen(lpFile);
296                         if (used < len)
297                         {
298                             wcscpy(res, lpFile);
299                             res += wcslen(lpFile);
300                         }
301                     }
302                     found_p1 = TRUE;
303                     break;
304 
305                 case 'w':
306                 case 'W':
307                     if (lpDir)
308                     {
309                         used += wcslen(lpDir);
310                         if (used < len)
311                         {
312                             wcscpy(res, lpDir);
313                             res += wcslen(lpDir);
314                         }
315                     }
316                     break;
317 
318                 case 'v':
319                 case 'V':
320                     if (lpFile)
321                     {
322                         used += wcslen(lpFile);
323                         if (used < len)
324                         {
325                             wcscpy(res, lpFile);
326                             res += wcslen(lpFile);
327                         }
328                         found_p1 = TRUE;
329                     }
330                     else if (lpDir)
331                     {
332                         used += wcslen(lpDir);
333                         if (used < len)
334                         {
335                             wcscpy(res, lpDir);
336                             res += wcslen(lpDir);
337                         }
338                     }
339                     break;
340 
341                 case 'i':
342                 case 'I':
343                     if (pidl)
344                     {
345                         DWORD chars = 0;
346                         /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
347                             * allowing a buffer of 100 should more than exceed all needs */
348                         WCHAR buf[100];
349                         LPVOID  pv;
350                         HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
351                         pv = SHLockShared(hmem, 0);
352                         chars = swprintf(buf, L":%p", pv);
353 
354                         if (chars >= ARRAY_SIZE(buf))
355                             ERR("pidl format buffer too small!\n");
356 
357                         used += chars;
358 
359                         if (used < len)
360                         {
361                             wcscpy(res, buf);
362                             res += chars;
363                         }
364                         SHUnlockShared(pv);
365                     }
366                     found_p1 = TRUE;
367                     break;
368 
369                 default:
370                     /*
371                      * Check if this is an env-variable here...
372                      */
373 
374                     /* Make sure that we have at least one more %.*/
375                     if (strchrW(fmt, '%'))
376                     {
377                         WCHAR   tmpBuffer[1024];
378                         PWSTR   tmpB = tmpBuffer;
379                         WCHAR   tmpEnvBuff[MAX_PATH];
380                         DWORD   envRet;
381 
382                         while (*fmt != '%')
383                             *tmpB++ = *fmt++;
384                         *tmpB++ = 0;
385 
386                         TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
387 
388                         envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
389                         if (envRet == 0 || envRet > MAX_PATH)
390                         {
391                             used += wcslen(tmpBuffer);
392                             if (used < len)
393                             {
394                                 wcscpy( res, tmpBuffer );
395                                 res += wcslen(tmpBuffer);
396                             }
397                         }
398                         else
399                         {
400                             used += wcslen(tmpEnvBuff);
401                             if (used < len)
402                             {
403                                 wcscpy( res, tmpEnvBuff );
404                                 res += wcslen(tmpEnvBuff);
405                             }
406                         }
407                     }
408                     done = TRUE;
409                     break;
410             }
411             /* Don't skip past terminator (catch a single '%' at the end) */
412             if (*fmt != '\0')
413             {
414                 fmt++;
415             }
416         }
417         else
418         {
419             used ++;
420             if (used < len)
421                 *res++ = *fmt++;
422             else
423                 fmt++;
424         }
425     }
426 
427     used++;
428     if (res - out < static_cast<int>(len))
429         *res = '\0';
430     else
431         out[len-1] = '\0';
432 
433     TRACE("used %i of %i space\n", used, len);
434     if (out_len)
435         *out_len = used;
436 
437     TRACE("After parsing: %p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
438           debugstr_w(lpFile), pidl, args);
439 
440     return found_p1;
441 }
442 
443 static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
444 {
445     STRRET strret;
446     CComPtr<IShellFolder> desktop;
447 
448     HRESULT hr = SHGetDesktopFolder(&desktop);
449 
450     if (SUCCEEDED(hr))
451     {
452         hr = desktop->GetDisplayNameOf(pidl, SHGDN_FORPARSING, &strret);
453 
454         if (SUCCEEDED(hr))
455             StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
456     }
457 
458     return hr;
459 }
460 
461 /*************************************************************************
462  *    SHELL_ExecuteW [Internal]
463  *
464  */
465 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
466                                const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
467 {
468     STARTUPINFOW  startup;
469     PROCESS_INFORMATION info;
470     UINT_PTR retval = SE_ERR_NOASSOC;
471     UINT gcdret = 0;
472     WCHAR curdir[MAX_PATH];
473     DWORD dwCreationFlags;
474     const WCHAR *lpDirectory = NULL;
475 
476     TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
477 
478     /* make sure we don't fail the CreateProcess if the calling app passes in
479      * a bad working directory */
480     if (psei->lpDirectory && psei->lpDirectory[0])
481     {
482         DWORD attr = GetFileAttributesW(psei->lpDirectory);
483         if (attr != INVALID_FILE_ATTRIBUTES && attr & FILE_ATTRIBUTE_DIRECTORY)
484             lpDirectory = psei->lpDirectory;
485     }
486 
487     /* ShellExecute specifies the command from psei->lpDirectory
488      * if present. Not from the current dir as CreateProcess does */
489     if (lpDirectory)
490         if ((gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
491             if (!SetCurrentDirectoryW( lpDirectory))
492                 ERR("cannot set directory %s\n", debugstr_w(lpDirectory));
493 
494     ZeroMemory(&startup, sizeof(STARTUPINFOW));
495     startup.cb = sizeof(STARTUPINFOW);
496     startup.dwFlags = STARTF_USESHOWWINDOW;
497     startup.wShowWindow = psei->nShow;
498     dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
499     if (!(psei->fMask & SEE_MASK_NO_CONSOLE))
500         dwCreationFlags |= CREATE_NEW_CONSOLE;
501     startup.lpTitle = (LPWSTR)(psei->fMask & (SEE_MASK_HASLINKNAME | SEE_MASK_HASTITLE) ? psei->lpClass : NULL);
502 
503     if (psei->fMask & SEE_MASK_HASLINKNAME)
504         startup.dwFlags |= STARTF_TITLEISLINKNAME;
505 
506     if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
507                        lpDirectory, &startup, &info))
508     {
509         /* Give 30 seconds to the app to come up, if desired. Probably only needed
510            when starting app immediately before making a DDE connection. */
511         if (shWait)
512             if (WaitForInputIdle(info.hProcess, 30000) == WAIT_FAILED)
513                 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
514         retval = 33;
515 
516         if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
517             psei_out->hProcess = info.hProcess;
518         else
519             CloseHandle( info.hProcess );
520         CloseHandle( info.hThread );
521     }
522     else if ((retval = GetLastError()) >= 32)
523     {
524         WARN("CreateProcess returned error %ld\n", retval);
525         retval = ERROR_BAD_FORMAT;
526     }
527 
528     TRACE("returning %lu\n", retval);
529 
530     psei_out->hInstApp = (HINSTANCE)retval;
531 
532     if (gcdret)
533         if (!SetCurrentDirectoryW(curdir))
534             ERR("cannot return to directory %s\n", debugstr_w(curdir));
535 
536     return retval;
537 }
538 
539 
540 /***********************************************************************
541  *           SHELL_BuildEnvW    [Internal]
542  *
543  * Build the environment for the new process, adding the specified
544  * path to the PATH variable. Returned pointer must be freed by caller.
545  */
546 static LPWSTR SHELL_BuildEnvW( const WCHAR *path )
547 {
548     WCHAR *strings, *new_env;
549     WCHAR *p, *p2;
550     int total = wcslen(path) + 1;
551     BOOL got_path = FALSE;
552 
553     if (!(strings = GetEnvironmentStringsW())) return NULL;
554     p = strings;
555     while (*p)
556     {
557         int len = wcslen(p) + 1;
558         if (!_wcsnicmp( p, L"PATH=", 5 )) got_path = TRUE;
559         total += len;
560         p += len;
561     }
562     if (!got_path) total += 5;  /* we need to create PATH */
563     total++;  /* terminating null */
564 
565     if (!(new_env = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, total * sizeof(WCHAR))))
566     {
567         FreeEnvironmentStringsW(strings);
568         return NULL;
569     }
570     p = strings;
571     p2 = new_env;
572     while (*p)
573     {
574         int len = wcslen(p) + 1;
575         memcpy(p2, p, len * sizeof(WCHAR));
576         if (!_wcsnicmp( p, L"PATH=", 5 ))
577         {
578             p2[len - 1] = ';';
579             wcscpy( p2 + len, path );
580             p2 += wcslen(path) + 1;
581         }
582         p += len;
583         p2 += len;
584     }
585     if (!got_path)
586     {
587         wcscpy(p2, L"PATH=");
588         wcscat(p2, path);
589         p2 += wcslen(p2) + 1;
590     }
591     *p2 = 0;
592     FreeEnvironmentStringsW(strings);
593     return new_env;
594 }
595 
596 
597 /***********************************************************************
598  *           SHELL_TryAppPathW    [Internal]
599  *
600  * Helper function for SHELL_FindExecutable
601  * @param lpResult - pointer to a buffer of size MAX_PATH
602  * On entry: szName is a filename (probably without path separators).
603  * On exit: if szName found in "App Path", place full path in lpResult, and return true
604  */
605 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
606 {
607     HKEY hkApp = 0;
608     WCHAR buffer[1024];
609     LONG len;
610     LONG res;
611     BOOL found = FALSE;
612 
613     if (env) *env = NULL;
614     wcscpy(buffer, L"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
615     wcscat(buffer, szName);
616     res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
617     if (res)
618     {
619         // Add ".exe" extension, if extension does not exists
620         if (PathAddExtensionW(buffer, L".exe"))
621         {
622             res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
623         }
624         if (res) goto end;
625     }
626 
627     len = MAX_PATH * sizeof(WCHAR);
628     res = RegQueryValueW(hkApp, NULL, lpResult, &len);
629     if (res) goto end;
630     found = TRUE;
631 
632     if (env)
633     {
634         DWORD count = sizeof(buffer);
635         if (!RegQueryValueExW(hkApp, L"Path", NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
636             *env = SHELL_BuildEnvW(buffer);
637     }
638 
639 end:
640     if (hkApp) RegCloseKey(hkApp);
641     return found;
642 }
643 
644 /*************************************************************************
645  *     SHELL_FindExecutableByVerb [Internal]
646  *
647  * called from SHELL_FindExecutable or SHELL_execute_class
648  * in/out:
649  *      classname a buffer, big enough, to get the key name to do actually the
650  *              command   "WordPad.Document.1\\shell\\open\\command"
651  *              passed as "WordPad.Document.1"
652  * in:
653  *      lpVerb the operation on it (open)
654  *      commandlen the size of command buffer (in bytes)
655  * out:
656  *      command a buffer, to store the command to do the
657  *              operation on the file
658  *      key a buffer, big enough, to get the key name to do actually the
659  *              command "WordPad.Document.1\\shell\\open\\command"
660  *              Can be NULL
661  */
662 static UINT SHELL_FindExecutableByVerb(LPCWSTR lpVerb, LPWSTR key, LPWSTR classname, LPWSTR command, LONG commandlen)
663 {
664     HKEY hkeyClass;
665     WCHAR verb[MAX_PATH];
666 
667     if (RegOpenKeyExW(HKEY_CLASSES_ROOT, classname, 0, 0x02000000, &hkeyClass))
668         return SE_ERR_NOASSOC;
669     if (!HCR_GetDefaultVerbW(hkeyClass, lpVerb, verb, ARRAY_SIZE(verb)))
670         return SE_ERR_NOASSOC;
671     RegCloseKey(hkeyClass);
672 
673     /* Looking for ...buffer\shell\<verb>\command */
674     wcscat(classname, L"\\shell\\");
675     wcscat(classname, verb);
676     wcscat(classname, L"\\command");
677 
678     if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, command,
679                        &commandlen) == ERROR_SUCCESS)
680     {
681         commandlen /= sizeof(WCHAR);
682         if (key) wcscpy(key, classname);
683 #if 0
684         LPWSTR tmp;
685         WCHAR param[256];
686         LONG paramlen = sizeof(param);
687 
688         /* FIXME: it seems all Windows version don't behave the same here.
689          * the doc states that this ddeexec information can be found after
690          * the exec names.
691          * on Win98, it doesn't appear, but I think it does on Win2k
692          */
693         /* Get the parameters needed by the application
694            from the associated ddeexec key */
695         tmp = strstrW(classname, L"\\command");
696         tmp[0] = '\0';
697         wcscat(classname, wDdeexec);
698         if (RegQueryValueW(HKEY_CLASSES_ROOT, classname, param,
699                            &paramlen) == ERROR_SUCCESS)
700         {
701             paramlen /= sizeof(WCHAR);
702             wcscat(command, L" ");
703             wcscat(command, param);
704             commandlen += paramlen;
705         }
706 #endif
707 
708         command[commandlen] = '\0';
709 
710         return 33; /* FIXME see SHELL_FindExecutable() */
711     }
712 
713     return SE_ERR_NOASSOC;
714 }
715 
716 /*************************************************************************
717  *    SHELL_FindExecutable [Internal]
718  *
719  * Utility for code sharing between FindExecutable and ShellExecute
720  * in:
721  *      lpFile the name of a file
722  *      lpVerb the operation on it (open)
723  * out:
724  *      lpResult a buffer, big enough :-(, to store the command to do the
725  *              operation on the file
726  *      key a buffer, big enough, to get the key name to do actually the
727  *              command (it'll be used afterwards for more information
728  *              on the operation)
729  */
730 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpVerb,
731                                  LPWSTR lpResult, DWORD resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
732 {
733     WCHAR *extension = NULL; /* pointer to file extension */
734     WCHAR classname[256];     /* registry name for this file type */
735     LONG  classnamelen = sizeof(classname); /* length of above */
736     WCHAR command[1024];     /* command from registry */
737     WCHAR wBuffer[256];      /* Used to GetProfileString */
738     UINT  retval = SE_ERR_NOASSOC;
739     WCHAR *tok;              /* token pointer */
740     WCHAR xlpFile[256];      /* result of SearchPath */
741     DWORD attribs;           /* file attributes */
742 
743     TRACE("%s\n", debugstr_w(lpFile));
744 
745     if (!lpResult)
746         return ERROR_INVALID_PARAMETER;
747 
748     xlpFile[0] = '\0';
749     lpResult[0] = '\0'; /* Start off with an empty return string */
750     if (key) *key = '\0';
751 
752     /* trap NULL parameters on entry */
753     if (!lpFile)
754     {
755         WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
756              debugstr_w(lpFile), debugstr_w(lpResult));
757         return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
758     }
759 
760     if (SHELL_TryAppPathW( lpFile, lpResult, env ))
761     {
762         TRACE("found %s via App Paths\n", debugstr_w(lpResult));
763         return 33;
764     }
765 
766     if (SearchPathW(lpPath, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
767     {
768         TRACE("SearchPathW returned non-zero\n");
769         lpFile = xlpFile;
770         /* The file was found in the application-supplied default directory (or the system search path) */
771     }
772     else if (lpPath && SearchPathW(NULL, lpFile, L".exe", ARRAY_SIZE(xlpFile), xlpFile, NULL))
773     {
774         TRACE("SearchPathW returned non-zero\n");
775         lpFile = xlpFile;
776         /* The file was found in one of the directories in the system-wide search path */
777     }
778 
779     attribs = GetFileAttributesW(lpFile);
780     if (attribs != INVALID_FILE_ATTRIBUTES && (attribs & FILE_ATTRIBUTE_DIRECTORY))
781     {
782         wcscpy(classname, L"Folder");
783     }
784     else
785     {
786         /* Did we get something? Anything? */
787         if (xlpFile[0] == 0)
788         {
789             TRACE("Returning SE_ERR_FNF\n");
790             return SE_ERR_FNF;
791         }
792         /* First thing we need is the file's extension */
793         extension = wcsrchr(xlpFile, '.'); /* Assume last "." is the one; */
794         /* File->Run in progman uses */
795         /* .\FILE.EXE :( */
796         TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
797 
798         if (extension == NULL || extension[1] == 0)
799         {
800             WARN("Returning SE_ERR_NOASSOC\n");
801             return SE_ERR_NOASSOC;
802         }
803 
804         /* Three places to check: */
805         /* 1. win.ini, [windows], programs (NB no leading '.') */
806         /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
807         /* 3. win.ini, [extensions], extension (NB no leading '.' */
808         /* All I know of the order is that registry is checked before */
809         /* extensions; however, it'd make sense to check the programs */
810         /* section first, so that's what happens here. */
811 
812         /* See if it's a program - if GetProfileString fails, we skip this
813          * section. Actually, if GetProfileString fails, we've probably
814          * got a lot more to worry about than running a program... */
815         if (GetProfileStringW(L"windows", L"programs", L"exe pif bat cmd com", wBuffer, ARRAY_SIZE(wBuffer)) > 0)
816         {
817             CharLowerW(wBuffer);
818             tok = wBuffer;
819             while (*tok)
820             {
821                 WCHAR *p = tok;
822                 while (*p && *p != ' ' && *p != '\t') p++;
823                 if (*p)
824                 {
825                     *p++ = 0;
826                     while (*p == ' ' || *p == '\t') p++;
827                 }
828 
829                 if (wcsicmp(tok, &extension[1]) == 0) /* have to skip the leading "." */
830                 {
831                     wcscpy(lpResult, xlpFile);
832                     /* Need to perhaps check that the file has a path
833                      * attached */
834                     TRACE("found %s\n", debugstr_w(lpResult));
835                     return 33;
836                     /* Greater than 32 to indicate success */
837                 }
838                 tok = p;
839             }
840         }
841 
842         /* Check registry */
843         if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, classname,
844                            &classnamelen) == ERROR_SUCCESS)
845         {
846             classnamelen /= sizeof(WCHAR);
847             if (classnamelen == ARRAY_SIZE(classname))
848                 classnamelen--;
849 
850             classname[classnamelen] = '\0';
851             TRACE("File type: %s\n", debugstr_w(classname));
852         }
853         else
854         {
855             *classname = '\0';
856         }
857     }
858 
859     if (*classname)
860     {
861         /* pass the verb string to SHELL_FindExecutableByVerb() */
862         retval = SHELL_FindExecutableByVerb(lpVerb, key, classname, command, sizeof(command));
863 
864         if (retval > 32)
865         {
866             DWORD finishedLen;
867             SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen, lpPath);
868             if (finishedLen > resultLen)
869                 ERR("Argify buffer not large enough.. truncated\n");
870             /* Remove double quotation marks and command line arguments */
871             if (*lpResult == '"')
872             {
873                 WCHAR *p = lpResult;
874                 while (*(p + 1) != '"')
875                 {
876                     *p = *(p + 1);
877                     p++;
878                 }
879                 *p = '\0';
880             }
881             else
882             {
883                 /* Truncate on first space */
884                 WCHAR *p = lpResult;
885                 while (*p != ' ' && *p != '\0')
886                     p++;
887                 *p = '\0';
888             }
889         }
890     }
891     else /* Check win.ini */
892     {
893         /* Toss the leading dot */
894         extension++;
895         if (GetProfileStringW(L"extensions", extension, L"", command, ARRAY_SIZE(command)) > 0)
896         {
897             if (wcslen(command) != 0)
898             {
899                 wcscpy(lpResult, command);
900                 tok = wcschr(lpResult, '^'); /* should be ^.extension? */
901                 if (tok != NULL)
902                 {
903                     tok[0] = '\0';
904                     wcscat(lpResult, xlpFile); /* what if no dir in xlpFile? */
905                     tok = wcschr(command, '^'); /* see above */
906                     if ((tok != NULL) && (wcslen(tok) > 5))
907                     {
908                         wcscat(lpResult, &tok[5]);
909                     }
910                 }
911                 retval = 33; /* FIXME - see above */
912             }
913         }
914     }
915 
916     TRACE("returning %s\n", debugstr_w(lpResult));
917     return retval;
918 }
919 
920 /******************************************************************
921  *        dde_cb
922  *
923  * callback for the DDE connection. not really useful
924  */
925 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
926                                 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
927                                 ULONG_PTR dwData1, ULONG_PTR dwData2)
928 {
929     TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
930           uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
931     return NULL;
932 }
933 
934 /******************************************************************
935  *        dde_connect
936  *
937  * ShellExecute helper. Used to do an operation with a DDE connection
938  *
939  * Handles both the direct connection (try #1), and if it fails,
940  * launching an application and trying (#2) to connect to it
941  *
942  */
943 static unsigned dde_connect(const WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
944                             const WCHAR* lpFile, WCHAR *env,
945                             LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
946                             const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
947 {
948     WCHAR       regkey[256];
949     WCHAR *     endkey = regkey + wcslen(key);
950     WCHAR       app[256], topic[256], ifexec[256], static_res[256];
951     WCHAR *     dynamic_res=NULL;
952     WCHAR *     res;
953     LONG        applen, topiclen, ifexeclen;
954     WCHAR *     exec;
955     DWORD       ddeInst = 0;
956     DWORD       tid;
957     DWORD       resultLen, endkeyLen;
958     HSZ         hszApp, hszTopic;
959     HCONV       hConv;
960     HDDEDATA    hDdeData;
961     unsigned    ret = SE_ERR_NOASSOC;
962     BOOL unicode = !(GetVersion() & 0x80000000);
963 
964     if (strlenW(key) + 1 > ARRAY_SIZE(regkey))
965     {
966         FIXME("input parameter %s larger than buffer\n", debugstr_w(key));
967         return 2;
968     }
969     wcscpy(regkey, key);
970     endkeyLen = ARRAY_SIZE(regkey) - (endkey - regkey);
971     if (strlenW(L"\\application") + 1 > endkeyLen)
972     {
973         FIXME("endkey %s overruns buffer\n", debugstr_w(L"\\application"));
974         return 2;
975     }
976     wcscpy(endkey, L"\\application");
977     applen = sizeof(app);
978     if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
979     {
980         WCHAR command[1024], fullpath[MAX_PATH];
981         LPWSTR ptr = NULL;
982         DWORD ret = 0;
983 
984         /* Get application command from start string and find filename of application */
985         if (*start == '"')
986         {
987             if (strlenW(start + 1) + 1 > ARRAY_SIZE(command))
988             {
989                 FIXME("size of input parameter %s larger than buffer\n",
990                       debugstr_w(start + 1));
991                 return 2;
992             }
993             wcscpy(command, start + 1);
994             if ((ptr = wcschr(command, '"')))
995                 * ptr = 0;
996             ret = SearchPathW(NULL, command, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr);
997         }
998         else
999         {
1000             LPCWSTR p;
1001             LPWSTR space;
1002             for (p = start; (space = const_cast<LPWSTR>(strchrW(p, ' '))); p = space + 1)
1003             {
1004                 int idx = space - start;
1005                 memcpy(command, start, idx * sizeof(WCHAR));
1006                 command[idx] = '\0';
1007                 if ((ret = SearchPathW(NULL, command, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr)))
1008                     break;
1009             }
1010             if (!ret)
1011                 ret = SearchPathW(NULL, start, L".exe", ARRAY_SIZE(fullpath), fullpath, &ptr);
1012         }
1013 
1014         if (!ret)
1015         {
1016             ERR("Unable to find application path for command %s\n", debugstr_w(start));
1017             return ERROR_ACCESS_DENIED;
1018         }
1019         if (strlenW(ptr) + 1 > ARRAY_SIZE(app))
1020         {
1021             FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr));
1022             return 2;
1023         }
1024         wcscpy(app, ptr);
1025 
1026         /* Remove extensions (including .so) */
1027         ptr = app + wcslen(app) - 3;
1028         if (ptr > app && !wcscmp(ptr, L".so"))
1029             *ptr = 0;
1030 
1031         ptr = const_cast<LPWSTR>(strrchrW(app, '.'));
1032         assert(ptr);
1033         *ptr = 0;
1034     }
1035 
1036     if (strlenW(L"\\topic") + 1 > endkeyLen)
1037     {
1038         FIXME("endkey %s overruns buffer\n", debugstr_w(L"\\topic"));
1039         return 2;
1040     }
1041     wcscpy(endkey, L"\\topic");
1042     topiclen = sizeof(topic);
1043     if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
1044     {
1045         wcscpy(topic, L"System");
1046     }
1047 
1048     if (unicode)
1049     {
1050         if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
1051             return 2;
1052     }
1053     else
1054     {
1055         if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
1056             return 2;
1057     }
1058 
1059     hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
1060     hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
1061 
1062     hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
1063     exec = ddeexec;
1064     if (!hConv)
1065     {
1066         TRACE("Launching %s\n", debugstr_w(start));
1067         ret = execfunc(start, env, TRUE, psei, psei_out);
1068         if (ret <= 32)
1069         {
1070             TRACE("Couldn't launch\n");
1071             goto error;
1072         }
1073         /* if ddeexec is NULL, then we just need to exit here */
1074         if (ddeexec == NULL)
1075         {
1076             TRACE("Exiting because ddeexec is NULL. ret=42.\n");
1077             /* See https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew */
1078             /* for reason why we use 42 here and also "Shell32_apitest ShellExecuteW" regression test */
1079             return 42;
1080         }
1081         /* if ddeexec is 'empty string', then we just need to exit here */
1082         if (wcscmp(ddeexec, L"") == 0)
1083         {
1084             TRACE("Exiting because ddeexec is 'empty string'. ret=42.\n");
1085             /* See https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutew */
1086             /* for reason why we use 42 here and also "Shell32_apitest ShellExecuteW" regression test */
1087             return 42;
1088         }
1089         hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
1090         if (!hConv)
1091         {
1092             TRACE("Couldn't connect. ret=%d\n", ret);
1093             DdeUninitialize(ddeInst);
1094             SetLastError(ERROR_DDE_FAIL);
1095             return 30; /* whatever */
1096         }
1097         if (strlenW(L"\\ifexec") + 1 > endkeyLen)
1098         {
1099             FIXME("endkey %s overruns buffer\n", debugstr_w(L"\\ifexec"));
1100             return 2;
1101         }
1102         strcpyW(endkey, L"\\ifexec");
1103         ifexeclen = sizeof(ifexec);
1104         if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
1105         {
1106             exec = ifexec;
1107         }
1108     }
1109 
1110     SHELL_ArgifyW(static_res, ARRAY_SIZE(static_res), exec, lpFile, pidl, szCommandline, &resultLen, NULL);
1111     if (resultLen > ARRAY_SIZE(static_res))
1112     {
1113         res = dynamic_res = static_cast<WCHAR *>(HeapAlloc(GetProcessHeap(), 0, resultLen * sizeof(WCHAR)));
1114         SHELL_ArgifyW(dynamic_res, resultLen, exec, lpFile, pidl, szCommandline, NULL, NULL);
1115     }
1116     else
1117         res = static_res;
1118     TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
1119 
1120     /* It's documented in the KB 330337 that IE has a bug and returns
1121      * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
1122      */
1123     if (unicode)
1124         hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0, XTYP_EXECUTE, 30000, &tid);
1125     else
1126     {
1127         DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
1128         char *resA = (LPSTR)HeapAlloc(GetProcessHeap(), 0, lenA);
1129         WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
1130         hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
1131                                          XTYP_EXECUTE, 10000, &tid );
1132         HeapFree(GetProcessHeap(), 0, resA);
1133     }
1134     if (hDdeData)
1135         DdeFreeDataHandle(hDdeData);
1136     else
1137         WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
1138     ret = 33;
1139 
1140     HeapFree(GetProcessHeap(), 0, dynamic_res);
1141 
1142     DdeDisconnect(hConv);
1143 
1144 error:
1145     DdeUninitialize(ddeInst);
1146 
1147     return ret;
1148 }
1149 
1150 /*************************************************************************
1151  *    execute_from_key [Internal]
1152  */
1153 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env,
1154                                  LPCWSTR szCommandline, LPCWSTR executable_name,
1155                                  SHELL_ExecuteW32 execfunc,
1156                                  LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
1157 {
1158     WCHAR cmd[256], param[1024], ddeexec[256];
1159     DWORD cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
1160     UINT_PTR retval = SE_ERR_NOASSOC;
1161     DWORD resultLen;
1162     LPWSTR tmp;
1163 
1164     TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
1165           debugstr_w(szCommandline), debugstr_w(executable_name));
1166 
1167     cmd[0] = '\0';
1168     param[0] = '\0';
1169 
1170     /* Get the application from the registry */
1171     if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, (LONG *)&cmdlen) == ERROR_SUCCESS)
1172     {
1173         TRACE("got cmd: %s\n", debugstr_w(cmd));
1174 
1175         /* Is there a replace() function anywhere? */
1176         cmdlen /= sizeof(WCHAR);
1177         if (cmdlen >= ARRAY_SIZE(cmd))
1178             cmdlen = ARRAY_SIZE(cmd) - 1;
1179         cmd[cmdlen] = '\0';
1180         SHELL_ArgifyW(param, ARRAY_SIZE(param), cmd, lpFile, (LPITEMIDLIST)psei->lpIDList, szCommandline, &resultLen,
1181                       (psei->lpDirectory && *psei->lpDirectory) ? psei->lpDirectory : NULL);
1182         if (resultLen > ARRAY_SIZE(param))
1183             ERR("Argify buffer not large enough, truncating\n");
1184     }
1185 
1186     /* Get the parameters needed by the application
1187        from the associated ddeexec key */
1188     tmp = const_cast<LPWSTR>(strstrW(key, L"command"));
1189     assert(tmp);
1190     wcscpy(tmp, L"ddeexec");
1191 
1192     if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, (LONG *)&ddeexeclen) == ERROR_SUCCESS)
1193     {
1194         TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
1195         if (!param[0]) strcpyW(param, executable_name);
1196         retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, (LPITEMIDLIST)psei->lpIDList, execfunc, psei, psei_out);
1197     }
1198     else if (param[0])
1199     {
1200         TRACE("executing: %s\n", debugstr_w(param));
1201         retval = execfunc(param, env, FALSE, psei, psei_out);
1202     }
1203     else
1204         WARN("Nothing appropriate found for %s\n", debugstr_w(key));
1205 
1206     return retval;
1207 }
1208 
1209 /*************************************************************************
1210  * FindExecutableA            [SHELL32.@]
1211  */
1212 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
1213 {
1214     HINSTANCE retval;
1215     WCHAR *wFile = NULL, *wDirectory = NULL;
1216     WCHAR wResult[MAX_PATH];
1217 
1218     if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
1219     if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
1220 
1221     retval = FindExecutableW(wFile, wDirectory, wResult);
1222     WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
1223     SHFree(wFile);
1224     SHFree(wDirectory);
1225 
1226     TRACE("returning %s\n", lpResult);
1227     return retval;
1228 }
1229 
1230 /*************************************************************************
1231  * FindExecutableW            [SHELL32.@]
1232  *
1233  * This function returns the executable associated with the specified file
1234  * for the default verb.
1235  *
1236  * PARAMS
1237  *  lpFile   [I] The file to find the association for. This must refer to
1238  *               an existing file otherwise FindExecutable fails and returns
1239  *               SE_ERR_FNF.
1240  *  lpResult [O] Points to a buffer into which the executable path is
1241  *               copied. This parameter must not be NULL otherwise
1242  *               FindExecutable() segfaults. The buffer must be of size at
1243  *               least MAX_PATH characters.
1244  *
1245  * RETURNS
1246  *  A value greater than 32 on success, less than or equal to 32 otherwise.
1247  *  See the SE_ERR_* constants.
1248  *
1249  * NOTES
1250  *  On Windows XP and 2003, FindExecutable() seems to first convert the
1251  *  filename into 8.3 format, thus taking into account only the first three
1252  *  characters of the extension, and expects to find an association for those.
1253  *  However other Windows versions behave sanely.
1254  */
1255 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1256 {
1257     UINT_PTR retval = SE_ERR_NOASSOC;
1258     WCHAR old_dir[1024];
1259     WCHAR res[MAX_PATH];
1260 
1261     TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1262 
1263     lpResult[0] = '\0'; /* Start off with an empty return string */
1264     if (lpFile == NULL)
1265         return (HINSTANCE)SE_ERR_FNF;
1266 
1267     if (lpDirectory)
1268     {
1269         GetCurrentDirectoryW(ARRAY_SIZE(old_dir), old_dir);
1270         SetCurrentDirectoryW(lpDirectory);
1271     }
1272 
1273     retval = SHELL_FindExecutable(lpDirectory, lpFile, L"open", res, MAX_PATH, NULL, NULL, NULL, NULL);
1274     if (retval > 32)
1275         strcpyW(lpResult, res);
1276 
1277     TRACE("returning %s\n", debugstr_w(lpResult));
1278     if (lpDirectory)
1279         SetCurrentDirectoryW(old_dir);
1280     return (HINSTANCE)retval;
1281 }
1282 
1283 /* FIXME: is this already implemented somewhere else? */
1284 static HKEY ShellExecute_GetClassKey(const SHELLEXECUTEINFOW *sei)
1285 {
1286     LPCWSTR ext = NULL, lpClass = NULL;
1287     LPWSTR cls = NULL;
1288     DWORD type = 0, sz = 0;
1289     HKEY hkey = 0;
1290     LONG r;
1291 
1292     if (sei->fMask & SEE_MASK_CLASSALL)
1293         return sei->hkeyClass;
1294 
1295     if (sei->fMask & SEE_MASK_CLASSNAME)
1296         lpClass = sei->lpClass;
1297     else
1298     {
1299         ext = PathFindExtensionW(sei->lpFile);
1300         TRACE("ext = %s\n", debugstr_w(ext));
1301         if (!ext)
1302             return hkey;
1303 
1304         r = RegOpenKeyW(HKEY_CLASSES_ROOT, ext, &hkey);
1305         if (r != ERROR_SUCCESS)
1306             return hkey;
1307 
1308         r = RegQueryValueExW(hkey, NULL, 0, &type, NULL, &sz);
1309         if (r == ERROR_SUCCESS && type == REG_SZ)
1310         {
1311             sz += sizeof (WCHAR);
1312             cls = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sz);
1313             cls[0] = 0;
1314             RegQueryValueExW(hkey, NULL, 0, &type, (LPBYTE) cls, &sz);
1315         }
1316 
1317         RegCloseKey( hkey );
1318         lpClass = cls;
1319     }
1320 
1321     TRACE("class = %s\n", debugstr_w(lpClass));
1322 
1323     hkey = 0;
1324     if (lpClass)
1325         RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey);
1326 
1327     HeapFree(GetProcessHeap(), 0, cls);
1328 
1329     return hkey;
1330 }
1331 
1332 static HRESULT shellex_get_dataobj( LPSHELLEXECUTEINFOW sei, CComPtr<IDataObject>& dataObj)
1333 {
1334     CComHeapPtr<ITEMIDLIST> allocatedPidl;
1335     LPITEMIDLIST pidl = NULL;
1336 
1337     if (sei->fMask & SEE_MASK_CLASSALL)
1338     {
1339         pidl = (LPITEMIDLIST)sei->lpIDList;
1340     }
1341     else
1342     {
1343         WCHAR fullpath[MAX_PATH];
1344         BOOL ret;
1345 
1346         fullpath[0] = 0;
1347         ret = GetFullPathNameW(sei->lpFile, MAX_PATH, fullpath, NULL);
1348         if (!ret)
1349             return HRESULT_FROM_WIN32(GetLastError());
1350 
1351         pidl = ILCreateFromPathW(fullpath);
1352         allocatedPidl.Attach(pidl);
1353     }
1354 
1355     CComPtr<IShellFolder> shf;
1356     LPCITEMIDLIST pidllast = NULL;
1357     HRESULT hr = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &shf), &pidllast);
1358     if (FAILED_UNEXPECTEDLY(hr))
1359         return hr;
1360 
1361     return shf->GetUIObjectOf(NULL, 1, &pidllast, IID_NULL_PPV_ARG(IDataObject, &dataObj));
1362 }
1363 
1364 static HRESULT shellex_run_context_menu_default(IShellExtInit *obj,
1365         LPSHELLEXECUTEINFOW sei)
1366 {
1367     CComPtr<IContextMenu> cm = NULL;
1368     CMINVOKECOMMANDINFOEX ici;
1369     MENUITEMINFOW info;
1370     WCHAR string[0x80];
1371     INT i, n, def = -1;
1372     HMENU hmenu = 0;
1373     HRESULT r;
1374 
1375     TRACE("%p %p\n", obj, sei);
1376 
1377     r = obj->QueryInterface(IID_PPV_ARG(IContextMenu, &cm));
1378     if (FAILED(r))
1379         return r;
1380 
1381     hmenu = CreateMenu();
1382     if (!hmenu)
1383         goto end;
1384 
1385     /* the number of the last menu added is returned in r */
1386     r = cm->QueryContextMenu(hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY);
1387     if (FAILED(r))
1388         goto end;
1389 
1390     n = GetMenuItemCount(hmenu);
1391     for (i = 0; i < n; i++)
1392     {
1393         memset(&info, 0, sizeof(info));
1394         info.cbSize = sizeof info;
1395         info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1396         info.dwTypeData = string;
1397         info.cch = sizeof string;
1398         string[0] = 0;
1399         GetMenuItemInfoW(hmenu, i, TRUE, &info);
1400 
1401         TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1402               info.fState, info.dwItemData, info.fType, info.wID);
1403         if ((!sei->lpVerb && (info.fState & MFS_DEFAULT)) ||
1404             (sei->lpVerb && !lstrcmpiW(sei->lpVerb, string)))
1405         {
1406             def = i;
1407             break;
1408         }
1409     }
1410 
1411     r = E_FAIL;
1412     if (def == -1)
1413         goto end;
1414 
1415     memset(&ici, 0, sizeof ici);
1416     ici.cbSize = sizeof ici;
1417     ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NO_CONSOLE | SEE_MASK_NOASYNC | SEE_MASK_ASYNCOK | SEE_MASK_FLAG_NO_UI));
1418     ici.nShow = sei->nShow;
1419     ici.lpVerb = MAKEINTRESOURCEA(def);
1420     ici.hwnd = sei->hwnd;
1421     ici.lpParametersW = sei->lpParameters;
1422 
1423     r = cm->InvokeCommand((LPCMINVOKECOMMANDINFO)&ici);
1424 
1425     TRACE("invoke command returned %08x\n", r);
1426 
1427 end:
1428     if (hmenu)
1429         DestroyMenu( hmenu );
1430     return r;
1431 }
1432 
1433 static HRESULT shellex_load_object_and_run(HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei)
1434 {
1435     TRACE("%p %s %p\n", hkey, debugstr_guid(guid), sei);
1436 
1437     CCoInit coInit;
1438 
1439     if (FAILED_UNEXPECTEDLY(coInit.hr))
1440         return coInit.hr;
1441 
1442     CComPtr<IShellExtInit> obj;
1443     HRESULT hr = CoCreateInstance(*guid, NULL, CLSCTX_INPROC_SERVER,
1444                          IID_PPV_ARG(IShellExtInit, &obj));
1445     if (FAILED_UNEXPECTEDLY(hr))
1446         return hr;
1447 
1448     CComPtr<IDataObject> dataobj;
1449     hr = shellex_get_dataobj(sei, dataobj);
1450     if (FAILED_UNEXPECTEDLY(hr))
1451         return hr;
1452 
1453     hr = obj->Initialize(NULL, dataobj, hkey);
1454     if (FAILED_UNEXPECTEDLY(hr))
1455         return hr;
1456 
1457     CComPtr<IObjectWithSite> ows;
1458     hr = obj->QueryInterface(IID_PPV_ARG(IObjectWithSite, &ows));
1459     if (FAILED_UNEXPECTEDLY(hr))
1460         return hr;
1461 
1462     ows->SetSite(NULL);
1463 
1464     return shellex_run_context_menu_default(obj, sei);
1465 }
1466 
1467 static HRESULT shellex_get_contextmenu(LPSHELLEXECUTEINFOW sei, CComPtr<IContextMenu>& cm)
1468 {
1469     CComHeapPtr<ITEMIDLIST> allocatedPidl;
1470     LPITEMIDLIST pidl = NULL;
1471 
1472     if (sei->lpIDList)
1473     {
1474         pidl = (LPITEMIDLIST)sei->lpIDList;
1475     }
1476     else
1477     {
1478         SFGAOF sfga = 0;
1479         HRESULT hr = SHParseDisplayName(sei->lpFile, NULL, &allocatedPidl, SFGAO_STORAGECAPMASK, &sfga);
1480         if (FAILED(hr))
1481         {
1482             WCHAR Buffer[MAX_PATH] = {};
1483             // FIXME: MAX_PATH.....
1484             UINT retval = SHELL_FindExecutable(sei->lpDirectory, sei->lpFile, sei->lpVerb, Buffer, _countof(Buffer), NULL, NULL, NULL, sei->lpParameters);
1485             if (retval <= 32)
1486                 return HRESULT_FROM_WIN32(retval);
1487 
1488             hr = SHParseDisplayName(Buffer, NULL, &allocatedPidl, SFGAO_STORAGECAPMASK, &sfga);
1489             // This should not happen, we found it...
1490             if (FAILED_UNEXPECTEDLY(hr))
1491                 return hr;
1492         }
1493 
1494         pidl = allocatedPidl;
1495     }
1496 
1497     CComPtr<IShellFolder> shf;
1498     LPCITEMIDLIST pidllast = NULL;
1499     HRESULT hr = SHBindToParent(pidl, IID_PPV_ARG(IShellFolder, &shf), &pidllast);
1500     if (FAILED(hr))
1501         return hr;
1502 
1503     return shf->GetUIObjectOf(NULL, 1, &pidllast, IID_NULL_PPV_ARG(IContextMenu, &cm));
1504 }
1505 
1506 static HRESULT ShellExecute_ContextMenuVerb(LPSHELLEXECUTEINFOW sei)
1507 {
1508     TRACE("%p\n", sei);
1509 
1510     CCoInit coInit;
1511 
1512     if (FAILED_UNEXPECTEDLY(coInit.hr))
1513         return coInit.hr;
1514 
1515     CComPtr<IContextMenu> cm;
1516     HRESULT hr = shellex_get_contextmenu(sei, cm);
1517     if (FAILED_UNEXPECTEDLY(hr))
1518         return hr;
1519 
1520     CComHeapPtr<char> verb, parameters;
1521     __SHCloneStrWtoA(&verb, sei->lpVerb);
1522     __SHCloneStrWtoA(&parameters, sei->lpParameters);
1523 
1524     CMINVOKECOMMANDINFOEX ici = {};
1525     ici.cbSize = sizeof ici;
1526     ici.fMask = (sei->fMask & (SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_FLAG_NO_UI));
1527     ici.nShow = sei->nShow;
1528     ici.lpVerb = verb;
1529     ici.hwnd = sei->hwnd;
1530     ici.lpParameters = parameters;
1531 
1532     HMENU hMenu = CreatePopupMenu();
1533     BOOL fDefault = !ici.lpVerb || !ici.lpVerb[0];
1534     hr = cm->QueryContextMenu(hMenu, 0, 1, 0x7fff, fDefault ? CMF_DEFAULTONLY : 0);
1535     if (!FAILED_UNEXPECTEDLY(hr))
1536     {
1537         if (fDefault)
1538         {
1539             INT uDefault = GetMenuDefaultItem(hMenu, FALSE, 0);
1540             uDefault = (uDefault != -1) ? uDefault - 1 : 0;
1541             ici.lpVerb = MAKEINTRESOURCEA(uDefault);
1542         }
1543 
1544         hr = cm->InvokeCommand((LPCMINVOKECOMMANDINFO)&ici);
1545         if (!FAILED_UNEXPECTEDLY(hr))
1546             hr = S_OK;
1547     }
1548 
1549     DestroyMenu(hMenu);
1550 
1551     return hr;
1552 }
1553 
1554 
1555 
1556 /*************************************************************************
1557  *    ShellExecute_FromContextMenu [Internal]
1558  */
1559 static LONG ShellExecute_FromContextMenuHandlers( LPSHELLEXECUTEINFOW sei )
1560 {
1561     HKEY hkey, hkeycm = 0;
1562     WCHAR szguid[39];
1563     HRESULT hr;
1564     GUID guid;
1565     DWORD i;
1566     LONG r;
1567 
1568     TRACE("%s\n", debugstr_w(sei->lpFile));
1569 
1570     hkey = ShellExecute_GetClassKey(sei);
1571     if (!hkey)
1572         return ERROR_FUNCTION_FAILED;
1573 
1574     r = RegOpenKeyW(hkey, L"shellex\\ContextMenuHandlers", &hkeycm);
1575     if (r == ERROR_SUCCESS)
1576     {
1577         i = 0;
1578         while (1)
1579         {
1580             r = RegEnumKeyW(hkeycm, i++, szguid, ARRAY_SIZE(szguid));
1581             if (r != ERROR_SUCCESS)
1582                 break;
1583 
1584             hr = CLSIDFromString(szguid, &guid);
1585             if (SUCCEEDED(hr))
1586             {
1587                 /* stop at the first one that succeeds in running */
1588                 hr = shellex_load_object_and_run(hkey, &guid, sei);
1589                 if (SUCCEEDED(hr))
1590                     break;
1591             }
1592         }
1593         RegCloseKey(hkeycm);
1594     }
1595 
1596     if (hkey != sei->hkeyClass)
1597         RegCloseKey(hkey);
1598     return r;
1599 }
1600 
1601 static UINT_PTR SHELL_quote_and_execute(LPCWSTR wcmd, LPCWSTR wszParameters, LPCWSTR lpstrProtocol, LPCWSTR wszApplicationName, LPWSTR env, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc);
1602 
1603 static UINT_PTR SHELL_execute_class(LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc)
1604 {
1605     WCHAR execCmd[1024], classname[1024];
1606     /* launch a document by fileclass like 'WordPad.Document.1' */
1607     /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1608     /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1609     ULONG cmask = (psei->fMask & SEE_MASK_CLASSALL);
1610     DWORD resultLen;
1611     BOOL done;
1612     UINT_PTR rslt;
1613 
1614     /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1615     if (cmask != SEE_MASK_CLASSNAME)
1616     {
1617         WCHAR wcmd[1024];
1618         HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1619                                (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass : NULL,
1620                                psei->lpVerb,
1621                                execCmd, sizeof(execCmd));
1622 
1623         /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1624         TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1625 
1626         wcmd[0] = '\0';
1627         done = SHELL_ArgifyW(wcmd, ARRAY_SIZE(wcmd), execCmd, wszApplicationName, (LPITEMIDLIST)psei->lpIDList, NULL, &resultLen,
1628                              (psei->lpDirectory && *psei->lpDirectory) ? psei->lpDirectory : NULL);
1629         if (!done && wszApplicationName[0])
1630         {
1631             strcatW(wcmd, L" ");
1632             if (*wszApplicationName != '"')
1633             {
1634                 strcatW(wcmd, L"\"");
1635                 strcatW(wcmd, wszApplicationName);
1636                 strcatW(wcmd, L"\"");
1637             }
1638             else
1639                 strcatW(wcmd, wszApplicationName);
1640         }
1641         if (resultLen > ARRAY_SIZE(wcmd))
1642             ERR("Argify buffer not large enough... truncating\n");
1643         return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1644     }
1645 
1646     strcpyW(classname, psei->lpClass);
1647     rslt = SHELL_FindExecutableByVerb(psei->lpVerb, NULL, classname, execCmd, sizeof(execCmd));
1648 
1649     TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt, debugstr_w(classname), debugstr_w(execCmd));
1650     if (33 > rslt)
1651         return rslt;
1652     rslt = SHELL_quote_and_execute( execCmd, L"", classname,
1653                                       wszApplicationName, NULL, psei,
1654                                       psei_out, execfunc );
1655     return rslt;
1656 
1657 }
1658 
1659 static BOOL SHELL_translate_idlist(LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen)
1660 {
1661     WCHAR buffer[MAX_PATH];
1662     BOOL appKnownSingular = FALSE;
1663 
1664     /* last chance to translate IDList: now also allow CLSID paths */
1665     if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST)sei->lpIDList, buffer, ARRAY_SIZE(buffer)))) {
1666         if (buffer[0] == ':' && buffer[1] == ':') {
1667             /* open shell folder for the specified class GUID */
1668             if (strlenW(buffer) + 1 > parametersLen)
1669                 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1670                     lstrlenW(buffer) + 1, parametersLen);
1671             lstrcpynW(wszParameters, buffer, parametersLen);
1672             if (strlenW(L"explorer.exe") > dwApplicationNameLen)
1673                 ERR("application len exceeds buffer size (%i), truncating\n",
1674                     dwApplicationNameLen);
1675             lstrcpynW(wszApplicationName, L"explorer.exe", dwApplicationNameLen);
1676             appKnownSingular = TRUE;
1677 
1678             sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1679         } else {
1680             WCHAR target[MAX_PATH];
1681             DWORD attribs;
1682             DWORD resultLen;
1683             /* Check if we're executing a directory and if so use the
1684                handler for the Folder class */
1685             strcpyW(target, buffer);
1686             attribs = GetFileAttributesW(buffer);
1687             if (attribs != INVALID_FILE_ATTRIBUTES &&
1688                     (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1689                     HCR_GetExecuteCommandW(0, L"Folder",
1690                                            sei->lpVerb,
1691                                            buffer, sizeof(buffer))) {
1692                 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1693                               buffer, target, (LPITEMIDLIST)sei->lpIDList, NULL, &resultLen,
1694                               (sei->lpDirectory && *sei->lpDirectory) ? sei->lpDirectory : NULL);
1695                 if (resultLen > dwApplicationNameLen)
1696                     ERR("Argify buffer not large enough... truncating\n");
1697                 appKnownSingular = FALSE;
1698             }
1699             sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1700         }
1701     }
1702     return appKnownSingular;
1703 }
1704 
1705 static UINT_PTR SHELL_quote_and_execute(LPCWSTR wcmd, LPCWSTR wszParameters, LPCWSTR wszKeyname, LPCWSTR wszApplicationName, LPWSTR env, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc)
1706 {
1707     UINT_PTR retval;
1708     DWORD len;
1709     WCHAR *wszQuotedCmd;
1710 
1711     /* Length of quotes plus length of command plus NULL terminator */
1712     len = 2 + lstrlenW(wcmd) + 1;
1713     if (wszParameters[0])
1714     {
1715         /* Length of space plus length of parameters */
1716         len += 1 + lstrlenW(wszParameters);
1717     }
1718     wszQuotedCmd = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1719     /* Must quote to handle case where cmd contains spaces,
1720      * else security hole if malicious user creates executable file "C:\\Program"
1721      */
1722     strcpyW(wszQuotedCmd, L"\"");
1723     strcatW(wszQuotedCmd, wcmd);
1724     strcatW(wszQuotedCmd, L"\"");
1725     if (wszParameters[0])
1726     {
1727         strcatW(wszQuotedCmd, L" ");
1728         strcatW(wszQuotedCmd, wszParameters);
1729     }
1730 
1731     TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(wszKeyname));
1732 
1733     if (*wszKeyname)
1734         retval = execute_from_key(wszKeyname, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1735     else
1736         retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1737     HeapFree(GetProcessHeap(), 0, wszQuotedCmd);
1738     return retval;
1739 }
1740 
1741 static UINT_PTR SHELL_execute_url(LPCWSTR lpFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc)
1742 {
1743     UINT_PTR retval;
1744     WCHAR *lpstrProtocol;
1745     LPCWSTR lpstrRes;
1746     INT iSize;
1747     DWORD len;
1748 
1749     lpstrRes = strchrW(lpFile, ':');
1750     if (lpstrRes)
1751         iSize = lpstrRes - lpFile;
1752     else
1753         iSize = strlenW(lpFile);
1754 
1755     TRACE("Got URL: %s\n", debugstr_w(lpFile));
1756     /* Looking for ...<protocol>\shell\<lpVerb>\command */
1757     len = iSize + lstrlenW(L"\\shell\\") + lstrlenW(L"\\command") + 1;
1758     if (psei->lpVerb && *psei->lpVerb)
1759         len += lstrlenW(psei->lpVerb);
1760     else
1761         len += lstrlenW(L"open");
1762     lpstrProtocol = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1763     memcpy(lpstrProtocol, lpFile, iSize * sizeof(WCHAR));
1764     lpstrProtocol[iSize] = '\0';
1765     strcatW(lpstrProtocol, L"\\shell\\");
1766     strcatW(lpstrProtocol, psei->lpVerb && *psei->lpVerb ? psei->lpVerb : L"open");
1767     strcatW(lpstrProtocol, L"\\command");
1768 
1769     retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1770                               wcmd, execfunc, psei, psei_out);
1771     HeapFree(GetProcessHeap(), 0, lpstrProtocol);
1772     return retval;
1773 }
1774 
1775 static void do_error_dialog(UINT_PTR retval, HWND hwnd, WCHAR* filename)
1776 {
1777     WCHAR msg[2048];
1778     DWORD_PTR msgArguments[3]  = { (DWORD_PTR)filename, 0, 0 };
1779     DWORD error_code;
1780 
1781     error_code = GetLastError();
1782     if (retval == SE_ERR_NOASSOC)
1783         LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, ARRAY_SIZE(msg));
1784     else
1785         FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
1786                        NULL,
1787                        error_code,
1788                        LANG_USER_DEFAULT,
1789                        msg,
1790                        ARRAY_SIZE(msg),
1791                        (va_list*)msgArguments);
1792 
1793     MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1794 }
1795 
1796 static WCHAR *expand_environment( const WCHAR *str )
1797 {
1798     WCHAR *buf;
1799     DWORD len;
1800 
1801     len = ExpandEnvironmentStringsW(str, NULL, 0);
1802     if (!len) return NULL;
1803 
1804     buf = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1805     if (!buf) return NULL;
1806 
1807     len = ExpandEnvironmentStringsW(str, buf, len);
1808     if (!len)
1809     {
1810         HeapFree(GetProcessHeap(), 0, buf);
1811         return NULL;
1812     }
1813     return buf;
1814 }
1815 
1816 /*************************************************************************
1817  *    SHELL_execute [Internal]
1818  */
1819 static BOOL SHELL_execute(LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
1820 {
1821     static const DWORD unsupportedFlags =
1822         SEE_MASK_ICON         | SEE_MASK_HOTKEY |
1823         SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1824         SEE_MASK_UNICODE       | SEE_MASK_ASYNCOK      | SEE_MASK_HMONITOR;
1825 
1826     WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1827     WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd;
1828     DWORD dwApplicationNameLen = MAX_PATH + 2;
1829     DWORD parametersLen = ARRAY_SIZE(parametersBuffer);
1830     DWORD dirLen = ARRAY_SIZE(dirBuffer);
1831     DWORD wcmdLen = ARRAY_SIZE(wcmdBuffer);
1832     DWORD len;
1833     SHELLEXECUTEINFOW sei_tmp;    /* modifiable copy of SHELLEXECUTEINFO struct */
1834     WCHAR wfileName[MAX_PATH];
1835     WCHAR *env;
1836     WCHAR wszKeyname[256];
1837     LPCWSTR lpFile;
1838     UINT_PTR retval = SE_ERR_NOASSOC;
1839     BOOL appKnownSingular = FALSE;
1840 
1841     /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1842     sei_tmp = *sei;
1843 
1844     TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1845           sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1846           debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1847           debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1848           ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1849           debugstr_w(sei_tmp.lpClass) : "not used");
1850 
1851     sei->hProcess = NULL;
1852 
1853     /* make copies of all path/command strings */
1854     if (!sei_tmp.lpFile)
1855     {
1856         wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen * sizeof(WCHAR));
1857         *wszApplicationName = '\0';
1858     }
1859     else if (*sei_tmp.lpFile == '\"' && sei_tmp.lpFile[(len = strlenW(sei_tmp.lpFile))-1] == '\"')
1860     {
1861         if(len-1 >= dwApplicationNameLen)
1862             dwApplicationNameLen = len;
1863 
1864         wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen * sizeof(WCHAR));
1865         memcpy(wszApplicationName, sei_tmp.lpFile + 1, len * sizeof(WCHAR));
1866 
1867         if(len > 2)
1868             wszApplicationName[len-2] = '\0';
1869         appKnownSingular = TRUE;
1870 
1871         TRACE("wszApplicationName=%s\n", debugstr_w(wszApplicationName));
1872     }
1873     else
1874     {
1875         DWORD l = strlenW(sei_tmp.lpFile) + 1;
1876         if(l > dwApplicationNameLen) dwApplicationNameLen = l + 1;
1877         wszApplicationName = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen * sizeof(WCHAR));
1878         memcpy(wszApplicationName, sei_tmp.lpFile, l * sizeof(WCHAR));
1879 
1880         if (wszApplicationName[2] == 0 && wszApplicationName[1] == L':' &&
1881             ((L'A' <= wszApplicationName[0] && wszApplicationName[0] <= L'Z') ||
1882              (L'a' <= wszApplicationName[0] && wszApplicationName[0] <= L'z')))
1883         {
1884             // 'C:' --> 'C:\'
1885             PathAddBackslashW(wszApplicationName);
1886         }
1887     }
1888 
1889     wszParameters = parametersBuffer;
1890     if (sei_tmp.lpParameters)
1891     {
1892         len = lstrlenW(sei_tmp.lpParameters) + 1;
1893         if (len > parametersLen)
1894         {
1895             wszParameters = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1896             parametersLen = len;
1897         }
1898         strcpyW(wszParameters, sei_tmp.lpParameters);
1899     }
1900     else
1901         *wszParameters = L'\0';
1902 
1903     wszDir = dirBuffer;
1904     if (sei_tmp.lpDirectory)
1905     {
1906         len = lstrlenW(sei_tmp.lpDirectory) + 1;
1907         if (len > dirLen)
1908         {
1909             wszDir = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1910             dirLen = len;
1911         }
1912         strcpyW(wszDir, sei_tmp.lpDirectory);
1913     }
1914     else
1915         *wszDir = L'\0';
1916 
1917     /* adjust string pointers to point to the new buffers */
1918     sei_tmp.lpFile = wszApplicationName;
1919     sei_tmp.lpParameters = wszParameters;
1920     sei_tmp.lpDirectory = wszDir;
1921 
1922     if (sei_tmp.fMask & unsupportedFlags)
1923     {
1924         FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1925     }
1926 
1927     /* process the IDList */
1928     if (sei_tmp.fMask & SEE_MASK_IDLIST &&
1929         (sei_tmp.fMask & SEE_MASK_INVOKEIDLIST) != SEE_MASK_INVOKEIDLIST)
1930     {
1931         CComPtr<IShellExecuteHookW> pSEH;
1932 
1933         HRESULT hr = SHBindToParent((LPCITEMIDLIST)sei_tmp.lpIDList, IID_PPV_ARG(IShellExecuteHookW, &pSEH), NULL);
1934 
1935         if (SUCCEEDED(hr))
1936         {
1937             hr = pSEH->Execute(&sei_tmp);
1938 
1939             if (hr == S_OK)
1940             {
1941                 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1942                 if (wszParameters != parametersBuffer)
1943                     HeapFree(GetProcessHeap(), 0, wszParameters);
1944                 if (wszDir != dirBuffer)
1945                     HeapFree(GetProcessHeap(), 0, wszDir);
1946                 return TRUE;
1947             }
1948         }
1949 
1950         SHGetPathFromIDListW((LPCITEMIDLIST)sei_tmp.lpIDList, wszApplicationName);
1951         appKnownSingular = TRUE;
1952         TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1953     }
1954 
1955     if (sei_tmp.fMask & SEE_MASK_DOENVSUBST)
1956     {
1957         WCHAR *tmp;
1958 
1959         tmp = expand_environment(sei_tmp.lpFile);
1960         if (!tmp)
1961         {
1962             return FALSE;
1963         }
1964         HeapFree(GetProcessHeap(), 0, wszApplicationName);
1965         sei_tmp.lpFile = wszApplicationName = tmp;
1966 
1967         tmp = expand_environment(sei_tmp.lpDirectory);
1968         if (!tmp)
1969         {
1970             return FALSE;
1971         }
1972         if (wszDir != dirBuffer) HeapFree(GetProcessHeap(), 0, wszDir);
1973         sei_tmp.lpDirectory = wszDir = tmp;
1974     }
1975 
1976     if ((sei_tmp.fMask & SEE_MASK_INVOKEIDLIST) == SEE_MASK_INVOKEIDLIST)
1977     {
1978         HRESULT hr = ShellExecute_ContextMenuVerb(&sei_tmp);
1979         if (SUCCEEDED(hr))
1980         {
1981             sei->hInstApp = (HINSTANCE)42;
1982             HeapFree(GetProcessHeap(), 0, wszApplicationName);
1983             if (wszParameters != parametersBuffer)
1984                 HeapFree(GetProcessHeap(), 0, wszParameters);
1985             if (wszDir != dirBuffer)
1986                 HeapFree(GetProcessHeap(), 0, wszDir);
1987             return TRUE;
1988         }
1989     }
1990 
1991 
1992     if (ERROR_SUCCESS == ShellExecute_FromContextMenuHandlers(&sei_tmp))
1993     {
1994         sei->hInstApp = (HINSTANCE) 33;
1995         HeapFree(GetProcessHeap(), 0, wszApplicationName);
1996         if (wszParameters != parametersBuffer)
1997             HeapFree(GetProcessHeap(), 0, wszParameters);
1998         if (wszDir != dirBuffer)
1999             HeapFree(GetProcessHeap(), 0, wszDir);
2000         return TRUE;
2001     }
2002 
2003     if (sei_tmp.fMask & SEE_MASK_CLASSALL)
2004     {
2005         retval = SHELL_execute_class(wszApplicationName, &sei_tmp, sei, execfunc);
2006         if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
2007         {
2008             OPENASINFO Info;
2009 
2010             //FIXME
2011             // need full path
2012 
2013             Info.pcszFile = wszApplicationName;
2014             Info.pcszClass = NULL;
2015             Info.oaifInFlags = OAIF_ALLOW_REGISTRATION | OAIF_EXEC;
2016 
2017             //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
2018             DBG_UNREFERENCED_LOCAL_VARIABLE(Info);
2019             do_error_dialog(retval, sei_tmp.hwnd, wszApplicationName);
2020         }
2021         HeapFree(GetProcessHeap(), 0, wszApplicationName);
2022         if (wszParameters != parametersBuffer)
2023             HeapFree(GetProcessHeap(), 0, wszParameters);
2024         if (wszDir != dirBuffer)
2025             HeapFree(GetProcessHeap(), 0, wszDir);
2026         return retval > 32;
2027     }
2028 
2029     /* Has the IDList not yet been translated? */
2030     if (sei_tmp.fMask & SEE_MASK_IDLIST)
2031     {
2032         appKnownSingular = SHELL_translate_idlist( &sei_tmp, wszParameters,
2033                            parametersLen,
2034                            wszApplicationName,
2035                            dwApplicationNameLen );
2036     }
2037 
2038     /* convert file URLs */
2039     if (UrlIsFileUrlW(sei_tmp.lpFile))
2040     {
2041         LPWSTR buf;
2042         DWORD size;
2043 
2044         size = MAX_PATH;
2045         buf = static_cast<LPWSTR>(HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR)));
2046         if (!buf || FAILED(PathCreateFromUrlW(sei_tmp.lpFile, buf, &size, 0)))
2047         {
2048             HeapFree(GetProcessHeap(), 0, buf);
2049             return SE_ERR_OOM;
2050         }
2051 
2052         HeapFree(GetProcessHeap(), 0, wszApplicationName);
2053         wszApplicationName = buf;
2054         sei_tmp.lpFile = wszApplicationName;
2055     }
2056     else /* or expand environment strings (not both!) */
2057     {
2058         len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
2059         if (len > 0)
2060         {
2061             LPWSTR buf;
2062             buf = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2063 
2064             ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len + 1);
2065             HeapFree(GetProcessHeap(), 0, wszApplicationName);
2066             wszApplicationName = buf;
2067             /* appKnownSingular unmodified */
2068 
2069             sei_tmp.lpFile = wszApplicationName;
2070         }
2071     }
2072 
2073     if (*sei_tmp.lpDirectory)
2074     {
2075         len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
2076         if (len > 0)
2077         {
2078             LPWSTR buf;
2079             len++;
2080             buf = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2081             ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
2082             if (wszDir != dirBuffer)
2083                 HeapFree(GetProcessHeap(), 0, wszDir);
2084             wszDir = buf;
2085             sei_tmp.lpDirectory = wszDir;
2086         }
2087     }
2088 
2089     /* Else, try to execute the filename */
2090     TRACE("execute: %s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
2091 
2092     /* separate out command line arguments from executable file name */
2093     if (!*sei_tmp.lpParameters && !appKnownSingular)
2094     {
2095         /* If the executable path is quoted, handle the rest of the command line as parameters. */
2096         if (sei_tmp.lpFile[0] == L'"')
2097         {
2098             LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
2099             LPWSTR dst = wfileName;
2100             LPWSTR end;
2101 
2102             /* copy the unquoted executable path to 'wfileName' */
2103             while(*src && *src != L'"')
2104                 *dst++ = *src++;
2105 
2106             *dst = L'\0';
2107 
2108             if (*src == L'"')
2109             {
2110                 end = ++src;
2111 
2112                 while(isspaceW(*src))
2113                     ++src;
2114             }
2115             else
2116                 end = src;
2117 
2118             /* copy the parameter string to 'wszParameters' */
2119             strcpyW(wszParameters, src);
2120 
2121             /* terminate previous command string after the quote character */
2122             *end = L'\0';
2123             lpFile = wfileName;
2124         }
2125         else
2126         {
2127             lpFile = sei_tmp.lpFile;
2128         }
2129     }
2130     else
2131         lpFile = sei_tmp.lpFile;
2132 
2133     wcmd = wcmdBuffer;
2134 
2135     /* Only execute if it has an executable extension */
2136     if (PathIsExeW(lpFile))
2137     {
2138         len = lstrlenW(wszApplicationName) + 3;
2139         if (sei_tmp.lpParameters[0])
2140             len += 1 + lstrlenW(wszParameters);
2141         if (len > wcmdLen)
2142         {
2143             wcmd = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
2144             wcmdLen = len;
2145         }
2146         swprintf(wcmd, L"\"%s\"", wszApplicationName);
2147         if (sei_tmp.lpParameters[0])
2148         {
2149             strcatW(wcmd, L" ");
2150             strcatW(wcmd, wszParameters);
2151         }
2152 
2153         retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
2154         if (retval > 32)
2155         {
2156             HeapFree(GetProcessHeap(), 0, wszApplicationName);
2157             if (wszParameters != parametersBuffer)
2158                 HeapFree(GetProcessHeap(), 0, wszParameters);
2159             if (wszDir != dirBuffer)
2160                 HeapFree(GetProcessHeap(), 0, wszDir);
2161             if (wcmd != wcmdBuffer)
2162                 HeapFree(GetProcessHeap(), 0, wcmd);
2163             return TRUE;
2164         }
2165     }
2166 
2167     /* Else, try to find the executable */
2168     wcmd[0] = L'\0';
2169     retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, wszKeyname, &env, (LPITEMIDLIST)sei_tmp.lpIDList, sei_tmp.lpParameters);
2170     if (retval > 32)  /* Found */
2171     {
2172         retval = SHELL_quote_and_execute(wcmd, wszParameters, wszKeyname,
2173                                          wszApplicationName, env, &sei_tmp,
2174                                          sei, execfunc);
2175         HeapFree(GetProcessHeap(), 0, env);
2176     }
2177     else if (PathIsDirectoryW(lpFile))
2178     {
2179         WCHAR wExec[MAX_PATH];
2180         WCHAR * lpQuotedFile = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * (strlenW(lpFile) + 3));
2181 
2182         if (lpQuotedFile)
2183         {
2184             retval = SHELL_FindExecutable(sei_tmp.lpDirectory, L"explorer",
2185                                           L"open", wExec, MAX_PATH,
2186                                           NULL, &env, NULL, NULL);
2187             if (retval > 32)
2188             {
2189                 swprintf(lpQuotedFile, L"\"%s\"", lpFile);
2190                 retval = SHELL_quote_and_execute(wExec, lpQuotedFile,
2191                                                  wszKeyname,
2192                                                  wszApplicationName, env,
2193                                                  &sei_tmp, sei, execfunc);
2194                 HeapFree(GetProcessHeap(), 0, env);
2195             }
2196             HeapFree(GetProcessHeap(), 0, lpQuotedFile);
2197         }
2198         else
2199             retval = 0; /* Out of memory */
2200     }
2201     else if (PathIsURLW(lpFile))    /* File not found, check for URL */
2202     {
2203         retval = SHELL_execute_url(lpFile, wcmd, &sei_tmp, sei, execfunc );
2204     }
2205     /* Check if file specified is in the form www.??????.*** */
2206     else if (!strncmpiW(lpFile, L"www", 3))
2207     {
2208         /* if so, prefix lpFile with http:// and call ShellExecute */
2209         WCHAR lpstrTmpFile[256];
2210         strcpyW(lpstrTmpFile, L"http://");
2211         strcatW(lpstrTmpFile, lpFile);
2212         retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
2213     }
2214 
2215     TRACE("retval %lu\n", retval);
2216 
2217     if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
2218     {
2219         OPENASINFO Info;
2220 
2221         //FIXME
2222         // need full path
2223 
2224         Info.pcszFile = wszApplicationName;
2225         Info.pcszClass = NULL;
2226         Info.oaifInFlags = OAIF_ALLOW_REGISTRATION | OAIF_EXEC;
2227 
2228         //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
2229         DBG_UNREFERENCED_LOCAL_VARIABLE(Info);
2230         do_error_dialog(retval, sei_tmp.hwnd, wszApplicationName);
2231     }
2232 
2233     HeapFree(GetProcessHeap(), 0, wszApplicationName);
2234     if (wszParameters != parametersBuffer)
2235         HeapFree(GetProcessHeap(), 0, wszParameters);
2236     if (wszDir != dirBuffer)
2237         HeapFree(GetProcessHeap(), 0, wszDir);
2238     if (wcmd != wcmdBuffer)
2239         HeapFree(GetProcessHeap(), 0, wcmd);
2240 
2241     sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
2242 
2243     return retval > 32;
2244 }
2245 
2246 /*************************************************************************
2247  * ShellExecuteA            [SHELL32.290]
2248  */
2249 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpVerb, LPCSTR lpFile,
2250                                LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd)
2251 {
2252     SHELLEXECUTEINFOA sei;
2253 
2254     TRACE("%p,%s,%s,%s,%s,%d\n",
2255           hWnd, debugstr_a(lpVerb), debugstr_a(lpFile),
2256           debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
2257 
2258     sei.cbSize = sizeof(sei);
2259     sei.fMask = SEE_MASK_FLAG_NO_UI;
2260     sei.hwnd = hWnd;
2261     sei.lpVerb = lpVerb;
2262     sei.lpFile = lpFile;
2263     sei.lpParameters = lpParameters;
2264     sei.lpDirectory = lpDirectory;
2265     sei.nShow = iShowCmd;
2266     sei.lpIDList = 0;
2267     sei.lpClass = 0;
2268     sei.hkeyClass = 0;
2269     sei.dwHotKey = 0;
2270     sei.hProcess = 0;
2271 
2272     ShellExecuteExA(&sei);
2273     return sei.hInstApp;
2274 }
2275 
2276 /*************************************************************************
2277  * ShellExecuteExA                [SHELL32.292]
2278  *
2279  */
2280 BOOL
2281 WINAPI
2282 DECLSPEC_HOTPATCH
2283 ShellExecuteExA(LPSHELLEXECUTEINFOA sei)
2284 {
2285     SHELLEXECUTEINFOW seiW;
2286     BOOL ret;
2287     WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
2288 
2289     TRACE("%p\n", sei);
2290 
2291     memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
2292 
2293     if (sei->lpVerb)
2294         seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
2295 
2296     if (sei->lpFile)
2297         seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
2298 
2299     if (sei->lpParameters)
2300         seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
2301 
2302     if (sei->lpDirectory)
2303         seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
2304 
2305     if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
2306         seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
2307     else
2308         seiW.lpClass = NULL;
2309 
2310     ret = SHELL_execute(&seiW, SHELL_ExecuteW);
2311 
2312     sei->hInstApp = seiW.hInstApp;
2313 
2314     if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
2315         sei->hProcess = seiW.hProcess;
2316 
2317     SHFree(wVerb);
2318     SHFree(wFile);
2319     SHFree(wParameters);
2320     SHFree(wDirectory);
2321     SHFree(wClass);
2322 
2323     return ret;
2324 }
2325 
2326 /*************************************************************************
2327  * ShellExecuteExW                [SHELL32.293]
2328  *
2329  */
2330 BOOL
2331 WINAPI
2332 DECLSPEC_HOTPATCH
2333 ShellExecuteExW(LPSHELLEXECUTEINFOW sei)
2334 {
2335     return SHELL_execute(sei, SHELL_ExecuteW);
2336 }
2337 
2338 /*************************************************************************
2339  * ShellExecuteW            [SHELL32.294]
2340  * from shellapi.h
2341  * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
2342  * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
2343  */
2344 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile,
2345                                LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
2346 {
2347     SHELLEXECUTEINFOW sei;
2348 
2349     TRACE("\n");
2350     sei.cbSize = sizeof(sei);
2351     sei.fMask = SEE_MASK_FLAG_NO_UI;
2352     sei.hwnd = hwnd;
2353     sei.lpVerb = lpVerb;
2354     sei.lpFile = lpFile;
2355     sei.lpParameters = lpParameters;
2356     sei.lpDirectory = lpDirectory;
2357     sei.nShow = nShowCmd;
2358     sei.lpIDList = 0;
2359     sei.lpClass = 0;
2360     sei.hkeyClass = 0;
2361     sei.dwHotKey = 0;
2362     sei.hProcess = 0;
2363 
2364     SHELL_execute(&sei, SHELL_ExecuteW);
2365     return sei.hInstApp;
2366 }
2367 
2368 /*************************************************************************
2369  * WOWShellExecute            [SHELL32.@]
2370  *
2371  * FIXME: the callback function most likely doesn't work the same way on Windows.
2372  */
2373 EXTERN_C HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpVerb, LPCSTR lpFile,
2374         LPCSTR lpParameters, LPCSTR lpDirectory, INT iShowCmd, void *callback)
2375 {
2376     SHELLEXECUTEINFOW seiW;
2377     WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
2378     HANDLE hProcess = 0;
2379 
2380     seiW.lpVerb = lpVerb ? __SHCloneStrAtoW(&wVerb, lpVerb) : NULL;
2381     seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
2382     seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
2383     seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
2384 
2385     seiW.cbSize = sizeof(seiW);
2386     seiW.fMask = 0;
2387     seiW.hwnd = hWnd;
2388     seiW.nShow = iShowCmd;
2389     seiW.lpIDList = 0;
2390     seiW.lpClass = 0;
2391     seiW.hkeyClass = 0;
2392     seiW.dwHotKey = 0;
2393     seiW.hProcess = hProcess;
2394 
2395     SHELL_execute(&seiW, (SHELL_ExecuteW32)callback);
2396 
2397     SHFree(wVerb);
2398     SHFree(wFile);
2399     SHFree(wParameters);
2400     SHFree(wDirectory);
2401     return seiW.hInstApp;
2402 }
2403 
2404 /*************************************************************************
2405  * OpenAs_RunDLLW          [SHELL32.@]
2406  */
2407 EXTERN_C void WINAPI
2408 OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2409 {
2410     OPENASINFO info;
2411     TRACE("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);
2412 
2413     ZeroMemory(&info, sizeof(info));
2414     info.pcszFile = cmdline;
2415     info.pcszClass = NULL;
2416     info.oaifInFlags = OAIF_ALLOW_REGISTRATION | OAIF_REGISTER_EXT | OAIF_EXEC;
2417 
2418     SHOpenWithDialog(hwnd, &info);
2419 }
2420 
2421 /*************************************************************************
2422  * OpenAs_RunDLLA          [SHELL32.@]
2423  */
2424 EXTERN_C void WINAPI
2425 OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2426 {
2427     LPWSTR pszCmdLineW = NULL;
2428     TRACE("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2429 
2430     if (cmdline)
2431         __SHCloneStrAtoW(&pszCmdLineW, cmdline);
2432     OpenAs_RunDLLW(hwnd, hinst, pszCmdLineW, cmdshow);
2433     SHFree(pszCmdLineW);
2434 }
2435 
2436 /*************************************************************************/
2437 
2438 static LPCWSTR
2439 SplitParams(LPCWSTR psz, LPWSTR pszArg0, size_t cchArg0)
2440 {
2441     LPCWSTR pch;
2442     size_t ich = 0;
2443     if (*psz == L'"')
2444     {
2445         // 1st argument is quoted. the string in quotes is quoted 1st argument.
2446         // [pch] --> [pszArg0+ich]
2447         for (pch = psz + 1; *pch && ich + 1 < cchArg0; ++ich, ++pch)
2448         {
2449             if (*pch == L'"' && pch[1] == L'"')
2450             {
2451                 // doubled double quotations found!
2452                 pszArg0[ich] = L'"';
2453             }
2454             else if (*pch == L'"')
2455             {
2456                 // single double quotation found!
2457                 ++pch;
2458                 break;
2459             }
2460             else
2461             {
2462                 // otherwise
2463                 pszArg0[ich] = *pch;
2464             }
2465         }
2466     }
2467     else
2468     {
2469         // 1st argument is unquoted. non-space sequence is 1st argument.
2470         // [pch] --> [pszArg0+ich]
2471         for (pch = psz; *pch && !iswspace(*pch) && ich + 1 < cchArg0; ++ich, ++pch)
2472         {
2473             pszArg0[ich] = *pch;
2474         }
2475     }
2476     pszArg0[ich] = 0;
2477 
2478     // skip space
2479     while (iswspace(*pch))
2480         ++pch;
2481 
2482     return pch;
2483 }
2484 
2485 HRESULT WINAPI ShellExecCmdLine(
2486     HWND hwnd,
2487     LPCWSTR pwszCommand,
2488     LPCWSTR pwszStartDir,
2489     int nShow,
2490     LPVOID pUnused,
2491     DWORD dwSeclFlags)
2492 {
2493     SHELLEXECUTEINFOW info;
2494     DWORD dwSize, dwError, dwType, dwFlags = SEE_MASK_DOENVSUBST | SEE_MASK_NOASYNC;
2495     LPCWSTR pszVerb = NULL;
2496     WCHAR szFile[MAX_PATH], szFile2[MAX_PATH];
2497     HRESULT hr;
2498     LPCWSTR pchParams;
2499     LPWSTR lpCommand = NULL;
2500 
2501     if (pwszCommand == NULL)
2502         RaiseException(EXCEPTION_ACCESS_VIOLATION, EXCEPTION_NONCONTINUABLE,
2503                        1, (ULONG_PTR*)pwszCommand);
2504 
2505     __SHCloneStrW(&lpCommand, pwszCommand);
2506     StrTrimW(lpCommand, L" \t");
2507 
2508     if (dwSeclFlags & SECL_NO_UI)
2509         dwFlags |= SEE_MASK_FLAG_NO_UI;
2510     if (dwSeclFlags & SECL_LOG_USAGE)
2511         dwFlags |= SEE_MASK_FLAG_LOG_USAGE;
2512     if (dwSeclFlags & SECL_USE_IDLIST)
2513         dwFlags |= SEE_MASK_INVOKEIDLIST;
2514 
2515     if (dwSeclFlags & SECL_RUNAS)
2516     {
2517         dwSize = 0;
2518         hr = AssocQueryStringW(0, ASSOCSTR_COMMAND, lpCommand, L"RunAs", NULL, &dwSize);
2519         if (SUCCEEDED(hr) && dwSize != 0)
2520         {
2521             pszVerb = L"runas";
2522         }
2523     }
2524 
2525     if (PathIsURLW(lpCommand) || UrlIsW(lpCommand, URLIS_APPLIABLE))
2526     {
2527         StringCchCopyW(szFile, _countof(szFile), lpCommand);
2528         pchParams = NULL;
2529     }
2530     else
2531     {
2532         pchParams = SplitParams(lpCommand, szFile, _countof(szFile));
2533         if (szFile[0] != UNICODE_NULL && szFile[1] == L':' &&
2534             szFile[2] == UNICODE_NULL)
2535         {
2536             PathAddBackslashW(szFile);
2537         }
2538 
2539         WCHAR szCurDir[MAX_PATH];
2540         GetCurrentDirectoryW(_countof(szCurDir), szCurDir);
2541         if (pwszStartDir)
2542         {
2543             SetCurrentDirectoryW(pwszStartDir);
2544         }
2545 
2546         if (PathIsRelativeW(szFile) &&
2547             GetFullPathNameW(szFile, _countof(szFile2), szFile2, NULL) &&
2548             PathFileExistsW(szFile2))
2549         {
2550             StringCchCopyW(szFile, _countof(szFile), szFile2);
2551         }
2552         else if (SearchPathW(NULL, szFile, NULL, _countof(szFile2), szFile2, NULL) ||
2553                  SearchPathW(NULL, szFile, L".exe", _countof(szFile2), szFile2, NULL) ||
2554                  SearchPathW(NULL, szFile, L".com", _countof(szFile2), szFile2, NULL) ||
2555                  SearchPathW(pwszStartDir, szFile, NULL, _countof(szFile2), szFile2, NULL) ||
2556                  SearchPathW(pwszStartDir, szFile, L".exe", _countof(szFile2), szFile2, NULL) ||
2557                  SearchPathW(pwszStartDir, szFile, L".com", _countof(szFile2), szFile2, NULL))
2558         {
2559             StringCchCopyW(szFile, _countof(szFile), szFile2);
2560         }
2561         else if (SearchPathW(NULL, lpCommand, NULL, _countof(szFile2), szFile2, NULL) ||
2562                  SearchPathW(NULL, lpCommand, L".exe", _countof(szFile2), szFile2, NULL) ||
2563                  SearchPathW(NULL, lpCommand, L".com", _countof(szFile2), szFile2, NULL) ||
2564                  SearchPathW(pwszStartDir, lpCommand, NULL, _countof(szFile2), szFile2, NULL) ||
2565                  SearchPathW(pwszStartDir, lpCommand, L".exe", _countof(szFile2), szFile2, NULL) ||
2566                  SearchPathW(pwszStartDir, lpCommand, L".com", _countof(szFile2), szFile2, NULL))
2567         {
2568             StringCchCopyW(szFile, _countof(szFile), szFile2);
2569             pchParams = NULL;
2570         }
2571 
2572         if (pwszStartDir)
2573         {
2574             SetCurrentDirectoryW(szCurDir);
2575         }
2576 
2577         if (!(dwSeclFlags & SECL_ALLOW_NONEXE))
2578         {
2579             if (!GetBinaryTypeW(szFile, &dwType))
2580             {
2581                 SHFree(lpCommand);
2582 
2583                 if (!(dwSeclFlags & SECL_NO_UI))
2584                 {
2585                     WCHAR szText[128 + MAX_PATH], szFormat[128];
2586                     LoadStringW(shell32_hInstance, IDS_FILE_NOT_FOUND, szFormat, _countof(szFormat));
2587                     StringCchPrintfW(szText, _countof(szText), szFormat, szFile);
2588                     MessageBoxW(hwnd, szText, NULL, MB_ICONERROR);
2589                 }
2590                 return CO_E_APPNOTFOUND;
2591             }
2592         }
2593         else
2594         {
2595             if (GetFileAttributesW(szFile) == INVALID_FILE_ATTRIBUTES)
2596             {
2597                 SHFree(lpCommand);
2598 
2599                 if (!(dwSeclFlags & SECL_NO_UI))
2600                 {
2601                     WCHAR szText[128 + MAX_PATH], szFormat[128];
2602                     LoadStringW(shell32_hInstance, IDS_FILE_NOT_FOUND, szFormat, _countof(szFormat));
2603                     StringCchPrintfW(szText, _countof(szText), szFormat, szFile);
2604                     MessageBoxW(hwnd, szText, NULL, MB_ICONERROR);
2605                 }
2606                 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
2607             }
2608         }
2609     }
2610 
2611     ZeroMemory(&info, sizeof(info));
2612     info.cbSize = sizeof(info);
2613     info.fMask = dwFlags;
2614     info.hwnd = hwnd;
2615     info.lpVerb = pszVerb;
2616     info.lpFile = szFile;
2617     info.lpParameters = (pchParams && *pchParams) ? pchParams : NULL;
2618     info.lpDirectory = pwszStartDir;
2619     info.nShow = nShow;
2620     if (ShellExecuteExW(&info))
2621     {
2622         if (info.lpIDList)
2623             CoTaskMemFree(info.lpIDList);
2624 
2625         SHFree(lpCommand);
2626 
2627         return S_OK;
2628     }
2629 
2630     dwError = GetLastError();
2631 
2632     SHFree(lpCommand);
2633 
2634     return HRESULT_FROM_WIN32(dwError);
2635 }
2636