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"
49 #include "wine/winbase16.h"
50 #include "shell32_main.h"
51 #include "undocshell.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(exec);
58 static const WCHAR wszOpen[] = {'o','p','e','n',0};
59 static const WCHAR wszExe[] = {'.','e','x','e',0};
60 static const WCHAR wszILPtr[] = {':','%','p',0};
61 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
62 static const WCHAR wszFolder[] = {'F','o','l','d','e','r',0};
63 static const WCHAR wszEmpty[] = {0};
66 /***********************************************************************
67 * SHELL_ArgifyW [Internal]
69 * this function is supposed to expand the escape sequences found in the registry
70 * some diving reported that the following were used:
71 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
76 * %I address of a global item ID (explorer switch /idlist)
77 * %L seems to be %1 as long filename followed by the 8+3 variation
79 * %* all following parameters (see batfile)
82 * FIXME: Careful of going over string boundaries. No checking is done to 'res'...
84 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args)
92 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
93 debugstr_w(lpFile), pidl, args);
127 while(*args && !isspace(*args))
130 while(isspace(*args))
135 /* else fall through */
137 if (!done || (*fmt == '1'))
139 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
140 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
145 /* Add double quotation marks unless we already have them
146 (e.g.: "file://%1" %* for exefile) or unless the arg is already
147 enclosed in double quotation marks */
148 if ((res == out || *(fmt + 1) != '"') && *cmd != '"')
164 * IE uses this a lot for activating things such as windows media
165 * player. This is not verified to be fully correct but it appears
171 strcpyW(res, lpFile);
172 res += strlenW(lpFile);
179 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
180 pv = SHLockShared(hmem, 0);
181 res += sprintfW(res, wszILPtr, pv);
188 * Check if this is a env-variable here...
191 /* Make sure that we have at least one more %.*/
192 if (strchrW(fmt, '%'))
194 WCHAR tmpBuffer[1024];
195 PWSTR tmpB = tmpBuffer;
196 WCHAR tmpEnvBuff[MAX_PATH];
203 TRACE("Checking %s to be a env-var\n", debugstr_w(tmpBuffer));
205 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
206 if (envRet == 0 || envRet > MAX_PATH)
207 strcpyW( res, tmpBuffer );
209 strcpyW( res, tmpEnvBuff );
215 /* Don't skip past terminator (catch a single '%' at the end) */
230 HRESULT SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl, LPSTR pszPath, UINT uOutSize)
233 IShellFolder* desktop;
235 HRESULT hr = SHGetDesktopFolder(&desktop);
238 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
241 StrRetToStrNA(pszPath, uOutSize, &strret, pidl);
243 IShellFolder_Release(desktop);
249 HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
252 IShellFolder* desktop;
254 HRESULT hr = SHGetDesktopFolder(&desktop);
257 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
260 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
262 IShellFolder_Release(desktop);
268 /*************************************************************************
269 * SHELL_ResolveShortCutW [Internal]
270 * read shortcut file at 'wcmd'
272 static HRESULT SHELL_ResolveShortCutW(LPWSTR wcmd, LPWSTR wargs, LPWSTR wdir, HWND hwnd, LPCWSTR lpVerb, int* pshowcmd, LPITEMIDLIST* ppidl)
276 HRESULT hr = SHGetDesktopFolder(&psf);
284 hr = IShellFolder_ParseDisplayName(psf, 0, 0, wcmd, &l, &pidl, 0);
289 hr = IShellFolder_GetUIObjectOf(psf, NULL, 1, (LPCITEMIDLIST*)&pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
292 hr = IShellLinkW_Resolve(psl, hwnd, 0);
295 hr = IShellLinkW_GetPath(psl, wcmd, MAX_PATH, NULL, SLGP_UNCPRIORITY);
299 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
300 hr = IShellLinkW_GetIDList(psl, ppidl);
302 if (SUCCEEDED(hr) && *ppidl) {
303 /* We got a PIDL instead of a file system path - try to translate it. */
304 if (SUCCEEDED(SHELL_GetPathFromIDListW(*ppidl, wcmd, MAX_PATH))) {
312 /* get command line arguments, working directory and display mode if available */
313 IShellLinkW_GetWorkingDirectory(psl, wdir, MAX_PATH);
314 IShellLinkW_GetArguments(psl, wargs, MAX_PATH);
315 IShellLinkW_GetShowCmd(psl, pshowcmd);
320 IShellLinkW_Release(psl);
326 IShellFolder_Release(psf);
332 /*************************************************************************
333 * SHELL_ExecuteW [Internal]
336 static UINT SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
337 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
339 STARTUPINFOW startup;
340 PROCESS_INFORMATION info;
343 WCHAR curdir[MAX_PATH];
345 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
346 /* ShellExecute specifies the command from psei->lpDirectory
347 * if present. Not from the current dir as CreateProcess does */
348 if( psei->lpDirectory && psei->lpDirectory[0] )
349 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
350 if( !SetCurrentDirectoryW( psei->lpDirectory))
351 ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory));
352 ZeroMemory(&startup,sizeof(STARTUPINFOW));
353 startup.cb = sizeof(STARTUPINFOW);
354 startup.dwFlags = STARTF_USESHOWWINDOW;
355 startup.wShowWindow = psei->nShow;
356 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT,
357 env, *psei->lpDirectory? psei->lpDirectory: NULL, &startup, &info))
359 /* Give 30 seconds to the app to come up, if desired. Probably only needed
360 when starting app immediately before making a DDE connection. */
362 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
363 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
365 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
366 psei_out->hProcess = info.hProcess;
368 CloseHandle( info.hProcess );
369 CloseHandle( info.hThread );
371 else if ((retval = GetLastError()) >= 32)
373 FIXME("Strange error set by CreateProcess: %d\n", retval);
374 retval = ERROR_BAD_FORMAT;
377 TRACE("returning %u\n", retval);
379 psei_out->hInstApp = (HINSTANCE)retval;
381 if( !SetCurrentDirectoryW( curdir))
382 ERR("cannot return to directory %s\n", debugstr_w(curdir));
388 /***********************************************************************
389 * SHELL_BuildEnvW [Internal]
391 * Build the environment for the new process, adding the specified
392 * path to the PATH variable. Returned pointer must be freed by caller.
394 static void *SHELL_BuildEnvW( const WCHAR *path )
396 static const WCHAR wPath[] = {'P','A','T','H','=',0};
397 WCHAR *strings, *new_env;
399 int total = strlenW(path) + 1;
400 BOOL got_path = FALSE;
402 if (!(strings = GetEnvironmentStringsW())) return NULL;
406 int len = strlenW(p) + 1;
407 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
411 if (!got_path) total += 5; /* we need to create PATH */
412 total++; /* terminating null */
414 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
416 FreeEnvironmentStringsW( strings );
423 int len = strlenW(p) + 1;
424 memcpy( p2, p, len * sizeof(WCHAR) );
425 if (!strncmpiW( p, wPath, 5 ))
428 strcpyW( p2 + len, path );
429 p2 += strlenW(path) + 1;
436 strcpyW( p2, wPath );
438 p2 += strlenW(p2) + 1;
441 FreeEnvironmentStringsW( strings );
446 /***********************************************************************
447 * SHELL_TryAppPathW [Internal]
449 * Helper function for SHELL_FindExecutable
450 * @param lpResult - pointer to a buffer of size MAX_PATH
451 * On entry: szName is a filename (probably without path separators).
452 * On exit: if szName found in "App Path", place full path in lpResult, and return true
454 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
456 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',
457 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
458 static const WCHAR wPath[] = {'P','a','t','h',0};
465 if (env) *env = NULL;
466 strcpyW(buffer, wszKeyAppPaths);
467 strcatW(buffer, szName);
468 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
471 len = MAX_PATH*sizeof(WCHAR);
472 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
478 DWORD count = sizeof(buffer);
479 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
480 *env = SHELL_BuildEnvW( buffer );
484 if (hkApp) RegCloseKey(hkApp);
488 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
490 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
492 /* Looking for ...buffer\shell\<verb>\command */
493 strcatW(filetype, wszShell);
494 strcatW(filetype, lpOperation);
495 strcatW(filetype, wCommand);
497 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
498 &commandlen) == ERROR_SUCCESS)
500 commandlen /= sizeof(WCHAR);
501 if (key) strcpyW(key, filetype);
505 LONG paramlen = sizeof(param);
506 static const WCHAR wSpace[] = {' ',0};
508 /* FIXME: it seems all Windows version don't behave the same here.
509 * the doc states that this ddeexec information can be found after
511 * on Win98, it doesn't appear, but I think it does on Win2k
513 /* Get the parameters needed by the application
514 from the associated ddeexec key */
515 tmp = strstrW(filetype, wCommand);
517 strcatW(filetype, wDdeexec);
518 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
519 ¶mlen) == ERROR_SUCCESS)
521 paramlen /= sizeof(WCHAR);
522 strcatW(command, wSpace);
523 strcatW(command, param);
524 commandlen += paramlen;
528 command[commandlen] = '\0';
530 return 33; /* FIXME see SHELL_FindExecutable() */
533 return 31; /* default - 'No association was found' */
536 /*************************************************************************
537 * SHELL_FindExecutable [Internal]
539 * Utility for code sharing between FindExecutable and ShellExecute
541 * lpFile the name of a file
542 * lpOperation the operation on it (open)
544 * lpResult a buffer, big enough :-(, to store the command to do the
545 * operation on the file
546 * key a buffer, big enough, to get the key name to do actually the
547 * command (it'll be used afterwards for more information
550 UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
551 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
553 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
554 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
555 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
556 WCHAR *extension = NULL; /* pointer to file extension */
557 WCHAR filetype[256]; /* registry name for this filetype */
558 LONG filetypelen = sizeof(filetype); /* length of above */
559 WCHAR command[1024]; /* command from registry */
560 WCHAR wBuffer[256]; /* Used to GetProfileString */
561 UINT retval = 31; /* default - 'No association was found' */
562 WCHAR *tok; /* token pointer */
563 WCHAR xlpFile[256]; /* result of SearchPath */
564 DWORD attribs; /* file attributes */
566 TRACE("%s\n", (lpFile != NULL) ? debugstr_w(lpFile) : "-");
569 lpResult[0] = '\0'; /* Start off with an empty return string */
570 if (key) *key = '\0';
572 /* trap NULL parameters on entry */
573 if ((lpFile == NULL) || (lpResult == NULL) || (lpOperation == NULL))
575 WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
576 debugstr_w(lpFile), debugstr_w(lpOperation), debugstr_w(lpResult));
577 return 2; /* File not found. Close enough, I guess. */
580 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
582 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
586 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
588 TRACE("SearchPathW returned non-zero\n");
590 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
593 attribs = GetFileAttributesW(lpFile);
594 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
596 strcpyW(filetype, wszFolder);
597 filetypelen = 6; /* strlen("Folder") */
601 /* First thing we need is the file's extension */
602 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
603 /* File->Run in progman uses */
605 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
607 if (extension == NULL || extension[1]==0)
609 WARN("Returning 31 - No association\n");
610 return 31; /* no association */
613 /* Three places to check: */
614 /* 1. win.ini, [windows], programs (NB no leading '.') */
615 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
616 /* 3. win.ini, [extensions], extension (NB no leading '.' */
617 /* All I know of the order is that registry is checked before */
618 /* extensions; however, it'd make sense to check the programs */
619 /* section first, so that's what happens here. */
621 /* See if it's a program - if GetProfileString fails, we skip this
622 * section. Actually, if GetProfileString fails, we've probably
623 * got a lot more to worry about than running a program... */
624 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
631 while (*p && *p != ' ' && *p != '\t') p++;
635 while (*p == ' ' || *p == '\t') p++;
638 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
640 strcpyW(lpResult, xlpFile);
641 /* Need to perhaps check that the file has a path
643 TRACE("found %s\n", debugstr_w(lpResult));
646 /* Greater than 32 to indicate success FIXME According to the
647 * docs, I should be returning a handle for the
648 * executable. Does this mean I'm supposed to open the
649 * executable file or something? More RTFM, I guess... */
656 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
657 &filetypelen) == ERROR_SUCCESS)
659 filetypelen /= sizeof(WCHAR);
660 filetype[filetypelen] = '\0';
661 TRACE("File type: %s\n", debugstr_w(filetype));
669 /* pass the operation string to SHELL_FindExecutableByOperation() */
670 filetype[filetypelen] = '\0';
671 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, lpOperation, key, filetype, command, sizeof(command));
675 WCHAR operation[MAX_PATH];
678 /* Looking for ...buffer\shell\<operation>\command */
679 strcatW(filetype, wszShell);
681 /* enumerate the operation subkeys in the registry and search for one with an associated command */
682 if (RegOpenKeyW(HKEY_CLASSES_ROOT, filetype, &hkey) == ERROR_SUCCESS)
687 if (RegEnumKeyW(hkey, idx, operation, MAX_PATH) != ERROR_SUCCESS)
690 filetype[filetypelen] = '\0';
691 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, operation, key, filetype, command, sizeof(command));
702 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args);
704 /* Remove double quotation marks and command line arguments */
705 if (*lpResult == '"')
708 while (*(p + 1) != '"')
717 else /* Check win.ini */
719 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
721 /* Toss the leading dot */
723 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
725 if (strlenW(command) != 0)
727 strcpyW(lpResult, command);
728 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
732 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
733 tok = strchrW(command, '^'); /* see above */
734 if ((tok != NULL) && (strlenW(tok)>5))
736 strcatW(lpResult, &tok[5]);
739 retval = 33; /* FIXME - see above */
744 TRACE("returning %s\n", debugstr_w(lpResult));
748 /******************************************************************
751 * callback for the DDE connection. not really usefull
753 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
754 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
755 ULONG_PTR dwData1, ULONG_PTR dwData2)
757 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
758 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
762 /******************************************************************
765 * ShellExecute helper. Used to do an operation with a DDE connection
767 * Handles both the direct connection (try #1), and if it fails,
768 * launching an application and trying (#2) to connect to it
771 static unsigned dde_connect(WCHAR* key, WCHAR* start, WCHAR* ddeexec,
772 const WCHAR* lpFile, WCHAR *env,
773 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
774 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
776 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
777 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
778 WCHAR * endkey = key + strlenW(key);
779 WCHAR app[256], topic[256], ifexec[256], res[256];
780 LONG applen, topiclen, ifexeclen;
784 HSZ hszApp, hszTopic;
789 strcpyW(endkey, wApplication);
790 applen = sizeof(app);
791 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
793 FIXME("default app name NIY %s\n", debugstr_w(key));
797 strcpyW(endkey, wTopic);
798 topiclen = sizeof(topic);
799 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
801 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
802 strcpyW(topic, wSystem);
805 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
810 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
811 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
813 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
817 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
818 TRACE("Launching '%s'\n", debugstr_w(start));
819 ret = execfunc(start, env, TRUE, psei, psei_out);
822 TRACE("Couldn't launch\n");
825 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
828 TRACE("Couldn't connect. ret=%d\n", ret);
829 DdeUninitialize(ddeInst);
830 SetLastError(ERROR_DDE_FAIL);
831 return 30; /* whatever */
833 strcpyW(endkey, wIfexec);
834 ifexeclen = sizeof(ifexec);
835 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
841 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline);
842 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
844 /* It's documented in the KB 330337 that IE has a bug and returns
845 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
847 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
848 XTYP_EXECUTE, 10000, &tid);
850 DdeFreeDataHandle(hDdeData);
852 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
855 DdeDisconnect(hConv);
858 DdeUninitialize(ddeInst);
863 /*************************************************************************
864 * execute_from_key [Internal]
866 static UINT execute_from_key(LPWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
867 SHELL_ExecuteW32 execfunc,
868 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
871 LONG cmdlen = sizeof(cmd);
876 /* Get the application for the registry */
877 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
879 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
880 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
883 LONG paramlen = sizeof(param);
887 /* Get the parameters needed by the application
888 from the associated ddeexec key */
889 tmp = strstrW(key, wCommand);
891 strcpyW(tmp, wDdeexec);
893 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, param, ¶mlen) == ERROR_SUCCESS)
895 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(param));
896 retval = dde_connect(key, cmd, param, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
900 /* Is there a replace() function anywhere? */
901 cmdlen /= sizeof(WCHAR);
903 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline);
904 retval = execfunc(param, env, FALSE, psei, psei_out);
907 else TRACE("ooch\n");
912 /*************************************************************************
913 * FindExecutableA [SHELL32.@]
915 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
918 WCHAR *wFile = NULL, *wDirectory = NULL;
919 WCHAR wResult[MAX_PATH];
921 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
922 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
924 retval = FindExecutableW(wFile, wDirectory, wResult);
925 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
926 if (wFile) SHFree( wFile );
927 if (wDirectory) SHFree( wDirectory );
929 TRACE("returning %s\n", lpResult);
930 return (HINSTANCE)retval;
933 /*************************************************************************
934 * FindExecutableW [SHELL32.@]
936 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
938 UINT retval = 31; /* default - 'No association was found' */
941 TRACE("File %s, Dir %s\n",
942 (lpFile != NULL ? debugstr_w(lpFile) : "-"), (lpDirectory != NULL ? debugstr_w(lpDirectory) : "-"));
944 lpResult[0] = '\0'; /* Start off with an empty return string */
946 /* trap NULL parameters on entry */
947 if ((lpFile == NULL) || (lpResult == NULL))
949 /* FIXME - should throw a warning, perhaps! */
950 return (HINSTANCE)2; /* File not found. Close enough, I guess. */
955 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
956 SetCurrentDirectoryW(lpDirectory);
959 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
961 TRACE("returning %s\n", debugstr_w(lpResult));
963 SetCurrentDirectoryW(old_dir);
964 return (HINSTANCE)retval;
967 /*************************************************************************
968 * ShellExecuteExW32 [Internal]
970 BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
972 static const WCHAR wQuote[] = {'"',0};
973 static const WCHAR wSpace[] = {' ',0};
974 static const WCHAR wWww[] = {'w','w','w',0};
975 static const WCHAR wFile[] = {'f','i','l','e',0};
976 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
977 static const WCHAR wExtLnk[] = {'.','l','n','k',0};
978 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
980 WCHAR wszApplicationName[MAX_PATH+2], wszParameters[1024], wszDir[MAX_PATH];
981 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
982 WCHAR wfileName[MAX_PATH];
984 WCHAR lpstrProtocol[256];
988 WCHAR buffer[MAX_PATH];
992 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
993 memcpy(&sei_tmp, sei, sizeof(sei_tmp));
995 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
996 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
997 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
998 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
999 (sei_tmp.fMask & SEE_MASK_CLASSNAME) ? debugstr_w(sei_tmp.lpClass) : "not used");
1001 sei->hProcess = NULL;
1003 /* make copies of all path/command strings */
1005 strcpyW(wszApplicationName, sei_tmp.lpFile);
1007 *wszApplicationName = '\0';
1009 if (sei_tmp.lpParameters)
1010 strcpyW(wszParameters, sei_tmp.lpParameters);
1012 *wszParameters = '\0';
1014 if (sei_tmp.lpDirectory)
1015 strcpyW(wszDir, sei_tmp.lpDirectory);
1019 /* adjust string pointers to point to the new buffers */
1020 sei_tmp.lpFile = wszApplicationName;
1021 sei_tmp.lpParameters = wszParameters;
1022 sei_tmp.lpDirectory = wszDir;
1024 if (sei_tmp.fMask & (SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1025 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1026 SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE |
1027 SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR ))
1029 FIXME("flags ignored: 0x%08lx\n", sei_tmp.fMask);
1032 /* process the IDList */
1033 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1035 IShellExecuteHookW* pSEH;
1037 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1041 hr = IShellExecuteHookW_Execute(pSEH, sei);
1043 IShellExecuteHookW_Release(pSEH);
1049 wszApplicationName[0] = '"';
1050 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName+1);
1051 strcatW(wszApplicationName, wQuote);
1052 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1055 if (sei_tmp.fMask & (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY))
1057 /* launch a document by fileclass like 'WordPad.Document.1' */
1058 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1059 /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1060 HCR_GetExecuteCommandW((sei_tmp.fMask & SEE_MASK_CLASSKEY) ? sei_tmp.hkeyClass : NULL,
1061 (sei_tmp.fMask & SEE_MASK_CLASSNAME) ? sei_tmp.lpClass: NULL,
1062 (sei_tmp.lpVerb) ? sei_tmp.lpVerb : wszOpen,
1063 wszParameters, sizeof(wszParameters)/sizeof(WCHAR));
1065 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1066 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(wszParameters), debugstr_w(wszApplicationName));
1069 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), wszParameters, wszApplicationName, sei_tmp.lpIDList, NULL);
1070 if (!done && wszApplicationName[0])
1072 strcatW(wcmd, wSpace);
1073 strcatW(wcmd, wszApplicationName);
1075 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1083 /* resolve shell shortcuts */
1084 ext = PathFindExtensionW(sei_tmp.lpFile);
1086 if (ext && !strncmpiW(ext, wExtLnk, sizeof(wExtLnk) / sizeof(WCHAR) - 1) &&
1087 (ext[sizeof(wExtLnk) / sizeof(WCHAR) - 1] == '\0' ||
1088 (sei_tmp.lpFile[0] == '"' && ext[sizeof(wExtLnk) / sizeof(WCHAR) - 1] == '"'))) /* or check for: shell_attribs & SFGAO_LINK */
1093 if (wszApplicationName[0] == '"')
1095 if (wszApplicationName[strlenW(wszApplicationName) - 1] == '"')
1097 wszApplicationName[strlenW(wszApplicationName) - 1] = '\0';
1109 /* expand paths before reading shell link */
1110 if (ExpandEnvironmentStringsW(Quoted ? sei_tmp.lpFile + 1 : sei_tmp.lpFile, buffer, MAX_PATH))
1111 lstrcpyW(Quoted ? wszApplicationName + 1 : wszApplicationName/*sei_tmp.lpFile*/, buffer);
1113 if (*sei_tmp.lpParameters)
1114 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1115 lstrcpyW(wszParameters/*sei_tmp.lpParameters*/, buffer);
1117 hr = SHELL_ResolveShortCutW((LPWSTR)(Quoted ? sei_tmp.lpFile + 1 : sei_tmp.lpFile),
1118 (LPWSTR)sei_tmp.lpParameters, (LPWSTR)sei_tmp.lpDirectory,
1119 sei_tmp.hwnd, sei_tmp.lpVerb?sei_tmp.lpVerb:wszEmpty, &sei_tmp.nShow, (LPITEMIDLIST*)&sei_tmp.lpIDList);
1122 wszApplicationName[strlenW(wszApplicationName) + 1] = '\0';
1123 wszApplicationName[strlenW(wszApplicationName)] = '"';
1127 sei->fMask |= SEE_MASK_IDLIST;
1131 /* repeat IDList processing if needed */
1132 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1134 IShellExecuteHookW* pSEH;
1136 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1140 hr = IShellExecuteHookW_Execute(pSEH, sei);
1142 IShellExecuteHookW_Release(pSEH);
1148 TRACE("-- idlist=%p (%s)\n", debugstr_w(sei_tmp.lpIDList), debugstr_w(sei_tmp.lpFile));
1154 /* Has the IDList not yet been translated? */
1155 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1157 /* last chance to translate IDList: now also allow CLSID paths */
1158 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp.lpIDList, buffer, sizeof(buffer)))) {
1159 if (buffer[0]==':' && buffer[1]==':') {
1160 /* open shell folder for the specified class GUID */
1161 strcpyW(wszParameters, buffer);
1162 strcpyW(wszApplicationName, wExplorer);
1164 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1165 } else if (HCR_GetExecuteCommandW(0, wszFolder, sei_tmp.lpVerb?sei_tmp.lpVerb:wszOpen, buffer, sizeof(buffer))) {
1166 SHELL_ArgifyW(wszApplicationName, sizeof(wszApplicationName)/sizeof(WCHAR), buffer, NULL, sei_tmp.lpIDList, NULL);
1168 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1173 /* expand environment strings */
1174 if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
1175 lstrcpyW(wszApplicationName, buffer);
1177 if (*sei_tmp.lpParameters)
1178 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1179 lstrcpyW(wszParameters, buffer);
1181 if (*sei_tmp.lpDirectory)
1182 if (ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buffer, MAX_PATH))
1183 lstrcpyW(wszDir, buffer);
1185 /* Else, try to execute the filename */
1186 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1188 /* separate out command line arguments from executable file name */
1189 if (!*sei_tmp.lpParameters) {
1190 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1191 if (sei_tmp.lpFile[0] == '"') {
1192 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1193 LPWSTR dst = wfileName;
1196 /* copy the unquoted executable path to 'wfileName' */
1197 while(*src && *src!='"')
1205 while(isspace(*src))
1210 /* copy the parameter string to 'wszParameters' */
1211 strcpyW(wszParameters, src);
1213 /* terminate previous command string after the quote character */
1218 /* If the executable name is not quoted, we have to use this search loop here,
1219 that in CreateProcess() is not sufficient because it does not handle shell links. */
1220 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1223 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1224 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1225 int idx = space-sei_tmp.lpFile;
1226 strncpyW(buffer, sei_tmp.lpFile, idx);
1229 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1230 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile), xlpFile, NULL))
1232 /* separate out command from parameter string */
1233 LPCWSTR p = space + 1;
1238 strcpyW(wszParameters, p);
1245 strcpyW(wfileName, sei_tmp.lpFile);
1248 strcpyW(wfileName, sei_tmp.lpFile);
1252 if (sei_tmp.lpParameters[0]) {
1253 strcatW(wszApplicationName, wSpace);
1254 strcatW(wszApplicationName, wszParameters);
1257 /* We set the default to open, and that should generally work.
1258 But that is not really the way the MS docs say to do it. */
1259 if (!sei_tmp.lpVerb)
1260 sei_tmp.lpVerb = wszOpen;
1262 retval = execfunc(wszApplicationName, NULL, FALSE, &sei_tmp, sei);
1266 /* Else, try to find the executable */
1268 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, 1024, lpstrProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1269 if (retval > 32) /* Found */
1271 WCHAR wszQuotedCmd[MAX_PATH+2];
1272 /* Must quote to handle case where cmd contains spaces,
1273 * else security hole if malicious user creates executable file "C:\\Program"
1275 strcpyW(wszQuotedCmd, wQuote);
1276 strcatW(wszQuotedCmd, wcmd);
1277 strcatW(wszQuotedCmd, wQuote);
1278 if (wszParameters[0]) {
1279 strcatW(wszQuotedCmd, wSpace);
1280 strcatW(wszQuotedCmd, wszParameters);
1282 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(sei_tmp.lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1284 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, sei_tmp.lpParameters, execfunc, &sei_tmp, sei);
1286 retval = execfunc(wszQuotedCmd, env, FALSE, &sei_tmp, sei);
1287 if (env) HeapFree( GetProcessHeap(), 0, env );
1289 else if (PathIsURLW((LPWSTR)lpFile)) /* File not found, check for URL */
1291 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1292 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1296 lpstrRes = strchrW(lpFile, ':');
1298 iSize = lpstrRes - lpFile;
1300 iSize = strlenW(lpFile);
1302 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1303 /* Looking for ...protocol\shell\lpOperation\command */
1304 strncpyW(lpstrProtocol, lpFile, iSize);
1305 lpstrProtocol[iSize] = '\0';
1306 strcatW(lpstrProtocol, wShell);
1307 strcatW(lpstrProtocol, sei_tmp.lpVerb? sei_tmp.lpVerb: wszOpen);
1308 strcatW(lpstrProtocol, wCommand);
1310 /* Remove File Protocol from lpFile */
1311 /* In the case file://path/file */
1312 if (!strncmpiW(lpFile, wFile, iSize))
1315 while (*lpFile == ':') lpFile++;
1317 retval = execute_from_key(lpstrProtocol, lpFile, NULL, sei_tmp.lpParameters, execfunc, &sei_tmp, sei);
1319 /* Check if file specified is in the form www.??????.*** */
1320 else if (!strncmpiW(lpFile, wWww, 3))
1322 /* if so, append lpFile http:// and call ShellExecute */
1323 WCHAR lpstrTmpFile[256];
1324 strcpyW(lpstrTmpFile, wHttp);
1325 strcatW(lpstrTmpFile, lpFile);
1326 retval = (UINT)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1329 TRACE("retval %u\n", retval);
1333 sei->hInstApp = (HINSTANCE)retval;
1337 sei->hInstApp = (HINSTANCE)33;
1341 /*************************************************************************
1342 * ShellExecuteA [SHELL32.290]
1344 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
1345 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
1347 SHELLEXECUTEINFOA sei;
1348 HANDLE hProcess = 0;
1350 TRACE("%p,%s,%s,%s,%s,%d\n",
1351 hWnd, lpOperation, lpFile, lpParameters, lpDirectory, iShowCmd);
1353 sei.cbSize = sizeof(sei);
1356 sei.lpVerb = lpOperation;
1357 sei.lpFile = lpFile;
1358 sei.lpParameters = lpParameters;
1359 sei.lpDirectory = lpDirectory;
1360 sei.nShow = iShowCmd;
1365 sei.hProcess = hProcess;
1367 ShellExecuteExA (&sei);
1368 return sei.hInstApp;
1371 /*************************************************************************
1372 * ShellExecuteEx [SHELL32.291]
1375 BOOL WINAPI ShellExecuteExAW (LPVOID sei)
1377 if (SHELL_OsIsUnicode())
1378 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1379 return ShellExecuteExA (sei);
1382 /*************************************************************************
1383 * ShellExecuteExA [SHELL32.292]
1386 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1388 SHELLEXECUTEINFOW seiW;
1390 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1394 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1397 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1400 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1402 if (sei->lpParameters)
1403 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1405 if (sei->lpDirectory)
1406 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1408 if ((sei->fMask & SEE_MASK_CLASSNAME) && sei->lpClass)
1409 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1411 seiW.lpClass = NULL;
1413 ret = ShellExecuteExW32 (&seiW, SHELL_ExecuteW);
1415 sei->hInstApp = seiW.hInstApp;
1417 if (wVerb) SHFree(wVerb);
1418 if (wFile) SHFree(wFile);
1419 if (wParameters) SHFree(wParameters);
1420 if (wDirectory) SHFree(wDirectory);
1421 if (wClass) SHFree(wClass);
1426 /*************************************************************************
1427 * ShellExecuteExW [SHELL32.293]
1430 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1432 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1435 /*************************************************************************
1436 * ShellExecuteW [SHELL32.294]
1438 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1439 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1441 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1442 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1444 SHELLEXECUTEINFOW sei;
1445 HANDLE hProcess = 0;
1448 sei.cbSize = sizeof(sei);
1451 sei.lpVerb = lpOperation;
1452 sei.lpFile = lpFile;
1453 sei.lpParameters = lpParameters;
1454 sei.lpDirectory = lpDirectory;
1455 sei.nShow = nShowCmd;
1460 sei.hProcess = hProcess;
1462 ShellExecuteExW32 (&sei, SHELL_ExecuteW);
1463 return sei.hInstApp;