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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
45 #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)
62 typedef UINT_PTR (*SHELL_ExecuteW32)(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
63 const SHELLEXECUTEINFOW *sei, LPSHELLEXECUTEINFOW sei_out);
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 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args, DWORD* out_len)
86 BOOL found_p1 = FALSE;
91 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
92 debugstr_w(lpFile), pidl, args);
138 while(*args && !isspace(*args))
147 while(isspace(*args))
152 /* else fall through */
154 if (!done || (*fmt == '1'))
156 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
157 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
162 used += strlenW(cmd);
173 * IE uses this a lot for activating things such as windows media
174 * player. This is not verified to be fully correct but it appears
180 used += strlenW(lpFile);
183 strcpyW(res, lpFile);
184 res += strlenW(lpFile);
194 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
195 * allowing a buffer of 100 should more than exceed all needs */
198 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
199 pv = SHLockShared(hmem, 0);
200 chars = sprintfW(buf, wszILPtr, pv);
201 if (chars >= sizeof(buf)/sizeof(WCHAR))
202 ERR("pidl format buffer too small!\n");
216 * Check if this is an env-variable here...
219 /* Make sure that we have at least one more %.*/
220 if (strchrW(fmt, '%'))
222 WCHAR tmpBuffer[1024];
223 PWSTR tmpB = tmpBuffer;
224 WCHAR tmpEnvBuff[MAX_PATH];
231 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
233 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
234 if (envRet == 0 || envRet > MAX_PATH)
236 used += strlenW(tmpBuffer);
239 strcpyW( res, tmpBuffer );
240 res += strlenW(tmpBuffer);
245 used += strlenW(tmpEnvBuff);
248 strcpyW( res, tmpEnvBuff );
249 res += strlenW(tmpEnvBuff);
256 /* Don't skip past terminator (catch a single '%' at the end) */
273 TRACE("used %i of %i space\n",used,len);
280 static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
283 IShellFolder* desktop;
285 HRESULT hr = SHGetDesktopFolder(&desktop);
288 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
291 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
293 IShellFolder_Release(desktop);
299 /*************************************************************************
300 * SHELL_ExecuteW [Internal]
303 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
304 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
306 STARTUPINFOW startup;
307 PROCESS_INFORMATION info;
308 UINT_PTR retval = SE_ERR_NOASSOC;
310 WCHAR curdir[MAX_PATH];
311 DWORD dwCreationFlags;
312 const WCHAR *lpDirectory = NULL;
314 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
316 /* make sure we don't fail the CreateProcess if the calling app passes in
317 * a bad working directory */
318 if (psei->lpDirectory && psei->lpDirectory[0])
320 DWORD attr = GetFileAttributesW(psei->lpDirectory);
321 if (attr != INVALID_FILE_ATTRIBUTES && attr & FILE_ATTRIBUTE_DIRECTORY)
322 lpDirectory = psei->lpDirectory;
325 /* ShellExecute specifies the command from psei->lpDirectory
326 * if present. Not from the current dir as CreateProcess does */
328 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
329 if( !SetCurrentDirectoryW( lpDirectory))
330 ERR("cannot set directory %s\n", debugstr_w(lpDirectory));
331 ZeroMemory(&startup,sizeof(STARTUPINFOW));
332 startup.cb = sizeof(STARTUPINFOW);
333 startup.dwFlags = STARTF_USESHOWWINDOW;
334 startup.wShowWindow = psei->nShow;
335 dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
336 if (psei->fMask & SEE_MASK_NO_CONSOLE)
337 dwCreationFlags |= CREATE_NEW_CONSOLE;
338 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
339 lpDirectory, &startup, &info))
341 /* Give 30 seconds to the app to come up, if desired. Probably only needed
342 when starting app immediately before making a DDE connection. */
344 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
345 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
347 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
348 psei_out->hProcess = info.hProcess;
350 CloseHandle( info.hProcess );
351 CloseHandle( info.hThread );
353 else if ((retval = GetLastError()) >= 32)
355 TRACE("CreateProcess returned error %ld\n", retval);
356 retval = ERROR_BAD_FORMAT;
359 TRACE("returning %lu\n", retval);
361 psei_out->hInstApp = (HINSTANCE)retval;
363 if( !SetCurrentDirectoryW( curdir))
364 ERR("cannot return to directory %s\n", debugstr_w(curdir));
370 /***********************************************************************
371 * SHELL_BuildEnvW [Internal]
373 * Build the environment for the new process, adding the specified
374 * path to the PATH variable. Returned pointer must be freed by caller.
376 static void *SHELL_BuildEnvW( const WCHAR *path )
378 static const WCHAR wPath[] = {'P','A','T','H','=',0};
379 WCHAR *strings, *new_env;
381 int total = strlenW(path) + 1;
382 BOOL got_path = FALSE;
384 if (!(strings = GetEnvironmentStringsW())) return NULL;
388 int len = strlenW(p) + 1;
389 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
393 if (!got_path) total += 5; /* we need to create PATH */
394 total++; /* terminating null */
396 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
398 FreeEnvironmentStringsW( strings );
405 int len = strlenW(p) + 1;
406 memcpy( p2, p, len * sizeof(WCHAR) );
407 if (!strncmpiW( p, wPath, 5 ))
410 strcpyW( p2 + len, path );
411 p2 += strlenW(path) + 1;
418 strcpyW( p2, wPath );
420 p2 += strlenW(p2) + 1;
423 FreeEnvironmentStringsW( strings );
428 /***********************************************************************
429 * SHELL_TryAppPathW [Internal]
431 * Helper function for SHELL_FindExecutable
432 * @param lpResult - pointer to a buffer of size MAX_PATH
433 * On entry: szName is a filename (probably without path separators).
434 * On exit: if szName found in "App Path", place full path in lpResult, and return true
436 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
438 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',
439 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
440 static const WCHAR wPath[] = {'P','a','t','h',0};
447 if (env) *env = NULL;
448 strcpyW(buffer, wszKeyAppPaths);
449 strcatW(buffer, szName);
450 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
453 len = MAX_PATH*sizeof(WCHAR);
454 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
460 DWORD count = sizeof(buffer);
461 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
462 *env = SHELL_BuildEnvW( buffer );
466 if (hkApp) RegCloseKey(hkApp);
470 /*************************************************************************
471 * SHELL_FindExecutableByOperation [Internal]
473 * called from SHELL_FindExecutable or SHELL_execute_class
475 * filetype a buffer, big enough, to get the key name to do actually the
476 * command "WordPad.Document.1\\shell\\open\\command"
477 * passed as "WordPad.Document.1"
479 * lpOperation the operation on it (open)
480 * commandlen the size of command buffer (in bytes)
482 * command a buffer, to store the command to do the
483 * operation on the file
484 * key a buffer, big enough, to get the key name to do actually the
485 * command "WordPad.Document.1\\shell\\open\\command"
488 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
490 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
492 WCHAR verb[MAX_PATH];
494 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, filetype, 0, 0x02000000, &hkeyClass))
495 return SE_ERR_NOASSOC;
496 if (!HCR_GetDefaultVerbW(hkeyClass, lpOperation, verb, sizeof(verb)/sizeof(verb[0])))
497 return SE_ERR_NOASSOC;
498 RegCloseKey(hkeyClass);
500 /* Looking for ...buffer\shell\<verb>\command */
501 strcatW(filetype, wszShell);
502 strcatW(filetype, verb);
503 strcatW(filetype, wCommand);
505 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
506 &commandlen) == ERROR_SUCCESS)
508 commandlen /= sizeof(WCHAR);
509 if (key) strcpyW(key, filetype);
513 LONG paramlen = sizeof(param);
514 static const WCHAR wSpace[] = {' ',0};
516 /* FIXME: it seems all Windows version don't behave the same here.
517 * the doc states that this ddeexec information can be found after
519 * on Win98, it doesn't appear, but I think it does on Win2k
521 /* Get the parameters needed by the application
522 from the associated ddeexec key */
523 tmp = strstrW(filetype, wCommand);
525 strcatW(filetype, wDdeexec);
526 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
527 ¶mlen) == ERROR_SUCCESS)
529 paramlen /= sizeof(WCHAR);
530 strcatW(command, wSpace);
531 strcatW(command, param);
532 commandlen += paramlen;
536 command[commandlen] = '\0';
538 return 33; /* FIXME see SHELL_FindExecutable() */
541 return SE_ERR_NOASSOC;
544 /*************************************************************************
545 * SHELL_FindExecutable [Internal]
547 * Utility for code sharing between FindExecutable and ShellExecute
549 * lpFile the name of a file
550 * lpOperation the operation on it (open)
552 * lpResult a buffer, big enough :-(, to store the command to do the
553 * operation on the file
554 * key a buffer, big enough, to get the key name to do actually the
555 * command (it'll be used afterwards for more information
558 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
559 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
561 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
562 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
563 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
564 WCHAR *extension = NULL; /* pointer to file extension */
565 WCHAR filetype[256]; /* registry name for this filetype */
566 LONG filetypelen = sizeof(filetype); /* length of above */
567 WCHAR command[1024]; /* command from registry */
568 WCHAR wBuffer[256]; /* Used to GetProfileString */
569 UINT retval = SE_ERR_NOASSOC;
570 WCHAR *tok; /* token pointer */
571 WCHAR xlpFile[256]; /* result of SearchPath */
572 DWORD attribs; /* file attributes */
574 TRACE("%s\n", debugstr_w(lpFile));
577 return ERROR_INVALID_PARAMETER;
580 lpResult[0] = '\0'; /* Start off with an empty return string */
581 if (key) *key = '\0';
583 /* trap NULL parameters on entry */
586 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
587 debugstr_w(lpFile), debugstr_w(lpResult));
588 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
591 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
593 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
597 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
599 TRACE("SearchPathW returned non-zero\n");
601 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
604 attribs = GetFileAttributesW(lpFile);
605 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
607 strcpyW(filetype, wszFolder);
611 /* Did we get something? Anything? */
614 TRACE("Returning SE_ERR_FNF\n");
617 /* First thing we need is the file's extension */
618 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
619 /* File->Run in progman uses */
621 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
623 if (extension == NULL || extension[1]==0)
625 WARN("Returning SE_ERR_NOASSOC\n");
626 return SE_ERR_NOASSOC;
629 /* Three places to check: */
630 /* 1. win.ini, [windows], programs (NB no leading '.') */
631 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
632 /* 3. win.ini, [extensions], extension (NB no leading '.' */
633 /* All I know of the order is that registry is checked before */
634 /* extensions; however, it'd make sense to check the programs */
635 /* section first, so that's what happens here. */
637 /* See if it's a program - if GetProfileString fails, we skip this
638 * section. Actually, if GetProfileString fails, we've probably
639 * got a lot more to worry about than running a program... */
640 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
647 while (*p && *p != ' ' && *p != '\t') p++;
651 while (*p == ' ' || *p == '\t') p++;
654 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
656 strcpyW(lpResult, xlpFile);
657 /* Need to perhaps check that the file has a path
659 TRACE("found %s\n", debugstr_w(lpResult));
661 /* Greater than 32 to indicate success */
668 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
669 &filetypelen) == ERROR_SUCCESS)
671 filetypelen /= sizeof(WCHAR);
672 if (filetypelen == sizeof(filetype)/sizeof(WCHAR))
674 filetype[filetypelen] = '\0';
675 TRACE("File type: %s\n", debugstr_w(filetype));
685 /* pass the operation string to SHELL_FindExecutableByOperation() */
686 retval = SHELL_FindExecutableByOperation(lpOperation, key, filetype, command, sizeof(command));
691 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
692 if (finishedLen > resultLen)
693 ERR("Argify buffer not large enough.. truncated\n");
695 /* Remove double quotation marks and command line arguments */
696 if (*lpResult == '"')
699 while (*(p + 1) != '"')
708 /* Truncate on first space */
710 while (*p != ' ' && *p != '\0')
716 else /* Check win.ini */
718 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
720 /* Toss the leading dot */
722 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
724 if (strlenW(command) != 0)
726 strcpyW(lpResult, command);
727 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
731 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
732 tok = strchrW(command, '^'); /* see above */
733 if ((tok != NULL) && (strlenW(tok)>5))
735 strcatW(lpResult, &tok[5]);
738 retval = 33; /* FIXME - see above */
743 TRACE("returning %s\n", debugstr_w(lpResult));
747 /******************************************************************
750 * callback for the DDE connection. not really useful
752 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
753 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
754 ULONG_PTR dwData1, ULONG_PTR dwData2)
756 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
757 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
761 /******************************************************************
764 * ShellExecute helper. Used to do an operation with a DDE connection
766 * Handles both the direct connection (try #1), and if it fails,
767 * launching an application and trying (#2) to connect to it
770 static unsigned dde_connect(const WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
771 const WCHAR* lpFile, WCHAR *env,
772 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
773 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
775 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
776 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
778 WCHAR * endkey = regkey + strlenW(key);
779 WCHAR app[256], topic[256], ifexec[256], res[256];
780 LONG applen, topiclen, ifexeclen;
785 HSZ hszApp, hszTopic;
788 unsigned ret = SE_ERR_NOASSOC;
789 BOOL unicode = !(GetVersion() & 0x80000000);
791 strcpyW(regkey, key);
792 strcpyW(endkey, wApplication);
793 applen = sizeof(app);
794 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
796 WCHAR command[1024], fullpath[MAX_PATH];
797 static const WCHAR wSo[] = { '.','s','o',0 };
798 int sizeSo = sizeof(wSo)/sizeof(WCHAR);
802 /* Get application command from start string and find filename of application */
805 strcpyW(command, start+1);
806 if ((ptr = strchrW(command, '"')))
808 ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
814 for (p=start; (space=strchrW(p, ' ')); p=space+1)
816 int idx = space-start;
817 memcpy(command, start, idx*sizeof(WCHAR));
819 if ((ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr)))
823 ret = SearchPathW(NULL, start, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
828 ERR("Unable to find application path for command %s\n", debugstr_w(start));
829 return ERROR_ACCESS_DENIED;
833 /* Remove extensions (including .so) */
834 ptr = app + strlenW(app) - (sizeSo-1);
835 if (strlenW(app) >= sizeSo &&
839 ptr = strrchrW(app, '.');
844 strcpyW(endkey, wTopic);
845 topiclen = sizeof(topic);
846 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
848 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
849 strcpyW(topic, wSystem);
854 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
859 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
863 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
864 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
866 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
870 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
871 TRACE("Launching %s\n", debugstr_w(start));
872 ret = execfunc(start, env, TRUE, psei, psei_out);
875 TRACE("Couldn't launch\n");
878 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
881 TRACE("Couldn't connect. ret=%d\n", ret);
882 DdeUninitialize(ddeInst);
883 SetLastError(ERROR_DDE_FAIL);
884 return 30; /* whatever */
886 strcpyW(endkey, wIfexec);
887 ifexeclen = sizeof(ifexec);
888 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
894 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline, &resultLen);
895 if (resultLen > sizeof(res)/sizeof(WCHAR))
896 ERR("Argify buffer not large enough, truncated\n");
897 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
899 /* It's documented in the KB 330337 that IE has a bug and returns
900 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
903 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
904 XTYP_EXECUTE, 30000, &tid);
907 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
908 char *resA = HeapAlloc(GetProcessHeap(), 0, lenA);
909 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
910 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
911 XTYP_EXECUTE, 10000, &tid );
912 HeapFree(GetProcessHeap(), 0, resA);
915 DdeFreeDataHandle(hDdeData);
917 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
920 DdeDisconnect(hConv);
923 DdeUninitialize(ddeInst);
928 /*************************************************************************
929 * execute_from_key [Internal]
931 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
932 LPCWSTR executable_name,
933 SHELL_ExecuteW32 execfunc,
934 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
936 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
937 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
938 WCHAR cmd[256], param[1024], ddeexec[256];
939 LONG cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
940 UINT_PTR retval = SE_ERR_NOASSOC;
944 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
945 debugstr_w(szCommandline), debugstr_w(executable_name));
950 /* Get the application from the registry */
951 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
953 TRACE("got cmd: %s\n", debugstr_w(cmd));
955 /* Is there a replace() function anywhere? */
956 cmdlen /= sizeof(WCHAR);
957 if (cmdlen >= sizeof(cmd)/sizeof(WCHAR))
958 cmdlen = sizeof(cmd)/sizeof(WCHAR)-1;
960 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
961 if (resultLen > sizeof(param)/sizeof(WCHAR))
962 ERR("Argify buffer not large enough, truncating\n");
965 /* Get the parameters needed by the application
966 from the associated ddeexec key */
967 tmp = strstrW(key, wCommand);
969 strcpyW(tmp, wDdeexec);
971 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, &ddeexeclen) == ERROR_SUCCESS)
973 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
974 if (!param[0]) strcpyW(param, executable_name);
975 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
979 TRACE("executing: %s\n", debugstr_w(param));
980 retval = execfunc(param, env, FALSE, psei, psei_out);
983 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
988 /*************************************************************************
989 * FindExecutableA [SHELL32.@]
991 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
994 WCHAR *wFile = NULL, *wDirectory = NULL;
995 WCHAR wResult[MAX_PATH];
997 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
998 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
1000 retval = FindExecutableW(wFile, wDirectory, wResult);
1001 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
1003 SHFree( wDirectory );
1005 TRACE("returning %s\n", lpResult);
1009 /*************************************************************************
1010 * FindExecutableW [SHELL32.@]
1012 * This function returns the executable associated with the specified file
1013 * for the default verb.
1016 * lpFile [I] The file to find the association for. This must refer to
1017 * an existing file otherwise FindExecutable fails and returns
1019 * lpResult [O] Points to a buffer into which the executable path is
1020 * copied. This parameter must not be NULL otherwise
1021 * FindExecutable() segfaults. The buffer must be of size at
1022 * least MAX_PATH characters.
1025 * A value greater than 32 on success, less than or equal to 32 otherwise.
1026 * See the SE_ERR_* constants.
1029 * On Windows XP and 2003, FindExecutable() seems to first convert the
1030 * filename into 8.3 format, thus taking into account only the first three
1031 * characters of the extension, and expects to find an association for those.
1032 * However other Windows versions behave sanely.
1034 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1036 UINT_PTR retval = SE_ERR_NOASSOC;
1037 WCHAR old_dir[1024];
1039 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1041 lpResult[0] = '\0'; /* Start off with an empty return string */
1043 return (HINSTANCE)SE_ERR_FNF;
1047 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
1048 SetCurrentDirectoryW(lpDirectory);
1051 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
1053 TRACE("returning %s\n", debugstr_w(lpResult));
1055 SetCurrentDirectoryW(old_dir);
1056 return (HINSTANCE)retval;
1059 /* FIXME: is this already implemented somewhere else? */
1060 static HKEY ShellExecute_GetClassKey( const SHELLEXECUTEINFOW *sei )
1062 LPCWSTR ext = NULL, lpClass = NULL;
1064 DWORD type = 0, sz = 0;
1068 if (sei->fMask & SEE_MASK_CLASSALL)
1069 return sei->hkeyClass;
1071 if (sei->fMask & SEE_MASK_CLASSNAME)
1072 lpClass = sei->lpClass;
1075 ext = PathFindExtensionW( sei->lpFile );
1076 TRACE("ext = %s\n", debugstr_w( ext ) );
1080 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
1081 if (r != ERROR_SUCCESS )
1084 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
1085 if ( r == ERROR_SUCCESS && type == REG_SZ )
1087 sz += sizeof (WCHAR);
1088 cls = HeapAlloc( GetProcessHeap(), 0, sz );
1090 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
1093 RegCloseKey( hkey );
1097 TRACE("class = %s\n", debugstr_w(lpClass) );
1101 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
1103 HeapFree( GetProcessHeap(), 0, cls );
1108 static IDataObject *shellex_get_dataobj( LPSHELLEXECUTEINFOW sei )
1110 LPCITEMIDLIST pidllast = NULL;
1111 IDataObject *dataobj = NULL;
1112 IShellFolder *shf = NULL;
1113 LPITEMIDLIST pidl = NULL;
1116 if (sei->fMask & SEE_MASK_CLASSALL)
1117 pidl = sei->lpIDList;
1120 WCHAR fullpath[MAX_PATH];
1124 ret = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
1128 pidl = ILCreateFromPathW( fullpath );
1131 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
1135 IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1136 &IID_IDataObject, NULL, (LPVOID*) &dataobj );
1139 if ( pidl != sei->lpIDList )
1142 IShellFolder_Release( shf );
1146 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1147 LPSHELLEXECUTEINFOW sei )
1149 IContextMenu *cm = NULL;
1150 CMINVOKECOMMANDINFOEX ici;
1157 TRACE("%p %p\n", obj, sei );
1159 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1163 hmenu = CreateMenu();
1167 /* the number of the last menu added is returned in r */
1168 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1172 n = GetMenuItemCount( hmenu );
1173 for ( i = 0; i < n; i++ )
1175 memset( &info, 0, sizeof info );
1176 info.cbSize = sizeof info;
1177 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1178 info.dwTypeData = string;
1179 info.cch = sizeof string;
1181 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1183 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1184 info.fState, info.dwItemData, info.fType, info.wID );
1185 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1186 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1197 memset( &ici, 0, sizeof ici );
1198 ici.cbSize = sizeof ici;
1199 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
1200 ici.nShow = sei->nShow;
1201 ici.lpVerb = MAKEINTRESOURCEA( def );
1202 ici.hwnd = sei->hwnd;
1203 ici.lpParametersW = sei->lpParameters;
1205 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1207 TRACE("invoke command returned %08x\n", r );
1211 DestroyMenu( hmenu );
1213 IContextMenu_Release( cm );
1217 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1219 IDataObject *dataobj = NULL;
1220 IObjectWithSite *ows = NULL;
1221 IShellExtInit *obj = NULL;
1224 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1226 r = CoInitialize( NULL );
1230 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1231 &IID_IShellExtInit, (LPVOID*)&obj );
1234 ERR("failed %08x\n", r );
1238 dataobj = shellex_get_dataobj( sei );
1241 ERR("failed to get data object\n");
1245 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1249 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1253 IObjectWithSite_SetSite( ows, NULL );
1255 r = shellex_run_context_menu_default( obj, sei );
1259 IObjectWithSite_Release( ows );
1261 IDataObject_Release( dataobj );
1263 IShellExtInit_Release( obj );
1269 /*************************************************************************
1270 * ShellExecute_FromContextMenu [Internal]
1272 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1274 static const WCHAR szcm[] = { 's','h','e','l','l','e','x','\\',
1275 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1276 HKEY hkey, hkeycm = 0;
1283 TRACE("%s\n", debugstr_w(sei->lpFile) );
1285 hkey = ShellExecute_GetClassKey( sei );
1287 return ERROR_FUNCTION_FAILED;
1289 r = RegOpenKeyW( hkey, szcm, &hkeycm );
1290 if ( r == ERROR_SUCCESS )
1295 r = RegEnumKeyW( hkeycm, i++, szguid, sizeof(szguid)/sizeof(szguid[0]) );
1296 if ( r != ERROR_SUCCESS )
1299 hr = CLSIDFromString( szguid, &guid );
1302 /* stop at the first one that succeeds in running */
1303 hr = shellex_load_object_and_run( hkey, &guid, sei );
1304 if ( SUCCEEDED( hr ) )
1308 RegCloseKey( hkeycm );
1311 if ( hkey != sei->hkeyClass )
1312 RegCloseKey( hkey );
1316 static UINT_PTR SHELL_quote_and_execute( LPCWSTR wcmd, LPCWSTR wszParameters, LPCWSTR lpstrProtocol, LPCWSTR wszApplicationName, LPWSTR env, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc );
1318 static UINT_PTR SHELL_execute_class( LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1320 static const WCHAR wQuote[] = {'"',0};
1321 static const WCHAR wSpace[] = {' ',0};
1322 WCHAR execCmd[1024], filetype[1024];
1323 /* launch a document by fileclass like 'WordPad.Document.1' */
1324 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1325 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1326 ULONG cmask=(psei->fMask & SEE_MASK_CLASSALL);
1331 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1332 if (cmask != SEE_MASK_CLASSNAME)
1335 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1336 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass: NULL,
1338 execCmd, sizeof(execCmd));
1340 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1341 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1344 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), execCmd, wszApplicationName, psei->lpIDList, NULL, &resultLen);
1345 if (!done && wszApplicationName[0])
1347 strcatW(wcmd, wSpace);
1348 if (*wszApplicationName != '"')
1350 strcatW(wcmd, wQuote);
1351 strcatW(wcmd, wszApplicationName);
1352 strcatW(wcmd, wQuote);
1355 strcatW(wcmd, wszApplicationName);
1357 if (resultLen > sizeof(wcmd)/sizeof(WCHAR))
1358 ERR("Argify buffer not large enough... truncating\n");
1359 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1362 strcpyW(filetype, psei->lpClass);
1363 rslt = SHELL_FindExecutableByOperation(psei->lpVerb, NULL, filetype, execCmd, sizeof(execCmd));
1365 TRACE("SHELL_FindExecutableByOperation returned %u (%s, %s)\n", (unsigned int)rslt, debugstr_w(filetype), debugstr_w(execCmd));
1368 rslt = SHELL_quote_and_execute( execCmd, wszEmpty, filetype,
1369 wszApplicationName, NULL, psei,
1370 psei_out, execfunc );
1374 static BOOL SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen )
1376 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1377 WCHAR buffer[MAX_PATH];
1378 BOOL appKnownSingular = FALSE;
1380 /* last chance to translate IDList: now also allow CLSID paths */
1381 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei->lpIDList, buffer, sizeof(buffer)))) {
1382 if (buffer[0]==':' && buffer[1]==':') {
1383 /* open shell folder for the specified class GUID */
1384 if (strlenW(buffer) + 1 > parametersLen)
1385 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1386 lstrlenW(buffer) + 1, parametersLen);
1387 lstrcpynW(wszParameters, buffer, parametersLen);
1388 if (strlenW(wExplorer) > dwApplicationNameLen)
1389 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1390 lstrlenW(wExplorer) + 1, dwApplicationNameLen);
1391 lstrcpynW(wszApplicationName, wExplorer, dwApplicationNameLen);
1392 appKnownSingular = TRUE;
1394 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1396 WCHAR target[MAX_PATH];
1399 /* Check if we're executing a directory and if so use the
1400 handler for the Folder class */
1401 strcpyW(target, buffer);
1402 attribs = GetFileAttributesW(buffer);
1403 if (attribs != INVALID_FILE_ATTRIBUTES &&
1404 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1405 HCR_GetExecuteCommandW(0, wszFolder,
1407 buffer, sizeof(buffer))) {
1408 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1409 buffer, target, sei->lpIDList, NULL, &resultLen);
1410 if (resultLen > dwApplicationNameLen)
1411 ERR("Argify buffer not large enough... truncating\n");
1412 appKnownSingular = FALSE;
1414 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1417 return appKnownSingular;
1420 static UINT_PTR SHELL_quote_and_execute( LPCWSTR wcmd, LPCWSTR wszParameters, LPCWSTR lpstrProtocol, LPCWSTR wszApplicationName, LPWSTR env, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1422 static const WCHAR wQuote[] = {'"',0};
1423 static const WCHAR wSpace[] = {' ',0};
1426 WCHAR *wszQuotedCmd;
1428 /* Length of quotes plus length of command plus NULL terminator */
1429 len = 2 + lstrlenW(wcmd) + 1;
1430 if (wszParameters[0])
1432 /* Length of space plus length of parameters */
1433 len += 1 + lstrlenW(wszParameters);
1435 wszQuotedCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1436 /* Must quote to handle case where cmd contains spaces,
1437 * else security hole if malicious user creates executable file "C:\\Program"
1439 strcpyW(wszQuotedCmd, wQuote);
1440 strcatW(wszQuotedCmd, wcmd);
1441 strcatW(wszQuotedCmd, wQuote);
1442 if (wszParameters[0]) {
1443 strcatW(wszQuotedCmd, wSpace);
1444 strcatW(wszQuotedCmd, wszParameters);
1446 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1448 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1450 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1451 HeapFree(GetProcessHeap(), 0, wszQuotedCmd);
1455 static UINT_PTR SHELL_execute_url( LPCWSTR lpFile, LPCWSTR wFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1457 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1458 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1460 WCHAR *lpstrProtocol;
1465 lpstrRes = strchrW(lpFile, ':');
1467 iSize = lpstrRes - lpFile;
1469 iSize = strlenW(lpFile);
1471 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1472 /* Looking for ...protocol\shell\lpOperation\command */
1473 len = iSize + lstrlenW(wShell) + lstrlenW(wCommand) + 1;
1475 len += lstrlenW(psei->lpVerb);
1477 len += lstrlenW(wszOpen);
1478 lpstrProtocol = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1479 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1480 lpstrProtocol[iSize] = '\0';
1481 strcatW(lpstrProtocol, wShell);
1482 strcatW(lpstrProtocol, psei->lpVerb? psei->lpVerb: wszOpen);
1483 strcatW(lpstrProtocol, wCommand);
1485 /* Remove File Protocol from lpFile */
1486 /* In the case file://path/file */
1487 if (!strncmpiW(lpFile, wFile, iSize))
1490 while (*lpFile == ':') lpFile++;
1492 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1493 wcmd, execfunc, psei, psei_out);
1494 HeapFree(GetProcessHeap(), 0, lpstrProtocol);
1498 static void do_error_dialog( UINT_PTR retval, HWND hwnd )
1501 int error_code=GetLastError();
1503 if (retval == SE_ERR_NOASSOC)
1504 LoadStringW(shell32_hInstance, IDS_SHLEXEC_NOASSOC, msg, sizeof(msg)/sizeof(WCHAR));
1506 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, msg, sizeof(msg)/sizeof(WCHAR), NULL);
1508 MessageBoxW(hwnd, msg, NULL, MB_ICONERROR);
1511 /*************************************************************************
1512 * SHELL_execute [Internal]
1514 static BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1516 static const WCHAR wSpace[] = {' ',0};
1517 static const WCHAR wWww[] = {'w','w','w',0};
1518 static const WCHAR wFile[] = {'f','i','l','e',0};
1519 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
1520 static const DWORD unsupportedFlags =
1521 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1522 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT |
1523 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1525 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1526 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd;
1527 DWORD dwApplicationNameLen = MAX_PATH+2;
1528 DWORD parametersLen = sizeof(parametersBuffer) / sizeof(WCHAR);
1529 DWORD dirLen = sizeof(dirBuffer) / sizeof(WCHAR);
1530 DWORD wcmdLen = sizeof(wcmdBuffer) / sizeof(WCHAR);
1532 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1533 WCHAR wfileName[MAX_PATH];
1535 WCHAR lpstrProtocol[256];
1537 UINT_PTR retval = SE_ERR_NOASSOC;
1538 BOOL appKnownSingular = FALSE;
1540 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1543 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1544 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1545 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1546 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1547 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1548 debugstr_w(sei_tmp.lpClass) : "not used");
1550 sei->hProcess = NULL;
1552 /* make copies of all path/command strings */
1553 if (!sei_tmp.lpFile)
1555 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1556 *wszApplicationName = '\0';
1558 else if (*sei_tmp.lpFile == '\"')
1560 DWORD l = strlenW(sei_tmp.lpFile+1);
1561 if(l >= dwApplicationNameLen) dwApplicationNameLen = l+1;
1562 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1563 memcpy(wszApplicationName, sei_tmp.lpFile+1, (l+1)*sizeof(WCHAR));
1564 if (wszApplicationName[l-1] == '\"')
1565 wszApplicationName[l-1] = '\0';
1566 appKnownSingular = TRUE;
1567 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1569 DWORD l = strlenW(sei_tmp.lpFile)+1;
1570 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1571 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1572 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1575 wszParameters = parametersBuffer;
1576 if (sei_tmp.lpParameters)
1578 len = lstrlenW(sei_tmp.lpParameters) + 1;
1579 if (len > parametersLen)
1581 wszParameters = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1582 parametersLen = len;
1584 strcpyW(wszParameters, sei_tmp.lpParameters);
1587 *wszParameters = '\0';
1590 if (sei_tmp.lpDirectory)
1592 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1595 wszDir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1598 strcpyW(wszDir, sei_tmp.lpDirectory);
1603 /* adjust string pointers to point to the new buffers */
1604 sei_tmp.lpFile = wszApplicationName;
1605 sei_tmp.lpParameters = wszParameters;
1606 sei_tmp.lpDirectory = wszDir;
1608 if (sei_tmp.fMask & unsupportedFlags)
1610 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1613 /* process the IDList */
1614 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1616 IShellExecuteHookW* pSEH;
1618 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1622 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1624 IShellExecuteHookW_Release(pSEH);
1627 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1628 if (wszParameters != parametersBuffer)
1629 HeapFree(GetProcessHeap(), 0, wszParameters);
1630 if (wszDir != dirBuffer)
1631 HeapFree(GetProcessHeap(), 0, wszDir);
1636 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1637 appKnownSingular = TRUE;
1638 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1641 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1643 sei->hInstApp = (HINSTANCE) 33;
1644 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1645 if (wszParameters != parametersBuffer)
1646 HeapFree(GetProcessHeap(), 0, wszParameters);
1647 if (wszDir != dirBuffer)
1648 HeapFree(GetProcessHeap(), 0, wszDir);
1652 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1654 retval = SHELL_execute_class( wszApplicationName, &sei_tmp, sei,
1656 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1657 do_error_dialog(retval, sei_tmp.hwnd);
1658 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1659 if (wszParameters != parametersBuffer)
1660 HeapFree(GetProcessHeap(), 0, wszParameters);
1661 if (wszDir != dirBuffer)
1662 HeapFree(GetProcessHeap(), 0, wszDir);
1666 /* Has the IDList not yet been translated? */
1667 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1669 appKnownSingular = SHELL_translate_idlist( &sei_tmp, wszParameters,
1672 dwApplicationNameLen );
1675 /* expand environment strings */
1676 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1680 buf = HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
1682 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len+1);
1683 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1684 dwApplicationNameLen = len+1;
1685 wszApplicationName = buf;
1686 /* appKnownSingular unmodified */
1688 sei_tmp.lpFile = wszApplicationName;
1691 if (*sei_tmp.lpParameters)
1693 len = ExpandEnvironmentStringsW(sei_tmp.lpParameters, NULL, 0);
1698 buf = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1699 ExpandEnvironmentStringsW(sei_tmp.lpParameters, buf, len);
1700 if (wszParameters != parametersBuffer)
1701 HeapFree(GetProcessHeap(), 0, wszParameters);
1702 wszParameters = buf;
1703 parametersLen = len;
1704 sei_tmp.lpParameters = wszParameters;
1708 if (*sei_tmp.lpDirectory)
1710 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1715 buf = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1716 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1717 if (wszDir != dirBuffer)
1718 HeapFree(GetProcessHeap(), 0, wszDir);
1720 sei_tmp.lpDirectory = wszDir;
1724 /* Else, try to execute the filename */
1725 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1727 /* separate out command line arguments from executable file name */
1728 if (!*sei_tmp.lpParameters && !appKnownSingular) {
1729 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1730 if (sei_tmp.lpFile[0] == '"') {
1731 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1732 LPWSTR dst = wfileName;
1735 /* copy the unquoted executable path to 'wfileName' */
1736 while(*src && *src!='"')
1744 while(isspace(*src))
1749 /* copy the parameter string to 'wszParameters' */
1750 strcpyW(wszParameters, src);
1752 /* terminate previous command string after the quote character */
1757 /* If the executable name is not quoted, we have to use this search loop here,
1758 that in CreateProcess() is not sufficient because it does not handle shell links. */
1759 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1762 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1763 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1764 int idx = space-sei_tmp.lpFile;
1765 memcpy(buffer, sei_tmp.lpFile, idx * sizeof(WCHAR));
1768 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1769 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile)/sizeof(xlpFile[0]), xlpFile, NULL))
1771 /* separate out command from parameter string */
1772 LPCWSTR p = space + 1;
1777 strcpyW(wszParameters, p);
1784 lstrcpynW(wfileName, sei_tmp.lpFile,sizeof(wfileName)/sizeof(WCHAR));
1787 lstrcpynW(wfileName, sei_tmp.lpFile,sizeof(wfileName)/sizeof(WCHAR));
1792 len = lstrlenW(wszApplicationName) + 1;
1793 if (sei_tmp.lpParameters[0])
1794 len += 1 + lstrlenW(wszParameters);
1797 wcmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1800 strcpyW(wcmd, wszApplicationName);
1801 if (sei_tmp.lpParameters[0]) {
1802 strcatW(wcmd, wSpace);
1803 strcatW(wcmd, wszParameters);
1806 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1808 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1809 if (wszParameters != parametersBuffer)
1810 HeapFree(GetProcessHeap(), 0, wszParameters);
1811 if (wszDir != dirBuffer)
1812 HeapFree(GetProcessHeap(), 0, wszDir);
1813 if (wcmd != wcmdBuffer)
1814 HeapFree(GetProcessHeap(), 0, wcmd);
1818 /* Else, try to find the executable */
1820 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, lpstrProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1821 if (retval > 32) /* Found */
1823 retval = SHELL_quote_and_execute( wcmd, wszParameters, lpstrProtocol,
1824 wszApplicationName, env, &sei_tmp,
1826 HeapFree( GetProcessHeap(), 0, env );
1828 else if (PathIsDirectoryW(lpFile))
1830 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r',0};
1831 static const WCHAR wQuote[] = {'"',0};
1832 WCHAR wExec[MAX_PATH];
1833 WCHAR * lpQuotedFile = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (strlenW(lpFile) + 3) );
1837 retval = SHELL_FindExecutable( sei_tmp.lpDirectory, wExplorer,
1838 wszOpen, wExec, MAX_PATH,
1839 NULL, &env, NULL, NULL );
1842 strcpyW(lpQuotedFile, wQuote);
1843 strcatW(lpQuotedFile, lpFile);
1844 strcatW(lpQuotedFile, wQuote);
1845 retval = SHELL_quote_and_execute( wExec, lpQuotedFile,
1847 wszApplicationName, env,
1848 &sei_tmp, sei, execfunc );
1849 HeapFree( GetProcessHeap(), 0, env );
1851 HeapFree( GetProcessHeap(), 0, lpQuotedFile );
1854 retval = 0; /* Out of memory */
1856 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1858 retval = SHELL_execute_url( lpFile, wFile, wcmd, &sei_tmp, sei, execfunc );
1860 /* Check if file specified is in the form www.??????.*** */
1861 else if (!strncmpiW(lpFile, wWww, 3))
1863 /* if so, append lpFile http:// and call ShellExecute */
1864 WCHAR lpstrTmpFile[256];
1865 strcpyW(lpstrTmpFile, wHttp);
1866 strcatW(lpstrTmpFile, lpFile);
1867 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1870 TRACE("retval %lu\n", retval);
1872 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1873 if (wszParameters != parametersBuffer)
1874 HeapFree(GetProcessHeap(), 0, wszParameters);
1875 if (wszDir != dirBuffer)
1876 HeapFree(GetProcessHeap(), 0, wszDir);
1877 if (wcmd != wcmdBuffer)
1878 HeapFree(GetProcessHeap(), 0, wcmd);
1880 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1882 if (retval <= 32 && !(sei_tmp.fMask & SEE_MASK_FLAG_NO_UI))
1883 do_error_dialog(retval, sei_tmp.hwnd);
1887 /*************************************************************************
1888 * ShellExecuteA [SHELL32.290]
1890 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
1891 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
1893 SHELLEXECUTEINFOA sei;
1895 TRACE("%p,%s,%s,%s,%s,%d\n",
1896 hWnd, debugstr_a(lpOperation), debugstr_a(lpFile),
1897 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1899 sei.cbSize = sizeof(sei);
1900 sei.fMask = SEE_MASK_FLAG_NO_UI;
1902 sei.lpVerb = lpOperation;
1903 sei.lpFile = lpFile;
1904 sei.lpParameters = lpParameters;
1905 sei.lpDirectory = lpDirectory;
1906 sei.nShow = iShowCmd;
1913 ShellExecuteExA (&sei);
1914 return sei.hInstApp;
1917 /*************************************************************************
1918 * ShellExecuteExA [SHELL32.292]
1921 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1923 SHELLEXECUTEINFOW seiW;
1925 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1929 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1932 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1935 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1937 if (sei->lpParameters)
1938 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1940 if (sei->lpDirectory)
1941 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1943 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1944 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1946 seiW.lpClass = NULL;
1948 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1950 sei->hInstApp = seiW.hInstApp;
1952 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1953 sei->hProcess = seiW.hProcess;
1957 SHFree(wParameters);
1964 /*************************************************************************
1965 * ShellExecuteExW [SHELL32.293]
1968 BOOL WINAPI DECLSPEC_HOTPATCH ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1970 return SHELL_execute( sei, SHELL_ExecuteW );
1973 /*************************************************************************
1974 * ShellExecuteW [SHELL32.294]
1976 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1977 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1979 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1980 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1982 SHELLEXECUTEINFOW sei;
1985 sei.cbSize = sizeof(sei);
1986 sei.fMask = SEE_MASK_FLAG_NO_UI;
1988 sei.lpVerb = lpOperation;
1989 sei.lpFile = lpFile;
1990 sei.lpParameters = lpParameters;
1991 sei.lpDirectory = lpDirectory;
1992 sei.nShow = nShowCmd;
1999 SHELL_execute( &sei, SHELL_ExecuteW );
2000 return sei.hInstApp;
2003 /*************************************************************************
2004 * WOWShellExecute [SHELL32.@]
2006 * FIXME: the callback function most likely doesn't work the same way on Windows.
2008 HINSTANCE WINAPI WOWShellExecute(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
2009 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd, void *callback)
2011 SHELLEXECUTEINFOW seiW;
2012 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL;
2013 HANDLE hProcess = 0;
2015 seiW.lpVerb = lpOperation ? __SHCloneStrAtoW(&wVerb, lpOperation) : NULL;
2016 seiW.lpFile = lpFile ? __SHCloneStrAtoW(&wFile, lpFile) : NULL;
2017 seiW.lpParameters = lpParameters ? __SHCloneStrAtoW(&wParameters, lpParameters) : NULL;
2018 seiW.lpDirectory = lpDirectory ? __SHCloneStrAtoW(&wDirectory, lpDirectory) : NULL;
2020 seiW.cbSize = sizeof(seiW);
2023 seiW.nShow = iShowCmd;
2028 seiW.hProcess = hProcess;
2030 SHELL_execute( &seiW, callback );
2034 SHFree(wParameters);
2036 return seiW.hInstApp;
2039 /*************************************************************************
2040 * OpenAs_RunDLLA [SHELL32.@]
2042 void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
2044 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
2047 /*************************************************************************
2048 * OpenAs_RunDLLW [SHELL32.@]
2050 void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
2052 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);