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 psei_out
->hInstApp
= (HINSTANCE
)retval
;
340 /***********************************************************************
341 * SHELL_BuildEnvW [Internal]
343 * Build the environment for the new process, adding the specified
344 * path to the PATH variable. Returned pointer must be freed by caller.
346 static void *SHELL_BuildEnvW( const WCHAR
*path
)
348 static const WCHAR wPath
[] = {'P','A','T','H','=',0};
349 WCHAR
*strings
, *new_env
;
351 int total
= strlenW(path
) + 1;
352 BOOL got_path
= FALSE
;
354 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
358 int len
= strlenW(p
) + 1;
359 if (!strncmpiW( p
, wPath
, 5 )) got_path
= TRUE
;
363 if (!got_path
) total
+= 5; /* we need to create PATH */
364 total
++; /* terminating null */
366 if (!(new_env
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) )))
368 FreeEnvironmentStringsW( strings
);
375 int len
= strlenW(p
) + 1;
376 memcpy( p2
, p
, len
* sizeof(WCHAR
) );
377 if (!strncmpiW( p
, wPath
, 5 ))
380 strcpyW( p2
+ len
, path
);
381 p2
+= strlenW(path
) + 1;
388 strcpyW( p2
, wPath
);
390 p2
+= strlenW(p2
) + 1;
393 FreeEnvironmentStringsW( strings
);
398 /***********************************************************************
399 * SHELL_TryAppPathW [Internal]
401 * Helper function for SHELL_FindExecutable
402 * @param lpResult - pointer to a buffer of size MAX_PATH
403 * On entry: szName is a filename (probably without path separators).
404 * On exit: if szName found in "App Path", place full path in lpResult, and return true
406 static BOOL
SHELL_TryAppPathW(LPCWSTR szName
, LPWSTR lpResult
, void** env
)
408 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',
409 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
410 static const WCHAR wPath
[] = {'P','a','t','h',0};
418 if (env
) *env
= NULL
;
419 strcpyW(buffer
, wszKeyAppPaths
);
420 strcatW(buffer
, szName
);
421 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
424 len
= MAX_PATH
*sizeof(WCHAR
);
425 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
431 DWORD count
= sizeof(buffer
);
432 if (!RegQueryValueExW(hkApp
, wPath
, NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
433 *env
= SHELL_BuildEnvW( buffer
);
437 if (hkApp
) RegCloseKey(hkApp
);
441 static UINT
SHELL_FindExecutableByOperation(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
, LPWSTR key
, LPWSTR filetype
, LPWSTR command
)
443 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
445 LONG commandlen
= 256*sizeof(WCHAR
); /* This is the most DOS can handle :) */
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
, &commandlen
) == ERROR_SUCCESS
)
454 commandlen
/= sizeof(WCHAR
);
455 if (key
) strcpyW(key
, filetype
);
461 /* FIXME: it seems all Windows version don't behave the same here.
462 * the doc states that this ddeexec information can be found after
464 * on Win98, it doesn't appear, but I think it does on Win2k
466 /* Get the parameters needed by the application
467 from the associated ddeexec key */
468 tmp
= strstr(filetype
, "command");
470 strcat(filetype
, "ddeexec");
472 if (RegQueryValueA(HKEY_CLASSES_ROOT
, filetype
, param
, ¶mlen
) == ERROR_SUCCESS
)
474 strcat(command
, " ");
475 strcat(command
, param
);
476 commandlen
+= paramlen
;
480 command
[commandlen
] = '\0';
482 return 33; /* FIXME see SHELL_FindExecutable() */
485 return 31; /* default - 'No association was found' */
488 /*************************************************************************
489 * SHELL_FindExecutable [Internal]
491 * Utility for code sharing between FindExecutable and ShellExecute
493 * lpFile the name of a file
494 * lpOperation the operation on it (open)
496 * lpResult a buffer, big enough :-(, to store the command to do the
497 * operation on the file
498 * key a buffer, big enough, to get the key name to do actually the
499 * command (it'll be used afterwards for more information
502 UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
,
503 LPWSTR lpResult
, LPWSTR key
, void **env
, LPITEMIDLIST pidl
, LPCWSTR args
)
505 static const WCHAR wWindows
[] = {'w','i','n','d','o','w','s',0};
506 static const WCHAR wPrograms
[] = {'p','r','o','g','r','a','m','s',0};
507 static const WCHAR wExtensions
[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
509 WCHAR
*extension
= NULL
; /* pointer to file extension */
510 WCHAR wtmpext
[5]; /* local copy to mung as we please */
511 WCHAR filetype
[256]; /* registry name for this filetype */
512 LONG filetypelen
= 256; /* length of above */
513 WCHAR command
[256]; /* command from registry */
514 LONG commandlen
= 256; /* This is the most DOS can handle :) */
515 WCHAR wBuffer
[256]; /* Used to GetProfileString */
516 UINT retval
= 31; /* default - 'No association was found' */
517 WCHAR
*tok
; /* token pointer */
518 WCHAR xlpFile
[256]; /* result of SearchPath */
519 DWORD attribs
; /* file attributes */
521 TRACE("%s\n", (lpFile
!= NULL
) ? debugstr_w(lpFile
) : "-");
523 lpResult
[0] = '\0'; /* Start off with an empty return string */
524 if (key
) *key
= '\0';
528 /* trap NULL parameters on entry */
529 if ((lpFile
== NULL
) || (lpResult
== NULL
))
531 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n", debugstr_w(lpFile
), debugstr_w(lpResult
));
532 return 2; /* File not found. Close enough, I guess. */
535 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
537 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
541 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
543 TRACE("SearchPathW returned non-zero\n");
545 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
548 attribs
= GetFileAttributesW(lpFile
);
549 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
551 strcpyW(filetype
, wszFolder
);
552 filetypelen
= 6; /* strlen("Folder") */
556 /* First thing we need is the file's extension */
557 extension
= strrchrW(xlpFile
, '.'); /* Assume last "." is the one; */
558 /* File->Run in progman uses */
560 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
562 if ((extension
== NULL
) || (extension
== &xlpFile
[strlenW(xlpFile
)]))
564 WARN("Returning 31 - No association\n");
565 return 31; /* no association */
567 /* Make local copy & lowercase it for reg & 'programs=' lookup */
568 lstrcpynW(wtmpext
, extension
, 5);
570 TRACE("%s file\n", debugstr_w(wtmpext
));
572 /* Three places to check: */
573 /* 1. win.ini, [windows], programs (NB no leading '.') */
574 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
575 /* 3. win.ini, [extensions], extension (NB no leading '.' */
576 /* All I know of the order is that registry is checked before */
577 /* extensions; however, it'd make sense to check the programs */
578 /* section first, so that's what happens here. */
580 /* See if it's a program - if GetProfileString fails, we skip this
581 * section. Actually, if GetProfileString fails, we've probably
582 * got a lot more to worry about than running a program... */
583 if (GetProfileStringW(wWindows
, wPrograms
, wExtensions
, wBuffer
, sizeof(wBuffer
)/sizeof(WCHAR
)) > 0)
590 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
594 while (*p
== ' ' || *p
== '\t') p
++;
597 if (strcmpW(tok
, &wtmpext
[1]) == 0) /* have to skip the leading "." */
599 strcpyW(lpResult
, xlpFile
);
600 /* Need to perhaps check that the file has a path
602 TRACE("found %s\n", debugstr_w(lpResult
));
605 /* Greater than 32 to indicate success FIXME According to the
606 * docs, I should be returning a handle for the
607 * executable. Does this mean I'm supposed to open the
608 * executable file or something? More RTFM, I guess... */
615 if (RegQueryValueW(HKEY_CLASSES_ROOT
, wtmpext
, filetype
,
616 &filetypelen
) == ERROR_SUCCESS
)
618 filetypelen
/= sizeof(WCHAR
);
619 filetype
[filetypelen
] = '\0';
627 /* pass the operation string to SHELL_FindExecutableByOperation() */
628 filetype
[filetypelen
] = '\0';
629 retval
= SHELL_FindExecutableByOperation(lpPath
, lpFile
, lpOperation
, key
, filetype
, command
);
633 WCHAR operation
[MAX_PATH
];
636 /* Looking for ...buffer\shell\lpOperation\command */
637 strcatW(filetype
, wszShell
);
639 /* enumerate the operation subkeys in the registry and search for one with an associated command */
640 if (RegOpenKeyW(HKEY_CLASSES_ROOT
, filetype
, &hkey
) == ERROR_SUCCESS
)
645 if (RegEnumKeyW(hkey
, idx
, operation
, MAX_PATH
) != ERROR_SUCCESS
)
648 filetype
[filetypelen
] = '\0';
649 retval
= SHELL_FindExecutableByOperation(lpPath
, lpFile
, operation
, key
, filetype
, command
);
661 SHELL_ArgifyW(lpResult
, MAX_PATH
, command
, xlpFile
, pidl
, args
);
663 /* Remove double quotation marks and command line arguments */
664 if (*lpResult
== '"')
667 while (*(p
+ 1) != '"')
676 else if (extension
) /* Check win.ini */
678 static const WCHAR wExtensions
[] = {'e','x','t','e','n','s','i','o','n','s',0};
680 /* Toss the leading dot */
682 if (GetProfileStringW(wExtensions
, extension
, wszEmpty
, command
, sizeof(command
)/sizeof(WCHAR
)) > 0)
684 if (strlenW(command
) != 0)
686 strcpyW(lpResult
, command
);
687 tok
= strchrW(lpResult
, '^'); /* should be ^.extension? */
692 strcatW(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
693 tok
= strchrW(command
, '^'); /* see above */
694 if ((tok
!= NULL
) && (strlenW(tok
)>5))
696 strcatW(lpResult
, &tok
[5]);
700 retval
= 33; /* FIXME - see above */
705 TRACE("returning %s\n", debugstr_w(lpResult
));
709 /******************************************************************
712 * callback for the DDE connection. not really usefull
714 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
716 HDDEDATA hData
, DWORD dwData1
, DWORD dwData2
)
721 /******************************************************************
724 * ShellExecute helper. Used to do an operation with a DDE connection
726 * Handles both the direct connection (try #1), and if it fails,
727 * launching an application and trying (#2) to connect to it
730 static unsigned dde_connect(WCHAR
* key
, WCHAR
* start
, WCHAR
* ddeexec
,
731 const WCHAR
* lpFile
, void *env
,
732 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
733 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
735 static const WCHAR wApplication
[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
736 static const WCHAR wTopic
[] = {'\\','t','o','p','i','c',0};
738 WCHAR
* endkey
= key
+ strlenW(key
);
739 WCHAR app
[256], topic
[256], ifexec
[256];
741 LONG applen
, topiclen
, ifexeclen
;
745 HSZ hszApp
, hszTopic
;
749 strcpyW(endkey
, wApplication
);
750 applen
= sizeof(app
); /* buffer length for RegQueryValueW() is in bytes! */
751 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, app
, &applen
) != ERROR_SUCCESS
)
753 FIXME("default app name NIY %s\n", debugstr_w(key
));
757 strcpyW(endkey
, wTopic
);
758 topiclen
= sizeof(topic
);
759 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, topic
, &topiclen
) != ERROR_SUCCESS
)
761 static const WCHAR wSystem
[] = {'S','y','s','t','e','m',0};
762 strcpyW(topic
, wSystem
);
765 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
770 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINANSI
);
771 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINANSI
);
773 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
777 static const WCHAR wIfexec
[] = {'\\','i','f','e','x','e','c',0};
778 TRACE("Launching '%s'\n", debugstr_w(start
));
779 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
782 TRACE("Couldn't launch\n");
785 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
788 TRACE("Couldn't connect. ret=%d\n", ret
);
789 ret
= 30; /* whatever */
792 strcpyW(endkey
, wIfexec
);
793 ifexeclen
= sizeof(ifexec
);
794 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
800 SHELL_ArgifyW(res
, sizeof(res
), exec
, lpFile
, pidl
, szCommandline
);
801 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
803 ret
= (DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0,
804 XTYP_EXECUTE
, 10000, &tid
) != DMLERR_NO_ERROR
) ? 31 : 33;
805 DdeDisconnect(hConv
);
807 DdeUninitialize(ddeInst
);
811 /*************************************************************************
812 * execute_from_key [Internal]
814 static UINT
execute_from_key(LPWSTR key
, LPCWSTR lpFile
, void *env
, LPCWSTR szCommandline
,
815 SHELL_ExecuteW32 execfunc
,
816 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
819 LONG cmdlen
= sizeof(cmd
);
824 /* Get the application for the registry */
825 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
827 static const WCHAR wCommand
[] = {'c','o','m','m','a','n','d',0};
828 static const WCHAR wDdeexec
[] = {'d','d','e','e','x','e','c',0};
832 LONG paramlen
= sizeof(param
);
834 cmdlen
/= sizeof(WCHAR
);
838 /* Get the parameters needed by the application
839 from the associated ddeexec key */
840 tmp
= strstrW(key
, wCommand
);
842 strcpyW(tmp
, wDdeexec
);
844 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, param
, ¶mlen
) == ERROR_SUCCESS
)
846 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(param
));
847 retval
= dde_connect(key
, cmd
, param
, lpFile
, env
, szCommandline
, psei
->lpIDList
, execfunc
, psei
, psei_out
);
851 /* Is there a replace() function anywhere? */
853 SHELL_ArgifyW(param
, sizeof(param
)/sizeof(WCHAR
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
);
854 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
857 else TRACE("ooch\n");
862 /*************************************************************************
863 * FindExecutableA [SHELL32.@]
865 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
868 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
869 WCHAR wResult
[MAX_PATH
];
871 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
872 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
874 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
875 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
876 if (wFile
) SHFree( wFile
);
877 if (wDirectory
) SHFree( wDirectory
);
879 TRACE("returning %s\n", lpResult
);
880 return (HINSTANCE
)retval
;
883 /*************************************************************************
884 * FindExecutableW [SHELL32.@]
886 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
888 UINT retval
= 31; /* default - 'No association was found' */
891 TRACE("File %s, Dir %s\n",
892 (lpFile
!= NULL
? debugstr_w(lpFile
) : "-"), (lpDirectory
!= NULL
? debugstr_w(lpDirectory
) : "-"));
894 lpResult
[0] = '\0'; /* Start off with an empty return string */
896 /* trap NULL parameters on entry */
897 if ((lpFile
== NULL
) || (lpResult
== NULL
))
899 /* FIXME - should throw a warning, perhaps! */
900 return (HINSTANCE
)2; /* File not found. Close enough, I guess. */
905 GetCurrentDirectoryW(sizeof(old_dir
)/sizeof(WCHAR
), old_dir
);
906 SetCurrentDirectoryW(lpDirectory
);
909 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, lpResult
, NULL
, NULL
, NULL
, NULL
);
911 TRACE("returning %s\n", debugstr_w(lpResult
));
913 SetCurrentDirectoryW(old_dir
);
914 return (HINSTANCE
)retval
;
917 /*************************************************************************
918 * ShellExecuteExW32 [Internal]
920 BOOL WINAPI
ShellExecuteExW32 (LPSHELLEXECUTEINFOW psei
, SHELL_ExecuteW32 execfunc
)
922 static const WCHAR wQuote
[] = {'"',0};
923 static const WCHAR wSpace
[] = {' ',0};
924 static const WCHAR wWww
[] = {'w','w','w',0};
925 static const WCHAR wFile
[] = {'f','i','l','e',0};
926 static const WCHAR wHttp
[] = {'h','t','t','p',':','/','/',0};
927 static const WCHAR wExtLnk
[] = {'.','l','n','k',0};
928 static const WCHAR wExplorer
[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
930 WCHAR wszApplicationName
[MAX_PATH
+2], wszParameters
[MAX_PATH
], wfileName
[MAX_PATH
], wszDir
[MAX_PATH
];
931 SHELLEXECUTEINFOW sei_tmp
; /* modifyable copy of SHELLEXECUTEINFO struct */
933 WCHAR wszProtocol
[256];
939 memcpy(&sei_tmp
, psei
, sizeof(sei_tmp
));
941 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
942 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
943 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
944 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
945 (sei_tmp
.fMask
& SEE_MASK_CLASSNAME
) ? debugstr_w(sei_tmp
.lpClass
) : "not used");
947 psei
->hProcess
= NULL
;
950 strcpyW(wszApplicationName
, sei_tmp
.lpFile
);
952 *wszApplicationName
= '\0';
954 if (sei_tmp
.lpParameters
)
955 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
957 *wszParameters
= '\0';
959 if (sei_tmp
.lpDirectory
)
960 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
964 sei_tmp
.lpFile
= wszApplicationName
;
965 sei_tmp
.lpParameters
= wszParameters
;
966 sei_tmp
.lpDirectory
= wszDir
;
968 if (sei_tmp
.fMask
& (SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
969 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
970 SEE_MASK_DOENVSUBST
| SEE_MASK_FLAG_NO_UI
| SEE_MASK_UNICODE
|
971 SEE_MASK_NO_CONSOLE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
))
973 FIXME("flags ignored: 0x%08lx\n", sei_tmp
.fMask
);
976 /* process the IDList */
977 if (sei_tmp
.fMask
& SEE_MASK_INVOKEIDLIST
) /* 0x0c: includes SEE_MASK_IDLIST */
979 IShellExecuteHookW
* pSEH
;
981 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
985 hr
= IShellExecuteHookW_Execute(pSEH
, psei
);
987 IShellExecuteHookW_Release(pSEH
);
993 /* try to translate PIDL directly into the corresponding file system path */
994 if (SUCCEEDED(SHELL_GetPathFromIDListW(sei_tmp
.lpIDList
, wszApplicationName
/*sei_tmp.lpFile*/, sizeof(wszApplicationName
)/sizeof(WCHAR
))))
996 sei_tmp
.lpIDList
= NULL
;
997 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1000 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1003 if (sei_tmp
.fMask
& (SEE_MASK_CLASSNAME
| SEE_MASK_CLASSKEY
))
1005 /* launch a document by fileclass like 'WordPad.Document.1' */
1006 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1007 /* FIXME: wszParameters should not be of a fixed size. Plus MAX_PATH is way too short! */
1008 if (sei_tmp
.fMask
& SEE_MASK_CLASSKEY
)
1009 HCR_GetExecuteCommandExW(sei_tmp
.hkeyClass
,
1010 sei_tmp
.fMask
&SEE_MASK_CLASSNAME
? sei_tmp
.lpClass
: NULL
,
1011 sei_tmp
.lpVerb
? sei_tmp
.lpVerb
: wszOpen
,
1012 wszParameters
/*sei_tmp.lpParameters*/, sizeof(wszParameters
)/sizeof(WCHAR
));
1013 else if (sei_tmp
.fMask
& SEE_MASK_CLASSNAME
)
1014 HCR_GetExecuteCommandW(sei_tmp
.lpClass
, sei_tmp
.lpVerb
? sei_tmp
.lpVerb
: wszOpen
,
1015 wszParameters
/*sei_tmp.lpParameters*/, sizeof(wszParameters
)/sizeof(WCHAR
));
1017 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1018 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(sei_tmp
.lpParameters
), debugstr_w(sei_tmp
.lpFile
));
1022 if (!SHELL_ArgifyW(buffer
, sizeof(buffer
)/sizeof(WCHAR
), sei_tmp
.lpParameters
, sei_tmp
.lpFile
, sei_tmp
.lpIDList
, NULL
) && sei_tmp
.lpFile
[0])
1024 strcatW(buffer
, wExtLnk
);
1025 strcatW(buffer
, sei_tmp
.lpFile
);
1028 retval
= execfunc(buffer
, NULL
, FALSE
, &sei_tmp
, psei
);
1035 /* Else, try to execute the filename */
1036 TRACE("execute:'%s','%s'\n", debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
));
1039 /* resolve shell shortcuts */
1040 ext
= PathFindExtensionW(sei_tmp
.lpFile
);
1042 if (ext
&& !_wcsicmp(ext
, wExtLnk
)) /* or check for: shell_attribs & SFGAO_LINK */
1044 HRESULT hr
= SHELL_ResolveShortCutW((LPWSTR
)sei_tmp
.lpFile
, (LPWSTR
)sei_tmp
.lpParameters
, (LPWSTR
)sei_tmp
.lpDirectory
,
1045 sei_tmp
.hwnd
, sei_tmp
.lpVerb
?sei_tmp
.lpVerb
:wszEmpty
, &sei_tmp
.nShow
, (LPITEMIDLIST
*)&sei_tmp
.lpIDList
);
1048 psei
->fMask
|= SEE_MASK_IDLIST
;
1052 /* repeat IDList processing if needed */
1053 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1055 IShellExecuteHookW
* pSEH
;
1057 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1061 hr
= IShellExecuteHookW_Execute(pSEH
, psei
);
1063 IShellExecuteHookW_Release(pSEH
);
1069 TRACE("-- idlist=%p (%s)\n", debugstr_w(sei_tmp
.lpIDList
), debugstr_w(sei_tmp
.lpFile
));
1075 /* Has the IDList not yet been translated? */
1076 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1078 /* last chance to translate IDList: now also allow CLSID paths */
1079 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp
.lpIDList
, buffer
, sizeof(buffer
)))) {
1080 if (buffer
[0]==':' && buffer
[1]==':') {
1081 /* open shell folder for the specified class GUID */
1082 strcpyW(wszParameters
, buffer
);
1083 strcpyW(wszApplicationName
, wExplorer
);
1085 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1086 } else if (HCR_GetExecuteCommandW(wszFolder
, sei_tmp
.lpVerb
?sei_tmp
.lpVerb
:wszOpen
, buffer
, sizeof(buffer
))) {
1087 SHELL_ArgifyW(wszApplicationName
, sizeof(wszApplicationName
)/sizeof(WCHAR
), buffer
, NULL
, sei_tmp
.lpIDList
, NULL
);
1089 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1095 /* expand environment strings */
1097 if (ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buffer
, MAX_PATH
))
1098 lstrcpyW(wszApplicationName
/*sei_tmp.lpFile*/, buffer
);
1100 if (*sei_tmp
.lpParameters
)
1101 if (ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buffer
, MAX_PATH
))
1102 lstrcpyW(wszParameters
/*sei_tmp.lpParameters*/, buffer
);
1104 if (*sei_tmp
.lpDirectory
)
1105 if (ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buffer
, MAX_PATH
))
1106 lstrcpyW(wszDir
/*sei_tmp.lpDirectory*/, buffer
);
1109 /* separate out command line arguments from executable file name */
1110 if (!*sei_tmp
.lpParameters
) {
1111 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1112 if (sei_tmp
.lpFile
[0] == '"') {
1113 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1114 LPWSTR dst
= wfileName
;
1117 /* copy the unquoted executabe path to 'wfileName' */
1118 while(*src
&& *src
!='"')
1126 while(isspace(*src
))
1131 /* copy the parameter string to 'wszParameters' */
1132 strcpyW(wszParameters
, src
);
1134 /* terminate previous command string after the quote character */
1139 /* If the executable name is not quoted, we have to use this search loop here,
1140 that in CreateProcess() is not sufficient because it does not handle shell links. */
1141 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1144 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1145 for(s
=beg
; (space
=strchrW(s
, ' ')); s
=space
+1) {
1146 int idx
= space
-sei_tmp
.lpFile
;
1147 strncpyW(buffer
, sei_tmp
.lpFile
, idx
);
1150 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1151 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
), xlpFile
, NULL
))
1153 /* separate out command from parameter string */
1154 LPCWSTR p
= space
+ 1;
1159 strcpyW(wszParameters
, p
);
1166 strcpyW(wfileName
, sei_tmp
.lpFile
);
1169 strcpyW(wfileName
, sei_tmp
.lpFile
);
1173 if (sei_tmp
.lpParameters
[0]) {
1174 strcatW(wszApplicationName
/*sei_tmp.lpFile*/, wSpace
);
1175 strcatW(wszApplicationName
/*sei_tmp.lpFile*/, sei_tmp
.lpParameters
);
1178 retval
= execfunc(sei_tmp
.lpFile
, NULL
, FALSE
, &sei_tmp
, psei
);
1181 /* Now, that we have successfully launched a process, we can free the PIDL.
1182 It may have been used before for %I command line options. */
1183 if (sei_tmp
.lpIDList
!=psei
->lpIDList
&& sei_tmp
.lpIDList
)
1184 SHFree(sei_tmp
.lpIDList
);
1186 TRACE("execfunc: retval=%d psei->hInstApp=%p\n", retval
, psei
->hInstApp
);
1190 /* Else, try to find the executable */
1192 retval
= SHELL_FindExecutable(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, lpFile
, sei_tmp
.lpVerb
, buffer
, wszProtocol
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1194 if (retval
> 32) /* Found */
1196 WCHAR wszQuotedCmd
[MAX_PATH
+2];
1197 /* Must quote to handle case where 'buffer' contains spaces,
1198 * else security hole if malicious user creates executable file "C:\\Program"
1200 * FIXME: If we don't have set explicitly command line arguments, we must first
1201 * split executable path from optional command line arguments. Otherwise we would quote
1202 * the complete string with executable path _and_ arguments, which is not what we want.
1204 strcpyW(wszQuotedCmd
, wQuote
);
1205 strcatW(wszQuotedCmd
, buffer
);
1206 strcatW(wszQuotedCmd
, wQuote
);
1208 if (*sei_tmp
.lpParameters
) {
1209 strcatW(wszQuotedCmd
, wSpace
);
1210 strcatW(wszQuotedCmd
, sei_tmp
.lpParameters
);
1213 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(buffer
), debugstr_w(wszQuotedCmd
), debugstr_w(wszProtocol
));
1216 retval
= execute_from_key(wszProtocol
, lpFile
, env
, sei_tmp
.lpParameters
, execfunc
, &sei_tmp
, psei
);
1218 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, &sei_tmp
, psei
);
1220 if (env
) HeapFree( GetProcessHeap(), 0, env
);
1222 else if (PathIsURLW((LPWSTR
)lpFile
)) /* File not found, check for URL */
1224 static const WCHAR wszShell
[] = {'\\','s','h','e','l','l',0};
1225 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
1229 lpstrRes
= strchrW(lpFile
, ':');
1231 iSize
= lpstrRes
- lpFile
;
1233 iSize
= strlenW(lpFile
);
1235 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1236 /* Looking for ...protocol\shell\<verb>\command */
1237 strncpyW(wszProtocol
, lpFile
, iSize
);
1238 wszProtocol
[iSize
] = '\0';
1239 strcatW(wszProtocol
, wszShell
);
1240 strcatW(wszProtocol
, sei_tmp
.lpVerb
? sei_tmp
.lpVerb
: wszOpen
); /*FIXME: enumerate registry subkeys - compare with the loop into SHELL_FindExecutable() */
1241 strcatW(wszProtocol
, wCommand
);
1243 /* Remove File Protocol from lpFile */
1244 /* In the case file://path/file */
1245 if (!strncmpiW(lpFile
, wFile
, iSize
))
1248 while (*lpFile
== ':') lpFile
++;
1251 retval
= execute_from_key(wszProtocol
, lpFile
, NULL
, sei_tmp
.lpParameters
, execfunc
, &sei_tmp
, psei
);
1253 /* Check if file specified is in the form www.??????.*** */
1254 else if (!strncmpiW(lpFile
, wWww
, 3))
1256 /* if so, append lpFile http:// and call ShellExecute */
1257 WCHAR lpstrTmpFile
[256];
1258 strcpyW(lpstrTmpFile
, wHttp
);
1259 strcatW(lpstrTmpFile
, lpFile
);
1260 retval
= (UINT
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1263 /* Now we can free the PIDL. It may have been used before for %I command line options. */
1264 if (sei_tmp
.lpIDList
!=psei
->lpIDList
&& sei_tmp
.lpIDList
)
1265 SHFree(sei_tmp
.lpIDList
);
1267 TRACE("ShellExecuteExW32 retval=%d\n", retval
);
1271 psei
->hInstApp
= (HINSTANCE
)retval
;
1275 psei
->hInstApp
= (HINSTANCE
)33;
1279 /*************************************************************************
1280 * ShellExecuteA [SHELL32.290]
1282 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
1283 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1285 SHELLEXECUTEINFOA sei
;
1286 HANDLE hProcess
= 0;
1289 sei
.cbSize
= sizeof(sei
);
1292 sei
.lpVerb
= lpOperation
;
1293 sei
.lpFile
= lpFile
;
1294 sei
.lpParameters
= lpParameters
;
1295 sei
.lpDirectory
= lpDirectory
;
1296 sei
.nShow
= iShowCmd
;
1301 sei
.hProcess
= hProcess
;
1303 ShellExecuteExA (&sei
);
1304 return sei
.hInstApp
;
1307 /*************************************************************************
1308 * ShellExecuteEx [SHELL32.291]
1311 BOOL WINAPI
ShellExecuteExAW (LPVOID sei
)
1313 if (SHELL_OsIsUnicode())
1314 return ShellExecuteExW32 (sei
, SHELL_ExecuteW
);
1315 return ShellExecuteExA (sei
);
1318 /*************************************************************************
1319 * ShellExecuteExA [SHELL32.292]
1322 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1324 SHELLEXECUTEINFOW seiW
;
1326 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1330 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1333 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1336 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1338 if (sei
->lpParameters
)
1339 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1341 if (sei
->lpDirectory
)
1342 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1344 if ((sei
->fMask
& SEE_MASK_CLASSNAME
) && sei
->lpClass
)
1345 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1347 seiW
.lpClass
= NULL
;
1349 ret
= ShellExecuteExW32 (&seiW
, SHELL_ExecuteW
);
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
);
1357 sei
->hInstApp
= seiW
.hInstApp
;
1359 TRACE("ShellExecuteExW(): ret=%d\n", ret
);
1364 /*************************************************************************
1365 * ShellExecuteExW [SHELL32.293]
1368 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1370 return ShellExecuteExW32 (sei
, SHELL_ExecuteW
);
1373 /*************************************************************************
1374 * ShellExecuteW [SHELL32.294]
1376 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1377 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1379 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1380 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1382 SHELLEXECUTEINFOW sei
;
1385 sei
.cbSize
= sizeof(sei
);
1388 sei
.lpVerb
= lpOperation
;
1389 sei
.lpFile
= lpFile
;
1390 sei
.lpParameters
= lpParameters
;
1391 sei
.lpDirectory
= lpDirectory
;
1392 sei
.nShow
= nShowCmd
;
1399 ShellExecuteExW32 (&sei
, SHELL_ExecuteW
);
1400 return sei
.hInstApp
;