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"
45 #include "wine/winbase16.h"
46 #include "shell32_main.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(exec);
53 static const WCHAR wszOpen[] = {'o','p','e','n',0};
54 static const WCHAR wszExe[] = {'.','e','x','e',0};
55 static const WCHAR wszILPtr[] = {':','%','p',0};
56 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
57 static const WCHAR wszFolder[] = {'F','o','l','d','e','r',0};
58 static const WCHAR wszEmpty[] = {0};
60 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
63 /***********************************************************************
64 * SHELL_ArgifyW [Internal]
66 * this function is supposed to expand the escape sequences found in the registry
67 * some diving reported that the following were used:
68 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
73 * %I address of a global item ID (explorer switch /idlist)
74 * %L seems to be %1 as long filename followed by the 8+3 variation
76 * %* all following parameters (see batfile)
79 * FIXME: Careful of going over string boundaries. No checking is done to 'res'...
81 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args)
85 BOOL found_p1 = FALSE;
90 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
91 debugstr_w(lpFile), pidl, args);
125 while(*args && !isspace(*args))
128 while(isspace(*args))
133 /* else fall through */
135 if (!done || (*fmt == '1'))
137 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
138 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
143 /* Add double quotation marks unless we already have them
144 (e.g.: "file://%1" %* for exefile) or unless the arg is already
145 enclosed in double quotation marks */
146 if ((res == out || *(fmt + 1) != '"') && *cmd != '"')
163 * IE uses this a lot for activating things such as windows media
164 * player. This is not verified to be fully correct but it appears
170 strcpyW(res, lpFile);
171 res += strlenW(lpFile);
179 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
180 pv = SHLockShared(hmem, 0);
181 res += sprintfW(res, wszILPtr, pv);
189 * Check if this is an env-variable here...
192 /* Make sure that we have at least one more %.*/
193 if (strchrW(fmt, '%'))
195 WCHAR tmpBuffer[1024];
196 PWSTR tmpB = tmpBuffer;
197 WCHAR tmpEnvBuff[MAX_PATH];
204 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
206 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
207 if (envRet == 0 || envRet > MAX_PATH)
208 strcpyW( res, tmpBuffer );
210 strcpyW( res, tmpEnvBuff );
216 /* Don't skip past terminator (catch a single '%' at the end) */
231 HRESULT SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl, LPSTR pszPath, UINT uOutSize)
234 IShellFolder* desktop;
236 HRESULT hr = SHGetDesktopFolder(&desktop);
239 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
242 StrRetToStrNA(pszPath, uOutSize, &strret, pidl);
244 IShellFolder_Release(desktop);
250 HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
253 IShellFolder* desktop;
255 HRESULT hr = SHGetDesktopFolder(&desktop);
258 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
261 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
263 IShellFolder_Release(desktop);
269 /*************************************************************************
270 * SHELL_ResolveShortCutW [Internal]
271 * read shortcut file at 'wcmd'
273 static HRESULT SHELL_ResolveShortCutW(LPWSTR wcmd, LPWSTR wargs, LPWSTR wdir, HWND hwnd, LPCWSTR lpVerb, int* pshowcmd, LPITEMIDLIST* ppidl)
277 HRESULT hr = SHGetDesktopFolder(&psf);
285 hr = IShellFolder_ParseDisplayName(psf, 0, 0, wcmd, &l, &pidl, 0);
290 hr = IShellFolder_GetUIObjectOf(psf, NULL, 1, (LPCITEMIDLIST*)&pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
293 hr = IShellLinkW_Resolve(psl, hwnd, 0);
296 hr = IShellLinkW_GetPath(psl, wcmd, MAX_PATH, NULL, SLGP_UNCPRIORITY);
300 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
301 hr = IShellLinkW_GetIDList(psl, ppidl);
303 if (SUCCEEDED(hr) && *ppidl) {
304 /* We got a PIDL instead of a file system path - try to translate it. */
305 if (SHGetPathFromIDListW(*ppidl, wcmd)) {
313 /* get command line arguments, working directory and display mode if available */
314 IShellLinkW_GetWorkingDirectory(psl, wdir, MAX_PATH);
315 IShellLinkW_GetArguments(psl, wargs, MAX_PATH);
316 IShellLinkW_GetShowCmd(psl, pshowcmd);
321 IShellLinkW_Release(psl);
327 IShellFolder_Release(psf);
333 /*************************************************************************
334 * SHELL_ExecuteW [Internal]
337 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
338 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
340 STARTUPINFOW startup;
341 PROCESS_INFORMATION info;
342 UINT_PTR retval = 31;
344 WCHAR curdir[MAX_PATH];
346 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
347 /* ShellExecute specifies the command from psei->lpDirectory
348 * if present. Not from the current dir as CreateProcess does */
349 if( psei->lpDirectory && psei->lpDirectory[0] )
350 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
351 if( !SetCurrentDirectoryW( psei->lpDirectory))
352 ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory));
353 ZeroMemory(&startup,sizeof(STARTUPINFOW));
354 startup.cb = sizeof(STARTUPINFOW);
355 startup.dwFlags = STARTF_USESHOWWINDOW;
356 startup.wShowWindow = psei->nShow;
357 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT,
358 env, *psei->lpDirectory? psei->lpDirectory: NULL, &startup, &info))
360 /* Give 30 seconds to the app to come up, if desired. Probably only needed
361 when starting app immediately before making a DDE connection. */
363 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
364 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
366 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
367 psei_out->hProcess = info.hProcess;
369 CloseHandle( info.hProcess );
370 CloseHandle( info.hThread );
372 else if ((retval = GetLastError()) >= 32)
374 TRACE("CreateProcess returned error %d\n", retval);
375 retval = ERROR_BAD_FORMAT;
378 TRACE("returning %u\n", retval);
380 psei_out->hInstApp = (HINSTANCE)retval;
382 if( !SetCurrentDirectoryW( curdir))
383 ERR("cannot return to directory %s\n", debugstr_w(curdir));
389 /***********************************************************************
390 * SHELL_BuildEnvW [Internal]
392 * Build the environment for the new process, adding the specified
393 * path to the PATH variable. Returned pointer must be freed by caller.
395 static void *SHELL_BuildEnvW( const WCHAR *path )
397 static const WCHAR wPath[] = {'P','A','T','H','=',0};
398 WCHAR *strings, *new_env;
400 int total = strlenW(path) + 1;
401 BOOL got_path = FALSE;
403 if (!(strings = GetEnvironmentStringsW())) return NULL;
407 int len = strlenW(p) + 1;
408 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
412 if (!got_path) total += 5; /* we need to create PATH */
413 total++; /* terminating null */
415 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
417 FreeEnvironmentStringsW( strings );
424 int len = strlenW(p) + 1;
425 memcpy( p2, p, len * sizeof(WCHAR) );
426 if (!strncmpiW( p, wPath, 5 ))
429 strcpyW( p2 + len, path );
430 p2 += strlenW(path) + 1;
437 strcpyW( p2, wPath );
439 p2 += strlenW(p2) + 1;
442 FreeEnvironmentStringsW( strings );
447 /***********************************************************************
448 * SHELL_TryAppPathW [Internal]
450 * Helper function for SHELL_FindExecutable
451 * @param lpResult - pointer to a buffer of size MAX_PATH
452 * On entry: szName is a filename (probably without path separators).
453 * On exit: if szName found in "App Path", place full path in lpResult, and return true
455 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
457 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',
458 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
459 static const WCHAR wPath[] = {'P','a','t','h',0};
466 if (env) *env = NULL;
467 strcpyW(buffer, wszKeyAppPaths);
468 strcatW(buffer, szName);
469 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
472 len = MAX_PATH*sizeof(WCHAR);
473 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
479 DWORD count = sizeof(buffer);
480 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
481 *env = SHELL_BuildEnvW( buffer );
485 if (hkApp) RegCloseKey(hkApp);
489 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
491 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
493 /* Looking for ...buffer\shell\<verb>\command */
494 strcatW(filetype, wszShell);
495 strcatW(filetype, lpOperation);
496 strcatW(filetype, wCommand);
498 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
499 &commandlen) == ERROR_SUCCESS)
501 commandlen /= sizeof(WCHAR);
502 if (key) strcpyW(key, filetype);
506 LONG paramlen = sizeof(param);
507 static const WCHAR wSpace[] = {' ',0};
509 /* FIXME: it seems all Windows version don't behave the same here.
510 * the doc states that this ddeexec information can be found after
512 * on Win98, it doesn't appear, but I think it does on Win2k
514 /* Get the parameters needed by the application
515 from the associated ddeexec key */
516 tmp = strstrW(filetype, wCommand);
518 strcatW(filetype, wDdeexec);
519 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
520 ¶mlen) == ERROR_SUCCESS)
522 paramlen /= sizeof(WCHAR);
523 strcatW(command, wSpace);
524 strcatW(command, param);
525 commandlen += paramlen;
529 command[commandlen] = '\0';
531 return 33; /* FIXME see SHELL_FindExecutable() */
534 return 31; /* default - 'No association was found' */
537 /*************************************************************************
538 * SHELL_FindExecutable [Internal]
540 * Utility for code sharing between FindExecutable and ShellExecute
542 * lpFile the name of a file
543 * lpOperation the operation on it (open)
545 * lpResult a buffer, big enough :-(, to store the command to do the
546 * operation on the file
547 * key a buffer, big enough, to get the key name to do actually the
548 * command (it'll be used afterwards for more information
551 UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
552 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
554 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
555 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
556 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
557 WCHAR *extension = NULL; /* pointer to file extension */
558 WCHAR filetype[256]; /* registry name for this filetype */
559 LONG filetypelen = sizeof(filetype); /* length of above */
560 WCHAR command[1024]; /* command from registry */
561 WCHAR wBuffer[256]; /* Used to GetProfileString */
562 UINT retval = 31; /* default - 'No association was found' */
563 WCHAR *tok; /* token pointer */
564 WCHAR xlpFile[256]; /* result of SearchPath */
565 DWORD attribs; /* file attributes */
567 TRACE("%s\n", (lpFile != NULL) ? debugstr_w(lpFile) : "-");
570 lpResult[0] = '\0'; /* Start off with an empty return string */
571 if (key) *key = '\0';
573 /* trap NULL parameters on entry */
574 if ((lpFile == NULL) || (lpResult == NULL) || (lpOperation == NULL))
576 WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
577 debugstr_w(lpFile), debugstr_w(lpOperation), debugstr_w(lpResult));
578 return 2; /* File not found. Close enough, I guess. */
581 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
583 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
587 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
589 TRACE("SearchPathW returned non-zero\n");
591 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
594 attribs = GetFileAttributesW(lpFile);
595 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
597 strcpyW(filetype, wszFolder);
598 filetypelen = 6; /* strlen("Folder") */
602 /* First thing we need is the file's extension */
603 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
604 /* File->Run in progman uses */
606 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
608 if (extension == NULL || extension[1]==0)
610 WARN("Returning 31 - No association\n");
611 return 31; /* no association */
614 /* Three places to check: */
615 /* 1. win.ini, [windows], programs (NB no leading '.') */
616 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
617 /* 3. win.ini, [extensions], extension (NB no leading '.' */
618 /* All I know of the order is that registry is checked before */
619 /* extensions; however, it'd make sense to check the programs */
620 /* section first, so that's what happens here. */
622 /* See if it's a program - if GetProfileString fails, we skip this
623 * section. Actually, if GetProfileString fails, we've probably
624 * got a lot more to worry about than running a program... */
625 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
632 while (*p && *p != ' ' && *p != '\t') p++;
636 while (*p == ' ' || *p == '\t') p++;
639 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
641 strcpyW(lpResult, xlpFile);
642 /* Need to perhaps check that the file has a path
644 TRACE("found %s\n", debugstr_w(lpResult));
647 /* Greater than 32 to indicate success FIXME According to the
648 * docs, I should be returning a handle for the
649 * executable. Does this mean I'm supposed to open the
650 * executable file or something? More RTFM, I guess... */
657 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
658 &filetypelen) == ERROR_SUCCESS)
660 filetypelen /= sizeof(WCHAR);
661 filetype[filetypelen] = '\0';
662 TRACE("File type: %s\n", debugstr_w(filetype));
670 /* pass the operation string to SHELL_FindExecutableByOperation() */
671 filetype[filetypelen] = '\0';
672 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, lpOperation, key, filetype, command, sizeof(command));
676 WCHAR operation[MAX_PATH];
679 /* Looking for ...buffer\shell\<operation>\command */
680 strcatW(filetype, wszShell);
682 /* enumerate the operation subkeys in the registry and search for one with an associated command */
683 if (RegOpenKeyW(HKEY_CLASSES_ROOT, filetype, &hkey) == ERROR_SUCCESS)
688 if (RegEnumKeyW(hkey, idx, operation, MAX_PATH) != ERROR_SUCCESS)
691 filetype[filetypelen] = '\0';
692 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, operation, key, filetype, command, sizeof(command));
703 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args);
705 /* Remove double quotation marks and command line arguments */
706 if (*lpResult == '"')
709 while (*(p + 1) != '"')
718 else /* Check win.ini */
720 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
722 /* Toss the leading dot */
724 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
726 if (strlenW(command) != 0)
728 strcpyW(lpResult, command);
729 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
733 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
734 tok = strchrW(command, '^'); /* see above */
735 if ((tok != NULL) && (strlenW(tok)>5))
737 strcatW(lpResult, &tok[5]);
740 retval = 33; /* FIXME - see above */
745 TRACE("returning %s\n", debugstr_w(lpResult));
749 /******************************************************************
752 * callback for the DDE connection. not really useful
754 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
755 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
756 ULONG_PTR dwData1, ULONG_PTR dwData2)
758 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
759 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
763 /******************************************************************
766 * ShellExecute helper. Used to do an operation with a DDE connection
768 * Handles both the direct connection (try #1), and if it fails,
769 * launching an application and trying (#2) to connect to it
772 static unsigned dde_connect(WCHAR* key, WCHAR* start, WCHAR* ddeexec,
773 const WCHAR* lpFile, WCHAR *env,
774 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
775 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
777 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
778 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
779 WCHAR * endkey = key + strlenW(key);
780 WCHAR app[256], topic[256], ifexec[256], res[256];
781 LONG applen, topiclen, ifexeclen;
785 HSZ hszApp, hszTopic;
790 strcpyW(endkey, wApplication);
791 applen = sizeof(app);
792 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
794 FIXME("default app name NIY %s\n", debugstr_w(key));
798 strcpyW(endkey, wTopic);
799 topiclen = sizeof(topic);
800 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
802 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
803 strcpyW(topic, wSystem);
806 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
811 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
812 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
814 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
818 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
819 TRACE("Launching '%s'\n", debugstr_w(start));
820 ret = execfunc(start, env, TRUE, psei, psei_out);
823 TRACE("Couldn't launch\n");
826 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
829 TRACE("Couldn't connect. ret=%d\n", ret);
830 DdeUninitialize(ddeInst);
831 SetLastError(ERROR_DDE_FAIL);
832 return 30; /* whatever */
834 strcpyW(endkey, wIfexec);
835 ifexeclen = sizeof(ifexec);
836 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
842 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline);
843 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
845 /* It's documented in the KB 330337 that IE has a bug and returns
846 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
848 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
849 XTYP_EXECUTE, 10000, &tid);
851 DdeFreeDataHandle(hDdeData);
853 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
856 DdeDisconnect(hConv);
859 DdeUninitialize(ddeInst);
864 /*************************************************************************
865 * execute_from_key [Internal]
867 static UINT_PTR execute_from_key(LPWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
868 SHELL_ExecuteW32 execfunc,
869 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
872 LONG cmdlen = sizeof(cmd);
873 UINT_PTR retval = 31;
877 /* Get the application for the registry */
878 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
880 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
881 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
884 LONG paramlen = sizeof(param);
888 /* Get the parameters needed by the application
889 from the associated ddeexec key */
890 tmp = strstrW(key, wCommand);
892 strcpyW(tmp, wDdeexec);
894 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, param, ¶mlen) == ERROR_SUCCESS)
896 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(param));
897 retval = dde_connect(key, cmd, param, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
901 /* Is there a replace() function anywhere? */
902 cmdlen /= sizeof(WCHAR);
904 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline);
905 retval = execfunc(param, env, FALSE, psei, psei_out);
908 else TRACE("ooch\n");
913 /*************************************************************************
914 * FindExecutableA [SHELL32.@]
916 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
919 WCHAR *wFile = NULL, *wDirectory = NULL;
920 WCHAR wResult[MAX_PATH];
922 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
923 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
925 retval = FindExecutableW(wFile, wDirectory, wResult);
926 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
927 if (wFile) SHFree( wFile );
928 if (wDirectory) SHFree( wDirectory );
930 TRACE("returning %s\n", lpResult);
934 /*************************************************************************
935 * FindExecutableW [SHELL32.@]
937 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
939 UINT_PTR retval = 31; /* default - 'No association was found' */
942 TRACE("File %s, Dir %s\n",
943 (lpFile != NULL ? debugstr_w(lpFile) : "-"), (lpDirectory != NULL ? debugstr_w(lpDirectory) : "-"));
945 lpResult[0] = '\0'; /* Start off with an empty return string */
947 /* trap NULL parameters on entry */
948 if ((lpFile == NULL) || (lpResult == NULL))
950 /* FIXME - should throw a warning, perhaps! */
951 return (HINSTANCE)2; /* File not found. Close enough, I guess. */
956 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
957 SetCurrentDirectoryW(lpDirectory);
960 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
962 TRACE("returning %s\n", debugstr_w(lpResult));
964 SetCurrentDirectoryW(old_dir);
965 return (HINSTANCE)retval;
968 /*************************************************************************
969 * ShellExecuteExW32 [Internal]
971 BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
973 static const WCHAR wQuote[] = {'"',0};
974 static const WCHAR wSpace[] = {' ',0};
975 static const WCHAR wWww[] = {'w','w','w',0};
976 static const WCHAR wFile[] = {'f','i','l','e',0};
977 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
978 static const WCHAR wExtLnk[] = {'.','l','n','k',0};
979 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
980 static const DWORD unsupportedFlags =
981 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
982 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI |
983 SEE_MASK_UNICODE | SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK |
986 WCHAR wszApplicationName[MAX_PATH+2], wszParameters[1024], wszDir[MAX_PATH];
987 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
988 WCHAR wfileName[MAX_PATH];
990 WCHAR lpstrProtocol[256];
992 UINT_PTR retval = 31;
994 WCHAR buffer[MAX_PATH];
998 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
999 memcpy(&sei_tmp, sei, sizeof(sei_tmp));
1001 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1002 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1003 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1004 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1005 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1006 debugstr_w(sei_tmp.lpClass) : "not used");
1008 sei->hProcess = NULL;
1010 /* make copies of all path/command strings */
1011 if (!sei_tmp.lpFile)
1012 *wszApplicationName = '\0';
1013 else if (*sei_tmp.lpFile == '\"')
1016 strcpyW(wszApplicationName, sei_tmp.lpFile+1);
1017 l=lstrlenW(wszApplicationName);
1018 if (wszApplicationName[l-1] == '\"')
1019 wszApplicationName[l-1] = '\0';
1020 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1023 strcpyW(wszApplicationName, sei_tmp.lpFile);
1025 if (sei_tmp.lpParameters)
1026 strcpyW(wszParameters, sei_tmp.lpParameters);
1028 *wszParameters = '\0';
1030 if (sei_tmp.lpDirectory)
1031 strcpyW(wszDir, sei_tmp.lpDirectory);
1035 /* adjust string pointers to point to the new buffers */
1036 sei_tmp.lpFile = wszApplicationName;
1037 sei_tmp.lpParameters = wszParameters;
1038 sei_tmp.lpDirectory = wszDir;
1040 if (sei_tmp.fMask & unsupportedFlags)
1042 FIXME("flags ignored: 0x%08lx\n", sei_tmp.fMask & unsupportedFlags);
1045 /* process the IDList */
1046 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1048 IShellExecuteHookW* pSEH;
1050 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1054 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1056 IShellExecuteHookW_Release(pSEH);
1062 wszApplicationName[0] = '"';
1063 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName+1);
1064 strcatW(wszApplicationName, wQuote);
1065 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1068 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1070 /* launch a document by fileclass like 'WordPad.Document.1' */
1071 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1072 /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1073 ULONG cmask=(sei_tmp.fMask & SEE_MASK_CLASSALL);
1074 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? sei_tmp.hkeyClass : NULL,
1075 (cmask == SEE_MASK_CLASSNAME) ? sei_tmp.lpClass: NULL,
1076 (sei_tmp.lpVerb) ? sei_tmp.lpVerb : wszOpen,
1077 wszParameters, sizeof(wszParameters)/sizeof(WCHAR));
1079 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1080 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(wszParameters), debugstr_w(wszApplicationName));
1083 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), wszParameters, wszApplicationName, sei_tmp.lpIDList, NULL);
1084 if (!done && wszApplicationName[0])
1086 strcatW(wcmd, wSpace);
1087 strcatW(wcmd, wszApplicationName);
1089 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1097 /* resolve shell shortcuts */
1098 ext = PathFindExtensionW(sei_tmp.lpFile);
1100 if (ext && !strncmpiW(ext, wExtLnk, sizeof(wExtLnk) / sizeof(WCHAR) - 1) &&
1101 (ext[sizeof(wExtLnk) / sizeof(WCHAR) - 1] == '\0' ||
1102 (sei_tmp.lpFile[0] == '"' && ext[sizeof(wExtLnk) / sizeof(WCHAR) - 1] == '"'))) /* or check for: shell_attribs & SFGAO_LINK */
1107 if (wszApplicationName[0] == '"')
1109 if (wszApplicationName[strlenW(wszApplicationName) - 1] == '"')
1111 wszApplicationName[strlenW(wszApplicationName) - 1] = '\0';
1123 /* expand paths before reading shell link */
1124 if (ExpandEnvironmentStringsW(Quoted ? sei_tmp.lpFile + 1 : sei_tmp.lpFile, buffer, MAX_PATH))
1125 lstrcpyW(Quoted ? wszApplicationName + 1 : wszApplicationName/*sei_tmp.lpFile*/, buffer);
1127 if (*sei_tmp.lpParameters)
1128 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1129 lstrcpyW(wszParameters/*sei_tmp.lpParameters*/, buffer);
1131 hr = SHELL_ResolveShortCutW((LPWSTR)(Quoted ? sei_tmp.lpFile + 1 : sei_tmp.lpFile),
1132 (LPWSTR)sei_tmp.lpParameters, (LPWSTR)sei_tmp.lpDirectory,
1133 sei_tmp.hwnd, sei_tmp.lpVerb?sei_tmp.lpVerb:wszEmpty, &sei_tmp.nShow, (LPITEMIDLIST*)&sei_tmp.lpIDList);
1136 wszApplicationName[strlenW(wszApplicationName) + 1] = '\0';
1137 wszApplicationName[strlenW(wszApplicationName)] = '"';
1141 sei->fMask |= SEE_MASK_IDLIST;
1145 /* repeat IDList processing if needed */
1146 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1148 IShellExecuteHookW* pSEH;
1150 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1154 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1156 IShellExecuteHookW_Release(pSEH);
1162 TRACE("-- idlist=%p (%s)\n", debugstr_w(sei_tmp.lpIDList), debugstr_w(sei_tmp.lpFile));
1168 /* Has the IDList not yet been translated? */
1169 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1171 /* last chance to translate IDList: now also allow CLSID paths */
1172 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp.lpIDList, buffer, sizeof(buffer)))) {
1173 if (buffer[0]==':' && buffer[1]==':') {
1174 /* open shell folder for the specified class GUID */
1175 strcpyW(wszParameters, buffer);
1176 strcpyW(wszApplicationName, wExplorer);
1178 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1179 } else if (HCR_GetExecuteCommandW(0, wszFolder, sei_tmp.lpVerb?sei_tmp.lpVerb:wszOpen, buffer, sizeof(buffer))) {
1180 SHELL_ArgifyW(wszApplicationName, sizeof(wszApplicationName)/sizeof(WCHAR), buffer, NULL, sei_tmp.lpIDList, NULL);
1182 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1187 /* expand environment strings */
1188 if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
1189 lstrcpyW(wszApplicationName, buffer);
1191 if (*sei_tmp.lpParameters)
1192 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1193 lstrcpyW(wszParameters, buffer);
1195 if (*sei_tmp.lpDirectory)
1196 if (ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buffer, MAX_PATH))
1197 lstrcpyW(wszDir, buffer);
1199 /* Else, try to execute the filename */
1200 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1202 /* separate out command line arguments from executable file name */
1203 if (!*sei_tmp.lpParameters) {
1204 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1205 if (sei_tmp.lpFile[0] == '"') {
1206 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1207 LPWSTR dst = wfileName;
1210 /* copy the unquoted executable path to 'wfileName' */
1211 while(*src && *src!='"')
1219 while(isspace(*src))
1224 /* copy the parameter string to 'wszParameters' */
1225 strcpyW(wszParameters, src);
1227 /* terminate previous command string after the quote character */
1232 /* If the executable name is not quoted, we have to use this search loop here,
1233 that in CreateProcess() is not sufficient because it does not handle shell links. */
1234 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1237 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1238 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1239 int idx = space-sei_tmp.lpFile;
1240 memcpy(buffer, sei_tmp.lpFile, idx * sizeof(WCHAR));
1243 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1244 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile), xlpFile, NULL))
1246 /* separate out command from parameter string */
1247 LPCWSTR p = space + 1;
1252 strcpyW(wszParameters, p);
1259 strcpyW(wfileName, sei_tmp.lpFile);
1262 strcpyW(wfileName, sei_tmp.lpFile);
1266 strcpyW(wcmd, wszApplicationName);
1267 if (sei_tmp.lpParameters[0]) {
1268 strcatW(wcmd, wSpace);
1269 strcatW(wcmd, wszParameters);
1272 /* We set the default to open, and that should generally work.
1273 But that is not really the way the MS docs say to do it. */
1274 if (!sei_tmp.lpVerb)
1275 sei_tmp.lpVerb = wszOpen;
1277 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1281 /* Else, try to find the executable */
1283 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, 1024, lpstrProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1284 if (retval > 32) /* Found */
1286 WCHAR wszQuotedCmd[MAX_PATH+2];
1287 /* Must quote to handle case where cmd contains spaces,
1288 * else security hole if malicious user creates executable file "C:\\Program"
1290 strcpyW(wszQuotedCmd, wQuote);
1291 strcatW(wszQuotedCmd, wcmd);
1292 strcatW(wszQuotedCmd, wQuote);
1293 if (wszParameters[0]) {
1294 strcatW(wszQuotedCmd, wSpace);
1295 strcatW(wszQuotedCmd, wszParameters);
1297 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(sei_tmp.lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1299 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, sei_tmp.lpParameters, execfunc, &sei_tmp, sei);
1301 retval = execfunc(wszQuotedCmd, env, FALSE, &sei_tmp, sei);
1302 HeapFree( GetProcessHeap(), 0, env );
1304 else if (PathIsURLW((LPWSTR)lpFile)) /* File not found, check for URL */
1306 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1307 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1311 lpstrRes = strchrW(lpFile, ':');
1313 iSize = lpstrRes - lpFile;
1315 iSize = strlenW(lpFile);
1317 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1318 /* Looking for ...protocol\shell\lpOperation\command */
1319 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1320 lpstrProtocol[iSize] = '\0';
1321 strcatW(lpstrProtocol, wShell);
1322 strcatW(lpstrProtocol, sei_tmp.lpVerb? sei_tmp.lpVerb: wszOpen);
1323 strcatW(lpstrProtocol, wCommand);
1325 /* Remove File Protocol from lpFile */
1326 /* In the case file://path/file */
1327 if (!strncmpiW(lpFile, wFile, iSize))
1330 while (*lpFile == ':') lpFile++;
1332 retval = execute_from_key(lpstrProtocol, lpFile, NULL, sei_tmp.lpParameters, execfunc, &sei_tmp, sei);
1334 /* Check if file specified is in the form www.??????.*** */
1335 else if (!strncmpiW(lpFile, wWww, 3))
1337 /* if so, append lpFile http:// and call ShellExecute */
1338 WCHAR lpstrTmpFile[256];
1339 strcpyW(lpstrTmpFile, wHttp);
1340 strcatW(lpstrTmpFile, lpFile);
1341 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1344 TRACE("retval %u\n", retval);
1348 sei->hInstApp = (HINSTANCE)retval;
1352 sei->hInstApp = (HINSTANCE)33;
1356 /*************************************************************************
1357 * ShellExecuteA [SHELL32.290]
1359 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
1360 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
1362 SHELLEXECUTEINFOA sei;
1363 HANDLE hProcess = 0;
1365 TRACE("%p,%s,%s,%s,%s,%d\n",
1366 hWnd, debugstr_a(lpOperation), debugstr_a(lpFile),
1367 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1369 sei.cbSize = sizeof(sei);
1372 sei.lpVerb = lpOperation;
1373 sei.lpFile = lpFile;
1374 sei.lpParameters = lpParameters;
1375 sei.lpDirectory = lpDirectory;
1376 sei.nShow = iShowCmd;
1381 sei.hProcess = hProcess;
1383 ShellExecuteExA (&sei);
1384 return sei.hInstApp;
1387 /*************************************************************************
1388 * ShellExecuteExA [SHELL32.292]
1391 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1393 SHELLEXECUTEINFOW seiW;
1395 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1399 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1402 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1405 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1407 if (sei->lpParameters)
1408 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1410 if (sei->lpDirectory)
1411 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1413 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1414 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1416 seiW.lpClass = NULL;
1418 ret = ShellExecuteExW32 (&seiW, SHELL_ExecuteW);
1420 sei->hInstApp = seiW.hInstApp;
1422 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1423 sei->hProcess = seiW.hProcess;
1425 if (wVerb) SHFree(wVerb);
1426 if (wFile) SHFree(wFile);
1427 if (wParameters) SHFree(wParameters);
1428 if (wDirectory) SHFree(wDirectory);
1429 if (wClass) SHFree(wClass);
1434 /*************************************************************************
1435 * ShellExecuteExW [SHELL32.293]
1438 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1440 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1443 /*************************************************************************
1444 * ShellExecuteW [SHELL32.294]
1446 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1447 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1449 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1450 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1452 SHELLEXECUTEINFOW sei;
1453 HANDLE hProcess = 0;
1456 sei.cbSize = sizeof(sei);
1459 sei.lpVerb = lpOperation;
1460 sei.lpFile = lpFile;
1461 sei.lpParameters = lpParameters;
1462 sei.lpDirectory = lpDirectory;
1463 sei.nShow = nShowCmd;
1468 sei.hProcess = hProcess;
1470 ShellExecuteExW32 (&sei, SHELL_ExecuteW);
1471 return sei.hInstApp;