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 "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 static BOOL SHELL_ArgifyW(WCHAR* out, int len, const WCHAR* fmt, const WCHAR* lpFile, LPITEMIDLIST pidl, LPCWSTR args, DWORD* out_len)
83 BOOL found_p1 = FALSE;
88 TRACE("%p, %d, %s, %s, %p, %p\n", out, len, debugstr_w(fmt),
89 debugstr_w(lpFile), pidl, args);
135 while(*args && !isspace(*args))
144 while(isspace(*args))
149 /* else fall through */
151 if (!done || (*fmt == '1'))
153 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
154 if (SearchPathW(NULL, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
159 used += strlenW(cmd);
170 * IE uses this a lot for activating things such as windows media
171 * player. This is not verified to be fully correct but it appears
177 used += strlenW(lpFile);
180 strcpyW(res, lpFile);
181 res += strlenW(lpFile);
191 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
192 * allowing a buffer of 100 should more than exceed all needs */
195 HGLOBAL hmem = SHAllocShared(pidl, ILGetSize(pidl), 0);
196 pv = SHLockShared(hmem, 0);
197 chars = sprintfW(buf, wszILPtr, pv);
198 if (chars >= sizeof(buf)/sizeof(WCHAR))
199 ERR("pidl format buffer too small!\n");
213 * Check if this is an env-variable here...
216 /* Make sure that we have at least one more %.*/
217 if (strchrW(fmt, '%'))
219 WCHAR tmpBuffer[1024];
220 PWSTR tmpB = tmpBuffer;
221 WCHAR tmpEnvBuff[MAX_PATH];
228 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer));
230 envRet = GetEnvironmentVariableW(tmpBuffer, tmpEnvBuff, MAX_PATH);
231 if (envRet == 0 || envRet > MAX_PATH)
233 used += strlenW(tmpBuffer);
236 strcpyW( res, tmpBuffer );
237 res += strlenW(tmpBuffer);
242 used += strlenW(tmpEnvBuff);
245 strcpyW( res, tmpEnvBuff );
246 res += strlenW(tmpEnvBuff);
253 /* Don't skip past terminator (catch a single '%' at the end) */
270 TRACE("used %i of %i space\n",used,len);
277 static HRESULT SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl, LPWSTR pszPath, UINT uOutSize)
280 IShellFolder* desktop;
282 HRESULT hr = SHGetDesktopFolder(&desktop);
285 hr = IShellFolder_GetDisplayNameOf(desktop, pidl, SHGDN_FORPARSING, &strret);
288 StrRetToStrNW(pszPath, uOutSize, &strret, pidl);
290 IShellFolder_Release(desktop);
296 /*************************************************************************
297 * SHELL_ExecuteW [Internal]
300 static UINT_PTR SHELL_ExecuteW(const WCHAR *lpCmd, WCHAR *env, BOOL shWait,
301 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
303 STARTUPINFOW startup;
304 PROCESS_INFORMATION info;
305 UINT_PTR retval = SE_ERR_NOASSOC;
307 WCHAR curdir[MAX_PATH];
308 DWORD dwCreationFlags;
309 const WCHAR *lpDirectory = NULL;
311 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
313 /* make sure we don't fail the CreateProcess if the calling app passes in
314 * a bad working directory */
315 if (psei->lpDirectory && psei->lpDirectory[0])
317 DWORD attr = GetFileAttributesW(psei->lpDirectory);
318 if (attr != INVALID_FILE_ATTRIBUTES && attr & FILE_ATTRIBUTE_DIRECTORY)
319 lpDirectory = psei->lpDirectory;
322 /* ShellExecute specifies the command from psei->lpDirectory
323 * if present. Not from the current dir as CreateProcess does */
325 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
326 if( !SetCurrentDirectoryW( lpDirectory))
327 ERR("cannot set directory %s\n", debugstr_w(lpDirectory));
328 ZeroMemory(&startup,sizeof(STARTUPINFOW));
329 startup.cb = sizeof(STARTUPINFOW);
330 startup.dwFlags = STARTF_USESHOWWINDOW;
331 startup.wShowWindow = psei->nShow;
332 dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
333 if (psei->fMask & SEE_MASK_NO_CONSOLE)
334 dwCreationFlags |= CREATE_NEW_CONSOLE;
335 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, dwCreationFlags, env,
336 lpDirectory, &startup, &info))
338 /* Give 30 seconds to the app to come up, if desired. Probably only needed
339 when starting app immediately before making a DDE connection. */
341 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
342 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
344 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
345 psei_out->hProcess = info.hProcess;
347 CloseHandle( info.hProcess );
348 CloseHandle( info.hThread );
350 else if ((retval = GetLastError()) >= 32)
352 TRACE("CreateProcess returned error %ld\n", retval);
353 retval = ERROR_BAD_FORMAT;
356 TRACE("returning %lu\n", retval);
358 psei_out->hInstApp = (HINSTANCE)retval;
360 if( !SetCurrentDirectoryW( curdir))
361 ERR("cannot return to directory %s\n", debugstr_w(curdir));
367 /***********************************************************************
368 * SHELL_BuildEnvW [Internal]
370 * Build the environment for the new process, adding the specified
371 * path to the PATH variable. Returned pointer must be freed by caller.
373 static void *SHELL_BuildEnvW( const WCHAR *path )
375 static const WCHAR wPath[] = {'P','A','T','H','=',0};
376 WCHAR *strings, *new_env;
378 int total = strlenW(path) + 1;
379 BOOL got_path = FALSE;
381 if (!(strings = GetEnvironmentStringsW())) return NULL;
385 int len = strlenW(p) + 1;
386 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
390 if (!got_path) total += 5; /* we need to create PATH */
391 total++; /* terminating null */
393 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
395 FreeEnvironmentStringsW( strings );
402 int len = strlenW(p) + 1;
403 memcpy( p2, p, len * sizeof(WCHAR) );
404 if (!strncmpiW( p, wPath, 5 ))
407 strcpyW( p2 + len, path );
408 p2 += strlenW(path) + 1;
415 strcpyW( p2, wPath );
417 p2 += strlenW(p2) + 1;
420 FreeEnvironmentStringsW( strings );
425 /***********************************************************************
426 * SHELL_TryAppPathW [Internal]
428 * Helper function for SHELL_FindExecutable
429 * @param lpResult - pointer to a buffer of size MAX_PATH
430 * On entry: szName is a filename (probably without path separators).
431 * On exit: if szName found in "App Path", place full path in lpResult, and return true
433 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
435 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',
436 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
437 static const WCHAR wPath[] = {'P','a','t','h',0};
444 if (env) *env = NULL;
445 strcpyW(buffer, wszKeyAppPaths);
446 strcatW(buffer, szName);
447 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
450 len = MAX_PATH*sizeof(WCHAR);
451 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
457 DWORD count = sizeof(buffer);
458 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
459 *env = SHELL_BuildEnvW( buffer );
463 if (hkApp) RegCloseKey(hkApp);
467 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
469 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
471 WCHAR verb[MAX_PATH];
473 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, filetype, 0, 0x02000000, &hkeyClass))
474 return SE_ERR_NOASSOC;
475 if (!HCR_GetDefaultVerbW(hkeyClass, lpOperation, verb, sizeof(verb)/sizeof(verb[0])))
476 return SE_ERR_NOASSOC;
477 RegCloseKey(hkeyClass);
479 /* Looking for ...buffer\shell\<verb>\command */
480 strcatW(filetype, wszShell);
481 strcatW(filetype, verb);
482 strcatW(filetype, wCommand);
484 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
485 &commandlen) == ERROR_SUCCESS)
487 commandlen /= sizeof(WCHAR);
488 if (key) strcpyW(key, filetype);
492 LONG paramlen = sizeof(param);
493 static const WCHAR wSpace[] = {' ',0};
495 /* FIXME: it seems all Windows version don't behave the same here.
496 * the doc states that this ddeexec information can be found after
498 * on Win98, it doesn't appear, but I think it does on Win2k
500 /* Get the parameters needed by the application
501 from the associated ddeexec key */
502 tmp = strstrW(filetype, wCommand);
504 strcatW(filetype, wDdeexec);
505 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
506 ¶mlen) == ERROR_SUCCESS)
508 paramlen /= sizeof(WCHAR);
509 strcatW(command, wSpace);
510 strcatW(command, param);
511 commandlen += paramlen;
515 command[commandlen] = '\0';
517 return 33; /* FIXME see SHELL_FindExecutable() */
520 return SE_ERR_NOASSOC;
523 /*************************************************************************
524 * SHELL_FindExecutable [Internal]
526 * Utility for code sharing between FindExecutable and ShellExecute
528 * lpFile the name of a file
529 * lpOperation the operation on it (open)
531 * lpResult a buffer, big enough :-(, to store the command to do the
532 * operation on the file
533 * key a buffer, big enough, to get the key name to do actually the
534 * command (it'll be used afterwards for more information
537 static UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
538 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
540 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
541 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
542 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
543 WCHAR *extension = NULL; /* pointer to file extension */
544 WCHAR filetype[256]; /* registry name for this filetype */
545 LONG filetypelen = sizeof(filetype); /* length of above */
546 WCHAR command[1024]; /* command from registry */
547 WCHAR wBuffer[256]; /* Used to GetProfileString */
548 UINT retval = SE_ERR_NOASSOC;
549 WCHAR *tok; /* token pointer */
550 WCHAR xlpFile[256]; /* result of SearchPath */
551 DWORD attribs; /* file attributes */
553 TRACE("%s\n", debugstr_w(lpFile));
556 return ERROR_INVALID_PARAMETER;
559 lpResult[0] = '\0'; /* Start off with an empty return string */
560 if (key) *key = '\0';
562 /* trap NULL parameters on entry */
565 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
566 debugstr_w(lpFile), debugstr_w(lpResult));
567 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
570 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
572 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
576 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
578 TRACE("SearchPathW returned non-zero\n");
580 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
583 attribs = GetFileAttributesW(lpFile);
584 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
586 strcpyW(filetype, wszFolder);
587 filetypelen = 6; /* strlen("Folder") */
591 /* First thing we need is the file's extension */
592 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
593 /* File->Run in progman uses */
595 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
597 if (extension == NULL || extension[1]==0)
599 WARN("Returning SE_ERR_NOASSOC\n");
600 return SE_ERR_NOASSOC;
603 /* Three places to check: */
604 /* 1. win.ini, [windows], programs (NB no leading '.') */
605 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
606 /* 3. win.ini, [extensions], extension (NB no leading '.' */
607 /* All I know of the order is that registry is checked before */
608 /* extensions; however, it'd make sense to check the programs */
609 /* section first, so that's what happens here. */
611 /* See if it's a program - if GetProfileString fails, we skip this
612 * section. Actually, if GetProfileString fails, we've probably
613 * got a lot more to worry about than running a program... */
614 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
621 while (*p && *p != ' ' && *p != '\t') p++;
625 while (*p == ' ' || *p == '\t') p++;
628 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
630 strcpyW(lpResult, xlpFile);
631 /* Need to perhaps check that the file has a path
633 TRACE("found %s\n", debugstr_w(lpResult));
635 /* Greater than 32 to indicate success */
642 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
643 &filetypelen) == ERROR_SUCCESS)
645 filetypelen /= sizeof(WCHAR);
646 if (filetypelen == sizeof(filetype)/sizeof(WCHAR))
648 filetype[filetypelen] = '\0';
649 TRACE("File type: %s\n", debugstr_w(filetype));
660 /* pass the operation string to SHELL_FindExecutableByOperation() */
661 filetype[filetypelen] = '\0';
662 retval = SHELL_FindExecutableByOperation(lpOperation, key, filetype, command, sizeof(command));
667 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
668 if (finishedLen > resultLen)
669 ERR("Argify buffer not large enough.. truncated\n");
671 /* Remove double quotation marks and command line arguments */
672 if (*lpResult == '"')
675 while (*(p + 1) != '"')
684 /* Truncate on first space */
686 while (*p != ' ' && *p != '\0')
692 else /* Check win.ini */
694 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
696 /* Toss the leading dot */
698 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
700 if (strlenW(command) != 0)
702 strcpyW(lpResult, command);
703 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
707 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
708 tok = strchrW(command, '^'); /* see above */
709 if ((tok != NULL) && (strlenW(tok)>5))
711 strcatW(lpResult, &tok[5]);
714 retval = 33; /* FIXME - see above */
719 TRACE("returning %s\n", debugstr_w(lpResult));
723 /******************************************************************
726 * callback for the DDE connection. not really useful
728 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
729 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
730 ULONG_PTR dwData1, ULONG_PTR dwData2)
732 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
733 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
737 /******************************************************************
740 * ShellExecute helper. Used to do an operation with a DDE connection
742 * Handles both the direct connection (try #1), and if it fails,
743 * launching an application and trying (#2) to connect to it
746 static unsigned dde_connect(const WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
747 const WCHAR* lpFile, WCHAR *env,
748 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
749 const SHELLEXECUTEINFOW *psei, LPSHELLEXECUTEINFOW psei_out)
751 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
752 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
754 WCHAR * endkey = regkey + strlenW(key);
755 WCHAR app[256], topic[256], ifexec[256], res[256];
756 LONG applen, topiclen, ifexeclen;
761 HSZ hszApp, hszTopic;
764 unsigned ret = SE_ERR_NOASSOC;
765 BOOL unicode = !(GetVersion() & 0x80000000);
767 strcpyW(regkey, key);
768 strcpyW(endkey, wApplication);
769 applen = sizeof(app);
770 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, app, &applen) != ERROR_SUCCESS)
772 WCHAR command[1024], fullpath[MAX_PATH];
773 static const WCHAR wSo[] = { '.','s','o',0 };
774 int sizeSo = sizeof(wSo)/sizeof(WCHAR);
778 /* Get application command from start string and find filename of application */
781 strcpyW(command, start+1);
782 if ((ptr = strchrW(command, '"')))
784 ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
789 for (p=(LPWSTR)start; (space=strchrW(p, ' ')); p=space+1)
791 int idx = space-start;
792 memcpy(command, start, idx*sizeof(WCHAR));
794 if ((ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr)))
798 ret = SearchPathW(NULL, start, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
803 ERR("Unable to find application path for command %s\n", debugstr_w(start));
804 return ERROR_ACCESS_DENIED;
808 /* Remove extensions (including .so) */
809 ptr = app + strlenW(app) - (sizeSo-1);
810 if (strlenW(app) >= sizeSo &&
814 ptr = strrchrW(app, '.');
819 strcpyW(endkey, wTopic);
820 topiclen = sizeof(topic);
821 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, topic, &topiclen) != ERROR_SUCCESS)
823 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
824 strcpyW(topic, wSystem);
829 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
834 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
838 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
839 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
841 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
845 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
846 TRACE("Launching %s\n", debugstr_w(start));
847 ret = execfunc(start, env, TRUE, psei, psei_out);
850 TRACE("Couldn't launch\n");
853 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
856 TRACE("Couldn't connect. ret=%d\n", ret);
857 DdeUninitialize(ddeInst);
858 SetLastError(ERROR_DDE_FAIL);
859 return 30; /* whatever */
861 strcpyW(endkey, wIfexec);
862 ifexeclen = sizeof(ifexec);
863 if (RegQueryValueW(HKEY_CLASSES_ROOT, regkey, ifexec, &ifexeclen) == ERROR_SUCCESS)
869 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline, &resultLen);
870 if (resultLen > sizeof(res)/sizeof(WCHAR))
871 ERR("Argify buffer not large enough, truncated\n");
872 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
874 /* It's documented in the KB 330337 that IE has a bug and returns
875 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
878 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
879 XTYP_EXECUTE, 30000, &tid);
882 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
883 char *resA = HeapAlloc(GetProcessHeap(), 0, lenA);
884 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
885 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
886 XTYP_EXECUTE, 10000, &tid );
887 HeapFree(GetProcessHeap(), 0, resA);
890 DdeFreeDataHandle(hDdeData);
892 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
895 DdeDisconnect(hConv);
898 DdeUninitialize(ddeInst);
903 /*************************************************************************
904 * execute_from_key [Internal]
906 static UINT_PTR execute_from_key(LPCWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
907 LPCWSTR executable_name,
908 SHELL_ExecuteW32 execfunc,
909 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
911 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
912 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
913 WCHAR cmd[256], param[1024], ddeexec[256];
914 LONG cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
915 UINT_PTR retval = SE_ERR_NOASSOC;
919 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
920 debugstr_w(szCommandline), debugstr_w(executable_name));
925 /* Get the application from the registry */
926 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
928 TRACE("got cmd: %s\n", debugstr_w(cmd));
930 /* Is there a replace() function anywhere? */
931 cmdlen /= sizeof(WCHAR);
933 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
934 if (resultLen > sizeof(param)/sizeof(WCHAR))
935 ERR("Argify buffer not large enough, truncating\n");
938 /* Get the parameters needed by the application
939 from the associated ddeexec key */
940 tmp = strstrW(key, wCommand);
942 strcpyW(tmp, wDdeexec);
944 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, &ddeexeclen) == ERROR_SUCCESS)
946 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
947 if (!param[0]) strcpyW(param, executable_name);
948 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
952 TRACE("executing: %s\n", debugstr_w(param));
953 retval = execfunc(param, env, FALSE, psei, psei_out);
956 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
961 /*************************************************************************
962 * FindExecutableA [SHELL32.@]
964 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
967 WCHAR *wFile = NULL, *wDirectory = NULL;
968 WCHAR wResult[MAX_PATH];
970 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
971 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
973 retval = FindExecutableW(wFile, wDirectory, wResult);
974 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
976 SHFree( wDirectory );
978 TRACE("returning %s\n", lpResult);
982 /*************************************************************************
983 * FindExecutableW [SHELL32.@]
985 * This function returns the executable associated with the specified file
986 * for the default verb.
989 * lpFile [I] The file to find the association for. This must refer to
990 * an existing file otherwise FindExecutable fails and returns
992 * lpResult [O] Points to a buffer into which the executable path is
993 * copied. This parameter must not be NULL otherwise
994 * FindExecutable() segfaults. The buffer must be of size at
995 * least MAX_PATH characters.
998 * A value greater than 32 on success, less than or equal to 32 otherwise.
999 * See the SE_ERR_* constants.
1002 * On Windows XP and 2003, FindExecutable() seems to first convert the
1003 * filename into 8.3 format, thus taking into account only the first three
1004 * characters of the extension, and expects to find an association for those.
1005 * However other Windows versions behave sanely.
1007 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
1009 UINT_PTR retval = SE_ERR_NOASSOC;
1010 WCHAR old_dir[1024];
1012 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1014 lpResult[0] = '\0'; /* Start off with an empty return string */
1016 return (HINSTANCE)SE_ERR_FNF;
1020 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
1021 SetCurrentDirectoryW(lpDirectory);
1024 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
1026 TRACE("returning %s\n", debugstr_w(lpResult));
1028 SetCurrentDirectoryW(old_dir);
1029 return (HINSTANCE)retval;
1032 /* FIXME: is this already implemented somewhere else? */
1033 static HKEY ShellExecute_GetClassKey( const SHELLEXECUTEINFOW *sei )
1035 LPCWSTR ext = NULL, lpClass = NULL;
1037 DWORD type = 0, sz = 0;
1041 if (sei->fMask & SEE_MASK_CLASSALL)
1042 return sei->hkeyClass;
1044 if (sei->fMask & SEE_MASK_CLASSNAME)
1045 lpClass = sei->lpClass;
1048 ext = PathFindExtensionW( sei->lpFile );
1049 TRACE("ext = %s\n", debugstr_w( ext ) );
1053 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
1054 if (r != ERROR_SUCCESS )
1057 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
1058 if ( r == ERROR_SUCCESS && type == REG_SZ )
1060 sz += sizeof (WCHAR);
1061 cls = HeapAlloc( GetProcessHeap(), 0, sz );
1063 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
1066 RegCloseKey( hkey );
1070 TRACE("class = %s\n", debugstr_w(lpClass) );
1074 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
1076 HeapFree( GetProcessHeap(), 0, cls );
1081 static IDataObject *shellex_get_dataobj( LPSHELLEXECUTEINFOW sei )
1083 LPCITEMIDLIST pidllast = NULL;
1084 IDataObject *dataobj = NULL;
1085 IShellFolder *shf = NULL;
1086 LPITEMIDLIST pidl = NULL;
1089 if (sei->fMask & SEE_MASK_CLASSALL)
1090 pidl = sei->lpIDList;
1093 WCHAR fullpath[MAX_PATH];
1097 ret = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
1101 pidl = ILCreateFromPathW( fullpath );
1104 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
1108 IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1109 &IID_IDataObject, NULL, (LPVOID*) &dataobj );
1112 if ( pidl != sei->lpIDList )
1115 IShellFolder_Release( shf );
1119 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1120 LPSHELLEXECUTEINFOW sei )
1122 IContextMenu *cm = NULL;
1123 CMINVOKECOMMANDINFOEX ici;
1130 TRACE("%p %p\n", obj, sei );
1132 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1136 hmenu = CreateMenu();
1140 /* the number of the last menu added is returned in r */
1141 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1145 n = GetMenuItemCount( hmenu );
1146 for ( i = 0; i < n; i++ )
1148 memset( &info, 0, sizeof info );
1149 info.cbSize = sizeof info;
1150 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1151 info.dwTypeData = string;
1152 info.cch = sizeof string;
1154 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1156 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1157 info.fState, info.dwItemData, info.fType, info.wID );
1158 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1159 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1170 memset( &ici, 0, sizeof ici );
1171 ici.cbSize = sizeof ici;
1172 ici.fMask = CMIC_MASK_UNICODE | (sei->fMask & (SEE_MASK_NOASYNC|SEE_MASK_ASYNCOK|SEE_MASK_FLAG_NO_UI));
1173 ici.nShow = sei->nShow;
1174 ici.lpVerb = MAKEINTRESOURCEA( def );
1175 ici.hwnd = sei->hwnd;
1176 ici.lpParametersW = sei->lpParameters;
1178 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1180 TRACE("invoke command returned %08x\n", r );
1184 DestroyMenu( hmenu );
1186 IContextMenu_Release( cm );
1190 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1192 IDataObject *dataobj = NULL;
1193 IObjectWithSite *ows = NULL;
1194 IShellExtInit *obj = NULL;
1197 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1199 r = CoInitialize( NULL );
1203 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1204 &IID_IShellExtInit, (LPVOID*)&obj );
1207 ERR("failed %08x\n", r );
1211 dataobj = shellex_get_dataobj( sei );
1214 ERR("failed to get data object\n");
1218 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1222 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1226 IObjectWithSite_SetSite( ows, NULL );
1228 r = shellex_run_context_menu_default( obj, sei );
1232 IObjectWithSite_Release( ows );
1234 IDataObject_Release( dataobj );
1236 IShellExtInit_Release( obj );
1242 /*************************************************************************
1243 * ShellExecute_FromContextMenu [Internal]
1245 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1247 static const WCHAR szcm[] = { 's','h','e','l','l','e','x','\\',
1248 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1249 HKEY hkey, hkeycm = 0;
1256 TRACE("%s\n", debugstr_w(sei->lpFile) );
1258 hkey = ShellExecute_GetClassKey( sei );
1260 return ERROR_FUNCTION_FAILED;
1262 r = RegOpenKeyW( hkey, szcm, &hkeycm );
1263 if ( r == ERROR_SUCCESS )
1268 r = RegEnumKeyW( hkeycm, i++, szguid, sizeof(szguid)/sizeof(szguid[0]) );
1269 if ( r != ERROR_SUCCESS )
1272 hr = CLSIDFromString( szguid, &guid );
1275 /* stop at the first one that succeeds in running */
1276 hr = shellex_load_object_and_run( hkey, &guid, sei );
1277 if ( SUCCEEDED( hr ) )
1281 RegCloseKey( hkeycm );
1284 if ( hkey != sei->hkeyClass )
1285 RegCloseKey( hkey );
1289 static UINT_PTR SHELL_execute_class( LPCWSTR wszApplicationName, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1291 static const WCHAR wSpace[] = {' ',0};
1292 WCHAR execCmd[1024], wcmd[1024];
1293 /* launch a document by fileclass like 'WordPad.Document.1' */
1294 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1295 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1296 ULONG cmask=(psei->fMask & SEE_MASK_CLASSALL);
1300 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? psei->hkeyClass : NULL,
1301 (cmask == SEE_MASK_CLASSNAME) ? psei->lpClass: NULL,
1303 execCmd, sizeof(execCmd));
1305 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1306 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd), debugstr_w(wszApplicationName));
1309 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), execCmd, wszApplicationName, psei->lpIDList, NULL, &resultLen);
1310 if (!done && wszApplicationName[0])
1312 strcatW(wcmd, wSpace);
1313 strcatW(wcmd, wszApplicationName);
1315 if (resultLen > sizeof(wcmd)/sizeof(WCHAR))
1316 ERR("Argify buffer not large enough... truncating\n");
1317 return execfunc(wcmd, NULL, FALSE, psei, psei_out);
1320 static BOOL SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei, LPWSTR wszParameters, DWORD parametersLen, LPWSTR wszApplicationName, DWORD dwApplicationNameLen )
1322 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1323 WCHAR buffer[MAX_PATH];
1324 BOOL appKnownSingular = FALSE;
1326 /* last chance to translate IDList: now also allow CLSID paths */
1327 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei->lpIDList, buffer, sizeof(buffer)))) {
1328 if (buffer[0]==':' && buffer[1]==':') {
1329 /* open shell folder for the specified class GUID */
1330 if (strlenW(buffer) + 1 > parametersLen)
1331 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1332 lstrlenW(buffer) + 1, parametersLen);
1333 lstrcpynW(wszParameters, buffer, parametersLen);
1334 if (strlenW(wExplorer) > dwApplicationNameLen)
1335 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1336 lstrlenW(wExplorer) + 1, dwApplicationNameLen);
1337 lstrcpynW(wszApplicationName, wExplorer, dwApplicationNameLen);
1338 appKnownSingular = TRUE;
1340 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1342 WCHAR target[MAX_PATH];
1345 /* Check if we're executing a directory and if so use the
1346 handler for the Folder class */
1347 strcpyW(target, buffer);
1348 attribs = GetFileAttributesW(buffer);
1349 if (attribs != INVALID_FILE_ATTRIBUTES &&
1350 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1351 HCR_GetExecuteCommandW(0, wszFolder,
1353 buffer, sizeof(buffer))) {
1354 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1355 buffer, target, sei->lpIDList, NULL, &resultLen);
1356 if (resultLen > dwApplicationNameLen)
1357 ERR("Argify buffer not large enough... truncating\n");
1358 appKnownSingular = FALSE;
1360 sei->fMask &= ~SEE_MASK_INVOKEIDLIST;
1363 return appKnownSingular;
1366 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 )
1368 static const WCHAR wQuote[] = {'"',0};
1369 static const WCHAR wSpace[] = {' ',0};
1372 WCHAR *wszQuotedCmd;
1374 /* Length of quotes plus length of command plus NULL terminator */
1375 len = 2 + lstrlenW(wcmd) + 1;
1376 if (wszParameters[0])
1378 /* Length of space plus length of parameters */
1379 len += 1 + lstrlenW(wszParameters);
1381 wszQuotedCmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1382 /* Must quote to handle case where cmd contains spaces,
1383 * else security hole if malicious user creates executable file "C:\\Program"
1385 strcpyW(wszQuotedCmd, wQuote);
1386 strcatW(wszQuotedCmd, wcmd);
1387 strcatW(wszQuotedCmd, wQuote);
1388 if (wszParameters[0]) {
1389 strcatW(wszQuotedCmd, wSpace);
1390 strcatW(wszQuotedCmd, wszParameters);
1392 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(psei->lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1394 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, psei->lpParameters, wcmd, execfunc, psei, psei_out);
1396 retval = execfunc(wszQuotedCmd, env, FALSE, psei, psei_out);
1397 HeapFree(GetProcessHeap(), 0, wszQuotedCmd);
1401 static UINT_PTR SHELL_execute_url( LPCWSTR lpFile, LPCWSTR wFile, LPCWSTR wcmd, LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out, SHELL_ExecuteW32 execfunc )
1403 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1404 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1406 WCHAR *lpstrProtocol;
1411 lpstrRes = strchrW(lpFile, ':');
1413 iSize = lpstrRes - lpFile;
1415 iSize = strlenW(lpFile);
1417 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1418 /* Looking for ...protocol\shell\lpOperation\command */
1419 len = iSize + lstrlenW(wShell) + lstrlenW(wCommand) + 1;
1421 len += lstrlenW(psei->lpVerb);
1423 len += lstrlenW(wszOpen);
1424 lpstrProtocol = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1425 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1426 lpstrProtocol[iSize] = '\0';
1427 strcatW(lpstrProtocol, wShell);
1428 strcatW(lpstrProtocol, psei->lpVerb? psei->lpVerb: wszOpen);
1429 strcatW(lpstrProtocol, wCommand);
1431 /* Remove File Protocol from lpFile */
1432 /* In the case file://path/file */
1433 if (!strncmpiW(lpFile, wFile, iSize))
1436 while (*lpFile == ':') lpFile++;
1438 retval = execute_from_key(lpstrProtocol, lpFile, NULL, psei->lpParameters,
1439 wcmd, execfunc, psei, psei_out);
1440 HeapFree(GetProcessHeap(), 0, lpstrProtocol);
1444 /*************************************************************************
1445 * SHELL_execute [Internal]
1447 BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1449 static const WCHAR wSpace[] = {' ',0};
1450 static const WCHAR wWww[] = {'w','w','w',0};
1451 static const WCHAR wFile[] = {'f','i','l','e',0};
1452 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
1453 static const DWORD unsupportedFlags =
1454 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1455 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI |
1456 SEE_MASK_UNICODE | SEE_MASK_ASYNCOK | SEE_MASK_HMONITOR;
1458 WCHAR parametersBuffer[1024], dirBuffer[MAX_PATH], wcmdBuffer[1024];
1459 WCHAR *wszApplicationName, *wszParameters, *wszDir, *wcmd;
1460 DWORD dwApplicationNameLen = MAX_PATH+2;
1461 DWORD parametersLen = sizeof(parametersBuffer) / sizeof(WCHAR);
1462 DWORD dirLen = sizeof(dirBuffer) / sizeof(WCHAR);
1463 DWORD wcmdLen = sizeof(wcmdBuffer) / sizeof(WCHAR);
1465 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1466 WCHAR wfileName[MAX_PATH];
1468 WCHAR lpstrProtocol[256];
1470 UINT_PTR retval = SE_ERR_NOASSOC;
1471 BOOL appKnownSingular = FALSE;
1473 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1476 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1477 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1478 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1479 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1480 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1481 debugstr_w(sei_tmp.lpClass) : "not used");
1483 sei->hProcess = NULL;
1485 /* make copies of all path/command strings */
1486 if (!sei_tmp.lpFile)
1488 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1489 *wszApplicationName = '\0';
1491 else if (*sei_tmp.lpFile == '\"')
1493 DWORD l = strlenW(sei_tmp.lpFile+1);
1494 if(l >= dwApplicationNameLen) dwApplicationNameLen = l+1;
1495 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1496 memcpy(wszApplicationName, sei_tmp.lpFile+1, (l+1)*sizeof(WCHAR));
1497 if (wszApplicationName[l-1] == '\"')
1498 wszApplicationName[l-1] = '\0';
1499 appKnownSingular = TRUE;
1500 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1502 DWORD l = strlenW(sei_tmp.lpFile)+1;
1503 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1504 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1505 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1508 wszParameters = parametersBuffer;
1509 if (sei_tmp.lpParameters)
1511 len = lstrlenW(sei_tmp.lpParameters) + 1;
1512 if (len > parametersLen)
1514 wszParameters = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1515 parametersLen = len;
1517 strcpyW(wszParameters, sei_tmp.lpParameters);
1520 *wszParameters = '\0';
1523 if (sei_tmp.lpDirectory)
1525 len = lstrlenW(sei_tmp.lpDirectory) + 1;
1528 wszDir = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1531 strcpyW(wszDir, sei_tmp.lpDirectory);
1536 /* adjust string pointers to point to the new buffers */
1537 sei_tmp.lpFile = wszApplicationName;
1538 sei_tmp.lpParameters = wszParameters;
1539 sei_tmp.lpDirectory = wszDir;
1541 if (sei_tmp.fMask & unsupportedFlags)
1543 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1546 /* process the IDList */
1547 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1549 IShellExecuteHookW* pSEH;
1551 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1555 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1557 IShellExecuteHookW_Release(pSEH);
1560 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1561 if (wszParameters != parametersBuffer)
1562 HeapFree(GetProcessHeap(), 0, wszParameters);
1563 if (wszDir != dirBuffer)
1564 HeapFree(GetProcessHeap(), 0, wszDir);
1569 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1570 appKnownSingular = TRUE;
1571 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1574 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1576 sei->hInstApp = (HINSTANCE) 33;
1577 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1578 if (wszParameters != parametersBuffer)
1579 HeapFree(GetProcessHeap(), 0, wszParameters);
1580 if (wszDir != dirBuffer)
1581 HeapFree(GetProcessHeap(), 0, wszDir);
1585 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1587 retval = SHELL_execute_class( wszApplicationName, &sei_tmp, sei,
1589 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1590 if (wszParameters != parametersBuffer)
1591 HeapFree(GetProcessHeap(), 0, wszParameters);
1592 if (wszDir != dirBuffer)
1593 HeapFree(GetProcessHeap(), 0, wszDir);
1597 /* Has the IDList not yet been translated? */
1598 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1600 appKnownSingular = SHELL_translate_idlist( &sei_tmp, wszParameters,
1603 dwApplicationNameLen );
1606 /* expand environment strings */
1607 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1611 buf = HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
1613 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len+1);
1614 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1615 dwApplicationNameLen = len+1;
1616 wszApplicationName = buf;
1617 /* appKnownSingular unmodified */
1619 sei_tmp.lpFile = wszApplicationName;
1622 if (*sei_tmp.lpParameters)
1624 len = ExpandEnvironmentStringsW(sei_tmp.lpParameters, NULL, 0);
1629 buf = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1630 ExpandEnvironmentStringsW(sei_tmp.lpParameters, buf, len);
1631 if (wszParameters != parametersBuffer)
1632 HeapFree(GetProcessHeap(), 0, wszParameters);
1633 wszParameters = buf;
1634 parametersLen = len;
1635 sei_tmp.lpParameters = wszParameters;
1639 if (*sei_tmp.lpDirectory)
1641 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1646 buf = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1647 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1648 if (wszDir != dirBuffer)
1649 HeapFree(GetProcessHeap(), 0, wszDir);
1651 sei_tmp.lpDirectory = wszDir;
1655 /* Else, try to execute the filename */
1656 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1658 /* separate out command line arguments from executable file name */
1659 if (!*sei_tmp.lpParameters && !appKnownSingular) {
1660 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1661 if (sei_tmp.lpFile[0] == '"') {
1662 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1663 LPWSTR dst = wfileName;
1666 /* copy the unquoted executable path to 'wfileName' */
1667 while(*src && *src!='"')
1675 while(isspace(*src))
1680 /* copy the parameter string to 'wszParameters' */
1681 strcpyW(wszParameters, src);
1683 /* terminate previous command string after the quote character */
1688 /* If the executable name is not quoted, we have to use this search loop here,
1689 that in CreateProcess() is not sufficient because it does not handle shell links. */
1690 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1693 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1694 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1695 int idx = space-sei_tmp.lpFile;
1696 memcpy(buffer, sei_tmp.lpFile, idx * sizeof(WCHAR));
1699 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1700 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile)/sizeof(xlpFile[0]), xlpFile, NULL))
1702 /* separate out command from parameter string */
1703 LPCWSTR p = space + 1;
1708 strcpyW(wszParameters, p);
1715 lstrcpynW(wfileName, sei_tmp.lpFile,sizeof(wfileName)/sizeof(WCHAR));
1718 lstrcpynW(wfileName, sei_tmp.lpFile,sizeof(wfileName)/sizeof(WCHAR));
1723 len = lstrlenW(wszApplicationName) + 1;
1724 if (sei_tmp.lpParameters[0])
1725 len += 1 + lstrlenW(wszParameters);
1728 wcmd = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1731 strcpyW(wcmd, wszApplicationName);
1732 if (sei_tmp.lpParameters[0]) {
1733 strcatW(wcmd, wSpace);
1734 strcatW(wcmd, wszParameters);
1737 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1739 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1740 if (wszParameters != parametersBuffer)
1741 HeapFree(GetProcessHeap(), 0, wszParameters);
1742 if (wszDir != dirBuffer)
1743 HeapFree(GetProcessHeap(), 0, wszDir);
1744 if (wcmd != wcmdBuffer)
1745 HeapFree(GetProcessHeap(), 0, wcmd);
1749 /* Else, try to find the executable */
1751 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, wcmdLen, lpstrProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1752 if (retval > 32) /* Found */
1754 retval = SHELL_quote_and_execute( wcmd, wszParameters, lpstrProtocol,
1755 wszApplicationName, env, &sei_tmp,
1757 HeapFree( GetProcessHeap(), 0, env );
1759 else if (PathIsDirectoryW(lpFile))
1761 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r',0};
1762 static const WCHAR wQuote[] = {'"',0};
1763 WCHAR wExec[MAX_PATH];
1764 WCHAR * lpQuotedFile = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR) * (strlenW(lpFile) + 3) );
1768 retval = SHELL_FindExecutable( sei_tmp.lpDirectory, wExplorer,
1769 wszOpen, wExec, MAX_PATH,
1770 NULL, &env, NULL, NULL );
1773 strcpyW(lpQuotedFile, wQuote);
1774 strcatW(lpQuotedFile, lpFile);
1775 strcatW(lpQuotedFile, wQuote);
1776 retval = SHELL_quote_and_execute( wExec, lpQuotedFile,
1778 wszApplicationName, env,
1779 &sei_tmp, sei, execfunc );
1780 HeapFree( GetProcessHeap(), 0, env );
1782 HeapFree( GetProcessHeap(), 0, lpQuotedFile );
1785 retval = 0; /* Out of memory */
1787 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1789 retval = SHELL_execute_url( lpFile, wFile, wcmd, &sei_tmp, sei, execfunc );
1791 /* Check if file specified is in the form www.??????.*** */
1792 else if (!strncmpiW(lpFile, wWww, 3))
1794 /* if so, append lpFile http:// and call ShellExecute */
1795 WCHAR lpstrTmpFile[256];
1796 strcpyW(lpstrTmpFile, wHttp);
1797 strcatW(lpstrTmpFile, lpFile);
1798 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1801 TRACE("retval %lu\n", retval);
1803 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1804 if (wszParameters != parametersBuffer)
1805 HeapFree(GetProcessHeap(), 0, wszParameters);
1806 if (wszDir != dirBuffer)
1807 HeapFree(GetProcessHeap(), 0, wszDir);
1808 if (wcmd != wcmdBuffer)
1809 HeapFree(GetProcessHeap(), 0, wcmd);
1811 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1815 /*************************************************************************
1816 * ShellExecuteA [SHELL32.290]
1818 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
1819 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
1821 SHELLEXECUTEINFOA sei;
1823 TRACE("%p,%s,%s,%s,%s,%d\n",
1824 hWnd, debugstr_a(lpOperation), debugstr_a(lpFile),
1825 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1827 sei.cbSize = sizeof(sei);
1830 sei.lpVerb = lpOperation;
1831 sei.lpFile = lpFile;
1832 sei.lpParameters = lpParameters;
1833 sei.lpDirectory = lpDirectory;
1834 sei.nShow = iShowCmd;
1841 ShellExecuteExA (&sei);
1842 return sei.hInstApp;
1845 /*************************************************************************
1846 * ShellExecuteExA [SHELL32.292]
1849 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1851 SHELLEXECUTEINFOW seiW;
1853 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1857 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1860 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1863 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1865 if (sei->lpParameters)
1866 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1868 if (sei->lpDirectory)
1869 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1871 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1872 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1874 seiW.lpClass = NULL;
1876 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1878 sei->hInstApp = seiW.hInstApp;
1880 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1881 sei->hProcess = seiW.hProcess;
1885 SHFree(wParameters);
1892 /*************************************************************************
1893 * ShellExecuteExW [SHELL32.293]
1896 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1898 return SHELL_execute( sei, SHELL_ExecuteW );
1901 /*************************************************************************
1902 * ShellExecuteW [SHELL32.294]
1904 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1905 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1907 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1908 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1910 SHELLEXECUTEINFOW sei;
1913 sei.cbSize = sizeof(sei);
1916 sei.lpVerb = lpOperation;
1917 sei.lpFile = lpFile;
1918 sei.lpParameters = lpParameters;
1919 sei.lpDirectory = lpDirectory;
1920 sei.nShow = nShowCmd;
1927 SHELL_execute( &sei, SHELL_ExecuteW );
1928 return sei.hInstApp;
1931 /*************************************************************************
1932 * OpenAs_RunDLLA [SHELL32.@]
1934 void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
1936 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
1939 /*************************************************************************
1940 * OpenAs_RunDLLW [SHELL32.@]
1942 void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
1944 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);