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"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(exec);
56 static const WCHAR wszOpen[] = {'o','p','e','n',0};
57 static const WCHAR wszExe[] = {'.','e','x','e',0};
58 static const WCHAR wszILPtr[] = {':','%','p',0};
59 static const WCHAR wszShell[] = {'\\','s','h','e','l','l','\\',0};
60 static const WCHAR wszFolder[] = {'F','o','l','d','e','r',0};
61 static const WCHAR wszEmpty[] = {0};
64 /***********************************************************************
65 * SHELL_ArgifyW [Internal]
67 * this function is supposed to expand the escape sequences found in the registry
68 * some diving reported that the following were used:
69 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
74 * %I address of a global item ID (explorer switch /idlist)
75 * %L seems to be %1 as long filename followed by the 8+3 variation
77 * %* all following parameters (see batfile)
81 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args)
121 while(*args && !isspace(*args))
124 while(isspace(*args))
129 /* else fall through */
131 if (!done || (*fmt == '1'))
133 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
134 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
139 /* Add double quotation marks unless we already have them (e.g.: "%1" %* for exefile) */
140 if (res==out || res[-1]!='"')
156 * IE uses this a lot for activating things such as windows media
157 * player. This is not verified to be fully correct but it appears
163 strcpyW(res, lpFile);
164 res += strlenW(lpFile);
171 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
172 pv = SHLockShared(hmem, 0);
173 res += sprintfW(res, wszILPtr, pv);
178 default: FIXME("Unknown escape sequence %%%c\n", *fmt);
192 HRESULT SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl, LPSTR pszPath, UINT uOutSize)
195 IShellFolder* desktop;
197 HRESULT hr = SHGetDesktopFolder(&desktop);
200 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
203 StrRetToStrNA(pszPath, uOutSize, &strret, pidl);
205 IShellFolder_Release(desktop);
211 HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
214 IShellFolder* desktop;
216 HRESULT hr = SHGetDesktopFolder(&desktop);
219 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
222 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
224 IShellFolder_Release(desktop);
230 /*************************************************************************
231 * SHELL_ResolveShortCutW [Internal]
232 * read shortcut file at 'wcmd'
234 static HRESULT SHELL_ResolveShortCutW(LPWSTR wcmd, LPWSTR wargs, LPWSTR wdir, HWND hwnd, LPCWSTR lpVerb, int* pshowcmd, LPITEMIDLIST* ppidl)
238 HRESULT hr = SHGetDesktopFolder(&psf);
246 hr = IShellFolder_ParseDisplayName(psf, 0, 0, wcmd, &l, &pidl, 0);
251 hr = IShellFolder_GetUIObjectOf(psf, NULL, 1, (LPCITEMIDLIST*)&pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
254 hr = IShellLinkW_Resolve(psl, hwnd, 0);
257 hr = IShellLinkW_GetPath(psl, wcmd, MAX_PATH, NULL, SLGP_UNCPRIORITY);
261 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
262 hr = IShellLinkW_GetIDList(psl, ppidl);
264 if (SUCCEEDED(hr) && *ppidl) {
265 /* We got a PIDL instead of a file system path - try to translate it. */
266 if (SUCCEEDED(SHELL_GetPathFromIDListW(*ppidl, wcmd, MAX_PATH))) {
274 /* get command line arguments, working directory and display mode if available */
275 IShellLinkW_GetWorkingDirectory(psl, wdir, MAX_PATH);
276 IShellLinkW_GetArguments(psl, wargs, MAX_PATH);
277 IShellLinkW_GetShowCmd(psl, pshowcmd);
282 IShellLinkW_Release(psl);
288 IShellFolder_Release(psf);
294 /*************************************************************************
295 * SHELL_ExecuteW [Internal]
298 static UINT SHELL_ExecuteW(const WCHAR *lpCmd, void *env, BOOL shWait,
299 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
301 STARTUPINFOW startup;
302 PROCESS_INFORMATION info;
305 WCHAR curdir[MAX_PATH];
307 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
308 /* ShellExecute specifies the command from psei->lpDirectory
309 * if present. Not from the current dir as CreateProcess does */
310 if( psei->lpDirectory && psei->lpDirectory[0] )
311 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
312 if( !SetCurrentDirectoryW( psei->lpDirectory))
313 ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory));
314 ZeroMemory(&startup,sizeof(STARTUPINFOW));
315 startup.cb = sizeof(STARTUPINFOW);
316 startup.dwFlags = STARTF_USESHOWWINDOW;
317 startup.wShowWindow = psei->nShow;
318 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, 0,
319 env, *psei->lpDirectory? psei->lpDirectory: NULL, &startup, &info))
321 /* Give 30 seconds to the app to come up, if desired. Probably only needed
322 when starting app immediately before making a DDE connection. */
324 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
325 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
327 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
328 psei_out->hProcess = info.hProcess;
330 CloseHandle( info.hProcess );
331 CloseHandle( info.hThread );
333 else if ((retval = GetLastError()) >= 32)
335 FIXME("Strange error set by CreateProcess: %d\n", retval);
336 retval = ERROR_BAD_FORMAT;
339 TRACE("returning %u\n", retval);
341 psei_out->hInstApp = (HINSTANCE)retval;
343 if( !SetCurrentDirectoryW( curdir))
344 ERR("cannot return to directory %s\n", debugstr_w(curdir));
350 /***********************************************************************
351 * SHELL_BuildEnvW [Internal]
353 * Build the environment for the new process, adding the specified
354 * path to the PATH variable. Returned pointer must be freed by caller.
356 static void *SHELL_BuildEnvW( const WCHAR *path )
358 static const WCHAR wPath[] = {'P','A','T','H','=',0};
359 WCHAR *strings, *new_env;
361 int total = strlenW(path) + 1;
362 BOOL got_path = FALSE;
364 if (!(strings = GetEnvironmentStringsW())) return NULL;
368 int len = strlenW(p) + 1;
369 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
373 if (!got_path) total += 5; /* we need to create PATH */
374 total++; /* terminating null */
376 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
378 FreeEnvironmentStringsW( strings );
385 int len = strlenW(p) + 1;
386 memcpy( p2, p, len * sizeof(WCHAR) );
387 if (!strncmpiW( p, wPath, 5 ))
390 strcpyW( p2 + len, path );
391 p2 += strlenW(path) + 1;
398 strcpyW( p2, wPath );
400 p2 += strlenW(p2) + 1;
403 FreeEnvironmentStringsW( strings );
408 /***********************************************************************
409 * SHELL_TryAppPathW [Internal]
411 * Helper function for SHELL_FindExecutable
412 * @param lpResult - pointer to a buffer of size MAX_PATH
413 * On entry: szName is a filename (probably without path separators).
414 * On exit: if szName found in "App Path", place full path in lpResult, and return true
416 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, void**env)
418 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',
419 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
420 static const WCHAR wPath[] = {'P','a','t','h',0};
427 if (env) *env = NULL;
428 strcpyW(buffer, wszKeyAppPaths);
429 strcatW(buffer, szName);
430 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
433 len = MAX_PATH*sizeof(WCHAR);
434 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
440 DWORD count = sizeof(buffer);
441 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
442 *env = SHELL_BuildEnvW( buffer );
446 if (hkApp) RegCloseKey(hkApp);
450 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
452 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
454 /* Looking for ...buffer\shell\<verb>\command */
455 strcatW(filetype, wszShell);
456 strcatW(filetype, lpOperation);
457 strcatW(filetype, wCommand);
459 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
460 &commandlen) == ERROR_SUCCESS)
462 commandlen /= sizeof(WCHAR);
463 if (key) strcpyW(key, filetype);
467 LONG paramlen = sizeof(param);
468 static const WCHAR wSpace[] = {' ',0};
470 /* FIXME: it seems all Windows version don't behave the same here.
471 * the doc states that this ddeexec information can be found after
473 * on Win98, it doesn't appear, but I think it does on Win2k
475 /* Get the parameters needed by the application
476 from the associated ddeexec key */
477 tmp = strstrW(filetype, wCommand);
479 strcatW(filetype, wDdeexec);
480 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
481 ¶mlen) == ERROR_SUCCESS)
483 paramlen /= sizeof(WCHAR);
484 strcatW(command, wSpace);
485 strcatW(command, param);
486 commandlen += paramlen;
490 command[commandlen] = '\0';
492 return 33; /* FIXME see SHELL_FindExecutable() */
495 return 31; /* default - 'No association was found' */
498 /*************************************************************************
499 * SHELL_FindExecutable [Internal]
501 * Utility for code sharing between FindExecutable and ShellExecute
503 * lpFile the name of a file
504 * lpOperation the operation on it (open)
506 * lpResult a buffer, big enough :-(, to store the command to do the
507 * operation on the file
508 * key a buffer, big enough, to get the key name to do actually the
509 * command (it'll be used afterwards for more information
512 UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
513 LPWSTR lpResult, int resultLen, LPWSTR key, void **env, LPITEMIDLIST pidl, LPCWSTR args)
515 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
516 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
517 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
518 WCHAR *extension = NULL; /* pointer to file extension */
519 WCHAR filetype[256]; /* registry name for this filetype */
520 LONG filetypelen = sizeof(filetype); /* length of above */
521 WCHAR command[1024]; /* command from registry */
522 WCHAR wBuffer[256]; /* Used to GetProfileString */
523 UINT retval = 31; /* default - 'No association was found' */
524 WCHAR *tok; /* token pointer */
525 WCHAR xlpFile[256]; /* result of SearchPath */
527 TRACE("%s\n", (lpFile != NULL) ? debugstr_w(lpFile) : "-");
530 lpResult[0] = '\0'; /* Start off with an empty return string */
531 if (key) *key = '\0';
533 /* trap NULL parameters on entry */
534 if ((lpFile == NULL) || (lpResult == NULL) || (lpOperation == NULL))
536 WARN("(lpFile=%s,lpResult=%s,lpOperation=%s): NULL parameter\n",
537 debugstr_w(lpFile), debugstr_w(lpOperation), debugstr_w(lpResult));
538 return 2; /* File not found. Close enough, I guess. */
541 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
543 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
547 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
549 TRACE("SearchPathW returned non-zero\n");
551 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
554 /* First thing we need is the file's extension */
555 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
556 /* File->Run in progman uses */
558 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
560 if (extension == NULL || extension[1]==0)
562 WARN("Returning 31 - No association\n");
563 return 31; /* no association */
566 /* Three places to check: */
567 /* 1. win.ini, [windows], programs (NB no leading '.') */
568 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
569 /* 3. win.ini, [extensions], extension (NB no leading '.' */
570 /* All I know of the order is that registry is checked before */
571 /* extensions; however, it'd make sense to check the programs */
572 /* section first, so that's what happens here. */
574 /* See if it's a program - if GetProfileString fails, we skip this
575 * section. Actually, if GetProfileString fails, we've probably
576 * got a lot more to worry about than running a program... */
577 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
584 while (*p && *p != ' ' && *p != '\t') p++;
588 while (*p == ' ' || *p == '\t') p++;
591 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
593 strcpyW(lpResult, xlpFile);
594 /* Need to perhaps check that the file has a path
596 TRACE("found %s\n", debugstr_w(lpResult));
599 /* Greater than 32 to indicate success FIXME According to the
600 * docs, I should be returning a handle for the
601 * executable. Does this mean I'm supposed to open the
602 * executable file or something? More RTFM, I guess... */
609 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
610 &filetypelen) == ERROR_SUCCESS)
612 filetypelen /= sizeof(WCHAR);
613 filetype[filetypelen] = '\0';
614 TRACE("File type: %s\n", debugstr_w(filetype));
621 /* pass the operation string to SHELL_FindExecutableByOperation() */
622 filetype[filetypelen] = '\0';
623 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, lpOperation, key, filetype, command, sizeof(command));
627 WCHAR operation[MAX_PATH];
630 /* Looking for ...buffer\shell\<operation>\command */
631 strcatW(filetype, wszShell);
633 /* enumerate the operation subkeys in the registry and search for one with an associated command */
634 if (RegOpenKeyW(HKEY_CLASSES_ROOT, filetype, &hkey) == ERROR_SUCCESS)
639 if (RegEnumKeyW(hkey, idx, operation, MAX_PATH) != ERROR_SUCCESS)
642 filetype[filetypelen] = '\0';
643 retval = SHELL_FindExecutableByOperation(lpPath, lpFile, operation, key, filetype, command, sizeof(command));
654 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args);
656 /* Remove double quotation marks and command line arguments */
657 if (*lpResult == '"')
660 while (*(p + 1) != '"')
669 else /* Check win.ini */
671 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
673 /* Toss the leading dot */
675 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
677 if (strlenW(command) != 0)
679 strcpyW(lpResult, command);
680 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
684 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
685 tok = strchrW(command, '^'); /* see above */
686 if ((tok != NULL) && (strlenW(tok)>5))
688 strcatW(lpResult, &tok[5]);
691 retval = 33; /* FIXME - see above */
696 TRACE("returning %s\n", debugstr_w(lpResult));
700 /******************************************************************
703 * callback for the DDE connection. not really usefull
705 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
707 HDDEDATA hData, DWORD dwData1, DWORD dwData2)
712 /******************************************************************
715 * ShellExecute helper. Used to do an operation with a DDE connection
717 * Handles both the direct connection (try #1), and if it fails,
718 * launching an application and trying (#2) to connect to it
721 static unsigned dde_connect(WCHAR* key, WCHAR* start, WCHAR* ddeexec,
722 const WCHAR* lpFile, void *env,
723 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
724 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
726 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
727 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
728 WCHAR * endkey = key + strlenW(key);
729 WCHAR app[256], topic[256], ifexec[256], res[256];
730 LONG applen, topiclen, ifexeclen;
734 HSZ hszApp, hszTopic;
738 strcpyW(endkey, wApplication);
739 applen = sizeof(app);
740 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
742 FIXME("default app name NIY %s\n", debugstr_w(key));
746 strcpyW(endkey, wTopic);
747 topiclen = sizeof(topic);
748 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
750 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
751 strcpyW(topic, wSystem);
754 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
759 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINANSI);
760 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINANSI);
762 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
766 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
767 TRACE("Launching '%s'\n", debugstr_w(start));
768 ret = execfunc(start, env, TRUE, psei, psei_out);
771 TRACE("Couldn't launch\n");
774 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
777 TRACE("Couldn't connect. ret=%d\n", ret);
778 DdeUninitialize(ddeInst);
779 SetLastError(ERROR_DDE_FAIL);
780 return 30; /* whatever */
782 strcpyW(endkey, wIfexec);
783 ifexeclen = sizeof(ifexec);
784 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
790 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline);
791 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
793 ret = (DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
794 XTYP_EXECUTE, 10000, &tid) != DMLERR_NO_ERROR) ? 31 : 33;
795 DdeDisconnect(hConv);
798 DdeUninitialize(ddeInst);
803 /*************************************************************************
804 * execute_from_key [Internal]
806 static UINT execute_from_key(LPWSTR key, LPCWSTR lpFile, void *env, LPCWSTR szCommandline,
807 SHELL_ExecuteW32 execfunc,
808 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
811 LONG cmdlen = sizeof(cmd);
816 /* Get the application for the registry */
817 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
819 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
820 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
823 LONG paramlen = sizeof(param);
827 /* Get the parameters needed by the application
828 from the associated ddeexec key */
829 tmp = strstrW(key, wCommand);
831 strcpyW(tmp, wDdeexec);
833 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, param, ¶mlen) == ERROR_SUCCESS)
835 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(param));
836 retval = dde_connect(key, cmd, param, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
840 /* Is there a replace() function anywhere? */
841 cmdlen /= sizeof(WCHAR);
843 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline);
844 retval = execfunc(param, env, FALSE, psei, psei_out);
847 else TRACE("ooch\n");
852 /*************************************************************************
853 * FindExecutableA [SHELL32.@]
855 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
858 WCHAR *wFile = NULL, *wDirectory = NULL;
859 WCHAR wResult[MAX_PATH];
861 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
862 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
864 retval = FindExecutableW(wFile, wDirectory, wResult);
865 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
866 if (wFile) SHFree( wFile );
867 if (wDirectory) SHFree( wDirectory );
869 TRACE("returning %s\n", lpResult);
870 return (HINSTANCE)retval;
873 /*************************************************************************
874 * FindExecutableW [SHELL32.@]
876 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
878 UINT retval = 31; /* default - 'No association was found' */
881 TRACE("File %s, Dir %s\n",
882 (lpFile != NULL ? debugstr_w(lpFile) : "-"), (lpDirectory != NULL ? debugstr_w(lpDirectory) : "-"));
884 lpResult[0] = '\0'; /* Start off with an empty return string */
886 /* trap NULL parameters on entry */
887 if ((lpFile == NULL) || (lpResult == NULL))
889 /* FIXME - should throw a warning, perhaps! */
890 return (HINSTANCE)2; /* File not found. Close enough, I guess. */
895 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
896 SetCurrentDirectoryW(lpDirectory);
899 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
901 TRACE("returning %s\n", debugstr_w(lpResult));
903 SetCurrentDirectoryW(old_dir);
904 return (HINSTANCE)retval;
907 /*************************************************************************
908 * ShellExecuteExW32 [Internal]
910 BOOL WINAPI ShellExecuteExW32 (LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc)
912 static const WCHAR wQuote[] = {'"',0};
913 static const WCHAR wSpace[] = {' ',0};
914 static const WCHAR wWww[] = {'w','w','w',0};
915 static const WCHAR wFile[] = {'f','i','l','e',0};
916 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
917 static const WCHAR wExtLnk[] = {'.','l','n','k',0};
918 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
920 WCHAR wszApplicationName[MAX_PATH+2], wszParameters[1024], wszDir[MAX_PATH];
921 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
922 WCHAR wfileName[MAX_PATH];
924 WCHAR lpstrProtocol[256];
928 WCHAR buffer[MAX_PATH];
932 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
933 memcpy(&sei_tmp, sei, sizeof(sei_tmp));
935 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
936 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
937 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
938 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
939 (sei_tmp.fMask & SEE_MASK_CLASSNAME) ? debugstr_w(sei_tmp.lpClass) : "not used");
941 sei->hProcess = NULL;
943 /* make copies of all path/command strings */
945 strcpyW(wszApplicationName, sei_tmp.lpFile);
947 *wszApplicationName = '\0';
949 if (sei_tmp.lpParameters)
950 strcpyW(wszParameters, sei_tmp.lpParameters);
952 *wszParameters = '\0';
954 if (sei_tmp.lpDirectory)
955 strcpyW(wszDir, sei_tmp.lpDirectory);
959 /* adjust string pointers to point to the new buffers */
960 sei_tmp.lpFile = wszApplicationName;
961 sei_tmp.lpParameters = wszParameters;
962 sei_tmp.lpDirectory = wszDir;
964 if (sei_tmp.fMask & (SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
965 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
966 SEE_MASK_DOENVSUBST | SEE_MASK_FLAG_NO_UI | SEE_MASK_UNICODE |
967 SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR ))
969 FIXME("flags ignored: 0x%08lx\n", sei_tmp.fMask);
972 /* process the IDList */
973 if (sei_tmp.fMask & SEE_MASK_IDLIST)
975 IShellExecuteHookW* pSEH;
977 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
981 hr = IShellExecuteHookW_Execute(pSEH, sei);
983 IShellExecuteHookW_Release(pSEH);
989 wszApplicationName[0] = '"';
990 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName+1);
991 strcatW(wszApplicationName, wQuote);
992 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
995 if (sei_tmp.fMask & (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY))
997 /* launch a document by fileclass like 'WordPad.Document.1' */
998 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
999 /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1000 HCR_GetExecuteCommandW((sei_tmp.fMask & SEE_MASK_CLASSKEY) ? sei_tmp.hkeyClass : NULL,
1001 (sei_tmp.fMask & SEE_MASK_CLASSNAME) ? sei_tmp.lpClass: NULL,
1002 (sei_tmp.lpVerb) ? sei_tmp.lpVerb : wszOpen,
1003 wszParameters, sizeof(wszParameters)/sizeof(WCHAR));
1005 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1006 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(wszParameters), debugstr_w(wszApplicationName));
1009 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), wszParameters, wszApplicationName, sei_tmp.lpIDList, NULL);
1010 if (!done && wszApplicationName[0])
1012 strcatW(wcmd, wSpace);
1013 strcatW(wcmd, wszApplicationName);
1015 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1023 /* resolve shell shortcuts */
1024 ext = PathFindExtensionW(sei_tmp.lpFile);
1026 if (ext && !strcmpiW(ext, wExtLnk)) /* or check for: shell_attribs & SFGAO_LINK */
1030 /* expand paths before reading shell link */
1031 if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
1032 lstrcpyW(wszApplicationName/*sei_tmp.lpFile*/, buffer);
1034 if (*sei_tmp.lpParameters)
1035 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1036 lstrcpyW(wszParameters/*sei_tmp.lpParameters*/, buffer);
1038 hr = SHELL_ResolveShortCutW((LPWSTR)sei_tmp.lpFile, (LPWSTR)sei_tmp.lpParameters, (LPWSTR)sei_tmp.lpDirectory,
1039 sei_tmp.hwnd, sei_tmp.lpVerb?sei_tmp.lpVerb:wszEmpty, &sei_tmp.nShow, (LPITEMIDLIST*)&sei_tmp.lpIDList);
1042 sei->fMask |= SEE_MASK_IDLIST;
1046 /* repeat IDList processing if needed */
1047 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1049 IShellExecuteHookW* pSEH;
1051 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1055 hr = IShellExecuteHookW_Execute(pSEH, sei);
1057 IShellExecuteHookW_Release(pSEH);
1063 TRACE("-- idlist=%p (%s)\n", debugstr_w(sei_tmp.lpIDList), debugstr_w(sei_tmp.lpFile));
1069 /* Has the IDList not yet been translated? */
1070 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1072 /* last chance to translate IDList: now also allow CLSID paths */
1073 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp.lpIDList, buffer, sizeof(buffer)))) {
1074 if (buffer[0]==':' && buffer[1]==':') {
1075 /* open shell folder for the specified class GUID */
1076 strcpyW(wszParameters, buffer);
1077 strcpyW(wszApplicationName, wExplorer);
1079 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1080 } else if (HCR_GetExecuteCommandW(0, wszFolder, sei_tmp.lpVerb?sei_tmp.lpVerb:wszOpen, buffer, sizeof(buffer))) {
1081 SHELL_ArgifyW(wszApplicationName, sizeof(wszApplicationName)/sizeof(WCHAR), buffer, NULL, sei_tmp.lpIDList, NULL);
1083 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1089 /* expand environment strings */
1090 if (ExpandEnvironmentStringsW(sei_tmp.lpFile, buffer, MAX_PATH))
1091 lstrcpyW(wszApplicationName, buffer);
1093 if (*sei_tmp.lpParameters)
1094 if (ExpandEnvironmentStringsW(sei_tmp.lpParameters, buffer, MAX_PATH))
1095 lstrcpyW(wszParameters, buffer);
1097 if (*sei_tmp.lpDirectory)
1098 if (ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buffer, MAX_PATH))
1099 lstrcpyW(wszDir, buffer);
1101 /* Else, try to execute the filename */
1102 TRACE("execute:'%s','%s','%s'\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1104 /* separate out command line arguments from executable file name */
1105 if (!*sei_tmp.lpParameters) {
1106 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1107 if (sei_tmp.lpFile[0] == '"') {
1108 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1109 LPWSTR dst = wfileName;
1112 /* copy the unquoted executable path to 'wfileName' */
1113 while(*src && *src!='"')
1121 while(isspace(*src))
1126 /* copy the parameter string to 'wszParameters' */
1127 strcpyW(wszParameters, src);
1129 /* terminate previous command string after the quote character */
1134 /* If the executable name is not quoted, we have to use this search loop here,
1135 that in CreateProcess() is not sufficient because it does not handle shell links. */
1136 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1139 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1140 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1141 int idx = space-sei_tmp.lpFile;
1142 strncpyW(buffer, sei_tmp.lpFile, idx);
1145 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1146 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile), xlpFile, NULL))
1148 /* separate out command from parameter string */
1149 LPCWSTR p = space + 1;
1154 strcpyW(wszParameters, p);
1161 strcpyW(wfileName, sei_tmp.lpFile);
1164 strcpyW(wfileName, sei_tmp.lpFile);
1168 if (sei_tmp.lpParameters[0]) {
1169 strcatW(wszApplicationName, wSpace);
1170 strcatW(wszApplicationName, wszParameters);
1173 retval = execfunc(wszApplicationName, NULL, FALSE, &sei_tmp, sei);
1177 /* Else, try to find the executable */
1179 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, 1024, lpstrProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1180 if (retval > 32) /* Found */
1182 WCHAR wszQuotedCmd[MAX_PATH+2];
1183 /* Must quote to handle case where cmd contains spaces,
1184 * else security hole if malicious user creates executable file "C:\\Program"
1186 strcpyW(wszQuotedCmd, wQuote);
1187 strcatW(wszQuotedCmd, wcmd);
1188 strcatW(wszQuotedCmd, wQuote);
1189 if (wszParameters[0]) {
1190 strcatW(wszQuotedCmd, wSpace);
1191 strcatW(wszQuotedCmd, wszParameters);
1193 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(sei_tmp.lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1195 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, sei_tmp.lpParameters, execfunc, &sei_tmp, sei);
1197 retval = execfunc(wszQuotedCmd, env, FALSE, &sei_tmp, sei);
1198 if (env) HeapFree( GetProcessHeap(), 0, env );
1200 else if (PathIsURLW((LPWSTR)lpFile)) /* File not found, check for URL */
1202 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1203 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1207 lpstrRes = strchrW(lpFile, ':');
1209 iSize = lpstrRes - lpFile;
1211 iSize = strlenW(lpFile);
1213 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1214 /* Looking for ...protocol\shell\lpOperation\command */
1215 strncpyW(lpstrProtocol, lpFile, iSize);
1216 lpstrProtocol[iSize] = '\0';
1217 strcatW(lpstrProtocol, wShell);
1218 strcatW(lpstrProtocol, sei_tmp.lpVerb? sei_tmp.lpVerb: wszOpen);
1219 strcatW(lpstrProtocol, wCommand);
1221 /* Remove File Protocol from lpFile */
1222 /* In the case file://path/file */
1223 if (!strncmpiW(lpFile, wFile, iSize))
1226 while (*lpFile == ':') lpFile++;
1228 retval = execute_from_key(lpstrProtocol, lpFile, NULL, sei_tmp.lpParameters, execfunc, &sei_tmp, sei);
1230 /* Check if file specified is in the form www.??????.*** */
1231 else if (!strncmpiW(lpFile, wWww, 3))
1233 /* if so, append lpFile http:// and call ShellExecute */
1234 WCHAR lpstrTmpFile[256];
1235 strcpyW(lpstrTmpFile, wHttp);
1236 strcatW(lpstrTmpFile, lpFile);
1237 retval = (UINT)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1240 TRACE("retval %u\n", retval);
1244 sei->hInstApp = (HINSTANCE)retval;
1248 sei->hInstApp = (HINSTANCE)33;
1252 /*************************************************************************
1253 * ShellExecuteA [SHELL32.290]
1255 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
1256 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
1258 SHELLEXECUTEINFOA sei;
1259 HANDLE hProcess = 0;
1262 sei.cbSize = sizeof(sei);
1265 sei.lpVerb = lpOperation;
1266 sei.lpFile = lpFile;
1267 sei.lpParameters = lpParameters;
1268 sei.lpDirectory = lpDirectory;
1269 sei.nShow = iShowCmd;
1274 sei.hProcess = hProcess;
1276 ShellExecuteExA (&sei);
1277 return sei.hInstApp;
1280 /*************************************************************************
1281 * ShellExecuteEx [SHELL32.291]
1284 BOOL WINAPI ShellExecuteExAW (LPVOID sei)
1286 if (SHELL_OsIsUnicode())
1287 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1288 return ShellExecuteExA (sei);
1291 /*************************************************************************
1292 * ShellExecuteExA [SHELL32.292]
1295 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1297 SHELLEXECUTEINFOW seiW;
1299 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1303 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1306 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1309 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1311 if (sei->lpParameters)
1312 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1314 if (sei->lpDirectory)
1315 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1317 if ((sei->fMask & SEE_MASK_CLASSNAME) && sei->lpClass)
1318 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1320 seiW.lpClass = NULL;
1322 ret = ShellExecuteExW32 (&seiW, SHELL_ExecuteW);
1324 sei->hInstApp = seiW.hInstApp;
1326 if (wVerb) SHFree(wVerb);
1327 if (wFile) SHFree(wFile);
1328 if (wParameters) SHFree(wParameters);
1329 if (wDirectory) SHFree(wDirectory);
1330 if (wClass) SHFree(wClass);
1335 /*************************************************************************
1336 * ShellExecuteExW [SHELL32.293]
1339 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1341 return ShellExecuteExW32 (sei, SHELL_ExecuteW);
1344 /*************************************************************************
1345 * ShellExecuteW [SHELL32.294]
1347 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1348 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1350 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1351 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1353 SHELLEXECUTEINFOW sei;
1354 HANDLE hProcess = 0;
1357 sei.cbSize = sizeof(sei);
1360 sei.lpVerb = lpOperation;
1361 sei.lpFile = lpFile;
1362 sei.lpParameters = lpParameters;
1363 sei.lpDirectory = lpDirectory;
1364 sei.nShow = nShowCmd;
1369 sei.hProcess = hProcess;
1371 ShellExecuteExW32 (&sei, SHELL_ExecuteW);
1372 return sei.hInstApp;