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 foward 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 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
303 STARTUPINFOW startup;
304 PROCESS_INFORMATION info;
305 UINT_PTR retval = SE_ERR_NOASSOC;
307 WCHAR curdir[MAX_PATH];
309 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd), debugstr_w(psei->lpDirectory));
310 /* ShellExecute specifies the command from psei->lpDirectory
311 * if present. Not from the current dir as CreateProcess does */
312 if( psei->lpDirectory && psei->lpDirectory[0] )
313 if( ( gcdret = GetCurrentDirectoryW( MAX_PATH, curdir)))
314 if( !SetCurrentDirectoryW( psei->lpDirectory))
315 ERR("cannot set directory %s\n", debugstr_w(psei->lpDirectory));
316 ZeroMemory(&startup,sizeof(STARTUPINFOW));
317 startup.cb = sizeof(STARTUPINFOW);
318 startup.dwFlags = STARTF_USESHOWWINDOW;
319 startup.wShowWindow = psei->nShow;
320 if (CreateProcessW(NULL, (LPWSTR)lpCmd, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, env,
321 psei->lpDirectory && *psei->lpDirectory ? psei->lpDirectory : NULL,
324 /* Give 30 seconds to the app to come up, if desired. Probably only needed
325 when starting app immediately before making a DDE connection. */
327 if (WaitForInputIdle( info.hProcess, 30000 ) == WAIT_FAILED)
328 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
330 if (psei->fMask & SEE_MASK_NOCLOSEPROCESS)
331 psei_out->hProcess = info.hProcess;
333 CloseHandle( info.hProcess );
334 CloseHandle( info.hThread );
336 else if ((retval = GetLastError()) >= 32)
338 TRACE("CreateProcess returned error %ld\n", retval);
339 retval = ERROR_BAD_FORMAT;
342 TRACE("returning %lu\n", retval);
344 psei_out->hInstApp = (HINSTANCE)retval;
346 if( !SetCurrentDirectoryW( curdir))
347 ERR("cannot return to directory %s\n", debugstr_w(curdir));
353 /***********************************************************************
354 * SHELL_BuildEnvW [Internal]
356 * Build the environment for the new process, adding the specified
357 * path to the PATH variable. Returned pointer must be freed by caller.
359 static void *SHELL_BuildEnvW( const WCHAR *path )
361 static const WCHAR wPath[] = {'P','A','T','H','=',0};
362 WCHAR *strings, *new_env;
364 int total = strlenW(path) + 1;
365 BOOL got_path = FALSE;
367 if (!(strings = GetEnvironmentStringsW())) return NULL;
371 int len = strlenW(p) + 1;
372 if (!strncmpiW( p, wPath, 5 )) got_path = TRUE;
376 if (!got_path) total += 5; /* we need to create PATH */
377 total++; /* terminating null */
379 if (!(new_env = HeapAlloc( GetProcessHeap(), 0, total * sizeof(WCHAR) )))
381 FreeEnvironmentStringsW( strings );
388 int len = strlenW(p) + 1;
389 memcpy( p2, p, len * sizeof(WCHAR) );
390 if (!strncmpiW( p, wPath, 5 ))
393 strcpyW( p2 + len, path );
394 p2 += strlenW(path) + 1;
401 strcpyW( p2, wPath );
403 p2 += strlenW(p2) + 1;
406 FreeEnvironmentStringsW( strings );
411 /***********************************************************************
412 * SHELL_TryAppPathW [Internal]
414 * Helper function for SHELL_FindExecutable
415 * @param lpResult - pointer to a buffer of size MAX_PATH
416 * On entry: szName is a filename (probably without path separators).
417 * On exit: if szName found in "App Path", place full path in lpResult, and return true
419 static BOOL SHELL_TryAppPathW( LPCWSTR szName, LPWSTR lpResult, WCHAR **env)
421 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',
422 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
423 static const WCHAR wPath[] = {'P','a','t','h',0};
430 if (env) *env = NULL;
431 strcpyW(buffer, wszKeyAppPaths);
432 strcatW(buffer, szName);
433 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, buffer, 0, KEY_READ, &hkApp);
436 len = MAX_PATH*sizeof(WCHAR);
437 res = RegQueryValueW(hkApp, NULL, lpResult, &len);
443 DWORD count = sizeof(buffer);
444 if (!RegQueryValueExW(hkApp, wPath, NULL, NULL, (LPBYTE)buffer, &count) && buffer[0])
445 *env = SHELL_BuildEnvW( buffer );
449 if (hkApp) RegCloseKey(hkApp);
453 static UINT SHELL_FindExecutableByOperation(LPCWSTR lpOperation, LPWSTR key, LPWSTR filetype, LPWSTR command, LONG commandlen)
455 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
457 WCHAR verb[MAX_PATH];
459 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, filetype, 0, 0x02000000, &hkeyClass))
460 return SE_ERR_NOASSOC;
461 if (!HCR_GetDefaultVerbW(hkeyClass, lpOperation, verb, sizeof(verb)))
462 return SE_ERR_NOASSOC;
463 RegCloseKey(hkeyClass);
465 /* Looking for ...buffer\shell\<verb>\command */
466 strcatW(filetype, wszShell);
467 strcatW(filetype, verb);
468 strcatW(filetype, wCommand);
470 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, command,
471 &commandlen) == ERROR_SUCCESS)
473 commandlen /= sizeof(WCHAR);
474 if (key) strcpyW(key, filetype);
478 LONG paramlen = sizeof(param);
479 static const WCHAR wSpace[] = {' ',0};
481 /* FIXME: it seems all Windows version don't behave the same here.
482 * the doc states that this ddeexec information can be found after
484 * on Win98, it doesn't appear, but I think it does on Win2k
486 /* Get the parameters needed by the application
487 from the associated ddeexec key */
488 tmp = strstrW(filetype, wCommand);
490 strcatW(filetype, wDdeexec);
491 if (RegQueryValueW(HKEY_CLASSES_ROOT, filetype, param,
492 ¶mlen) == ERROR_SUCCESS)
494 paramlen /= sizeof(WCHAR);
495 strcatW(command, wSpace);
496 strcatW(command, param);
497 commandlen += paramlen;
501 command[commandlen] = '\0';
503 return 33; /* FIXME see SHELL_FindExecutable() */
506 return SE_ERR_NOASSOC;
509 /*************************************************************************
510 * SHELL_FindExecutable [Internal]
512 * Utility for code sharing between FindExecutable and ShellExecute
514 * lpFile the name of a file
515 * lpOperation the operation on it (open)
517 * lpResult a buffer, big enough :-(, to store the command to do the
518 * operation on the file
519 * key a buffer, big enough, to get the key name to do actually the
520 * command (it'll be used afterwards for more information
523 UINT SHELL_FindExecutable(LPCWSTR lpPath, LPCWSTR lpFile, LPCWSTR lpOperation,
524 LPWSTR lpResult, int resultLen, LPWSTR key, WCHAR **env, LPITEMIDLIST pidl, LPCWSTR args)
526 static const WCHAR wWindows[] = {'w','i','n','d','o','w','s',0};
527 static const WCHAR wPrograms[] = {'p','r','o','g','r','a','m','s',0};
528 static const WCHAR wExtensions[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
529 WCHAR *extension = NULL; /* pointer to file extension */
530 WCHAR filetype[256]; /* registry name for this filetype */
531 LONG filetypelen = sizeof(filetype); /* length of above */
532 WCHAR command[1024]; /* command from registry */
533 WCHAR wBuffer[256]; /* Used to GetProfileString */
534 UINT retval = SE_ERR_NOASSOC;
535 WCHAR *tok; /* token pointer */
536 WCHAR xlpFile[256]; /* result of SearchPath */
537 DWORD attribs; /* file attributes */
539 TRACE("%s\n", debugstr_w(lpFile));
542 return ERROR_INVALID_PARAMETER;
545 lpResult[0] = '\0'; /* Start off with an empty return string */
546 if (key) *key = '\0';
548 /* trap NULL parameters on entry */
551 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
552 debugstr_w(lpFile), debugstr_w(lpResult));
553 return ERROR_FILE_NOT_FOUND; /* File not found. Close enough, I guess. */
556 if (SHELL_TryAppPathW( lpFile, lpResult, env ))
558 TRACE("found %s via App Paths\n", debugstr_w(lpResult));
562 if (SearchPathW(lpPath, lpFile, wszExe, sizeof(xlpFile)/sizeof(WCHAR), xlpFile, NULL))
564 TRACE("SearchPathW returned non-zero\n");
566 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
569 attribs = GetFileAttributesW(lpFile);
570 if (attribs!=INVALID_FILE_ATTRIBUTES && (attribs&FILE_ATTRIBUTE_DIRECTORY))
572 strcpyW(filetype, wszFolder);
573 filetypelen = 6; /* strlen("Folder") */
577 /* First thing we need is the file's extension */
578 extension = strrchrW(xlpFile, '.'); /* Assume last "." is the one; */
579 /* File->Run in progman uses */
581 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile), debugstr_w(extension));
583 if (extension == NULL || extension[1]==0)
585 WARN("Returning SE_ERR_NOASSOC\n");
586 return SE_ERR_NOASSOC;
589 /* Three places to check: */
590 /* 1. win.ini, [windows], programs (NB no leading '.') */
591 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
592 /* 3. win.ini, [extensions], extension (NB no leading '.' */
593 /* All I know of the order is that registry is checked before */
594 /* extensions; however, it'd make sense to check the programs */
595 /* section first, so that's what happens here. */
597 /* See if it's a program - if GetProfileString fails, we skip this
598 * section. Actually, if GetProfileString fails, we've probably
599 * got a lot more to worry about than running a program... */
600 if (GetProfileStringW(wWindows, wPrograms, wExtensions, wBuffer, sizeof(wBuffer)/sizeof(WCHAR)) > 0)
607 while (*p && *p != ' ' && *p != '\t') p++;
611 while (*p == ' ' || *p == '\t') p++;
614 if (strcmpiW(tok, &extension[1]) == 0) /* have to skip the leading "." */
616 strcpyW(lpResult, xlpFile);
617 /* Need to perhaps check that the file has a path
619 TRACE("found %s\n", debugstr_w(lpResult));
622 /* Greater than 32 to indicate success FIXME According to the
623 * docs, I should be returning a handle for the
624 * executable. Does this mean I'm supposed to open the
625 * executable file or something? More RTFM, I guess... */
632 if (RegQueryValueW(HKEY_CLASSES_ROOT, extension, filetype,
633 &filetypelen) == ERROR_SUCCESS)
635 filetypelen /= sizeof(WCHAR);
636 if (filetypelen == sizeof(filetype)/sizeof(WCHAR))
638 filetype[filetypelen] = '\0';
639 TRACE("File type: %s\n", debugstr_w(filetype));
650 /* pass the operation string to SHELL_FindExecutableByOperation() */
651 filetype[filetypelen] = '\0';
652 retval = SHELL_FindExecutableByOperation(lpOperation, key, filetype, command, sizeof(command));
657 SHELL_ArgifyW(lpResult, resultLen, command, xlpFile, pidl, args, &finishedLen);
658 if (finishedLen > resultLen)
659 ERR("Argify buffer not large enough.. truncated\n");
661 /* Remove double quotation marks and command line arguments */
662 if (*lpResult == '"')
665 while (*(p + 1) != '"')
674 /* Truncate on first space, like Windows:
675 * http://support.microsoft.com/?scid=kb%3Ben-us%3B140724
678 while (*p != ' ' && *p != '\0')
684 else /* Check win.ini */
686 static const WCHAR wExtensions[] = {'e','x','t','e','n','s','i','o','n','s',0};
688 /* Toss the leading dot */
690 if (GetProfileStringW(wExtensions, extension, wszEmpty, command, sizeof(command)/sizeof(WCHAR)) > 0)
692 if (strlenW(command) != 0)
694 strcpyW(lpResult, command);
695 tok = strchrW(lpResult, '^'); /* should be ^.extension? */
699 strcatW(lpResult, xlpFile); /* what if no dir in xlpFile? */
700 tok = strchrW(command, '^'); /* see above */
701 if ((tok != NULL) && (strlenW(tok)>5))
703 strcatW(lpResult, &tok[5]);
706 retval = 33; /* FIXME - see above */
711 TRACE("returning %s\n", debugstr_w(lpResult));
715 /******************************************************************
718 * callback for the DDE connection. not really useful
720 static HDDEDATA CALLBACK dde_cb(UINT uType, UINT uFmt, HCONV hConv,
721 HSZ hsz1, HSZ hsz2, HDDEDATA hData,
722 ULONG_PTR dwData1, ULONG_PTR dwData2)
724 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
725 uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
729 /******************************************************************
732 * ShellExecute helper. Used to do an operation with a DDE connection
734 * Handles both the direct connection (try #1), and if it fails,
735 * launching an application and trying (#2) to connect to it
738 static unsigned dde_connect(WCHAR* key, const WCHAR* start, WCHAR* ddeexec,
739 const WCHAR* lpFile, WCHAR *env,
740 LPCWSTR szCommandline, LPITEMIDLIST pidl, SHELL_ExecuteW32 execfunc,
741 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
743 static const WCHAR wApplication[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
744 static const WCHAR wTopic[] = {'\\','t','o','p','i','c',0};
745 WCHAR * endkey = key + strlenW(key);
746 WCHAR app[256], topic[256], ifexec[256], res[256];
747 LONG applen, topiclen, ifexeclen;
752 HSZ hszApp, hszTopic;
755 unsigned ret = SE_ERR_NOASSOC;
756 BOOL unicode = !(GetVersion() & 0x80000000);
758 strcpyW(endkey, wApplication);
759 applen = sizeof(app);
760 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, app, &applen) != ERROR_SUCCESS)
762 WCHAR command[1024], fullpath[MAX_PATH];
763 static const WCHAR wSo[] = { '.','s','o',0 };
764 int sizeSo = sizeof(wSo)/sizeof(WCHAR);
768 /* Get application command from start string and find filename of application */
771 strcpyW(command, start+1);
772 if ((ptr = strchrW(command, '"')))
774 ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
779 for (p=(LPWSTR)start; (space=strchrW(p, ' ')); p=space+1)
781 int idx = space-start;
782 memcpy(command, start, idx*sizeof(WCHAR));
784 if ((ret = SearchPathW(NULL, command, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr)))
788 ret = SearchPathW(NULL, start, wszExe, sizeof(fullpath)/sizeof(WCHAR), fullpath, &ptr);
793 ERR("Unable to find application path for command %s\n", debugstr_w(start));
794 return ERROR_ACCESS_DENIED;
798 /* Remove extensions (including .so) */
799 ptr = app + strlenW(app) - (sizeSo-1);
800 if (strlenW(app) >= sizeSo &&
804 ptr = strrchrW(app, '.');
809 strcpyW(endkey, wTopic);
810 topiclen = sizeof(topic);
811 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, topic, &topiclen) != ERROR_SUCCESS)
813 static const WCHAR wSystem[] = {'S','y','s','t','e','m',0};
814 strcpyW(topic, wSystem);
819 if (DdeInitializeW(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
824 if (DdeInitializeA(&ddeInst, dde_cb, APPCMD_CLIENTONLY, 0L) != DMLERR_NO_ERROR)
828 hszApp = DdeCreateStringHandleW(ddeInst, app, CP_WINUNICODE);
829 hszTopic = DdeCreateStringHandleW(ddeInst, topic, CP_WINUNICODE);
831 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
835 static const WCHAR wIfexec[] = {'\\','i','f','e','x','e','c',0};
836 TRACE("Launching %s\n", debugstr_w(start));
837 ret = execfunc(start, env, TRUE, psei, psei_out);
840 TRACE("Couldn't launch\n");
843 hConv = DdeConnect(ddeInst, hszApp, hszTopic, NULL);
846 TRACE("Couldn't connect. ret=%d\n", ret);
847 DdeUninitialize(ddeInst);
848 SetLastError(ERROR_DDE_FAIL);
849 return 30; /* whatever */
851 strcpyW(endkey, wIfexec);
852 ifexeclen = sizeof(ifexec);
853 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ifexec, &ifexeclen) == ERROR_SUCCESS)
859 SHELL_ArgifyW(res, sizeof(res)/sizeof(WCHAR), exec, lpFile, pidl, szCommandline, &resultLen);
860 if (resultLen > sizeof(res)/sizeof(WCHAR))
861 ERR("Argify buffer not large enough, truncated\n");
862 TRACE("%s %s => %s\n", debugstr_w(exec), debugstr_w(lpFile), debugstr_w(res));
864 /* It's documented in the KB 330337 that IE has a bug and returns
865 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
868 hDdeData = DdeClientTransaction((LPBYTE)res, (strlenW(res) + 1) * sizeof(WCHAR), hConv, 0L, 0,
869 XTYP_EXECUTE, 30000, &tid);
872 DWORD lenA = WideCharToMultiByte(CP_ACP, 0, res, -1, NULL, 0, NULL, NULL);
873 char *resA = HeapAlloc(GetProcessHeap(), 0, lenA);
874 WideCharToMultiByte(CP_ACP, 0, res, -1, resA, lenA, NULL, NULL);
875 hDdeData = DdeClientTransaction( (LPBYTE)resA, lenA, hConv, 0L, 0,
876 XTYP_EXECUTE, 10000, &tid );
877 HeapFree(GetProcessHeap(), 0, resA);
880 DdeFreeDataHandle(hDdeData);
882 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst));
885 DdeDisconnect(hConv);
888 DdeUninitialize(ddeInst);
893 /*************************************************************************
894 * execute_from_key [Internal]
896 static UINT_PTR execute_from_key(LPWSTR key, LPCWSTR lpFile, WCHAR *env, LPCWSTR szCommandline,
897 LPCWSTR executable_name,
898 SHELL_ExecuteW32 execfunc,
899 LPSHELLEXECUTEINFOW psei, LPSHELLEXECUTEINFOW psei_out)
901 static const WCHAR wCommand[] = {'c','o','m','m','a','n','d',0};
902 static const WCHAR wDdeexec[] = {'d','d','e','e','x','e','c',0};
903 WCHAR cmd[256], param[1024], ddeexec[256];
904 LONG cmdlen = sizeof(cmd), ddeexeclen = sizeof(ddeexec);
905 UINT_PTR retval = SE_ERR_NOASSOC;
909 TRACE("%s %s %s %s %s\n", debugstr_w(key), debugstr_w(lpFile), debugstr_w(env),
910 debugstr_w(szCommandline), debugstr_w(executable_name));
915 /* Get the application from the registry */
916 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, cmd, &cmdlen) == ERROR_SUCCESS)
918 TRACE("got cmd: %s\n", debugstr_w(cmd));
920 /* Is there a replace() function anywhere? */
921 cmdlen /= sizeof(WCHAR);
923 SHELL_ArgifyW(param, sizeof(param)/sizeof(WCHAR), cmd, lpFile, psei->lpIDList, szCommandline, &resultLen);
924 if (resultLen > sizeof(param)/sizeof(WCHAR))
925 ERR("Argify buffer not large enough, truncating\n");
928 /* Get the parameters needed by the application
929 from the associated ddeexec key */
930 tmp = strstrW(key, wCommand);
932 strcpyW(tmp, wDdeexec);
934 if (RegQueryValueW(HKEY_CLASSES_ROOT, key, ddeexec, &ddeexeclen) == ERROR_SUCCESS)
936 TRACE("Got ddeexec %s => %s\n", debugstr_w(key), debugstr_w(ddeexec));
937 if (!param[0]) strcpyW(param, executable_name);
938 retval = dde_connect(key, param, ddeexec, lpFile, env, szCommandline, psei->lpIDList, execfunc, psei, psei_out);
942 TRACE("executing: %s\n", debugstr_w(param));
943 retval = execfunc(param, env, FALSE, psei, psei_out);
946 WARN("Nothing appropriate found for %s\n", debugstr_w(key));
951 /*************************************************************************
952 * FindExecutableA [SHELL32.@]
954 HINSTANCE WINAPI FindExecutableA(LPCSTR lpFile, LPCSTR lpDirectory, LPSTR lpResult)
957 WCHAR *wFile = NULL, *wDirectory = NULL;
958 WCHAR wResult[MAX_PATH];
960 if (lpFile) __SHCloneStrAtoW(&wFile, lpFile);
961 if (lpDirectory) __SHCloneStrAtoW(&wDirectory, lpDirectory);
963 retval = FindExecutableW(wFile, wDirectory, wResult);
964 WideCharToMultiByte(CP_ACP, 0, wResult, -1, lpResult, MAX_PATH, NULL, NULL);
966 SHFree( wDirectory );
968 TRACE("returning %s\n", lpResult);
972 /*************************************************************************
973 * FindExecutableW [SHELL32.@]
975 * This function returns the executable associated with the specified file
976 * for the default verb.
979 * lpFile [I] The file to find the association for. This must refer to
980 * an existing file otherwise FindExecutable fails and returns
982 * lpResult [O] Points to a buffer into which the executable path is
983 * copied. This parameter must not be NULL otherwise
984 * FindExecutable() segfaults. The buffer must be of size at
985 * least MAX_PATH characters.
988 * A value greater than 32 on success, less than or equal to 32 otherwise.
989 * See the SE_ERR_* constants.
992 * On Windows XP and 2003, FindExecutable() seems to first convert the
993 * filename into 8.3 format, thus taking into account only the first three
994 * characters of the extension, and expects to find an association for those.
995 * However other Windows versions behave sanely.
997 HINSTANCE WINAPI FindExecutableW(LPCWSTR lpFile, LPCWSTR lpDirectory, LPWSTR lpResult)
999 UINT_PTR retval = SE_ERR_NOASSOC;
1000 WCHAR old_dir[1024];
1002 TRACE("File %s, Dir %s\n", debugstr_w(lpFile), debugstr_w(lpDirectory));
1004 lpResult[0] = '\0'; /* Start off with an empty return string */
1006 return (HINSTANCE)SE_ERR_FNF;
1010 GetCurrentDirectoryW(sizeof(old_dir)/sizeof(WCHAR), old_dir);
1011 SetCurrentDirectoryW(lpDirectory);
1014 retval = SHELL_FindExecutable(lpDirectory, lpFile, wszOpen, lpResult, MAX_PATH, NULL, NULL, NULL, NULL);
1016 TRACE("returning %s\n", debugstr_w(lpResult));
1018 SetCurrentDirectoryW(old_dir);
1019 return (HINSTANCE)retval;
1022 /* FIXME: is this already implemented somewhere else? */
1023 static HKEY ShellExecute_GetClassKey( LPSHELLEXECUTEINFOW sei )
1025 LPCWSTR ext = NULL, lpClass = NULL;
1027 DWORD type = 0, sz = 0;
1031 if (sei->fMask & SEE_MASK_CLASSALL)
1032 return sei->hkeyClass;
1034 if (sei->fMask & SEE_MASK_CLASSNAME)
1035 lpClass = sei->lpClass;
1038 ext = PathFindExtensionW( sei->lpFile );
1039 TRACE("ext = %s\n", debugstr_w( ext ) );
1043 r = RegOpenKeyW( HKEY_CLASSES_ROOT, ext, &hkey );
1044 if (r != ERROR_SUCCESS )
1047 r = RegQueryValueExW( hkey, NULL, 0, &type, NULL, &sz );
1048 if ( r == ERROR_SUCCESS && type == REG_SZ )
1050 sz += sizeof (WCHAR);
1051 cls = HeapAlloc( GetProcessHeap(), 0, sz );
1053 RegQueryValueExW( hkey, NULL, 0, &type, (LPBYTE) cls, &sz );
1056 RegCloseKey( hkey );
1060 TRACE("class = %s\n", debugstr_w(lpClass) );
1064 RegOpenKeyW( HKEY_CLASSES_ROOT, lpClass, &hkey );
1066 HeapFree( GetProcessHeap(), 0, cls );
1071 static IDataObject *shellex_get_dataobj( LPSHELLEXECUTEINFOW sei )
1073 LPCITEMIDLIST pidllast = NULL;
1074 IDataObject *dataobj = NULL;
1075 IShellFolder *shf = NULL;
1076 LPITEMIDLIST pidl = NULL;
1079 if (sei->fMask & SEE_MASK_CLASSALL)
1080 pidl = sei->lpIDList;
1083 WCHAR fullpath[MAX_PATH];
1086 r = GetFullPathNameW( sei->lpFile, MAX_PATH, fullpath, NULL );
1090 pidl = ILCreateFromPathW( fullpath );
1093 r = SHBindToParent( pidl, &IID_IShellFolder, (LPVOID*)&shf, &pidllast );
1097 IShellFolder_GetUIObjectOf( shf, NULL, 1, &pidllast,
1098 &IID_IDataObject, NULL, (LPVOID*) &dataobj );
1101 if ( pidl != sei->lpIDList )
1104 IShellFolder_Release( shf );
1108 static HRESULT shellex_run_context_menu_default( IShellExtInit *obj,
1109 LPSHELLEXECUTEINFOW sei )
1111 IContextMenu *cm = NULL;
1112 CMINVOKECOMMANDINFOEX ici;
1119 TRACE("%p %p\n", obj, sei );
1121 r = IShellExtInit_QueryInterface( obj, &IID_IContextMenu, (LPVOID*) &cm );
1125 hmenu = CreateMenu();
1129 /* the number of the last menu added is returned in r */
1130 r = IContextMenu_QueryContextMenu( cm, hmenu, 0, 0x20, 0x7fff, CMF_DEFAULTONLY );
1134 n = GetMenuItemCount( hmenu );
1135 for ( i = 0; i < n; i++ )
1137 memset( &info, 0, sizeof info );
1138 info.cbSize = sizeof info;
1139 info.fMask = MIIM_FTYPE | MIIM_STRING | MIIM_STATE | MIIM_DATA | MIIM_ID;
1140 info.dwTypeData = string;
1141 info.cch = sizeof string;
1143 GetMenuItemInfoW( hmenu, i, TRUE, &info );
1145 TRACE("menu %d %s %08x %08lx %08x %08x\n", i, debugstr_w(string),
1146 info.fState, info.dwItemData, info.fType, info.wID );
1147 if ( ( !sei->lpVerb && (info.fState & MFS_DEFAULT) ) ||
1148 ( sei->lpVerb && !lstrcmpiW( sei->lpVerb, string ) ) )
1159 memset( &ici, 0, sizeof ici );
1160 ici.cbSize = sizeof ici;
1161 ici.fMask = CMIC_MASK_UNICODE;
1162 ici.nShow = sei->nShow;
1163 ici.lpVerb = MAKEINTRESOURCEA( def );
1164 ici.hwnd = sei->hwnd;
1165 ici.lpParametersW = sei->lpParameters;
1167 r = IContextMenu_InvokeCommand( cm, (LPCMINVOKECOMMANDINFO) &ici );
1169 TRACE("invoke command returned %08x\n", r );
1173 DestroyMenu( hmenu );
1175 IContextMenu_Release( cm );
1179 static HRESULT shellex_load_object_and_run( HKEY hkey, LPCGUID guid, LPSHELLEXECUTEINFOW sei )
1181 IDataObject *dataobj = NULL;
1182 IObjectWithSite *ows = NULL;
1183 IShellExtInit *obj = NULL;
1186 TRACE("%p %s %p\n", hkey, debugstr_guid( guid ), sei );
1188 r = CoInitialize( NULL );
1192 r = CoCreateInstance( guid, NULL, CLSCTX_INPROC_SERVER,
1193 &IID_IShellExtInit, (LPVOID*)&obj );
1196 ERR("failed %08x\n", r );
1200 dataobj = shellex_get_dataobj( sei );
1203 ERR("failed to get data object\n");
1207 r = IShellExtInit_Initialize( obj, NULL, dataobj, hkey );
1211 r = IShellExtInit_QueryInterface( obj, &IID_IObjectWithSite, (LPVOID*) &ows );
1215 IObjectWithSite_SetSite( ows, NULL );
1217 r = shellex_run_context_menu_default( obj, sei );
1221 IObjectWithSite_Release( ows );
1223 IDataObject_Release( dataobj );
1225 IShellExtInit_Release( obj );
1231 /*************************************************************************
1232 * ShellExecute_FromContextMenu [Internal]
1234 static LONG ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei )
1236 static const WCHAR szcm[] = { 's','h','e','l','l','e','x','\\',
1237 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1238 HKEY hkey, hkeycm = 0;
1245 TRACE("%s\n", debugstr_w(sei->lpFile) );
1247 hkey = ShellExecute_GetClassKey( sei );
1249 return ERROR_FUNCTION_FAILED;
1251 r = RegOpenKeyW( hkey, szcm, &hkeycm );
1252 if ( r == ERROR_SUCCESS )
1257 r = RegEnumKeyW( hkeycm, i++, szguid, 39 );
1258 if ( r != ERROR_SUCCESS )
1261 hr = CLSIDFromString( szguid, &guid );
1264 /* stop at the first one that succeeds in running */
1265 hr = shellex_load_object_and_run( hkey, &guid, sei );
1266 if ( SUCCEEDED( hr ) )
1270 RegCloseKey( hkeycm );
1273 if ( hkey != sei->hkeyClass )
1274 RegCloseKey( hkey );
1278 /*************************************************************************
1279 * SHELL_execute [Internal]
1281 BOOL SHELL_execute( LPSHELLEXECUTEINFOW sei, SHELL_ExecuteW32 execfunc )
1283 static const WCHAR wQuote[] = {'"',0};
1284 static const WCHAR wSpace[] = {' ',0};
1285 static const WCHAR wWww[] = {'w','w','w',0};
1286 static const WCHAR wFile[] = {'f','i','l','e',0};
1287 static const WCHAR wHttp[] = {'h','t','t','p',':','/','/',0};
1288 static const WCHAR wExplorer[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1289 static const DWORD unsupportedFlags =
1290 SEE_MASK_INVOKEIDLIST | SEE_MASK_ICON | SEE_MASK_HOTKEY |
1291 SEE_MASK_CONNECTNETDRV | SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI |
1292 SEE_MASK_UNICODE | SEE_MASK_NO_CONSOLE | SEE_MASK_ASYNCOK |
1295 WCHAR *wszApplicationName, wszParameters[1024], wszDir[MAX_PATH];
1296 DWORD dwApplicationNameLen = MAX_PATH+2;
1298 SHELLEXECUTEINFOW sei_tmp; /* modifiable copy of SHELLEXECUTEINFO struct */
1299 WCHAR wfileName[MAX_PATH];
1301 WCHAR lpstrProtocol[256];
1303 UINT_PTR retval = SE_ERR_NOASSOC;
1305 WCHAR buffer[MAX_PATH];
1307 BOOL appKnownSingular = FALSE;
1309 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1310 memcpy(&sei_tmp, sei, sizeof(sei_tmp));
1312 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1313 sei_tmp.fMask, sei_tmp.hwnd, debugstr_w(sei_tmp.lpVerb),
1314 debugstr_w(sei_tmp.lpFile), debugstr_w(sei_tmp.lpParameters),
1315 debugstr_w(sei_tmp.lpDirectory), sei_tmp.nShow,
1316 ((sei_tmp.fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME) ?
1317 debugstr_w(sei_tmp.lpClass) : "not used");
1319 sei->hProcess = NULL;
1321 /* make copies of all path/command strings */
1322 if (!sei_tmp.lpFile)
1324 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1325 *wszApplicationName = '\0';
1327 else if (*sei_tmp.lpFile == '\"')
1329 DWORD l = strlenW(sei_tmp.lpFile+1);
1330 if(l >= dwApplicationNameLen) dwApplicationNameLen = l+1;
1331 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1332 memcpy(wszApplicationName, sei_tmp.lpFile+1, (l+1)*sizeof(WCHAR));
1333 if (wszApplicationName[l-1] == '\"')
1334 wszApplicationName[l-1] = '\0';
1335 appKnownSingular = TRUE;
1336 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName));
1338 DWORD l = strlenW(sei_tmp.lpFile)+1;
1339 if(l > dwApplicationNameLen) dwApplicationNameLen = l+1;
1340 wszApplicationName = HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen*sizeof(WCHAR));
1341 memcpy(wszApplicationName, sei_tmp.lpFile, l*sizeof(WCHAR));
1344 if (sei_tmp.lpParameters)
1345 strcpyW(wszParameters, sei_tmp.lpParameters);
1347 *wszParameters = '\0';
1349 if (sei_tmp.lpDirectory)
1350 strcpyW(wszDir, sei_tmp.lpDirectory);
1354 /* adjust string pointers to point to the new buffers */
1355 sei_tmp.lpFile = wszApplicationName;
1356 sei_tmp.lpParameters = wszParameters;
1357 sei_tmp.lpDirectory = wszDir;
1359 if (sei_tmp.fMask & unsupportedFlags)
1361 FIXME("flags ignored: 0x%08x\n", sei_tmp.fMask & unsupportedFlags);
1364 /* process the IDList */
1365 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1367 IShellExecuteHookW* pSEH;
1369 HRESULT hr = SHBindToParent(sei_tmp.lpIDList, &IID_IShellExecuteHookW, (LPVOID*)&pSEH, NULL);
1373 hr = IShellExecuteHookW_Execute(pSEH, &sei_tmp);
1375 IShellExecuteHookW_Release(pSEH);
1378 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1383 SHGetPathFromIDListW(sei_tmp.lpIDList, wszApplicationName);
1384 appKnownSingular = TRUE;
1385 TRACE("-- idlist=%p (%s)\n", sei_tmp.lpIDList, debugstr_w(wszApplicationName));
1388 if ( ERROR_SUCCESS == ShellExecute_FromContextMenu( &sei_tmp ) )
1390 sei->hInstApp = (HINSTANCE) 33;
1391 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1395 if (sei_tmp.fMask & SEE_MASK_CLASSALL)
1397 /* launch a document by fileclass like 'WordPad.Document.1' */
1398 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1399 /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1400 ULONG cmask=(sei_tmp.fMask & SEE_MASK_CLASSALL);
1402 HCR_GetExecuteCommandW((cmask == SEE_MASK_CLASSKEY) ? sei_tmp.hkeyClass : NULL,
1403 (cmask == SEE_MASK_CLASSNAME) ? sei_tmp.lpClass: NULL,
1405 wszParameters, sizeof(wszParameters)/sizeof(WCHAR));
1407 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1408 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(wszParameters), debugstr_w(wszApplicationName));
1411 done = SHELL_ArgifyW(wcmd, sizeof(wcmd)/sizeof(WCHAR), wszParameters, wszApplicationName, sei_tmp.lpIDList, NULL, &resultLen);
1412 if (!done && wszApplicationName[0])
1414 strcatW(wcmd, wSpace);
1415 strcatW(wcmd, wszApplicationName);
1417 if (resultLen > sizeof(wcmd)/sizeof(WCHAR))
1418 ERR("Argify buffer not large enough... truncating\n");
1419 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1421 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1425 /* Has the IDList not yet been translated? */
1426 if (sei_tmp.fMask & SEE_MASK_IDLIST)
1428 /* last chance to translate IDList: now also allow CLSID paths */
1429 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp.lpIDList, buffer, sizeof(buffer)))) {
1430 if (buffer[0]==':' && buffer[1]==':') {
1431 /* open shell folder for the specified class GUID */
1432 strcpyW(wszParameters, buffer);
1433 strcpyW(wszApplicationName, wExplorer);
1434 appKnownSingular = TRUE;
1436 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1438 WCHAR target[MAX_PATH];
1441 /* Check if we're executing a directory and if so use the
1442 handler for the Folder class */
1443 strcpyW(target, buffer);
1444 attribs = GetFileAttributesW(buffer);
1445 if (attribs != INVALID_FILE_ATTRIBUTES &&
1446 (attribs & FILE_ATTRIBUTE_DIRECTORY) &&
1447 HCR_GetExecuteCommandW(0, wszFolder,
1449 buffer, sizeof(buffer))) {
1450 SHELL_ArgifyW(wszApplicationName, dwApplicationNameLen,
1451 buffer, target, sei_tmp.lpIDList, NULL, &resultLen);
1452 if (resultLen > dwApplicationNameLen)
1453 ERR("Argify buffer not large enough... truncating\n");
1454 appKnownSingular = FALSE;
1456 sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1461 /* expand environment strings */
1462 len = ExpandEnvironmentStringsW(sei_tmp.lpFile, NULL, 0);
1466 buf = HeapAlloc(GetProcessHeap(),0,(len+1)*sizeof(WCHAR));
1468 ExpandEnvironmentStringsW(sei_tmp.lpFile, buf, len+1);
1469 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1470 dwApplicationNameLen = len+1;
1471 wszApplicationName = buf;
1472 /* appKnownSingular unmodified */
1474 sei_tmp.lpFile = wszApplicationName;
1477 if (*sei_tmp.lpParameters)
1479 len = ExpandEnvironmentStringsW(sei_tmp.lpParameters, NULL, 0);
1484 buf = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1485 ExpandEnvironmentStringsW(sei_tmp.lpParameters, buf, len);
1487 ERR("Parameters exceeds buffer size (%i > 1024)\n",len);
1488 lstrcpynW(wszParameters, buf, min(1024,len));
1489 HeapFree(GetProcessHeap(),0,buf);
1493 if (*sei_tmp.lpDirectory)
1495 len = ExpandEnvironmentStringsW(sei_tmp.lpDirectory, NULL, 0);
1500 buf = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
1501 ExpandEnvironmentStringsW(sei_tmp.lpDirectory, buf, len);
1503 ERR("Directory exceeds buffer size (%i > 1024)\n",len);
1504 lstrcpynW(wszDir, buf, min(1024,len));
1505 HeapFree(GetProcessHeap(),0,buf);
1509 /* Else, try to execute the filename */
1510 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName), debugstr_w(wszParameters), debugstr_w(wszDir));
1512 /* separate out command line arguments from executable file name */
1513 if (!*sei_tmp.lpParameters && !appKnownSingular) {
1514 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1515 if (sei_tmp.lpFile[0] == '"') {
1516 LPWSTR src = wszApplicationName/*sei_tmp.lpFile*/ + 1;
1517 LPWSTR dst = wfileName;
1520 /* copy the unquoted executable path to 'wfileName' */
1521 while(*src && *src!='"')
1529 while(isspace(*src))
1534 /* copy the parameter string to 'wszParameters' */
1535 strcpyW(wszParameters, src);
1537 /* terminate previous command string after the quote character */
1542 /* If the executable name is not quoted, we have to use this search loop here,
1543 that in CreateProcess() is not sufficient because it does not handle shell links. */
1544 WCHAR buffer[MAX_PATH], xlpFile[MAX_PATH];
1547 LPWSTR beg = wszApplicationName/*sei_tmp.lpFile*/;
1548 for(s=beg; (space=strchrW(s, ' ')); s=space+1) {
1549 int idx = space-sei_tmp.lpFile;
1550 memcpy(buffer, sei_tmp.lpFile, idx * sizeof(WCHAR));
1553 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1554 if (SearchPathW(*sei_tmp.lpDirectory? sei_tmp.lpDirectory: NULL, buffer, wszExe, sizeof(xlpFile), xlpFile, NULL))
1556 /* separate out command from parameter string */
1557 LPCWSTR p = space + 1;
1562 strcpyW(wszParameters, p);
1569 strcpyW(wfileName, sei_tmp.lpFile);
1572 strcpyW(wfileName, sei_tmp.lpFile);
1576 strcpyW(wcmd, wszApplicationName);
1577 if (sei_tmp.lpParameters[0]) {
1578 strcatW(wcmd, wSpace);
1579 strcatW(wcmd, wszParameters);
1582 retval = execfunc(wcmd, NULL, FALSE, &sei_tmp, sei);
1584 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1588 /* Else, try to find the executable */
1590 retval = SHELL_FindExecutable(sei_tmp.lpDirectory, lpFile, sei_tmp.lpVerb, wcmd, 1024, lpstrProtocol, &env, sei_tmp.lpIDList, sei_tmp.lpParameters);
1591 if (retval > 32) /* Found */
1593 WCHAR wszQuotedCmd[MAX_PATH+2];
1594 /* Must quote to handle case where cmd contains spaces,
1595 * else security hole if malicious user creates executable file "C:\\Program"
1597 strcpyW(wszQuotedCmd, wQuote);
1598 strcatW(wszQuotedCmd, wcmd);
1599 strcatW(wszQuotedCmd, wQuote);
1600 if (wszParameters[0]) {
1601 strcatW(wszQuotedCmd, wSpace);
1602 strcatW(wszQuotedCmd, wszParameters);
1604 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName), debugstr_w(sei_tmp.lpVerb), debugstr_w(wszQuotedCmd), debugstr_w(lpstrProtocol));
1606 retval = execute_from_key(lpstrProtocol, wszApplicationName, env, sei_tmp.lpParameters, wcmd, execfunc, &sei_tmp, sei);
1608 retval = execfunc(wszQuotedCmd, env, FALSE, &sei_tmp, sei);
1609 HeapFree( GetProcessHeap(), 0, env );
1611 else if (PathIsURLW(lpFile)) /* File not found, check for URL */
1613 static const WCHAR wShell[] = {'\\','s','h','e','l','l','\\',0};
1614 static const WCHAR wCommand[] = {'\\','c','o','m','m','a','n','d',0};
1618 lpstrRes = strchrW(lpFile, ':');
1620 iSize = lpstrRes - lpFile;
1622 iSize = strlenW(lpFile);
1624 TRACE("Got URL: %s\n", debugstr_w(lpFile));
1625 /* Looking for ...protocol\shell\lpOperation\command */
1626 memcpy(lpstrProtocol, lpFile, iSize*sizeof(WCHAR));
1627 lpstrProtocol[iSize] = '\0';
1628 strcatW(lpstrProtocol, wShell);
1629 strcatW(lpstrProtocol, sei_tmp.lpVerb? sei_tmp.lpVerb: wszOpen);
1630 strcatW(lpstrProtocol, wCommand);
1632 /* Remove File Protocol from lpFile */
1633 /* In the case file://path/file */
1634 if (!strncmpiW(lpFile, wFile, iSize))
1637 while (*lpFile == ':') lpFile++;
1639 retval = execute_from_key(lpstrProtocol, lpFile, NULL, sei_tmp.lpParameters, wcmd, execfunc, &sei_tmp, sei);
1641 /* Check if file specified is in the form www.??????.*** */
1642 else if (!strncmpiW(lpFile, wWww, 3))
1644 /* if so, append lpFile http:// and call ShellExecute */
1645 WCHAR lpstrTmpFile[256];
1646 strcpyW(lpstrTmpFile, wHttp);
1647 strcatW(lpstrTmpFile, lpFile);
1648 retval = (UINT_PTR)ShellExecuteW(sei_tmp.hwnd, sei_tmp.lpVerb, lpstrTmpFile, NULL, NULL, 0);
1651 TRACE("retval %lu\n", retval);
1653 HeapFree(GetProcessHeap(), 0, wszApplicationName);
1655 sei->hInstApp = (HINSTANCE)(retval > 32 ? 33 : retval);
1659 /*************************************************************************
1660 * ShellExecuteA [SHELL32.290]
1662 HINSTANCE WINAPI ShellExecuteA(HWND hWnd, LPCSTR lpOperation,LPCSTR lpFile,
1663 LPCSTR lpParameters,LPCSTR lpDirectory, INT iShowCmd)
1665 SHELLEXECUTEINFOA sei;
1667 TRACE("%p,%s,%s,%s,%s,%d\n",
1668 hWnd, debugstr_a(lpOperation), debugstr_a(lpFile),
1669 debugstr_a(lpParameters), debugstr_a(lpDirectory), iShowCmd);
1671 sei.cbSize = sizeof(sei);
1674 sei.lpVerb = lpOperation;
1675 sei.lpFile = lpFile;
1676 sei.lpParameters = lpParameters;
1677 sei.lpDirectory = lpDirectory;
1678 sei.nShow = iShowCmd;
1685 ShellExecuteExA (&sei);
1686 return sei.hInstApp;
1689 /*************************************************************************
1690 * ShellExecuteExA [SHELL32.292]
1693 BOOL WINAPI ShellExecuteExA (LPSHELLEXECUTEINFOA sei)
1695 SHELLEXECUTEINFOW seiW;
1697 WCHAR *wVerb = NULL, *wFile = NULL, *wParameters = NULL, *wDirectory = NULL, *wClass = NULL;
1701 memcpy(&seiW, sei, sizeof(SHELLEXECUTEINFOW));
1704 seiW.lpVerb = __SHCloneStrAtoW(&wVerb, sei->lpVerb);
1707 seiW.lpFile = __SHCloneStrAtoW(&wFile, sei->lpFile);
1709 if (sei->lpParameters)
1710 seiW.lpParameters = __SHCloneStrAtoW(&wParameters, sei->lpParameters);
1712 if (sei->lpDirectory)
1713 seiW.lpDirectory = __SHCloneStrAtoW(&wDirectory, sei->lpDirectory);
1715 if ((sei->fMask & SEE_MASK_CLASSALL) == SEE_MASK_CLASSNAME && sei->lpClass)
1716 seiW.lpClass = __SHCloneStrAtoW(&wClass, sei->lpClass);
1718 seiW.lpClass = NULL;
1720 ret = SHELL_execute( &seiW, SHELL_ExecuteW );
1722 sei->hInstApp = seiW.hInstApp;
1724 if (sei->fMask & SEE_MASK_NOCLOSEPROCESS)
1725 sei->hProcess = seiW.hProcess;
1729 SHFree(wParameters);
1736 /*************************************************************************
1737 * ShellExecuteExW [SHELL32.293]
1740 BOOL WINAPI ShellExecuteExW (LPSHELLEXECUTEINFOW sei)
1742 return SHELL_execute( sei, SHELL_ExecuteW );
1745 /*************************************************************************
1746 * ShellExecuteW [SHELL32.294]
1748 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1749 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1751 HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpOperation, LPCWSTR lpFile,
1752 LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
1754 SHELLEXECUTEINFOW sei;
1757 sei.cbSize = sizeof(sei);
1760 sei.lpVerb = lpOperation;
1761 sei.lpFile = lpFile;
1762 sei.lpParameters = lpParameters;
1763 sei.lpDirectory = lpDirectory;
1764 sei.nShow = nShowCmd;
1771 SHELL_execute( &sei, SHELL_ExecuteW );
1772 return sei.hInstApp;
1775 /*************************************************************************
1776 * OpenAs_RunDLLA [SHELL32.@]
1778 void WINAPI OpenAs_RunDLLA(HWND hwnd, HINSTANCE hinst, LPCSTR cmdline, int cmdshow)
1780 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_a(cmdline), cmdshow);
1783 /*************************************************************************
1784 * OpenAs_RunDLLW [SHELL32.@]
1786 void WINAPI OpenAs_RunDLLW(HWND hwnd, HINSTANCE hinst, LPCWSTR cmdline, int cmdshow)
1788 FIXME("%p, %p, %s, %d\n", hwnd, hinst, debugstr_w(cmdline), cmdshow);