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