2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2002 Eric Pouech
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.
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.
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
23 #include "wine/port.h"
47 #include "wine/winbase16.h"
48 #include "shell32_main.h"
49 #include "undocshell.h"
51 #include "wine/debug.h"
53 WINE_DEFAULT_DEBUG_CHANNEL(exec);
55 static const WCHAR wszOpen[] = {'o','p','e','n',0};
56 static const WCHAR wszExe[] = {'.','e','x','e',0};
57 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
60 /***********************************************************************
61 * SHELL_ArgifyW [Internal]
63 * this function is supposed to expand the escape sequences found in the registry
64 * some diving reported that the following were used:
65 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
70 * %I address of a global item ID (explorer switch /idlist)
71 * %L seems to be %1 as long filename followed by the 8+3 variation
73 * %* all following parameters (see batfile)
75 static BOOL SHELL_ArgifyW(WCHAR* res, int len, const WCHAR* fmt, const WCHAR* lpFile)
92 if (!done || (*fmt == '1'))
94 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
96 strcpyW(res, xlpFile);
97 res += strlenW(xlpFile);
101 strcpyW(res, lpFile);
102 res += strlenW(lpFile);
107 * IE uses this alot for activating things such as windows media
108 * player. This is not verified to be fully correct but it appears
113 res += strlenW(lpFile);
116 default: FIXME("Unknown escape sequence %%%c\n", *fmt);
128 /*************************************************************************
129 * SHELL_ExecuteW [Internal]
132 static UINT SHELL_ExecuteW(WCHAR *lpCmd, void *env, LPSHELLEXECUTEINFOW sei, BOOL shWait)
134 STARTUPINFOW startup;
135 PROCESS_INFORMATION info;
138 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(sei->lpDirectory));
139 ZeroMemory(&startup,sizeof(STARTUPINFOW));
140 startup.cb = sizeof(STARTUPINFOW);
141 startup.dwFlags = STARTF_USESHOWWINDOW;
142 startup.wShowWindow = sei->nShow;
143 if (CreateProcessW(NULL, lpCmd, NULL, NULL, FALSE, 0,
144 env, sei->lpDirectory, &startup, &info))
146 /* Give 30 seconds to the app to come up, if desired. Probably only needed
147 when starting app immediately before making a DDE connection. */
149 if (WaitForInputIdle( info.hProcess, 30000 ) == -1)
150 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
152 if(sei->fMask & SEE_MASK_NOCLOSEPROCESS)
153 sei->hProcess = info.hProcess;
155 CloseHandle( info.hProcess );
156 CloseHandle( info.hThread );
158 else if ((retval = GetLastError()) >= 32)
160 FIXME("Strange error set by CreateProcess: %d\n", retval);
161 retval = ERROR_BAD_FORMAT;
164 TRACE("returning %u\n", retval);
166 sei->hInstApp = (HINSTANCE)retval;
171 /***********************************************************************
172 * SHELL_BuildEnvW [Internal]
174 * Build the environment for the new process, adding the specified
175 * path to the PATH variable. Returned pointer must be freed by caller.
177 static void *SHELL_BuildEnvW( const WCHAR *path )
179 static const WCHAR wPath[] = {'P','A','T','H','=',0};
180 WCHAR *strings, *new_env;
182 int total = strlenW(path) + 1;
183 BOOL got_path = FALSE;
185 if (!(strings = GetEnvironmentStringsW())) return NULL;
189 int len = strlenW(p) + 1;
190 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
194 if (!got_path) total += 5; /* we need to create PATH */
195 total++; /* terminating null */
197 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
199 FreeEnvironmentStringsW( strings );
206 int len = strlenW(p) + 1;
207 memcpy( p2, p, len * sizeof(WCHAR) );
208 if (!strncmpiW( p, wPath, 5 ))
211 strcpyW( p2 + len, path );
212 p2 += strlenW(path) + 1;
219 strcpyW( p2, wPath );
221 p2 += strlenW(p2) + 1;
224 FreeEnvironmentStringsW( strings );
229 /***********************************************************************
230 * SHELL_TryAppPathW [Internal]
232 * Helper function for SHELL_FindExecutable
233 * @param lpResult - pointer to a buffer of size MAX_PATH
234 * On entry: szName is a filename (probably without path separators).
235 * On exit: if szName found in "App Path", place full path in lpResult, and return true
237 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, void**env)
239 static const WCHAR wszKeyAppPaths[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',
240 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
241 static const WCHAR wPath[] = {'P','a','t','h',0};
248 if (env) *env = NULL;
249 strcpyW(buffer, wszKeyAppPaths);
250 strcatW(buffer, szName);
251 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
254 len = MAX_PATH*sizeof(WCHAR);
255 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
261 DWORD count = sizeof(buffer);
262 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
263 *env = SHELL_BuildEnvW( buffer );
267 if (hkApp) RegCloseKey(hkApp);
271 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
273 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
275 /* Looking for ...buffer\shell\<verb>\command */
276 strcatW(filetype, wszShell);
277 strcatW(filetype, lpOperation);
278 strcatW(filetype, wCommand);
280 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
281 &commandlen) == ERROR_SUCCESS)
283 commandlen /= sizeof(WCHAR);
284 if (key) strcpyW(key, filetype);
288 LONG paramlen = sizeof(param);
289 static const WCHAR wSpace[] = {' ',0};
291 /* FIXME: it seems all Windows version don't behave the same here.
292 * the doc states that this ddeexec information can be found after
294 * on Win98, it doesn't appear, but I think it does on Win2k
296 /* Get the parameters needed by the application
297 from the associated ddeexec key */
298 tmp = strstrW(filetype, wCommand);
300 strcatW(filetype, wDdeexec);
301 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
302 ¶mlen) == ERROR_SUCCESS)
304 paramlen /= sizeof(WCHAR);
305 strcatW(command, wSpace);
306 strcatW(command, param);
307 commandlen += paramlen;
311 command[commandlen] = '\0';
313 return 33; /* FIXME see SHELL_FindExecutable() */
316 return 31; /* default - 'No association was found' */
319 /*************************************************************************
320 * SHELL_FindExecutable [Internal]
322 * Utility for code sharing between FindExecutable and ShellExecute
324 * lpFile the name of a file
325 * lpOperation the operation on it (open)
327 * lpResult a buffer, big enough :-(, to store the command to do the
328 * operation on the file
329 * key a buffer, big enough, to get the key name to do actually the
330 * command (it'll be used afterwards for more information
333 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
334 LPWSTR lpResult, LPWSTR key, void **env)
336 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
337 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
338 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
339 WCHAR *extension = NULL; /* pointer to file extension */
340 WCHAR wtmpext[5]; /* local copy to mung as we please */
341 WCHAR filetype[256]; /* registry name for this filetype */
342 LONG filetypelen = sizeof(filetype); /* length of above */
343 WCHAR command[256]; /* command from registry */
344 WCHAR wBuffer[256]; /* Used to GetProfileString */
345 UINT retval = 31; /* default - 'No association was found' */
346 WCHAR *tok; /* token pointer */
347 WCHAR xlpFile[256] = {0}; /* result of SearchPath */
349 TRACE("%s\n", (lpFile != NULL) ? debugstr_w(lpFile) : "-");
351 lpResult[0] = '\0'; /* Start off with an empty return string */
352 if (key) *key = '\0';
354 /* trap NULL parameters on entry */
355 if ((lpFile == NULL) || (lpResult == NULL) || (lpOperation == NULL))
357 WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
358 debugstr_w(lpFile), debugstr_w(lpOperation), debugstr_w(lpResult));
359 return 2; /* File not found. Close enough, I guess. */
362 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
364 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
368 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
370 TRACE("SearchPathW returned non-zero\n");
372 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
375 /* First thing we need is the file's extension */
376 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
377 /* File->Run in progman uses */
379 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
381 if ((extension == NULL) || (extension == &xlpFile[strlenW(xlpFile)]))
383 WARN("Returning 31 - No association\n");
384 return 31; /* no association */
387 /* Make local copy & lowercase it for reg & 'programs=' lookup */
388 lstrcpynW(wtmpext, extension, 5);
390 TRACE("%s file\n", debugstr_w(wtmpext));
392 /* Three places to check: */
393 /* 1. win.ini, [windows], programs (NB no leading '.') */
394 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
395 /* 3. win.ini, [extensions], extension (NB no leading '.' */
396 /* All I know of the order is that registry is checked before */
397 /* extensions; however, it'd make sense to check the programs */
398 /* section first, so that's what happens here. */
400 /* See if it's a program - if GetProfileString fails, we skip this
401 * section. Actually, if GetProfileString fails, we've probably
402 * got a lot more to worry about than running a program... */
403 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
410 while (*p && *p != ' ' && *p != '\t') p++;
414 while (*p == ' ' || *p == '\t') p++;
417 if (strcmpW(tok, &wtmpext[1]) == 0) /* have to skip the leading "." */
419 strcpyW(lpResult, xlpFile);
420 /* Need to perhaps check that the file has a path
422 TRACE("found %s\n", debugstr_w(lpResult));
425 /* Greater than 32 to indicate success FIXME According to the
426 * docs, I should be returning a handle for the
427 * executable. Does this mean I'm supposed to open the
428 * executable file or something? More RTFM, I guess... */
435 if (RegQueryValueW(HKEY_CLASSES_ROOT, wtmpext, filetype,
436 &filetypelen) == ERROR_SUCCESS)
438 filetypelen /= sizeof(WCHAR);
439 filetype[filetypelen] = '\0';
440 TRACE("File type: %s\n", debugstr_w(filetype));
447 /* pass the operation string to SHELL_FindExecutableByOperation() */
448 filetype[filetypelen] = '\0';
449 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, lpOperation, key, filetype, command, sizeof(command));
453 WCHAR operation[MAX_PATH];
456 /* Looking for ...buffer\shell\<operation>\command */
457 strcatW(filetype, wszShell);
459 /* enumerate the operation subkeys in the registry and search for one with an associated command */
460 if (RegOpenKeyW(HKEY_CLASSES_ROOT, filetype, &hkey) == ERROR_SUCCESS)
465 if (RegEnumKeyW(hkey, idx, operation, MAX_PATH) != ERROR_SUCCESS)
468 filetype[filetypelen] = '\0';
469 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, operation, key, filetype, command, sizeof(command));
480 SHELL_ArgifyW(lpResult, 1024 /*FIXME*/, command, xlpFile);
483 else /* Check win.ini */
485 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
486 static const WCHAR wEmpty[] = {0};
488 /* Toss the leading dot */
490 if (GetProfileStringW(wExtensions, extension, wEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
492 if (strlenW(command) != 0)
494 strcpyW(lpResult, command);
495 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
499 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
500 tok = strchrW(command, '^'); /* see above */
501 if ((tok != NULL) && (strlenW(tok)>5))
503 strcatW(lpResult, &tok[5]);
506 retval = 33; /* FIXME - see above */
511 TRACE("returning %s\n", debugstr_w(lpResult));
515 /******************************************************************
518 * callback for the DDE connection. not really usefull
520 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
522 HDDEDATA hData, DWORD dwData1, DWORD dwData2)
527 /******************************************************************
530 * ShellExecute helper. Used to do an operation with a DDE connection
532 * Handles both the direct connection (try #1), and if it fails,
533 * launching an application and trying (#2) to connect to it
536 static unsigned dde_connect(WCHAR* key, WCHAR* start, WCHAR* ddeexec,
537 const WCHAR* lpFile, void *env,
538 LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
540 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
541 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
542 WCHAR * endkey = key + strlenW(key);
543 WCHAR app[256], topic[256], ifexec[256], res[256];
544 LONG applen, topiclen, ifexeclen;
548 HSZ hszApp, hszTopic;
552 strcpyW(endkey, wApplication);
553 applen = sizeof(app);
554 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
556 FIXME("default app name NIY %s\n", debugstr_w(key));
560 strcpyW(endkey, wTopic);
561 topiclen = sizeof(topic);
562 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
564 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
565 strcpyW(topic, wSystem);
568 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
573 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINANSI);
574 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINANSI);
576 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
580 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
581 TRACE("Launching '%s'\n", debugstr_w(start));
582 ret = execfunc(start, env, sei, TRUE);
585 TRACE("Couldn't launch\n");
588 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
591 TRACE("Couldn't connect. ret=%d\n", ret);
592 DdeUninitialize(ddeInst);
593 SetLastError(ERROR_DDE_FAIL);
594 return 30; /* whatever */
596 strcpyW(endkey, wIfexec);
597 ifexeclen = sizeof(ifexec);
598 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
604 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile);
605 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
607 ret = (DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
608 XTYP_EXECUTE, 10000, &tid) != DMLERR_NO_ERROR) ? 31 : 33;
609 DdeDisconnect(hConv);
611 DdeUninitialize(ddeInst);
615 /*************************************************************************
616 * execute_from_key [Internal]
618 static UINT execute_from_key(LPWSTR key, LPCWSTR lpFile, void *env,
619 LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
621 WCHAR cmd[1024] = {0};
622 LONG cmdlen = sizeof(cmd);
625 /* Get the application for the registry */
626 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
628 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
629 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
631 WCHAR param[256] = {0};
632 LONG paramlen = sizeof(param);
634 /* Get the parameters needed by the application
635 from the associated ddeexec key */
636 tmp = strstrW(key, wCommand);
638 strcpyW(tmp, wDdeexec);
640 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, param, ¶mlen) == ERROR_SUCCESS)
642 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(param));
643 retval = dde_connect(key, cmd, param, lpFile, env, sei, execfunc);
647 /* Is there a replace() function anywhere? */
648 cmdlen /= sizeof(WCHAR);
650 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile);
651 retval = execfunc(param, env, sei, FALSE);
654 else TRACE("ooch\n");
659 /*************************************************************************
660 * FindExecutableA [SHELL32.@]
662 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
665 WCHAR *wFile = NULL, *wDirectory = NULL;
666 WCHAR wResult[MAX_PATH];
668 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
669 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
671 retval = FindExecutableW(wFile, wDirectory, wResult);
672 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
673 if (wFile) SHFree( wFile );
674 if (wDirectory) SHFree( wDirectory );
676 TRACE("returning %s\n", lpResult);
677 return (HINSTANCE)retval;
680 /*************************************************************************
681 * FindExecutableW [SHELL32.@]
683 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
685 UINT retval = 31; /* default - 'No association was found' */
688 TRACE("File %s, Dir %s\n",
689 (lpFile != NULL ? debugstr_w(lpFile) : "-"), (lpDirectory != NULL ? debugstr_w(lpDirectory) : "-"));
691 lpResult[0] = '\0'; /* Start off with an empty return string */
693 /* trap NULL parameters on entry */
694 if ((lpFile == NULL) || (lpResult == NULL))
696 /* FIXME - should throw a warning, perhaps! */
697 return (HINSTANCE)2; /* File not found. Close enough, I guess. */
702 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
703 SetCurrentDirectoryW(lpDirectory);
706 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, NULL, NULL);
708 TRACE("returning %s\n", debugstr_w(lpResult));
710 SetCurrentDirectoryW(old_dir);
711 return (HINSTANCE)retval;
714 /*************************************************************************
715 * ShellExecuteExW32 [Internal]
717 BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
719 static const WCHAR wQuote[] = {'"',0};
720 static const WCHAR wSpace[] = {' ',0};
721 static const WCHAR wWww[] = {'w','w','w',0};
722 static const WCHAR wFile[] = {'f','i','l','e',0};
723 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
724 WCHAR wszApplicationName[MAX_PATH+2],wszCommandline[1024],wszPidl[20],wfileName[MAX_PATH];
728 WCHAR lpstrProtocol[256];
734 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
735 sei->fMask, sei->hwnd, debugstr_w(sei->lpVerb),
736 debugstr_w(sei->lpFile), debugstr_w(sei->lpParameters),
737 debugstr_w(sei->lpDirectory), sei->nShow,
738 (sei->fMask & SEE_MASK_CLASSNAME) ? debugstr_w(sei->lpClass) : "not used");
740 sei->hProcess = NULL;
741 ZeroMemory(wszApplicationName,MAX_PATH);
743 strcpyW(wszApplicationName, sei->lpFile);
745 ZeroMemory(wszCommandline,1024);
746 if (sei->lpParameters)
747 strcpyW(wszCommandline, sei->lpParameters);
749 if (sei->fMask & (SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
750 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
751 SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE |
752 SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR ))
754 FIXME("flags ignored: 0x%08lx\n", sei->fMask);
757 /* process the IDList */
758 if ( (sei->fMask & SEE_MASK_INVOKEIDLIST) == SEE_MASK_INVOKEIDLIST) /*0x0c*/
760 wszApplicationName[0] = '"';
761 SHGetPathFromIDListW(sei->lpIDList,wszApplicationName + 1);
762 strcatW(wszApplicationName, wQuote);
763 TRACE("-- idlist=%p (%s)\n", sei->lpIDList, debugstr_w(wszApplicationName));
767 if (sei->fMask & SEE_MASK_IDLIST )
769 static const WCHAR wI[] = {'%','I',0}, wP[] = {':','%','p',0};
770 pos = strstrW(wszCommandline, wI);
774 HGLOBAL hmem = SHAllocShared ( sei->lpIDList, ILGetSize(sei->lpIDList), 0);
775 pv = SHLockShared(hmem,0);
776 sprintfW(wszPidl,wP,pv );
779 gap = strlenW(wszPidl);
780 len = strlenW(pos)-2;
781 memmove(pos+gap,pos+2,len*sizeof(WCHAR));
782 memcpy(pos,wszPidl,gap*sizeof(WCHAR));
787 if (sei->fMask & (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY))
789 /* launch a document by fileclass like 'WordPad.Document.1' */
790 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
791 /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
792 HCR_GetExecuteCommandW((sei->fMask & SEE_MASK_CLASSKEY) ? sei->hkeyClass : NULL,
793 (sei->fMask & SEE_MASK_CLASSNAME) ? sei->lpClass: NULL,
794 (sei->lpVerb) ? sei->lpVerb : wszOpen,
795 wszCommandline, sizeof(wszCommandline)/sizeof(WCHAR));
797 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
798 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(wszCommandline), debugstr_w(wszApplicationName));
801 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), wszCommandline, wszApplicationName);
802 if (!done && wszApplicationName[0])
804 strcatW(wcmd, wSpace);
805 strcatW(wcmd, wszApplicationName);
807 retval = execfunc(wcmd, NULL, sei, FALSE);
814 /* Else, try to execute the filename */
815 TRACE("execute:'%s','%s'\n", debugstr_w(wszApplicationName), debugstr_w(wszCommandline));
817 strcpyW(wfileName, wszApplicationName);
819 if (wszCommandline[0]) {
820 strcatW(wszApplicationName, wSpace);
821 strcatW(wszApplicationName, wszCommandline);
824 retval = execfunc(wszApplicationName, NULL, sei, FALSE);
828 /* Else, try to find the executable */
830 retval = SHELL_FindExecutable(sei->lpDirectory, lpFile, sei->lpVerb, wcmd, lpstrProtocol, &env);
831 if (retval > 32) /* Found */
833 WCHAR wszQuotedCmd[MAX_PATH+2];
834 /* Must quote to handle case where cmd contains spaces,
835 * else security hole if malicious user creates executable file "C:\\Program"
837 strcpyW(wszQuotedCmd, wQuote);
838 strcatW(wszQuotedCmd, wcmd);
839 strcatW(wszQuotedCmd, wQuote);
840 if (wszCommandline[0]) {
841 strcatW(wszQuotedCmd, wSpace);
842 strcatW(wszQuotedCmd, wszCommandline);
844 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(sei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
846 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, sei, execfunc);
848 retval = execfunc(wszQuotedCmd, env, sei, FALSE);
849 if (env) HeapFree( GetProcessHeap(), 0, env );
851 else if (PathIsURLW((LPWSTR)lpFile)) /* File not found, check for URL */
853 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
854 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
858 lpstrRes = strchrW(lpFile, ':');
860 iSize = lpstrRes - lpFile;
862 iSize = strlenW(lpFile);
864 TRACE("Got URL: %s\n", debugstr_w(lpFile));
865 /* Looking for ...protocol\shell\lpOperation\command */
866 strncpyW(lpstrProtocol, lpFile, iSize);
867 lpstrProtocol[iSize] = '\0';
868 strcatW(lpstrProtocol, wShell);
869 strcatW(lpstrProtocol, sei->lpVerb? sei->lpVerb: wszOpen);
870 strcatW(lpstrProtocol, wCommand);
872 /* Remove File Protocol from lpFile */
873 /* In the case file://path/file */
874 if (!strncmpiW(lpFile, wFile, iSize))
877 while (*lpFile == ':') lpFile++;
879 retval = execute_from_key(lpstrProtocol, lpFile, NULL, sei, execfunc);
881 /* Check if file specified is in the form www.??????.*** */
882 else if (!strncmpiW(lpFile, wWww, 3))
884 /* if so, append lpFile http:// and call ShellExecute */
885 WCHAR lpstrTmpFile[256];
886 strcpyW(lpstrTmpFile, wHttp);
887 strcatW(lpstrTmpFile, lpFile);
888 retval = (UINT)ShellExecuteW(sei->hwnd, sei->lpVerb, lpstrTmpFile, NULL, NULL, 0);
891 TRACE("retval %u\n", retval);
895 sei->hInstApp = (HINSTANCE)retval;
899 sei->hInstApp = (HINSTANCE)33;
903 /*************************************************************************
904 * ShellExecuteA [SHELL32.290]
906 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
907 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
909 SHELLEXECUTEINFOA sei;
913 sei.cbSize = sizeof(sei);
916 sei.lpVerb = lpOperation;
918 sei.lpParameters = lpParameters;
919 sei.lpDirectory = lpDirectory;
920 sei.nShow = iShowCmd;
925 sei.hProcess = hProcess;
927 ShellExecuteExA (&sei);
931 /*************************************************************************
932 * ShellExecuteEx [SHELL32.291]
935 BOOL WINAPI ShellExecuteExAW (LPVOID sei)
937 if (SHELL_OsIsUnicode())
938 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
939 return ShellExecuteExA (sei);
942 /*************************************************************************
943 * ShellExecuteExA [SHELL32.292]
946 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
948 SHELLEXECUTEINFOW seiW;
950 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
954 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
957 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
960 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
962 if (sei->lpParameters)
963 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
965 if (sei->lpDirectory)
966 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
968 if ((sei->fMask & SEE_MASK_CLASSNAME) && sei->lpClass)
969 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
973 ret = ShellExecuteExW32 (&seiW, SHELL_ExecuteW);
975 sei->hInstApp = seiW.hInstApp;
977 if (wVerb) SHFree(wVerb);
978 if (wFile) SHFree(wFile);
979 if (wParameters) SHFree(wParameters);
980 if (wDirectory) SHFree(wDirectory);
981 if (wClass) SHFree(wClass);
986 /*************************************************************************
987 * ShellExecuteExW [SHELL32.293]
990 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
992 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
995 /*************************************************************************
996 * ShellExecuteW [SHELL32.294]
998 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
999 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1001 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1002 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1004 SHELLEXECUTEINFOW sei;
1005 HANDLE hProcess = 0;
1008 sei.cbSize = sizeof(sei);
1011 sei.lpVerb = lpOperation;
1012 sei.lpFile = lpFile;
1013 sei.lpParameters = lpParameters;
1014 sei.lpDirectory = lpDirectory;
1015 sei.nShow = nShowCmd;
1020 sei.hProcess = hProcess;
1022 ShellExecuteExW32 (&sei, SHELL_ExecuteW);
1023 return sei.hInstApp;