Completed Italian language support.
[wine] / dlls / shell32 / shlexec.c
1 /*
2  *                              Shell Library Functions
3  *
4  * Copyright 1998 Marcus Meissner
5  * Copyright 2002 Eric Pouech
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #include <ctype.h>
33 #include <assert.h>
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winerror.h"
38 #include "winreg.h"
39 #include "wownt32.h"
40 #include "heap.h"
41 #include "shellapi.h"
42 #include "wingdi.h"
43 #include "winuser.h"
44 #include "shlobj.h"
45 #include "shlwapi.h"
46 #include "ddeml.h"
47
48 #include "wine/winbase16.h"
49 #include "shell32_main.h"
50 #include "undocshell.h"
51
52 #include "wine/debug.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(exec);
55
56 /***********************************************************************
57  * this function is supposed to expand the escape sequences found in the registry
58  * some diving reported that the following were used:
59  * + %1, %2...  seem to report to parameter of index N in ShellExecute pmts
60  *      %1 file
61  *      %2 printer
62  *      %3 driver
63  *      %4 port
64  * %I address of a global item ID (explorer switch /idlist)
65  * %L seems to be %1 as long filename followed by the 8+3 variation
66  * %S ???
67  * %* all following parameters (see batfile)
68  */
69 static BOOL argify(char* res, int len, const char* fmt, const char* lpFile)
70 {
71     char        xlpFile[1024];
72     BOOL        done = FALSE;
73
74     while (*fmt)
75     {
76         if (*fmt == '%')
77         {
78             switch (*++fmt)
79             {
80             case '\0':
81             case '%':
82                 *res++ = '%';
83                 break;
84             case '1':
85             case '*':
86                 if (!done || (*fmt == '1'))
87                 {
88                     if (SearchPathA(NULL, lpFile, ".exe", sizeof(xlpFile), xlpFile, NULL))
89                     {
90                         strcpy(res, xlpFile);
91                         res += strlen(xlpFile);
92                     }
93                     else
94                     {
95                         strcpy(res, lpFile);
96                         res += strlen(lpFile);
97                     }
98                 }
99                 break;
100             /*
101              * IE uses this alot for activating things such as windows media
102              * player. This is not verified to be fully correct but it appears
103              * to work just fine.
104              */
105             case 'L':
106                 strcpy(res,lpFile);
107                 res += strlen(lpFile);
108                 break;
109
110             default: FIXME("Unknown escape sequence %%%c\n", *fmt);
111             }
112             fmt++;
113             done = TRUE;
114         }
115         else
116             *res++ = *fmt++;
117     }
118     *res = '\0';
119     return done;
120 }
121
122 /*************************************************************************
123  *      SHELL_ExecuteA [Internal]
124  *
125  */
126 static UINT SHELL_ExecuteA(char *lpCmd, void *env, LPSHELLEXECUTEINFOA sei, BOOL shWait)
127 {
128     STARTUPINFOA  startup;
129     PROCESS_INFORMATION info;
130     UINT retval = 31;
131
132     TRACE("Execute %s from directory %s\n", lpCmd, sei->lpDirectory);
133     ZeroMemory(&startup,sizeof(STARTUPINFOA));
134     startup.cb = sizeof(STARTUPINFOA);
135     startup.dwFlags = STARTF_USESHOWWINDOW;
136     startup.wShowWindow = sei->nShow;
137     if (CreateProcessA(NULL, lpCmd, NULL, NULL, FALSE, 0,
138                        env, sei->lpDirectory, &startup, &info))
139     {
140         /* Give 30 seconds to the app to come up, if desired. Probably only needed
141            when starting app immediately before making a DDE connection. */
142         if (shWait)
143             if (WaitForInputIdle( info.hProcess, 30000 ) == -1)
144                 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
145         retval = 33;
146         if(sei->fMask & SEE_MASK_NOCLOSEPROCESS)
147             sei->hProcess = info.hProcess;
148         else
149             CloseHandle( info.hProcess );
150         CloseHandle( info.hThread );
151     }
152     else if ((retval = GetLastError()) >= 32)
153     {
154         FIXME("Strange error set by CreateProcess: %d\n", retval);
155         retval = ERROR_BAD_FORMAT;
156     }
157
158     sei->hInstApp = (HINSTANCE)retval;
159     return retval;
160 }
161
162
163 /***********************************************************************
164  *           build_env
165  *
166  * Build the environment for the new process, adding the specified
167  * path to the PATH variable. Returned pointer must be freed by caller.
168  */
169 static void *build_env( const char *path )
170 {
171     char *strings, *new_env;
172     char *p, *p2;
173     int total = strlen(path) + 1;
174     BOOL got_path = FALSE;
175
176     if (!(strings = GetEnvironmentStringsA())) return NULL;
177     p = strings;
178     while (*p)
179     {
180         int len = strlen(p) + 1;
181         if (!strncasecmp( p, "PATH=", 5 )) got_path = TRUE;
182         total += len;
183         p += len;
184     }
185     if (!got_path) total += 5;  /* we need to create PATH */
186     total++;  /* terminating null */
187
188     if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total )))
189     {
190         FreeEnvironmentStringsA( strings );
191         return NULL;
192     }
193     p = strings;
194     p2 = new_env;
195     while (*p)
196     {
197         int len = strlen(p) + 1;
198         memcpy( p2, p, len );
199         if (!strncasecmp( p, "PATH=", 5 ))
200         {
201             p2[len - 1] = ';';
202             strcpy( p2 + len, path );
203             p2 += strlen(path) + 1;
204         }
205         p += len;
206         p2 += len;
207     }
208     if (!got_path)
209     {
210         strcpy( p2, "PATH=" );
211         strcat( p2, path );
212         p2 += strlen(p2) + 1;
213     }
214     *p2 = 0;
215     FreeEnvironmentStringsA( strings );
216     return new_env;
217 }
218
219
220 /***********************************************************************
221  *           SHELL_TryAppPath
222  *
223  * Helper function for SHELL_FindExecutable
224  * @param lpResult - pointer to a buffer of size MAX_PATH
225  * On entry: szName is a filename (probably without path separators).
226  * On exit: if szName found in "App Path", place full path in lpResult, and return true
227  */
228 static BOOL SHELL_TryAppPath( LPCSTR szName, LPSTR lpResult, void**env)
229 {
230     HKEY hkApp = 0;
231     char buffer[256];
232     LONG len;
233     LONG res;
234     BOOL found = FALSE;
235
236     if (env) *env = NULL;
237     sprintf(buffer, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s", szName);
238     res = RegOpenKeyExA(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
239     if (res) goto end;
240
241     len = MAX_PATH;
242     res = RegQueryValueA(hkApp, NULL, lpResult, &len);
243     if (res) goto end;
244     found = TRUE;
245
246     if (env)
247     {
248         DWORD count = sizeof(buffer);
249         if (!RegQueryValueExA(hkApp, "Path", NULL, NULL, buffer, &count) && buffer[0])
250             *env = build_env( buffer );
251     }
252
253 end:
254     if (hkApp) RegCloseKey(hkApp);
255     return found;
256 }
257
258 /*************************************************************************
259  *      SHELL_FindExecutable [Internal]
260  *
261  * Utility for code sharing between FindExecutable and ShellExecute
262  * in:
263  *      lpFile the name of a file
264  *      lpOperation the operation on it (open)
265  * out:
266  *      lpResult a buffer, big enough :-(, to store the command to do the
267  *              operation on the file
268  *      key a buffer, big enough, to get the key name to do actually the
269  *              command (it'll be used afterwards for more information
270  *              on the operation)
271  */
272 static UINT SHELL_FindExecutable(LPCSTR lpPath, LPCSTR lpFile, LPCSTR lpOperation,
273                                  LPSTR lpResult, LPSTR key, void **env)
274 {
275     char *extension = NULL; /* pointer to file extension */
276     char tmpext[5];         /* local copy to mung as we please */
277     char filetype[256];     /* registry name for this filetype */
278     LONG filetypelen = 256; /* length of above */
279     char command[256];      /* command from registry */
280     LONG commandlen = 256;  /* This is the most DOS can handle :) */
281     char buffer[256];       /* Used to GetProfileString */
282     UINT retval = 31;  /* default - 'No association was found' */
283     char *tok;              /* token pointer */
284     char xlpFile[256] = ""; /* result of SearchPath */
285
286     TRACE("%s\n", (lpFile != NULL) ? lpFile : "-");
287
288     lpResult[0] = '\0'; /* Start off with an empty return string */
289     if (key) *key = '\0';
290
291     /* trap NULL parameters on entry */
292     if ((lpFile == NULL) || (lpResult == NULL) || (lpOperation == NULL))
293     {
294         WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
295              lpFile, lpOperation, lpResult);
296         return 2; /* File not found. Close enough, I guess. */
297     }
298
299     if (SHELL_TryAppPath( lpFile, lpResult, env ))
300     {
301         TRACE("found %s via App Paths\n", lpResult);
302         return 33;
303     }
304
305     if (SearchPathA(lpPath, lpFile, ".exe", sizeof(xlpFile), xlpFile, NULL))
306     {
307         TRACE("SearchPathA returned non-zero\n");
308         lpFile = xlpFile;
309         /* Hey, isn't this value ignored?  Why make this call?  Shouldn't we return here?  --dank*/
310     }
311
312     /* First thing we need is the file's extension */
313     extension = strrchr(xlpFile, '.'); /* Assume last "." is the one; */
314                                        /* File->Run in progman uses */
315                                        /* .\FILE.EXE :( */
316     TRACE("xlpFile=%s,extension=%s\n", xlpFile, extension);
317
318     if ((extension == NULL) || (extension == &xlpFile[strlen(xlpFile)]))
319     {
320         WARN("Returning 31 - No association\n");
321         return 31; /* no association */
322     }
323
324     /* Make local copy & lowercase it for reg & 'programs=' lookup */
325     lstrcpynA(tmpext, extension, 5);
326     CharLowerA(tmpext);
327     TRACE("%s file\n", tmpext);
328
329     /* Three places to check: */
330     /* 1. win.ini, [windows], programs (NB no leading '.') */
331     /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
332     /* 3. win.ini, [extensions], extension (NB no leading '.' */
333     /* All I know of the order is that registry is checked before */
334     /* extensions; however, it'd make sense to check the programs */
335     /* section first, so that's what happens here. */
336
337     /* See if it's a program - if GetProfileString fails, we skip this
338      * section. Actually, if GetProfileString fails, we've probably
339      * got a lot more to worry about than running a program... */
340     if (GetProfileStringA("windows", "programs", "exe pif bat cmd com",
341                           buffer, sizeof(buffer)) > 0)
342     {
343         UINT i;
344
345         for (i = 0;i<strlen(buffer); i++) buffer[i] = tolower(buffer[i]);
346
347         tok = strtok(buffer, " \t"); /* ? */
348         while (tok!= NULL)
349         {
350             if (strcmp(tok, &tmpext[1]) == 0) /* have to skip the leading "." */
351             {
352                 strcpy(lpResult, xlpFile);
353                 /* Need to perhaps check that the file has a path
354                  * attached */
355                 TRACE("found %s\n", lpResult);
356                 return 33;
357
358                 /* Greater than 32 to indicate success FIXME According to the
359                  * docs, I should be returning a handle for the
360                  * executable. Does this mean I'm supposed to open the
361                  * executable file or something? More RTFM, I guess... */
362             }
363             tok = strtok(NULL, " \t");
364         }
365     }
366
367     /* Check registry */
368     if (RegQueryValueA(HKEY_CLASSES_ROOT, tmpext, filetype,
369                        &filetypelen) == ERROR_SUCCESS)
370     {
371         filetype[filetypelen] = '\0';
372         TRACE("File type: %s\n", filetype);
373
374         /* Looking for ...buffer\shell\lpOperation\command */
375         strcat(filetype, "\\shell\\");
376         strcat(filetype, lpOperation);
377         strcat(filetype, "\\command");
378
379         if (RegQueryValueA(HKEY_CLASSES_ROOT, filetype, command,
380                            &commandlen) == ERROR_SUCCESS)
381         {
382             if (key) strcpy(key, filetype);
383 #if 0
384             LPSTR tmp;
385             char param[256];
386             LONG paramlen = 256;
387
388             /* FIXME: it seems all Windows version don't behave the same here.
389              * the doc states that this ddeexec information can be found after
390              * the exec names.
391              * on Win98, it doesn't appear, but I think it does on Win2k
392              */
393             /* Get the parameters needed by the application
394                from the associated ddeexec key */
395             tmp = strstr(filetype, "command");
396             tmp[0] = '\0';
397             strcat(filetype, "ddeexec");
398
399             if (RegQueryValueA(HKEY_CLASSES_ROOT, filetype, param, &paramlen) == ERROR_SUCCESS)
400             {
401                 strcat(command, " ");
402                 strcat(command, param);
403                 commandlen += paramlen;
404             }
405 #endif
406             command[commandlen] = '\0';
407             argify(lpResult, sizeof(lpResult), command, xlpFile);
408             retval = 33; /* FIXME see above */
409         }
410     }
411     else /* Check win.ini */
412     {
413         /* Toss the leading dot */
414         extension++;
415         if (GetProfileStringA("extensions", extension, "", command,
416                               sizeof(command)) > 0)
417         {
418             if (strlen(command) != 0)
419             {
420                 strcpy(lpResult, command);
421                 tok = strstr(lpResult, "^"); /* should be ^.extension? */
422                 if (tok != NULL)
423                 {
424                     tok[0] = '\0';
425                     strcat(lpResult, xlpFile); /* what if no dir in xlpFile? */
426                     tok = strstr(command, "^"); /* see above */
427                     if ((tok != NULL) && (strlen(tok)>5))
428                     {
429                         strcat(lpResult, &tok[5]);
430                     }
431                 }
432                 retval = 33; /* FIXME - see above */
433             }
434         }
435     }
436
437     TRACE("returning %s\n", lpResult);
438     return retval;
439 }
440
441 /******************************************************************
442  *              dde_cb
443  *
444  * callback for the DDE connection. not really usefull
445  */
446 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
447                                 HSZ hsz1, HSZ hsz2,
448                                 HDDEDATA hData, DWORD dwData1, DWORD dwData2)
449 {
450     return NULL;
451 }
452
453 /******************************************************************
454  *              dde_connect
455  *
456  * ShellExecute helper. Used to do an operation with a DDE connection
457  *
458  * Handles both the direct connection (try #1), and if it fails,
459  * launching an application and trying (#2) to connect to it
460  *
461  */
462 static unsigned dde_connect(char* key, char* start, char* ddeexec,
463                             const char* lpFile, void *env,
464                             LPSHELLEXECUTEINFOA sei, SHELL_ExecuteA1632 execfunc)
465 {
466     char*       endkey = key + strlen(key);
467     char        app[256], topic[256], ifexec[256], res[256];
468     LONG        applen, topiclen, ifexeclen;
469     char*       exec;
470     DWORD       ddeInst = 0;
471     DWORD       tid;
472     HSZ         hszApp, hszTopic;
473     HCONV       hConv;
474     unsigned    ret = 31;
475
476     strcpy(endkey, "\\application");
477     applen = sizeof(app);
478     if (RegQueryValueA(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
479     {
480         FIXME("default app name NIY %s\n", key);
481         return 2;
482     }
483
484     strcpy(endkey, "\\topic");
485     topiclen = sizeof(topic);
486     if (RegQueryValueA(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
487     {
488         strcpy(topic, "System");
489     }
490
491     if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
492     {
493         return 2;
494     }
495
496     hszApp = DdeCreateStringHandleA(ddeInst, app, CP_WINANSI);
497     hszTopic = DdeCreateStringHandleA(ddeInst, topic, CP_WINANSI);
498
499     hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
500     exec = ddeexec;
501     if (!hConv)
502     {
503         TRACE("Launching '%s'\n", start);
504         ret = execfunc(start, env, sei, TRUE);
505         if (ret < 32)
506         {
507             TRACE("Couldn't launch\n");
508             goto error;
509         }
510         hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
511         if (!hConv)
512         {
513             TRACE("Couldn't connect. ret=%d\n", ret);
514             ret = 30; /* whatever */
515             goto error;
516         }
517         strcpy(endkey, "\\ifexec");
518         ifexeclen = sizeof(ifexec);
519         if (RegQueryValueA(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
520         {
521             exec = ifexec;
522         }
523     }
524
525     argify(res, sizeof(res), exec, lpFile);
526     TRACE("%s %s => %s\n", exec, lpFile, res);
527
528     ret = (DdeClientTransaction(res, strlen(res) + 1, hConv, 0L, 0,
529                                 XTYP_EXECUTE, 10000, &tid) != DMLERR_NO_ERROR) ? 31 : 33;
530     DdeDisconnect(hConv);
531  error:
532     DdeUninitialize(ddeInst);
533     return ret;
534 }
535
536 /*************************************************************************
537  *      execute_from_key [Internal]
538  */
539 static UINT execute_from_key(LPSTR key, LPCSTR lpFile, void *env,
540                              LPSHELLEXECUTEINFOA sei, SHELL_ExecuteA1632 execfunc)
541 {
542     char cmd[1024] = "";
543     LONG cmdlen = sizeof(cmd);
544     UINT retval = 31;
545
546     /* Get the application for the registry */
547     if (RegQueryValueA(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
548     {
549         LPSTR tmp;
550         char param[256] = "";
551         LONG paramlen = 256;
552
553         /* Get the parameters needed by the application
554            from the associated ddeexec key */
555         tmp = strstr(key, "command");
556         assert(tmp);
557         strcpy(tmp, "ddeexec");
558
559         if (RegQueryValueA(HKEY_CLASSES_ROOT, key, param, &paramlen) == ERROR_SUCCESS)
560         {
561             TRACE("Got ddeexec %s => %s\n", key, param);
562             retval = dde_connect(key, cmd, param, lpFile, env, sei, execfunc);
563         }
564         else
565         {
566             /* Is there a replace() function anywhere? */
567             cmd[cmdlen] = '\0';
568             argify(param, sizeof(param), cmd, lpFile);
569             retval = execfunc(param, env, sei, FALSE);
570         }
571     }
572     else TRACE("ooch\n");
573
574     return retval;
575 }
576
577 /*************************************************************************
578  * FindExecutableA                      [SHELL32.@]
579  */
580 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
581 {
582     UINT retval = 31;    /* default - 'No association was found' */
583     char old_dir[1024];
584
585     TRACE("File %s, Dir %s\n",
586           (lpFile != NULL ? lpFile : "-"), (lpDirectory != NULL ? lpDirectory : "-"));
587
588     lpResult[0] = '\0'; /* Start off with an empty return string */
589
590     /* trap NULL parameters on entry */
591     if ((lpFile == NULL) || (lpResult == NULL))
592     {
593         /* FIXME - should throw a warning, perhaps! */
594         return (HINSTANCE)2; /* File not found. Close enough, I guess. */
595     }
596
597     if (lpDirectory)
598     {
599         GetCurrentDirectoryA(sizeof(old_dir), old_dir);
600         SetCurrentDirectoryA(lpDirectory);
601     }
602
603     retval = SHELL_FindExecutable(lpDirectory, lpFile, "open", lpResult, NULL, NULL);
604
605     TRACE("returning %s\n", lpResult);
606     if (lpDirectory)
607         SetCurrentDirectoryA(old_dir);
608     return (HINSTANCE)retval;
609 }
610
611 /*************************************************************************
612  * FindExecutableW                      [SHELL32.@]
613  */
614 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
615 {
616     FIXME("(%p,%p,%p): stub\n", lpFile, lpDirectory, lpResult);
617     return (HINSTANCE)31;    /* default - 'No association was found' */
618 }
619
620 /*************************************************************************
621  *      ShellExecuteExA32 [Internal]
622  */
623 BOOL WINAPI ShellExecuteExA32 (LPSHELLEXECUTEINFOA sei, SHELL_ExecuteA1632 execfunc)
624 {
625     CHAR szApplicationName[MAX_PATH + 2],szCommandline[MAX_PATH],szPidl[20],fileName[MAX_PATH];
626     LPSTR pos;
627     void *env;
628     int gap, len;
629     char lpstrProtocol[256];
630     LPCSTR lpFile,lpOperation;
631     UINT retval = 31;
632     char cmd[1024];
633     BOOL done;
634
635     TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
636             sei->fMask, sei->hwnd, debugstr_a(sei->lpVerb),
637             debugstr_a(sei->lpFile), debugstr_a(sei->lpParameters),
638             debugstr_a(sei->lpDirectory), sei->nShow,
639             (sei->fMask & SEE_MASK_CLASSNAME) ? debugstr_a(sei->lpClass) : "not used");
640
641     sei->hProcess = NULL;
642     ZeroMemory(szApplicationName,MAX_PATH);
643     if (sei->lpFile)
644         strcpy(szApplicationName, sei->lpFile);
645
646     ZeroMemory(szCommandline,MAX_PATH);
647     if (sei->lpParameters)
648         strcpy(szCommandline, sei->lpParameters);
649
650     if (sei->fMask & (SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
651         SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
652         SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE |
653         SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR ))
654     {
655         FIXME("flags ignored: 0x%08lx\n", sei->fMask);
656     }
657
658     /* process the IDList */
659     if ( (sei->fMask & SEE_MASK_INVOKEIDLIST) == SEE_MASK_INVOKEIDLIST) /*0x0c*/
660     {
661         szApplicationName[0] = '"';
662         SHGetPathFromIDListA (sei->lpIDList,szApplicationName + 1);
663         strcat(szApplicationName, "\"");
664         TRACE("-- idlist=%p (%s)\n", sei->lpIDList, szApplicationName);
665     }
666     else
667     {
668         if (sei->fMask & SEE_MASK_IDLIST )
669         {
670             pos = strstr(szCommandline, "%I");
671             if (pos)
672             {
673                 LPVOID pv;
674                 HGLOBAL hmem = SHAllocShared ( sei->lpIDList, ILGetSize(sei->lpIDList), 0);
675                 pv = SHLockShared(hmem,0);
676                 sprintf(szPidl,":%p",pv );
677                 SHUnlockShared(pv);
678
679                 gap = strlen(szPidl);
680                 len = strlen(pos)-2;
681                 memmove(pos+gap,pos+2,len);
682                 memcpy(pos,szPidl,gap);
683             }
684         }
685     }
686
687     if (sei->fMask & (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY))
688     {
689         /* launch a document by fileclass like 'WordPad.Document.1' */
690         /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
691         /* FIXME: szCommandline should not be of a fixed size. Plus MAX_PATH is way too short! */
692         if (sei->fMask & SEE_MASK_CLASSKEY)
693             HCR_GetExecuteCommandExA(sei->hkeyClass,
694                                     (sei->fMask & SEE_MASK_CLASSNAME) ? sei->lpClass: NULL,
695                                     (sei->lpVerb) ? sei->lpVerb : "open", szCommandline, sizeof(szCommandline));
696         else if (sei->fMask & SEE_MASK_CLASSNAME)
697             HCR_GetExecuteCommandA(sei->lpClass, (sei->lpVerb) ? sei->lpVerb :
698                                   "open", szCommandline, sizeof(szCommandline));
699
700         /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
701         TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", szCommandline, szApplicationName);
702
703         cmd[0] = '\0';
704         done = argify(cmd, sizeof(cmd), szCommandline, szApplicationName);
705         if (!done && szApplicationName[0])
706         {
707             strcat(cmd, " ");
708             strcat(cmd, szApplicationName);
709         }
710         retval = execfunc(cmd, NULL, sei, FALSE);
711         if (retval > 32)
712             return TRUE;
713         else
714             return FALSE;
715     }
716
717     /* We set the default to open, and that should generally work.
718        But that is not really the way the MS docs say to do it. */
719     if (sei->lpVerb == NULL)
720         lpOperation = "open";
721     else
722         lpOperation = sei->lpVerb;
723
724     /* Else, try to execute the filename */
725     TRACE("execute:'%s','%s'\n",szApplicationName, szCommandline);
726
727     strcpy(fileName, szApplicationName);
728     lpFile = fileName;
729     if (szCommandline[0]) {
730         strcat(szApplicationName, " ");
731         strcat(szApplicationName, szCommandline);
732     }
733
734     retval = execfunc(szApplicationName, NULL, sei, FALSE);
735     if (retval > 32)
736         return TRUE;
737
738     /* Else, try to find the executable */
739     cmd[0] = '\0';
740     retval = SHELL_FindExecutable(sei->lpDirectory, lpFile, lpOperation, cmd, lpstrProtocol, &env);
741     if (retval > 32)  /* Found */
742     {
743         CHAR szQuotedCmd[MAX_PATH+2];
744         /* Must quote to handle case where cmd contains spaces, 
745          * else security hole if malicious user creates executable file "C:\\Program"
746          */
747         if (szCommandline[0])
748             sprintf(szQuotedCmd, "\"%s\" %s", cmd, szCommandline);
749         else
750             sprintf(szQuotedCmd, "\"%s\"", cmd);
751         TRACE("%s/%s => %s/%s\n", szApplicationName, lpOperation, szQuotedCmd, lpstrProtocol);
752         if (*lpstrProtocol)
753             retval = execute_from_key(lpstrProtocol, szApplicationName, env, sei, execfunc);
754         else
755             retval = execfunc(szQuotedCmd, env, sei, FALSE);
756         if (env) HeapFree( GetProcessHeap(), 0, env );
757     }
758     else if (PathIsURLA((LPSTR)lpFile))    /* File not found, check for URL */
759     {
760         LPSTR lpstrRes;
761         INT iSize;
762
763         lpstrRes = strchr(lpFile, ':');
764         if (lpstrRes)
765             iSize = lpstrRes - lpFile;
766         else
767             iSize = strlen(lpFile);
768
769         TRACE("Got URL: %s\n", lpFile);
770         /* Looking for ...protocol\shell\lpOperation\command */
771         strncpy(lpstrProtocol, lpFile, iSize);
772         lpstrProtocol[iSize] = '\0';
773         strcat(lpstrProtocol, "\\shell\\");
774         strcat(lpstrProtocol, lpOperation);
775         strcat(lpstrProtocol, "\\command");
776
777         /* Remove File Protocol from lpFile */
778         /* In the case file://path/file     */
779         if (!strncasecmp(lpFile, "file", iSize))
780         {
781             lpFile += iSize;
782             while (*lpFile == ':') lpFile++;
783         }
784         retval = execute_from_key(lpstrProtocol, lpFile, NULL, sei, execfunc);
785     }
786     /* Check if file specified is in the form www.??????.*** */
787     else if (!strncasecmp(lpFile, "www", 3))
788     {
789         /* if so, append lpFile http:// and call ShellExecute */
790         char lpstrTmpFile[256] = "http://" ;
791         strcat(lpstrTmpFile, lpFile);
792         retval = (UINT)ShellExecuteA(sei->hwnd, lpOperation, lpstrTmpFile, NULL, NULL, 0);
793     }
794
795     if (retval <= 32)
796     {
797         sei->hInstApp = (HINSTANCE)retval;
798         return FALSE;
799     }
800
801     sei->hInstApp = (HINSTANCE)33;
802     return TRUE;
803 }
804
805 /*************************************************************************
806  * ShellExecuteA                        [SHELL32.290]
807  */
808 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
809                                LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
810 {
811     SHELLEXECUTEINFOA sei;
812     HANDLE hProcess = 0;
813
814     TRACE("\n");
815     sei.cbSize = sizeof(sei);
816     sei.fMask = 0;
817     sei.hwnd = hWnd;
818     sei.lpVerb = lpOperation;
819     sei.lpFile = lpFile;
820     sei.lpParameters = lpParameters;
821     sei.lpDirectory = lpDirectory;
822     sei.nShow = iShowCmd;
823     sei.lpIDList = 0;
824     sei.lpClass = 0;
825     sei.hkeyClass = 0;
826     sei.dwHotKey = 0;
827     sei.hProcess = hProcess;
828
829     ShellExecuteExA32 (&sei, SHELL_ExecuteA);
830     return sei.hInstApp;
831 }
832
833 /*************************************************************************
834  * ShellExecuteEx                               [SHELL32.291]
835  *
836  */
837 BOOL WINAPI ShellExecuteExAW (LPVOID sei)
838 {
839     if (SHELL_OsIsUnicode())
840         return ShellExecuteExW (sei);
841     return ShellExecuteExA32 (sei, SHELL_ExecuteA);
842 }
843
844 /*************************************************************************
845  * ShellExecuteExA                              [SHELL32.292]
846  *
847  */
848 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
849 {
850     return  ShellExecuteExA32 (sei, SHELL_ExecuteA);
851 }
852
853 /*************************************************************************
854  * ShellExecuteExW                              [SHELL32.293]
855  *
856  */
857 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
858 {
859     SHELLEXECUTEINFOA seiA;
860     DWORD ret;
861
862     TRACE("%p\n", sei);
863
864     memcpy(&seiA, sei, sizeof(SHELLEXECUTEINFOA));
865
866     if (sei->lpVerb)
867         seiA.lpVerb = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpVerb);
868
869     if (sei->lpFile)
870         seiA.lpFile = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpFile);
871
872     if (sei->lpParameters)
873         seiA.lpParameters = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpParameters);
874
875     if (sei->lpDirectory)
876         seiA.lpDirectory = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpDirectory);
877
878     if ((sei->fMask & SEE_MASK_CLASSNAME) && sei->lpClass)
879         seiA.lpClass = HEAP_strdupWtoA( GetProcessHeap(), 0, sei->lpClass);
880     else
881         seiA.lpClass = NULL;
882
883     ret = ShellExecuteExA(&seiA);
884
885     if (seiA.lpVerb)    HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpVerb );
886     if (seiA.lpFile)    HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpFile );
887     if (seiA.lpParameters)      HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpParameters );
888     if (seiA.lpDirectory)       HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpDirectory );
889     if (seiA.lpClass)   HeapFree( GetProcessHeap(), 0, (LPSTR) seiA.lpClass );
890
891     return ret;
892 }
893
894 /*************************************************************************
895  * ShellExecuteW                        [SHELL32.294]
896  * from shellapi.h
897  * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
898  * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
899  */
900 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
901                                LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
902 {
903     SHELLEXECUTEINFOW sei;
904     HANDLE hProcess = 0;
905
906     TRACE("\n");
907     sei.cbSize = sizeof(sei);
908     sei.fMask = 0;
909     sei.hwnd = hwnd;
910     sei.lpVerb = lpOperation;
911     sei.lpFile = lpFile;
912     sei.lpParameters = lpParameters;
913     sei.lpDirectory = lpDirectory;
914     sei.nShow = nShowCmd;
915     sei.lpIDList = 0;
916     sei.lpClass = 0;
917     sei.hkeyClass = 0;
918     sei.dwHotKey = 0;
919     sei.hProcess = hProcess;
920
921     ShellExecuteExW (&sei);
922     return sei.hInstApp;
923 }