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"
48 #include "wine/winbase16.h"
49 #include "shell32_main.h"
50 #include "undocshell.h"
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
57 /***********************************************************************
58 * this function is supposed to expand the escape sequences found in the registry
59 * some diving reported that the following were used:
60 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
65 * %I address of a global item ID (explorer switch /idlist)
66 * %L seems to be %1 as long filename followed by the 8+3 variation
68 * %* all following parameters (see batfile)
70 static BOOL
argifyA(char* out
, int len
, const char* fmt
, const char* lpFile
, LPITEMIDLIST pidl
, LPCSTR args
)
110 while(*args
&& !isspace(*args
))
113 while(isspace(*args
))
120 if (!done
|| (*fmt
== '1'))
122 /*FIXME Is SearchPath() really needed? We already have separated out the parameter string in args. */
123 if (SearchPathA(NULL
, lpFile
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
128 /* Add double quotation marks unless we already have them (e.g.: "%1" %* for exefile) */
129 if (res
!= out
&& *(res
- 1) == '"')
144 * IE uses this alot for activating things such as windows media
145 * player. This is not verified to be fully correct but it appears
152 res
+= strlen(lpFile
);
159 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
160 pv
= SHLockShared(hmem
, 0);
161 res
+= sprintf(res
, ":%p", pv
);
166 default: FIXME("Unknown escape sequence %%%c\n", *fmt
);
181 HRESULT
SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl
, LPSTR pszPath
, UINT uOutSize
)
184 IShellFolder
* desktop
;
186 HRESULT hr
= SHGetDesktopFolder(&desktop
);
189 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
192 StrRetToStrNA(pszPath
, uOutSize
, &strret
, pidl
);
194 IShellFolder_Release(desktop
);
200 HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
203 IShellFolder
* desktop
;
205 HRESULT hr
= SHGetDesktopFolder(&desktop
);
208 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
211 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
213 IShellFolder_Release(desktop
);
219 /*************************************************************************
220 * SHELL_ResolveShortCutW [Internal]
221 * read shortcut file at 'wcmd' and fill psei with its content
223 static HRESULT
SHELL_ResolveShortCutW(LPWSTR wcmd
, LPWSTR wargs
, LPWSTR wdir
, HWND hwnd
, LPCWSTR lpVerb
, int* pshowcmd
, LPITEMIDLIST
* ppidl
)
227 HRESULT hr
= SHGetDesktopFolder(&psf
);
235 hr
= IShellFolder_ParseDisplayName(psf
, 0, 0, wcmd
, &l
, &pidl
, 0);
240 hr
= IShellFolder_GetUIObjectOf(psf
, NULL
, 1, (LPCITEMIDLIST
*)&pidl
, &IID_IShellLinkW
, NULL
, (LPVOID
*)&psl
);
243 hr
= IShellLinkW_Resolve(psl
, hwnd
, 0);
246 hr
= IShellLinkW_GetPath(psl
, wcmd
, MAX_PATH
, NULL
, SLGP_UNCPRIORITY
);
250 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
251 hr
= IShellLinkW_GetIDList(psl
, ppidl
);
253 if (SUCCEEDED(hr
) && *ppidl
) {
254 /* We got a PIDL instead of a file system path - try to translate it. */
255 if (SUCCEEDED(SHELL_GetPathFromIDListW(*ppidl
, wcmd
, MAX_PATH
))) {
263 /* get command line arguments, working directory and display mode if available */
264 IShellLinkW_GetWorkingDirectory(psl
, wdir
, MAX_PATH
);
265 IShellLinkW_GetArguments(psl
, wargs
, MAX_PATH
);
266 IShellLinkW_GetShowCmd(psl
, pshowcmd
);
271 IShellLinkW_Release(psl
);
277 IShellFolder_Release(psf
);
283 /*************************************************************************
284 * SHELL_ResolveShortCutA [Internal]
285 * read shortcut file at 'psei->lpFile' and fill psei with its content
287 static HRESULT
SHELL_ResolveShortCutA(LPSHELLEXECUTEINFOA psei
)
289 WCHAR wcmd
[MAX_PATH
], wargs
[MAX_PATH
], wdir
[MAX_PATH
], wverb
[MAX_PATH
];
293 if (MultiByteToWideChar(CP_ACP
, 0, psei
->lpFile
, -1, wcmd
, MAX_PATH
)) {
294 MultiByteToWideChar(CP_ACP
, 0, psei
->lpDirectory
, -1, wdir
, MAX_PATH
);
295 MultiByteToWideChar(CP_ACP
, 0, psei
->lpVerb
?psei
->lpVerb
:"", -1, wverb
, MAX_PATH
);
297 hr
= SHELL_ResolveShortCutW(wcmd
, wargs
, wdir
, psei
->hwnd
, wverb
, &psei
->nShow
, (LPITEMIDLIST
*)&psei
->lpIDList
);
300 psei
->fMask
|= SEE_MASK_IDLIST
;
303 WideCharToMultiByte(CP_ACP
, 0, wcmd
, -1, (LPSTR
)psei
->lpFile
, MAX_PATH
/*sizeof(szApplicationName)*/, NULL
, NULL
);
304 WideCharToMultiByte(CP_ACP
, 0, wdir
, -1, (LPSTR
)psei
->lpDirectory
, MAX_PATH
/*sizeof(szDir)*/, NULL
, NULL
);
305 WideCharToMultiByte(CP_ACP
, 0, wargs
, -1, (LPSTR
)psei
->lpParameters
, MAX_PATH
/*sizeof(szParameters)*/, NULL
, NULL
);
307 FIXME("We could not resolve the shell shortcut.\n");
313 /*************************************************************************
314 * SHELL_ExecuteA [Internal]
317 static UINT
SHELL_ExecuteA(const char *lpCmd
, void *env
, BOOL shWait
,
318 LPSHELLEXECUTEINFOA psei
, LPSHELLEXECUTEINFOA psei_out
)
320 STARTUPINFOA startup
;
321 PROCESS_INFORMATION info
;
324 TRACE("Execute %s from directory %s\n", lpCmd
, psei
->lpDirectory
);
326 ZeroMemory(&startup
,sizeof(STARTUPINFOA
));
327 startup
.cb
= sizeof(STARTUPINFOA
);
328 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
329 startup
.wShowWindow
= psei
->nShow
;
331 if (CreateProcessA(NULL
, (LPSTR
)lpCmd
, NULL
, NULL
, FALSE
, 0,
332 env
, *psei
->lpDirectory
? psei
->lpDirectory
: NULL
, &startup
, &info
))
334 /* Give 30 seconds to the app to come up, if desired. Probably only needed
335 when starting app immediately before making a DDE connection. */
337 if (WaitForInputIdle( info
.hProcess
, 30000 ) == -1)
338 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
341 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
342 psei_out
->hProcess
= info
.hProcess
;
344 CloseHandle(info
.hProcess
);
346 CloseHandle(info
.hThread
);
348 else if ((retval
= GetLastError()) >= 32)
350 FIXME("Strange error set by CreateProcess: %d\n", retval
);
351 retval
= ERROR_BAD_FORMAT
;
354 psei_out
->hInstApp
= (HINSTANCE
)retval
;
359 /***********************************************************************
362 * Build the environment for the new process, adding the specified
363 * path to the PATH variable. Returned pointer must be freed by caller.
365 static void *build_env( const char *path
)
367 char *strings
, *new_env
;
369 int total
= strlen(path
) + 1;
370 BOOL got_path
= FALSE
;
372 if (!(strings
= GetEnvironmentStringsA())) return NULL
;
376 int len
= strlen(p
) + 1;
377 if (!strncasecmp( p
, "PATH=", 5 )) got_path
= TRUE
;
381 if (!got_path
) total
+= 5; /* we need to create PATH */
382 total
++; /* terminating null */
384 if (!(new_env
= HeapAlloc( GetProcessHeap(), 0, total
)))
386 FreeEnvironmentStringsA( strings
);
393 int len
= strlen(p
) + 1;
394 memcpy( p2
, p
, len
);
395 if (!strncasecmp( p
, "PATH=", 5 ))
398 strcpy( p2
+ len
, path
);
399 p2
+= strlen(path
) + 1;
406 strcpy( p2
, "PATH=" );
408 p2
+= strlen(p2
) + 1;
411 FreeEnvironmentStringsA( strings
);
416 /***********************************************************************
419 * Helper function for SHELL_FindExecutable
420 * @param lpResult - pointer to a buffer of size MAX_PATH
421 * On entry: szName is a filename (probably without path separators).
422 * On exit: if szName found in "App Path", place full path in lpResult, and return true
424 static BOOL
SHELL_TryAppPath(LPCSTR szName
, LPSTR lpResult
, void** env
)
432 if (env
) *env
= NULL
;
433 sprintf(buffer
, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s", szName
);
434 res
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
438 res
= RegQueryValueA(hkApp
, NULL
, lpResult
, &len
);
444 DWORD count
= sizeof(buffer
);
445 if (!RegQueryValueExA(hkApp
, "Path", NULL
, NULL
, buffer
, &count
) && buffer
[0])
446 *env
= build_env( buffer
);
450 if (hkApp
) RegCloseKey(hkApp
);
454 static UINT
_FindExecutableByOperation(LPCSTR lpPath
, LPCSTR lpFile
, LPCSTR lpOperation
, LPSTR key
, LPSTR filetype
, LPSTR command
)
456 LONG commandlen
= 256; /* This is the most DOS can handle :) */
458 /* Looking for ...buffer\shell\<verb>\command */
459 strcat(filetype
, "\\shell\\");
460 strcat(filetype
, lpOperation
);
461 strcat(filetype
, "\\command");
463 if (RegQueryValueA(HKEY_CLASSES_ROOT
, filetype
, command
, &commandlen
) == ERROR_SUCCESS
)
465 if (key
) strcpy(key
, filetype
);
471 /* FIXME: it seems all Windows version don't behave the same here.
472 * the doc states that this ddeexec information can be found after
474 * on Win98, it doesn't appear, but I think it does on Win2k
476 /* Get the parameters needed by the application
477 from the associated ddeexec key */
478 tmp
= strstr(filetype
, "command");
480 strcat(filetype
, "ddeexec");
482 if (RegQueryValueA(HKEY_CLASSES_ROOT
, filetype
, param
, ¶mlen
) == ERROR_SUCCESS
)
484 strcat(command
, " ");
485 strcat(command
, param
);
486 commandlen
+= paramlen
;
490 command
[commandlen
] = '\0';
492 return 33; /* FIXME see SHELL_FindExecutable() */
495 return 31; /* default - 'No association was found' */
498 /*************************************************************************
499 * SHELL_FindExecutable [Internal]
501 * Utility for code sharing between FindExecutable and ShellExecute
503 * lpFile the name of a file
504 * lpOperation the operation on it (open)
506 * lpResult a buffer, big enough :-(, to store the command to do the
507 * operation on the file
508 * key a buffer, big enough, to get the key name to do actually the
509 * command (it'll be used afterwards for more information
512 UINT
SHELL_FindExecutable(LPCSTR lpPath
, LPCSTR lpFile
, LPCSTR lpOperation
,
513 LPSTR lpResult
, LPSTR key
, void **env
, LPITEMIDLIST pidl
, LPCSTR args
)
515 char *extension
= NULL
; /* pointer to file extension */
516 char tmpext
[5]; /* local copy to munge as we please */
517 char filetype
[256]; /* registry name for this filetype */
518 LONG filetypelen
= 256; /* length of above */
519 char command
[256]; /* command from registry */
520 char buffer
[256]; /* Used to GetProfileString */
521 UINT retval
= 31; /* default - 'No association was found' */
522 char *tok
; /* token pointer */
523 char xlpFile
[256] = ""; /* result of SearchPath */
524 DWORD attribs
; /* file attributes */
526 TRACE("%s\n", (lpFile
!= NULL
) ? lpFile
: "-");
528 lpResult
[0] = '\0'; /* Start off with an empty return string */
529 if (key
) *key
= '\0';
531 /* trap NULL parameters on entry */
532 if ((lpFile
== NULL
) || (lpResult
== NULL
))
534 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n", lpFile
, lpResult
);
535 return 2; /* File not found. Close enough, I guess. */
538 if (SHELL_TryAppPath( lpFile
, lpResult
, env
))
540 TRACE("found %s via App Paths\n", lpResult
);
544 if (SearchPathA(lpPath
, lpFile
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
546 TRACE("SearchPathA returned non-zero\n");
548 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
551 attribs
= GetFileAttributesA(lpFile
);
553 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
555 strcpy(filetype
, "Folder");
556 filetypelen
= 6; /* strlen("Folder") */
560 /* First thing we need is the file's extension */
561 extension
= PathFindExtensionA(xlpFile
); /* Assume last "." is the one; */
562 /* File->Run in progman uses */
564 TRACE("xlpFile=%s,extension=%s\n", xlpFile
, extension
);
566 if ((extension
== NULL
) || (extension
== &xlpFile
[strlen(xlpFile
)]))
568 WARN("Returning 31 - No association\n");
569 return 31; /* no association */
572 /* Make local copy & lowercase it for reg & 'programs=' lookup */
573 lstrcpynA(tmpext
, extension
, 5);
575 TRACE("%s file\n", tmpext
);
577 /* Three places to check: */
578 /* 1. win.ini, [windows], programs (NB no leading '.') */
579 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
580 /* 3. win.ini, [extensions], extension (NB no leading '.' */
581 /* All I know of the order is that registry is checked before */
582 /* extensions; however, it'd make sense to check the programs */
583 /* section first, so that's what happens here. */
585 /* See if it's a program - if GetProfileString fails, we skip this
586 * section. Actually, if GetProfileString fails, we've probably
587 * got a lot more to worry about than running a program... */
588 if (GetProfileStringA("windows", "programs", "exe pif bat cmd com",
589 buffer
, sizeof(buffer
)) > 0)
593 for (i
=0; i
<strlen(buffer
); i
++) buffer
[i
] = tolower(buffer
[i
]);
595 tok
= strtok(buffer
, " \t"); /* ? */
598 if (strcmp(tok
, &tmpext
[1]) == 0) /* have to skip the leading "." */
600 strcpy(lpResult
, xlpFile
);
601 /* Need to perhaps check that the file has a path
603 TRACE("found %s\n", lpResult
);
606 /* Greater than 32 to indicate success FIXME According to the
607 * docs, I should be returning a handle for the
608 * executable. Does this mean I'm supposed to open the
609 * executable file or something? More RTFM, I guess... */
611 tok
= strtok(NULL
, " \t");
616 if (RegQueryValueA(HKEY_CLASSES_ROOT
, tmpext
, filetype
, &filetypelen
) != ERROR_SUCCESS
)
624 /* pass the operation string to _FindExecutableByOperation() */
625 filetype
[filetypelen
] = '\0';
626 retval
= _FindExecutableByOperation(lpPath
, lpFile
, lpOperation
, key
, filetype
, command
);
630 char operation
[MAX_PATH
];
633 strcat(filetype
, "\\shell");
635 /* enumerate the operation subkeys in the registry and search for one with an associated command */
636 if (RegOpenKeyA(HKEY_CLASSES_ROOT
, filetype
, &hkey
) == ERROR_SUCCESS
)
641 if (RegEnumKeyA(hkey
, idx
, operation
, MAX_PATH
) != ERROR_SUCCESS
)
644 filetype
[filetypelen
] = '\0';
645 retval
= _FindExecutableByOperation(lpPath
, lpFile
, operation
, key
, filetype
, command
);
657 argifyA(lpResult
, sizeof(lpResult
), command
, xlpFile
, pidl
, args
);
659 /* Remove double quotation marks and command line arguments */
660 if (*lpResult
== '"')
663 while (*(p
+ 1) != '"')
672 else if (extension
) /* Check win.ini */
674 /* Toss the leading dot */
676 if (GetProfileStringA("extensions", extension
, "", command
,
677 sizeof(command
)) > 0)
679 if (strlen(command
) != 0)
681 strcpy(lpResult
, command
);
682 tok
= strstr(lpResult
, "^"); /* should be ^.extension? */
687 strcat(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
688 tok
= strstr(command
, "^"); /* see above */
689 if ((tok
!= NULL
) && (strlen(tok
)>5))
690 strcat(lpResult
, &tok
[5]);
693 retval
= 33; /* FIXME - see above */
698 TRACE("returning %s\n", lpResult
);
702 /******************************************************************
705 * callback for the DDE connection. not really usefull
707 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
709 HDDEDATA hData
, DWORD dwData1
, DWORD dwData2
)
714 /******************************************************************
717 * ShellExecute helper. Used to do an operation with a DDE connection
719 * Handles both the direct connection (try #1), and if it fails,
720 * launching an application and trying (#2) to connect to it
723 static unsigned dde_connect(char* key
, char* start
, char* ddeexec
,
724 const char* lpFile
, void *env
,
725 LPCSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteA1632 execfunc
,
726 LPSHELLEXECUTEINFOA psei
, LPSHELLEXECUTEINFOA psei_out
)
728 char* endkey
= key
+ strlen(key
);
729 char app
[256], topic
[256], ifexec
[256];
731 LONG applen
, topiclen
, ifexeclen
;
735 HSZ hszApp
, hszTopic
;
739 strcpy(endkey
, "\\application");
740 applen
= sizeof(app
);
741 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, app
, &applen
) != ERROR_SUCCESS
)
743 FIXME("default app name NIY %s\n", key
);
747 strcpy(endkey
, "\\topic");
748 topiclen
= sizeof(topic
);
749 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, topic
, &topiclen
) != ERROR_SUCCESS
)
751 strcpy(topic
, "System");
754 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
759 hszApp
= DdeCreateStringHandleA(ddeInst
, app
, CP_WINANSI
);
760 hszTopic
= DdeCreateStringHandleA(ddeInst
, topic
, CP_WINANSI
);
762 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
766 TRACE("Launching '%s'\n", start
);
767 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
770 TRACE("Couldn't launch\n");
773 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
776 TRACE("Couldn't connect. ret=%d\n", ret
);
777 ret
= 30; /* whatever */
780 strcpy(endkey
, "\\ifexec");
781 ifexeclen
= sizeof(ifexec
);
782 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
788 argifyA(res
, sizeof(res
), exec
, lpFile
, pidl
, szCommandline
);
789 TRACE("%s %s => %s\n", exec
, lpFile
, res
);
791 ret
= (DdeClientTransaction((LPBYTE
)res
, strlen(res
)+1, hConv
, 0L, 0,
792 XTYP_EXECUTE
, 10000, &tid
) != DMLERR_NO_ERROR
) ? 31 : 33;
793 DdeDisconnect(hConv
);
795 DdeUninitialize(ddeInst
);
799 /*************************************************************************
800 * execute_from_key [Internal]
802 static UINT
execute_from_key(LPSTR key
, LPCSTR lpFile
, void *env
, LPCSTR szCommandline
,
803 SHELL_ExecuteA1632 execfunc
,
804 LPSHELLEXECUTEINFOA psei
, LPSHELLEXECUTEINFOA psei_out
)
807 LONG cmdlen
= sizeof(cmd
);
810 /* Get the application for the registry */
811 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
814 char param
[256] = "";
817 /* Get the parameters needed by the application
818 from the associated ddeexec key */
819 tmp
= strstr(key
, "command");
821 strcpy(tmp
, "ddeexec");
823 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, param
, ¶mlen
) == ERROR_SUCCESS
)
825 TRACE("Got ddeexec %s => %s\n", key
, param
);
826 retval
= dde_connect(key
, cmd
, param
, lpFile
, env
, szCommandline
, psei
->lpIDList
, execfunc
, psei
, psei_out
);
830 /* Is there a replace() function anywhere? */
832 argifyA(param
, sizeof(param
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
);
834 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
837 else TRACE("ooch\n");
842 /*************************************************************************
843 * FindExecutableA [SHELL32.@]
845 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
847 UINT retval
= 31; /* default - 'No association was found' */
850 TRACE("File %s, Dir %s\n",
851 (lpFile
!= NULL
? lpFile
: "-"), (lpDirectory
!= NULL
? lpDirectory
: "-"));
853 lpResult
[0] = '\0'; /* Start off with an empty return string */
855 /* trap NULL parameters on entry */
856 if ((lpFile
== NULL
) || (lpResult
== NULL
))
858 /* FIXME - should throw a warning, perhaps! */
859 return (HINSTANCE
)2; /* File not found. Close enough, I guess. */
864 GetCurrentDirectoryA(sizeof(old_dir
), old_dir
);
865 SetCurrentDirectoryA(lpDirectory
);
868 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, "open", lpResult
, NULL
, NULL
, NULL
, NULL
);
870 TRACE("returning %s\n", lpResult
);
872 SetCurrentDirectoryA(old_dir
);
873 return (HINSTANCE
)retval
;
876 /*************************************************************************
877 * FindExecutableW [SHELL32.@]
879 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
881 FIXME("(%p,%p,%p): stub\n", lpFile
, lpDirectory
, lpResult
);
882 return (HINSTANCE
)31; /* default - 'No association was found' */
885 /*************************************************************************
886 * ShellExecuteExA32 [Internal]
888 * FIXME: use PathProcessCommandA() to processes the command line string
889 * use PathResolveA() to search for the fully qualified executable path
891 BOOL WINAPI
ShellExecuteExA32(LPSHELLEXECUTEINFOA psei
, SHELL_ExecuteA1632 execfunc
)
893 CHAR szApplicationName
[MAX_PATH
+2], szParameters
[MAX_PATH
], fileName
[MAX_PATH
], szDir
[MAX_PATH
];
894 SHELLEXECUTEINFOA sei_tmp
; /* modifyable copy of SHELLEXECUTEINFO struct */
896 char szProtocol
[256];
902 memcpy(&sei_tmp
, psei
, sizeof(sei_tmp
));
904 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
905 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_a(sei_tmp
.lpVerb
),
906 debugstr_a(sei_tmp
.lpFile
), debugstr_a(sei_tmp
.lpParameters
),
907 debugstr_a(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
908 (sei_tmp
.fMask
& SEE_MASK_CLASSNAME
) ? debugstr_a(sei_tmp
.lpClass
) : "not used");
910 psei
->hProcess
= NULL
;
913 strcpy(szApplicationName
, sei_tmp
.lpFile
);
915 *szApplicationName
= '\0';
917 if (sei_tmp
.lpParameters
)
918 strcpy(szParameters
, sei_tmp
.lpParameters
);
920 *szParameters
= '\0';
922 if (sei_tmp
.lpDirectory
)
923 strcpy(szDir
, sei_tmp
.lpDirectory
);
927 sei_tmp
.lpFile
= szApplicationName
;
928 sei_tmp
.lpParameters
= szParameters
;
929 sei_tmp
.lpDirectory
= szDir
;
931 if (sei_tmp
.fMask
& (SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
932 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
933 SEE_MASK_DOENVSUBST
| SEE_MASK_FLAG_NO_UI
| SEE_MASK_UNICODE
|
934 SEE_MASK_NO_CONSOLE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
))
936 FIXME("flags ignored: 0x%08lx\n", sei_tmp
.fMask
);
939 /* process the IDList */
940 if (sei_tmp
.fMask
& SEE_MASK_INVOKEIDLIST
) /* 0x0c: includes SEE_MASK_IDLIST */
942 IShellExecuteHookA
* pSEH
;
944 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookA
, (LPVOID
*)&pSEH
, NULL
);
948 hr
= IShellExecuteHookA_Execute(pSEH
, psei
);
950 IShellExecuteHookA_Release(pSEH
);
956 /* try to translate PIDL directly into the corresponding file system path */
957 if (SUCCEEDED(SHELL_GetPathFromIDListA(sei_tmp
.lpIDList
, szApplicationName
/*sei_tmp.lpFile*/, sizeof(szApplicationName
))))
959 sei_tmp
.lpIDList
= NULL
;
960 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
963 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, sei_tmp
.lpFile
);
966 if (sei_tmp
.fMask
& (SEE_MASK_CLASSNAME
| SEE_MASK_CLASSKEY
))
968 /* launch a document by fileclass like 'WordPad.Document.1' */
969 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
970 /* FIXME: szParameters should not be of a fixed size. Plus MAX_PATH is way too short! */
971 if (sei_tmp
.fMask
& SEE_MASK_CLASSKEY
)
972 HCR_GetExecuteCommandExA(sei_tmp
.hkeyClass
,
973 sei_tmp
.fMask
&SEE_MASK_CLASSNAME
? sei_tmp
.lpClass
: NULL
,
974 sei_tmp
.lpVerb
?sei_tmp
.lpVerb
:"open",
975 szParameters
/*sei_tmp.lpParameters*/, sizeof(szParameters
));
976 else if (sei_tmp
.fMask
& SEE_MASK_CLASSNAME
)
977 HCR_GetExecuteCommandA(sei_tmp
.lpClass
, sei_tmp
.lpVerb
?sei_tmp
.lpVerb
:"open",
978 szParameters
/*sei_tmp.lpParameters*/, sizeof(szParameters
));
980 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
981 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", sei_tmp
.lpParameters
, sei_tmp
.lpFile
);
985 if (!argifyA(buffer
, sizeof(buffer
), sei_tmp
.lpParameters
, sei_tmp
.lpFile
, sei_tmp
.lpIDList
, NULL
) && sei_tmp
.lpFile
[0])
988 strcat(buffer
, sei_tmp
.lpFile
);
991 retval
= execfunc(buffer
, NULL
, FALSE
, &sei_tmp
, psei
);
998 /* Else, try to execute the filename */
999 TRACE("execute:'%s','%s'\n", sei_tmp
.lpFile
, sei_tmp
.lpParameters
);
1002 /* resolve shell shortcuts */
1003 ext
= PathFindExtensionA(sei_tmp
.lpFile
);
1005 if (ext
&& !strcasecmp(ext
, ".lnk")) /* or check for: shell_attribs & SFGAO_LINK */
1007 if (SUCCEEDED(SHELL_ResolveShortCutA(&sei_tmp
)))
1009 /* repeat IDList processing if needed */
1010 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1012 IShellExecuteHookA
* pSEH
;
1014 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookA
, (LPVOID
*)&pSEH
, NULL
);
1018 hr
= IShellExecuteHookA_Execute(pSEH
, psei
);
1020 IShellExecuteHookA_Release(pSEH
);
1026 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, sei_tmp
.lpFile
);
1032 /* Has the IDList not yet been translated? */
1033 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1035 /* last chance to translate IDList: now also allow CLSID paths */
1036 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteA(sei_tmp
.lpIDList
, buffer
, sizeof(buffer
)))) {
1037 if (buffer
[0]==':' && buffer
[1]==':') {
1038 /* open shell folder for the specified class GUID */
1039 strcpy(szParameters
, buffer
);
1040 strcpy(szApplicationName
, "explorer.exe");
1042 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1043 } else if (HCR_GetExecuteCommandA("Folder", sei_tmp
.lpVerb
?sei_tmp
.lpVerb
:"open", buffer
, sizeof(buffer
))) {
1044 argifyA(szApplicationName
, sizeof(szApplicationName
), buffer
, NULL
, sei_tmp
.lpIDList
, NULL
);
1046 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1052 /* expand environment strings */
1054 if (ExpandEnvironmentStringsA(sei_tmp
.lpFile
, buffer
, MAX_PATH
))
1055 lstrcpyA(szApplicationName
/*sei_tmp.lpFile*/, buffer
);
1057 if (*sei_tmp
.lpParameters
)
1058 if (ExpandEnvironmentStringsA(sei_tmp
.lpParameters
, buffer
, MAX_PATH
))
1059 lstrcpyA(szParameters
/*sei_tmp.lpParameters*/, buffer
);
1061 if (*sei_tmp
.lpDirectory
)
1062 if (ExpandEnvironmentStringsA(sei_tmp
.lpDirectory
, buffer
, MAX_PATH
))
1063 lstrcpyA(szDir
/*sei_tmp.lpDirectory*/, buffer
);
1066 /* separate out command line arguments from executable file name */
1067 if (!*sei_tmp
.lpParameters
) {
1068 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1069 if (sei_tmp
.lpFile
[0] == '"') {
1070 LPSTR src
= szApplicationName
/*sei_tmp.lpFile*/ + 1;
1071 LPSTR dst
= fileName
;
1074 /* copy the unquoted executabe path to 'fileName' */
1075 while(*src
&& *src
!='"')
1083 while(isspace(*src
))
1088 /* copy the parameter string to 'szParameters' */
1089 strcpy(szParameters
, src
);
1091 /* terminate previous command string after the quote character */
1096 /* If the executable name is not quoted, we have to use this search loop here,
1097 that in CreateProcess() is not sufficient because it does not handle shell links. */
1098 char buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1101 LPSTR beg
= szApplicationName
/*sei_tmp.lpFile*/;
1102 for(s
=beg
; (space
=strchr(s
, ' ')); s
=space
+1) {
1103 int idx
= space
-sei_tmp
.lpFile
;
1104 strncpy(buffer
, sei_tmp
.lpFile
, idx
);
1107 if (SearchPathA(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
1109 /* separate out command from parameter string */
1110 LPCSTR p
= space
+ 1;
1115 strcpy(szParameters
, p
);
1122 strcpy(fileName
, sei_tmp
.lpFile
);
1125 strcpy(fileName
, sei_tmp
.lpFile
);
1129 if (sei_tmp
.lpParameters
[0]) {
1130 strcat(szApplicationName
/*sei_tmp.lpFile*/, " ");
1131 strcat(szApplicationName
/*sei_tmp.lpFile*/, sei_tmp
.lpParameters
);
1134 retval
= execfunc(sei_tmp
.lpFile
, NULL
, FALSE
, &sei_tmp
, psei
);
1137 /* Now, that we have successfully launched a process, we can free the PIDL.
1138 It may have been used before for %I command line options. */
1139 if (sei_tmp
.lpIDList
!=psei
->lpIDList
&& sei_tmp
.lpIDList
)
1140 SHFree(sei_tmp
.lpIDList
);
1142 TRACE("execfunc: retval=%d psei->hInstApp=%p\n", retval
, psei
->hInstApp
);
1146 /* Else, try to find the executable */
1148 retval
= SHELL_FindExecutable(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, lpFile
, sei_tmp
.lpVerb
, buffer
, szProtocol
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1150 if (retval
> 32) /* Found */
1152 CHAR szQuotedCmd
[MAX_PATH
+2];
1153 /* Must quote to handle case where 'buffer' contains spaces,
1154 * else security hole if malicious user creates executable file "C:\\Program"
1156 * FIXME: If we don't have set explicitly command line arguments, we must first
1157 * split executable path from optional command line arguments. Otherwise we would quote
1158 * the complete string with executable path _and_ arguments, which is not what we want.
1160 if (sei_tmp
.lpParameters
[0])
1161 sprintf(szQuotedCmd
, "\"%s\" %s", buffer
, sei_tmp
.lpParameters
);
1163 sprintf(szQuotedCmd
, "\"%s\"", buffer
);
1165 TRACE("%s/%s => %s/%s\n", sei_tmp
.lpFile
, sei_tmp
.lpVerb
?sei_tmp
.lpVerb
:"NULL", szQuotedCmd
, szProtocol
);
1168 retval
= execute_from_key(szProtocol
, lpFile
, env
, sei_tmp
.lpParameters
, execfunc
, &sei_tmp
, psei
);
1170 retval
= execfunc(szQuotedCmd
, env
, FALSE
, &sei_tmp
, psei
);
1172 if (env
) HeapFree( GetProcessHeap(), 0, env
);
1174 else if (PathIsURLA((LPSTR
)lpFile
)) /* File not found, check for URL */
1179 lpstrRes
= strchr(lpFile
, ':');
1181 iSize
= lpstrRes
- lpFile
;
1183 iSize
= strlen(lpFile
);
1185 TRACE("Got URL: %s\n", lpFile
);
1186 /* Looking for ...protocol\shell\<verb>\command */
1187 strncpy(szProtocol
, lpFile
, iSize
);
1188 szProtocol
[iSize
] = '\0';
1189 strcat(szProtocol
, "\\shell\\");
1190 strcat(szProtocol
, sei_tmp
.lpVerb
? sei_tmp
.lpVerb
: "open"); /*FIXME: enumerate registry subkeys - compare with the loop into SHELL_FindExecutable() */
1191 strcat(szProtocol
, "\\command");
1193 /* Remove File Protocol from lpFile */
1194 /* In the case file://path/file */
1195 if (!strncasecmp(lpFile
, "file", iSize
))
1198 while (*lpFile
== ':') lpFile
++;
1201 retval
= execute_from_key(szProtocol
, lpFile
, NULL
, sei_tmp
.lpParameters
, execfunc
, &sei_tmp
, psei
);
1203 /* Check if file specified is in the form www.??????.*** */
1204 else if (!strncasecmp(lpFile
, "www", 3))
1206 /* if so, append lpFile http:// and call ShellExecute */
1207 char lpstrTmpFile
[256] = "http://" ;
1208 strcat(lpstrTmpFile
, lpFile
);
1209 retval
= (UINT
)ShellExecuteA(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1212 /* Now we can free the PIDL. It may have been used before for %I command line options. */
1213 if (sei_tmp
.lpIDList
!=psei
->lpIDList
&& sei_tmp
.lpIDList
)
1214 SHFree(sei_tmp
.lpIDList
);
1216 TRACE("ShellExecuteExA32 retval=%d\n", retval
);
1220 psei
->hInstApp
= (HINSTANCE
)retval
;
1224 psei
->hInstApp
= (HINSTANCE
)33;
1228 /*************************************************************************
1229 * ShellExecuteA [SHELL32.290]
1231 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
1232 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1234 SHELLEXECUTEINFOA sei
;
1235 HANDLE hProcess
= 0;
1238 sei
.cbSize
= sizeof(sei
);
1241 sei
.lpVerb
= lpOperation
;
1242 sei
.lpFile
= lpFile
;
1243 sei
.lpParameters
= lpParameters
;
1244 sei
.lpDirectory
= lpDirectory
;
1245 sei
.nShow
= iShowCmd
;
1250 sei
.hProcess
= hProcess
;
1252 ShellExecuteExA32(&sei
, SHELL_ExecuteA
);
1253 return sei
.hInstApp
;
1256 /*************************************************************************
1257 * ShellExecuteEx [SHELL32.291]
1260 BOOL WINAPI
ShellExecuteExAW (LPVOID sei
)
1262 if (SHELL_OsIsUnicode())
1263 return ShellExecuteExW(sei
);
1264 return ShellExecuteExA32(sei
, SHELL_ExecuteA
);
1267 /*************************************************************************
1268 * ShellExecuteExA [SHELL32.292]
1271 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1273 BOOL ret
= ShellExecuteExA32(sei
, SHELL_ExecuteA
);
1275 TRACE("ShellExecuteExA(): ret=%d\n", ret
);
1280 /*************************************************************************
1281 * ShellExecuteExW [SHELL32.293]
1284 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1286 SHELLEXECUTEINFOA seiA
;
1291 memcpy(&seiA
, sei
, sizeof(SHELLEXECUTEINFOA
));
1294 seiA
.lpVerb
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpVerb
);
1297 seiA
.lpFile
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpFile
);
1299 if (sei
->lpParameters
)
1300 seiA
.lpParameters
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpParameters
);
1302 if (sei
->lpDirectory
)
1303 seiA
.lpDirectory
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpDirectory
);
1305 if ((sei
->fMask
& SEE_MASK_CLASSNAME
) && sei
->lpClass
)
1306 seiA
.lpClass
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpClass
);
1308 seiA
.lpClass
= NULL
;
1310 ret
= ShellExecuteExA(&seiA
);
1312 if (seiA
.lpVerb
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpVerb
);
1313 if (seiA
.lpFile
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpFile
);
1314 if (seiA
.lpParameters
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpParameters
);
1315 if (seiA
.lpDirectory
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpDirectory
);
1316 if (seiA
.lpClass
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpClass
);
1318 sei
->hInstApp
= seiA
.hInstApp
;
1320 TRACE("ShellExecuteExW(): ret=%d\n", ret
);
1325 /*************************************************************************
1326 * ShellExecuteW [SHELL32.294]
1328 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1329 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1331 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1332 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1334 SHELLEXECUTEINFOW sei
;
1335 HANDLE hProcess
= 0;
1339 sei
.cbSize
= sizeof(sei
);
1342 sei
.lpVerb
= lpOperation
;
1343 sei
.lpFile
= lpFile
;
1344 sei
.lpParameters
= lpParameters
;
1345 sei
.lpDirectory
= lpDirectory
;
1346 sei
.nShow
= nShowCmd
;
1351 sei
.hProcess
= hProcess
;
1353 ret
= ShellExecuteExW(&sei
);
1355 TRACE("ShellExecuteW(): ret=%d module=%p", ret
, sei
.hInstApp
);
1356 return sei
.hInstApp
;