2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2002 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
47 #include "wine/winbase16.h"
48 #include "shell32_main.h"
49 #include "undocshell.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
56 static const WCHAR wszOpen
[] = {'o','p','e','n',0};
57 static const WCHAR wszExe
[] = {'.','e','x','e',0};
58 static const WCHAR wszILPtr
[] = {':','%','p',0};
59 static const WCHAR wszShell
[] = {'\\','s','h','e','l','l','\\',0};
60 static const WCHAR wszFolder
[] = {'F','o','l','d','e','r',0};
61 static const WCHAR wszEmpty
[] = {0};
64 /***********************************************************************
65 * SHELL_ArgifyW [Internal]
67 * this function is supposed to expand the escape sequences found in the registry
68 * some diving reported that the following were used:
69 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
74 * %I address of a global item ID (explorer switch /idlist)
75 * %L seems to be %1 as long filename followed by the 8+3 variation
77 * %* all following parameters (see batfile)
81 static BOOL
SHELL_ArgifyW(WCHAR
* out
, int len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
)
121 while(*args
&& !isspace(*args
))
124 while(isspace(*args
))
131 if (!done
|| (*fmt
== '1'))
133 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
134 if (SearchPathW(NULL
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
139 /* Add double quotation marks unless we already have them (e.g.: "%1" %* for exefile) */
140 if (res
!= out
&& *(res
- 1) == '"')
155 * IE uses this alot for activating things such as windows media
156 * player. This is not verified to be fully correct but it appears
162 strcpyW(res
, lpFile
);
163 res
+= strlenW(lpFile
);
170 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
171 pv
= SHLockShared(hmem
, 0);
172 res
+= sprintfW(res
, wszILPtr
, pv
);
177 default: FIXME("Unknown escape sequence %%%c\n", *fmt
);
192 HRESULT
SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl
, LPSTR pszPath
, UINT uOutSize
)
195 IShellFolder
* desktop
;
197 HRESULT hr
= SHGetDesktopFolder(&desktop
);
200 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
203 StrRetToStrNA(pszPath
, uOutSize
, &strret
, pidl
);
205 IShellFolder_Release(desktop
);
211 HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
214 IShellFolder
* desktop
;
216 HRESULT hr
= SHGetDesktopFolder(&desktop
);
219 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
222 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
224 IShellFolder_Release(desktop
);
230 /*************************************************************************
231 * SHELL_ResolveShortCutW [Internal]
232 * read shortcut file at 'wcmd' and fill psei with its content
234 static HRESULT
SHELL_ResolveShortCutW(LPWSTR wcmd
, LPWSTR wargs
, LPWSTR wdir
, HWND hwnd
, LPCWSTR lpVerb
, int* pshowcmd
, LPITEMIDLIST
* ppidl
)
238 HRESULT hr
= SHGetDesktopFolder(&psf
);
246 hr
= IShellFolder_ParseDisplayName(psf
, 0, 0, wcmd
, &l
, &pidl
, 0);
251 hr
= IShellFolder_GetUIObjectOf(psf
, NULL
, 1, (LPCITEMIDLIST
*)&pidl
, &IID_IShellLinkW
, NULL
, (LPVOID
*)&psl
);
254 hr
= IShellLinkW_Resolve(psl
, hwnd
, 0);
257 hr
= IShellLinkW_GetPath(psl
, wcmd
, MAX_PATH
, NULL
, SLGP_UNCPRIORITY
);
261 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
262 hr
= IShellLinkW_GetIDList(psl
, ppidl
);
264 if (SUCCEEDED(hr
) && *ppidl
) {
265 /* We got a PIDL instead of a file system path - try to translate it. */
266 if (SUCCEEDED(SHELL_GetPathFromIDListW(*ppidl
, wcmd
, MAX_PATH
))) {
274 /* get command line arguments, working directory and display mode if available */
275 IShellLinkW_GetWorkingDirectory(psl
, wdir
, MAX_PATH
);
276 IShellLinkW_GetArguments(psl
, wargs
, MAX_PATH
);
277 IShellLinkW_GetShowCmd(psl
, pshowcmd
);
282 IShellLinkW_Release(psl
);
288 IShellFolder_Release(psf
);
294 /*************************************************************************
295 * SHELL_ExecuteW [Internal]
298 static UINT
SHELL_ExecuteW(const WCHAR
*lpCmd
, void *env
, BOOL shWait
,
299 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
301 STARTUPINFOW startup
;
302 PROCESS_INFORMATION info
;
305 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
307 ZeroMemory(&startup
, sizeof(startup
));
308 startup
.cb
= sizeof(STARTUPINFOW
);
309 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
310 startup
.wShowWindow
= psei
->nShow
;
312 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, 0,
313 env
, *psei
->lpDirectory
? psei
->lpDirectory
: NULL
, &startup
, &info
))
315 /* Give 30 seconds to the app to come up, if desired. Probably only needed
316 when starting app immediately before making a DDE connection. */
318 if (WaitForInputIdle( info
.hProcess
, 30000 ) == -1)
319 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
322 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
323 psei_out
->hProcess
= info
.hProcess
;
325 CloseHandle(info
.hProcess
);
327 CloseHandle(info
.hThread
);
329 else if ((retval
= GetLastError()) >= 32)
331 FIXME("Strange error set by CreateProcess: %d\n", retval
);
332 retval
= ERROR_BAD_FORMAT
;
335 TRACE("returning %u\n", retval
);
337 psei_out
->hInstApp
= (HINSTANCE
)retval
;
342 /***********************************************************************
343 * SHELL_BuildEnvW [Internal]
345 * Build the environment for the new process, adding the specified
346 * path to the PATH variable. Returned pointer must be freed by caller.
348 static void *SHELL_BuildEnvW( const WCHAR
*path
)
350 static const WCHAR wPath
[] = {'P','A','T','H','=',0};
351 WCHAR
*strings
, *new_env
;
353 int total
= strlenW(path
) + 1;
354 BOOL got_path
= FALSE
;
356 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
360 int len
= strlenW(p
) + 1;
361 if (!strncmpiW( p
, wPath
, 5 )) got_path
= TRUE
;
365 if (!got_path
) total
+= 5; /* we need to create PATH */
366 total
++; /* terminating null */
368 if (!(new_env
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) )))
370 FreeEnvironmentStringsW( strings
);
377 int len
= strlenW(p
) + 1;
378 memcpy( p2
, p
, len
* sizeof(WCHAR
) );
379 if (!strncmpiW( p
, wPath
, 5 ))
382 strcpyW( p2
+ len
, path
);
383 p2
+= strlenW(path
) + 1;
390 strcpyW( p2
, wPath
);
392 p2
+= strlenW(p2
) + 1;
395 FreeEnvironmentStringsW( strings
);
400 /***********************************************************************
401 * SHELL_TryAppPathW [Internal]
403 * Helper function for SHELL_FindExecutable
404 * @param lpResult - pointer to a buffer of size MAX_PATH
405 * On entry: szName is a filename (probably without path separators).
406 * On exit: if szName found in "App Path", place full path in lpResult, and return true
408 static BOOL
SHELL_TryAppPathW(LPCWSTR szName
, LPWSTR lpResult
, void** env
)
410 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',
411 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
412 static const WCHAR wPath
[] = {'P','a','t','h',0};
420 if (env
) *env
= NULL
;
421 strcpyW(buffer
, wszKeyAppPaths
);
422 strcatW(buffer
, szName
);
423 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
426 len
= MAX_PATH
*sizeof(WCHAR
);
427 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
433 DWORD count
= sizeof(buffer
);
434 if (!RegQueryValueExW(hkApp
, wPath
, NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
435 *env
= SHELL_BuildEnvW( buffer
);
439 if (hkApp
) RegCloseKey(hkApp
);
443 static UINT
SHELL_FindExecutableByOperation(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
, LPWSTR key
, LPWSTR filetype
, LPWSTR command
, LONG commandlen
)
445 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
447 /* Looking for ...buffer\shell\<verb>\command */
448 strcatW(filetype
, wszShell
);
449 strcatW(filetype
, lpOperation
);
450 strcatW(filetype
, wCommand
);
452 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, command
,
453 &commandlen
) == ERROR_SUCCESS
)
455 commandlen
/= sizeof(WCHAR
);
456 if (key
) strcpyW(key
, filetype
);
460 LONG paramlen
= sizeof(param
);
461 static const WCHAR wSpace
[] = {' ',0};
463 /* FIXME: it seems all Windows version don't behave the same here.
464 * the doc states that this ddeexec information can be found after
466 * on Win98, it doesn't appear, but I think it does on Win2k
468 /* Get the parameters needed by the application
469 from the associated ddeexec key */
470 tmp
= strstrW(filetype
, wCommand
);
472 strcatW(filetype
, wDdeexec
);
473 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, param
,
474 ¶mlen
) == ERROR_SUCCESS
)
476 paramlen
/= sizeof(WCHAR
);
477 strcatW(command
, wSpace
);
478 strcatW(command
, param
);
479 commandlen
+= paramlen
;
483 command
[commandlen
] = '\0';
485 return 33; /* FIXME see SHELL_FindExecutable() */
488 return 31; /* default - 'No association was found' */
491 /*************************************************************************
492 * SHELL_FindExecutable [Internal]
494 * Utility for code sharing between FindExecutable and ShellExecute
496 * lpFile the name of a file
497 * lpOperation the operation on it (open)
499 * lpResult a buffer, big enough :-(, to store the command to do the
500 * operation on the file
501 * key a buffer, big enough, to get the key name to do actually the
502 * command (it'll be used afterwards for more information
505 UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
,
506 LPWSTR lpResult
, int resultLen
, LPWSTR key
, void **env
, LPITEMIDLIST pidl
, LPCWSTR args
)
508 static const WCHAR wWindows
[] = {'w','i','n','d','o','w','s',0};
509 static const WCHAR wPrograms
[] = {'p','r','o','g','r','a','m','s',0};
510 static const WCHAR wExtensions
[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
512 WCHAR
*extension
= NULL
; /* pointer to file extension */
513 WCHAR wtmpext
[5]; /* local copy to mung as we please */
514 WCHAR filetype
[256]; /* registry name for this filetype */
515 LONG filetypelen
= sizeof(filetype
); /* length of above */
516 WCHAR command
[256]; /* command from registry */
517 WCHAR wBuffer
[256]; /* Used to GetProfileString */
518 UINT retval
= 31; /* default - 'No association was found' */
519 WCHAR
*tok
; /* token pointer */
520 WCHAR xlpFile
[256]; /* result of SearchPath */
521 DWORD attribs
; /* file attributes */
523 TRACE("%s\n", (lpFile
!= NULL
) ? debugstr_w(lpFile
) : "-");
525 lpResult
[0] = '\0'; /* Start off with an empty return string */
526 if (key
) *key
= '\0';
530 /* trap NULL parameters on entry */
531 if ((lpFile
== NULL
) || (lpResult
== NULL
))
533 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n", debugstr_w(lpFile
), debugstr_w(lpResult
));
534 return 2; /* File not found. Close enough, I guess. */
537 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
539 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
543 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
545 TRACE("SearchPathW returned non-zero\n");
547 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
550 attribs
= GetFileAttributesW(lpFile
);
551 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
553 strcpyW(filetype
, wszFolder
);
554 filetypelen
= 6; /* strlen("Folder") */
558 /* First thing we need is the file's extension */
559 extension
= strrchrW(xlpFile
, '.'); /* Assume last "." is the one; */
560 /* File->Run in progman uses */
562 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
564 if ((extension
== NULL
) || (extension
== &xlpFile
[strlenW(xlpFile
)]))
566 WARN("Returning 31 - No association\n");
567 return 31; /* no association */
569 /* Make local copy & lowercase it for reg & 'programs=' lookup */
570 lstrcpynW(wtmpext
, extension
, 5);
572 TRACE("%s file\n", debugstr_w(wtmpext
));
574 /* Three places to check: */
575 /* 1. win.ini, [windows], programs (NB no leading '.') */
576 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
577 /* 3. win.ini, [extensions], extension (NB no leading '.' */
578 /* All I know of the order is that registry is checked before */
579 /* extensions; however, it'd make sense to check the programs */
580 /* section first, so that's what happens here. */
582 /* See if it's a program - if GetProfileString fails, we skip this
583 * section. Actually, if GetProfileString fails, we've probably
584 * got a lot more to worry about than running a program... */
585 if (GetProfileStringW(wWindows
, wPrograms
, wExtensions
, wBuffer
, sizeof(wBuffer
)/sizeof(WCHAR
)) > 0)
592 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
596 while (*p
== ' ' || *p
== '\t') p
++;
599 if (strcmpW(tok
, &wtmpext
[1]) == 0) /* have to skip the leading "." */
601 strcpyW(lpResult
, xlpFile
);
602 /* Need to perhaps check that the file has a path
604 TRACE("found %s\n", debugstr_w(lpResult
));
607 /* Greater than 32 to indicate success FIXME According to the
608 * docs, I should be returning a handle for the
609 * executable. Does this mean I'm supposed to open the
610 * executable file or something? More RTFM, I guess... */
617 if (RegQueryValueW(HKEY_CLASSES_ROOT
, wtmpext
, filetype
,
618 &filetypelen
) == ERROR_SUCCESS
)
620 filetypelen
/= sizeof(WCHAR
);
621 filetype
[filetypelen
] = '\0';
622 TRACE("File type: %s\n", debugstr_w(filetype
));
630 /* pass the operation string to SHELL_FindExecutableByOperation() */
631 filetype
[filetypelen
] = '\0';
632 retval
= SHELL_FindExecutableByOperation(lpPath
, lpFile
, lpOperation
, key
, filetype
, command
, sizeof(command
));
636 WCHAR operation
[MAX_PATH
];
639 /* Looking for ...buffer\shell\<operation>\command */
640 strcatW(filetype
, wszShell
);
642 /* enumerate the operation subkeys in the registry and search for one with an associated command */
643 if (RegOpenKeyW(HKEY_CLASSES_ROOT
, filetype
, &hkey
) == ERROR_SUCCESS
)
648 if (RegEnumKeyW(hkey
, idx
, operation
, MAX_PATH
) != ERROR_SUCCESS
)
651 filetype
[filetypelen
] = '\0';
652 retval
= SHELL_FindExecutableByOperation(lpPath
, lpFile
, operation
, key
, filetype
, command
, sizeof(command
));
663 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
);
665 /* Remove double quotation marks and command line arguments */
666 if (*lpResult
== '"')
669 while (*(p
+ 1) != '"')
678 else if (extension
) /* Check win.ini */
680 static const WCHAR wExtensions
[] = {'e','x','t','e','n','s','i','o','n','s',0};
682 /* Toss the leading dot */
684 if (GetProfileStringW(wExtensions
, extension
, wszEmpty
, command
, sizeof(command
)/sizeof(WCHAR
)) > 0)
686 if (strlenW(command
) != 0)
688 strcpyW(lpResult
, command
);
689 tok
= strchrW(lpResult
, '^'); /* should be ^.extension? */
694 strcatW(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
695 tok
= strchrW(command
, '^'); /* see above */
696 if ((tok
!= NULL
) && (strlenW(tok
)>5))
698 strcatW(lpResult
, &tok
[5]);
702 retval
= 33; /* FIXME - see above */
707 TRACE("returning %s\n", debugstr_w(lpResult
));
711 /******************************************************************
714 * callback for the DDE connection. not really usefull
716 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
718 HDDEDATA hData
, DWORD dwData1
, DWORD dwData2
)
723 /******************************************************************
726 * ShellExecute helper. Used to do an operation with a DDE connection
728 * Handles both the direct connection (try #1), and if it fails,
729 * launching an application and trying (#2) to connect to it
732 static unsigned dde_connect(WCHAR
* key
, WCHAR
* start
, WCHAR
* ddeexec
,
733 const WCHAR
* lpFile
, void *env
,
734 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
735 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
737 static const WCHAR wApplication
[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
738 static const WCHAR wTopic
[] = {'\\','t','o','p','i','c',0};
740 WCHAR
* endkey
= key
+ strlenW(key
);
741 WCHAR app
[256], topic
[256], ifexec
[256];
743 LONG applen
, topiclen
, ifexeclen
;
747 HSZ hszApp
, hszTopic
;
751 strcpyW(endkey
, wApplication
);
752 applen
= sizeof(app
);
753 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, app
, &applen
) != ERROR_SUCCESS
)
755 FIXME("default app name NIY %s\n", debugstr_w(key
));
759 strcpyW(endkey
, wTopic
);
760 topiclen
= sizeof(topic
);
761 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, topic
, &topiclen
) != ERROR_SUCCESS
)
763 static const WCHAR wSystem
[] = {'S','y','s','t','e','m',0};
764 strcpyW(topic
, wSystem
);
767 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
772 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINANSI
);
773 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINANSI
);
775 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
779 static const WCHAR wIfexec
[] = {'\\','i','f','e','x','e','c',0};
780 TRACE("Launching '%s'\n", debugstr_w(start
));
781 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
784 TRACE("Couldn't launch\n");
787 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
790 TRACE("Couldn't connect. ret=%d\n", ret
);
791 DdeUninitialize(ddeInst
);
792 SetLastError(ERROR_DDE_FAIL
);
793 return ret
= 30; /* whatever */
795 strcpyW(endkey
, wIfexec
);
796 ifexeclen
= sizeof(ifexec
);
797 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
803 SHELL_ArgifyW(res
, sizeof(res
), exec
, lpFile
, pidl
, szCommandline
);
804 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
806 ret
= (DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0,
807 XTYP_EXECUTE
, 10000, &tid
) != DMLERR_NO_ERROR
) ? 31 : 33;
808 DdeDisconnect(hConv
);
810 DdeUninitialize(ddeInst
);
814 /*************************************************************************
815 * execute_from_key [Internal]
817 static UINT
execute_from_key(LPWSTR key
, LPCWSTR lpFile
, void *env
, LPCWSTR szCommandline
,
818 SHELL_ExecuteW32 execfunc
,
819 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
822 LONG cmdlen
= sizeof(cmd
);
827 /* Get the application for the registry */
828 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
830 static const WCHAR wCommand
[] = {'c','o','m','m','a','n','d',0};
831 static const WCHAR wDdeexec
[] = {'d','d','e','e','x','e','c',0};
835 LONG paramlen
= sizeof(param
);
839 /* Get the parameters needed by the application
840 from the associated ddeexec key */
841 tmp
= strstrW(key
, wCommand
);
843 strcpyW(tmp
, wDdeexec
);
845 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, param
, ¶mlen
) == ERROR_SUCCESS
)
847 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(param
));
848 retval
= dde_connect(key
, cmd
, param
, lpFile
, env
, szCommandline
, psei
->lpIDList
, execfunc
, psei
, psei_out
);
852 /* Is there a replace() function anywhere? */
853 cmdlen
/= sizeof(WCHAR
);
855 SHELL_ArgifyW(param
, sizeof(param
)/sizeof(WCHAR
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
);
856 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
859 else TRACE("ooch\n");
864 /*************************************************************************
865 * FindExecutableA [SHELL32.@]
867 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
870 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
871 WCHAR wResult
[MAX_PATH
];
873 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
874 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
876 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
877 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
878 if (wFile
) SHFree( wFile
);
879 if (wDirectory
) SHFree( wDirectory
);
881 TRACE("returning %s\n", lpResult
);
882 return (HINSTANCE
)retval
;
885 /*************************************************************************
886 * FindExecutableW [SHELL32.@]
888 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
890 UINT retval
= 31; /* default - 'No association was found' */
893 TRACE("File %s, Dir %s\n",
894 (lpFile
!= NULL
? debugstr_w(lpFile
) : "-"), (lpDirectory
!= NULL
? debugstr_w(lpDirectory
) : "-"));
896 lpResult
[0] = '\0'; /* Start off with an empty return string */
898 /* trap NULL parameters on entry */
899 if ((lpFile
== NULL
) || (lpResult
== NULL
))
901 /* FIXME - should throw a warning, perhaps! */
902 return (HINSTANCE
)2; /* File not found. Close enough, I guess. */
907 GetCurrentDirectoryW(sizeof(old_dir
)/sizeof(WCHAR
), old_dir
);
908 SetCurrentDirectoryW(lpDirectory
);
911 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, lpResult
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
913 TRACE("returning %s\n", debugstr_w(lpResult
));
915 SetCurrentDirectoryW(old_dir
);
916 return (HINSTANCE
)retval
;
919 /*************************************************************************
920 * ShellExecuteExW32 [Internal]
922 BOOL WINAPI
ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei
, SHELL_ExecuteW32 execfunc
)
924 static const WCHAR wQuote
[] = {'"',0};
925 static const WCHAR wSpace
[] = {' ',0};
926 static const WCHAR wWww
[] = {'w','w','w',0};
927 static const WCHAR wFile
[] = {'f','i','l','e',0};
928 static const WCHAR wHttp
[] = {'h','t','t','p',':','/','/',0};
929 static const WCHAR wExtLnk
[] = {'.','l','n','k',0};
930 static const WCHAR wExplorer
[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
932 WCHAR wszApplicationName
[MAX_PATH
+2], wszParameters
[1024], wfileName
[MAX_PATH
], wszDir
[MAX_PATH
];
933 SHELLEXECUTEINFOW sei_tmp
; /* modifyable copy of SHELLEXECUTEINFO struct */
935 WCHAR wszProtocol
[256];
941 memcpy(&sei_tmp
, psei
, sizeof(sei_tmp
));
943 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
944 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
945 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
946 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
947 (sei_tmp
.fMask
& SEE_MASK_CLASSNAME
) ? debugstr_w(sei_tmp
.lpClass
) : "not used");
949 psei
->hProcess
= NULL
;
952 strcpyW(wszApplicationName
, sei_tmp
.lpFile
);
954 *wszApplicationName
= '\0';
956 if (sei_tmp
.lpParameters
)
957 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
959 *wszParameters
= '\0';
961 if (sei_tmp
.lpDirectory
)
962 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
966 sei_tmp
.lpFile
= wszApplicationName
;
967 sei_tmp
.lpParameters
= wszParameters
;
968 sei_tmp
.lpDirectory
= wszDir
;
970 if (sei_tmp
.fMask
& (SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
971 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
972 SEE_MASK_DOENVSUBST
| SEE_MASK_FLAG_NO_UI
| SEE_MASK_UNICODE
|
973 SEE_MASK_NO_CONSOLE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
))
975 FIXME("flags ignored: 0x%08lx\n", sei_tmp
.fMask
);
978 /* process the IDList */
979 if (sei_tmp
.fMask
& SEE_MASK_INVOKEIDLIST
) /* 0x0c: includes SEE_MASK_IDLIST */
981 IShellExecuteHookW
* pSEH
;
983 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
987 hr
= IShellExecuteHookW_Execute(pSEH
, psei
);
989 IShellExecuteHookW_Release(pSEH
);
995 /* try to translate PIDL directly into the corresponding file system path */
996 if (SUCCEEDED(SHELL_GetPathFromIDListW(sei_tmp
.lpIDList
, wszApplicationName
/*sei_tmp.lpFile*/, sizeof(wszApplicationName
)/sizeof(WCHAR
))))
998 sei_tmp
.lpIDList
= NULL
;
999 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1002 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1005 if (sei_tmp
.fMask
& (SEE_MASK_CLASSNAME
| SEE_MASK_CLASSKEY
))
1007 /* launch a document by fileclass like 'WordPad.Document.1' */
1008 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1009 /* FIXME: wszParameters should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1010 HCR_GetExecuteCommandW((sei_tmp
.fMask
& SEE_MASK_CLASSKEY
)? sei_tmp
.hkeyClass
: NULL
,
1011 (sei_tmp
.fMask
& SEE_MASK_CLASSNAME
)? sei_tmp
.lpClass
: NULL
,
1012 (sei_tmp
.lpVerb
)? sei_tmp
.lpVerb
: wszOpen
,
1013 wszParameters
/*sei_tmp.lpParameters*/, sizeof(wszParameters
)/sizeof(WCHAR
));
1015 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1016 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(sei_tmp
.lpParameters
), debugstr_w(sei_tmp
.lpFile
));
1020 if (!SHELL_ArgifyW(buffer
, sizeof(buffer
)/sizeof(WCHAR
), sei_tmp
.lpParameters
, sei_tmp
.lpFile
, sei_tmp
.lpIDList
, NULL
) && sei_tmp
.lpFile
[0])
1022 strcatW(buffer
, wExtLnk
);
1023 strcatW(buffer
, sei_tmp
.lpFile
);
1026 retval
= execfunc(buffer
, NULL
, FALSE
, &sei_tmp
, psei
);
1033 /* Else, try to execute the filename */
1034 TRACE("execute:'%s','%s'\n", debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
));
1037 /* resolve shell shortcuts */
1038 ext
= PathFindExtensionW(sei_tmp
.lpFile
);
1040 if (ext
&& !strcmpiW(ext
, wExtLnk
)) /* or check for: shell_attribs & SFGAO_LINK */
1042 HRESULT hr
= SHELL_ResolveShortCutW((LPWSTR
)sei_tmp
.lpFile
, (LPWSTR
)sei_tmp
.lpParameters
, (LPWSTR
)sei_tmp
.lpDirectory
,
1043 sei_tmp
.hwnd
, sei_tmp
.lpVerb
?sei_tmp
.lpVerb
:wszEmpty
, &sei_tmp
.nShow
, (LPITEMIDLIST
*)&sei_tmp
.lpIDList
);
1046 psei
->fMask
|= SEE_MASK_IDLIST
;
1050 /* repeat IDList processing if needed */
1051 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1053 IShellExecuteHookW
* pSEH
;
1055 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1059 hr
= IShellExecuteHookW_Execute(pSEH
, psei
);
1061 IShellExecuteHookW_Release(pSEH
);
1067 TRACE("-- idlist=%p (%s)\n", debugstr_w(sei_tmp
.lpIDList
), debugstr_w(sei_tmp
.lpFile
));
1073 /* Has the IDList not yet been translated? */
1074 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1076 /* last chance to translate IDList: now also allow CLSID paths */
1077 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp
.lpIDList
, buffer
, sizeof(buffer
)))) {
1078 if (buffer
[0]==':' && buffer
[1]==':') {
1079 /* open shell folder for the specified class GUID */
1080 strcpyW(wszParameters
, buffer
);
1081 strcpyW(wszApplicationName
, wExplorer
);
1083 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1084 } else if (HCR_GetExecuteCommandW(0, wszFolder
, sei_tmp
.lpVerb
?sei_tmp
.lpVerb
:wszOpen
, buffer
, sizeof(buffer
))) {
1085 SHELL_ArgifyW(wszApplicationName
, sizeof(wszApplicationName
)/sizeof(WCHAR
), buffer
, NULL
, sei_tmp
.lpIDList
, NULL
);
1087 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1093 /* expand environment strings */
1095 if (ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buffer
, MAX_PATH
))
1096 lstrcpyW(wszApplicationName
/*sei_tmp.lpFile*/, buffer
);
1098 if (*sei_tmp
.lpParameters
)
1099 if (ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buffer
, MAX_PATH
))
1100 lstrcpyW(wszParameters
/*sei_tmp.lpParameters*/, buffer
);
1102 if (*sei_tmp
.lpDirectory
)
1103 if (ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buffer
, MAX_PATH
))
1104 lstrcpyW(wszDir
/*sei_tmp.lpDirectory*/, buffer
);
1107 /* separate out command line arguments from executable file name */
1108 if (!*sei_tmp
.lpParameters
) {
1109 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1110 if (sei_tmp
.lpFile
[0] == '"') {
1111 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1112 LPWSTR dst
= wfileName
;
1115 /* copy the unquoted executabe path to 'wfileName' */
1116 while(*src
&& *src
!='"')
1124 while(isspace(*src
))
1129 /* copy the parameter string to 'wszParameters' */
1130 strcpyW(wszParameters
, src
);
1132 /* terminate previous command string after the quote character */
1137 /* If the executable name is not quoted, we have to use this search loop here,
1138 that in CreateProcess() is not sufficient because it does not handle shell links. */
1139 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1142 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1143 for(s
=beg
; (space
=strchrW(s
, ' ')); s
=space
+1) {
1144 int idx
= space
-sei_tmp
.lpFile
;
1145 strncpyW(buffer
, sei_tmp
.lpFile
, idx
);
1148 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1149 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
), xlpFile
, NULL
))
1151 /* separate out command from parameter string */
1152 LPCWSTR p
= space
+ 1;
1157 strcpyW(wszParameters
, p
);
1164 strcpyW(wfileName
, sei_tmp
.lpFile
);
1167 strcpyW(wfileName
, sei_tmp
.lpFile
);
1171 if (sei_tmp
.lpParameters
[0]) {
1172 strcatW(wszApplicationName
/*sei_tmp.lpFile*/, wSpace
);
1173 strcatW(wszApplicationName
/*sei_tmp.lpFile*/, sei_tmp
.lpParameters
);
1176 retval
= execfunc(sei_tmp
.lpFile
, NULL
, FALSE
, &sei_tmp
, psei
);
1179 /* Now, that we have successfully launched a process, we can free the PIDL.
1180 It may have been used before for %I command line options. */
1181 if (sei_tmp
.lpIDList
!=psei
->lpIDList
&& sei_tmp
.lpIDList
)
1182 SHFree(sei_tmp
.lpIDList
);
1184 TRACE("execfunc: retval=%d psei->hInstApp=%p\n", retval
, psei
->hInstApp
);
1188 /* Else, try to find the executable */
1190 retval
= SHELL_FindExecutable(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, lpFile
, sei_tmp
.lpVerb
, buffer
, 1024, wszProtocol
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1192 if (retval
> 32) /* Found */
1194 WCHAR wszQuotedCmd
[MAX_PATH
+2];
1195 /* Must quote to handle case where 'buffer' contains spaces,
1196 * else security hole if malicious user creates executable file "C:\\Program"
1198 * FIXME: If we don't have set explicitly command line arguments, we must first
1199 * split executable path from optional command line arguments. Otherwise we would quote
1200 * the complete string with executable path _and_ arguments, which is not what we want.
1202 strcpyW(wszQuotedCmd
, wQuote
);
1203 strcatW(wszQuotedCmd
, buffer
);
1204 strcatW(wszQuotedCmd
, wQuote
);
1206 if (*sei_tmp
.lpParameters
) {
1207 strcatW(wszQuotedCmd
, wSpace
);
1208 strcatW(wszQuotedCmd
, sei_tmp
.lpParameters
);
1211 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(buffer
), debugstr_w(wszQuotedCmd
), debugstr_w(wszProtocol
));
1214 retval
= execute_from_key(wszProtocol
, lpFile
, env
, sei_tmp
.lpParameters
, execfunc
, &sei_tmp
, psei
);
1216 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, &sei_tmp
, psei
);
1218 if (env
) HeapFree( GetProcessHeap(), 0, env
);
1220 else if (PathIsURLW((LPWSTR
)lpFile
)) /* File not found, check for URL */
1222 static const WCHAR wszShell
[] = {'\\','s','h','e','l','l','\\',0};
1223 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
1227 lpstrRes
= strchrW(lpFile
, ':');
1229 iSize
= lpstrRes
- lpFile
;
1231 iSize
= strlenW(lpFile
);
1233 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1234 /* Looking for ...protocol\shell\<verb>\command */
1235 strncpyW(wszProtocol
, lpFile
, iSize
);
1236 wszProtocol
[iSize
] = '\0';
1237 strcatW(wszProtocol
, wszShell
);
1238 strcatW(wszProtocol
, sei_tmp
.lpVerb
? sei_tmp
.lpVerb
: wszOpen
);
1239 strcatW(wszProtocol
, wCommand
);
1241 /* Remove File Protocol from lpFile */
1242 /* In the case file://path/file */
1243 if (!strncmpiW(lpFile
, wFile
, iSize
))
1246 while (*lpFile
== ':') lpFile
++;
1249 retval
= execute_from_key(wszProtocol
, lpFile
, NULL
, sei_tmp
.lpParameters
, execfunc
, &sei_tmp
, psei
);
1251 /* Check if file specified is in the form www.??????.*** */
1252 else if (!strncmpiW(lpFile
, wWww
, 3))
1254 /* if so, append lpFile http:// and call ShellExecute */
1255 WCHAR lpstrTmpFile
[256];
1256 strcpyW(lpstrTmpFile
, wHttp
);
1257 strcatW(lpstrTmpFile
, lpFile
);
1258 retval
= (UINT
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1261 /* Now we can free the PIDL. It may have been used before for %I command line options. */
1262 if (sei_tmp
.lpIDList
!=psei
->lpIDList
&& sei_tmp
.lpIDList
)
1263 SHFree(sei_tmp
.lpIDList
);
1265 TRACE("retval %u\n", retval
);
1269 psei
->hInstApp
= (HINSTANCE
)retval
;
1273 psei
->hInstApp
= (HINSTANCE
)33;
1277 /*************************************************************************
1278 * ShellExecuteA [SHELL32.290]
1280 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
1281 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1283 SHELLEXECUTEINFOA sei
;
1284 HANDLE hProcess
= 0;
1287 sei
.cbSize
= sizeof(sei
);
1290 sei
.lpVerb
= lpOperation
;
1291 sei
.lpFile
= lpFile
;
1292 sei
.lpParameters
= lpParameters
;
1293 sei
.lpDirectory
= lpDirectory
;
1294 sei
.nShow
= iShowCmd
;
1299 sei
.hProcess
= hProcess
;
1301 ShellExecuteExA (&sei
);
1302 return sei
.hInstApp
;
1305 /*************************************************************************
1306 * ShellExecuteEx [SHELL32.291]
1309 BOOL WINAPI
ShellExecuteExAW (LPVOID sei
)
1311 if (SHELL_OsIsUnicode())
1312 return ShellExecuteExW32 (sei
, SHELL_ExecuteW
);
1313 return ShellExecuteExA (sei
);
1316 /*************************************************************************
1317 * ShellExecuteExA [SHELL32.292]
1320 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1322 SHELLEXECUTEINFOW seiW
;
1324 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1328 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1331 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1334 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1336 if (sei
->lpParameters
)
1337 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1339 if (sei
->lpDirectory
)
1340 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1342 if ((sei
->fMask
& SEE_MASK_CLASSNAME
) && sei
->lpClass
)
1343 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1345 seiW
.lpClass
= NULL
;
1347 ret
= ShellExecuteExW32 (&seiW
, SHELL_ExecuteW
);
1349 sei
->hInstApp
= seiW
.hInstApp
;
1351 if (wVerb
) SHFree(wVerb
);
1352 if (wFile
) SHFree(wFile
);
1353 if (wParameters
) SHFree(wParameters
);
1354 if (wDirectory
) SHFree(wDirectory
);
1355 if (wClass
) SHFree(wClass
);
1360 /*************************************************************************
1361 * ShellExecuteExW [SHELL32.293]
1364 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1366 return ShellExecuteExW32 (sei
, SHELL_ExecuteW
);
1369 /*************************************************************************
1370 * ShellExecuteW [SHELL32.294]
1372 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1373 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1375 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1376 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1378 SHELLEXECUTEINFOW sei
;
1381 sei
.cbSize
= sizeof(sei
);
1384 sei
.lpVerb
= lpOperation
;
1385 sei
.lpFile
= lpFile
;
1386 sei
.lpParameters
= lpParameters
;
1387 sei
.lpDirectory
= lpDirectory
;
1388 sei
.nShow
= nShowCmd
;
1395 ShellExecuteExW32 (&sei
, SHELL_ExecuteW
);
1396 return sei
.hInstApp
;