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"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
56 /***********************************************************************
57 * this function is supposed to expand the escape sequences found in the registry
58 * some diving reported that the following were used:
59 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
64 * %I address of a global item ID (explorer switch /idlist)
65 * %L seems to be %1 as long filename followed by the 8+3 variation
67 * %* all following parameters (see batfile)
69 static BOOL
argify(char* out
, int len
, const char* fmt
, const char* lpFile
, LPITEMIDLIST pidl
, LPCSTR args
)
110 while(*args
&& !isspace(*args
))
114 while(isspace(*args
))
121 if (!done
|| (*fmt
== '1'))
123 /*FIXME Is SearchPath() really needed? We already have separated out the parameter string in args. */
124 if (SearchPathA(NULL
, lpFile
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
129 /* Add double quotation marks unless we already have them (e.g.: "%1" %* for exefile) */
130 if (res
!= out
&& *(res
- 1) == '"')
147 * IE uses this alot for activating things such as windows media
148 * player. This is not verified to be fully correct but it appears
155 res
+= strlen(lpFile
);
162 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
163 pv
= SHLockShared(hmem
, 0);
164 res
+= sprintf(res
, ":%p", pv
);
169 default: FIXME("Unknown escape sequence %%%c\n", *fmt
);
181 /*************************************************************************
182 * _ResolveShortCut [Internal]
185 static HRESULT
_ResolveShortCut(LPWSTR path
, LPWSTR wdir
, LPWSTR args
, HWND hwnd
, int* pshowcmd
, LPITEMIDLIST
* ppidl
)
189 HRESULT hr
= SHGetDesktopFolder(&psf
);
197 hr
= IShellFolder_ParseDisplayName(psf
, 0, 0, path
, &l
, &pidl
, 0);
202 hr
= IShellFolder_GetUIObjectOf(psf
, NULL
, 1, (LPCITEMIDLIST
*)&pidl
, &IID_IShellLinkW
, NULL
, (LPVOID
*)&psl
);
205 hr
= IShellLinkW_Resolve(psl
, hwnd
, SLR_NO_UI
);
208 hr
= IShellLinkW_GetPath(psl
, path
, MAX_PATH
, NULL
, SLGP_UNCPRIORITY
);
212 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
213 hr
= IShellLinkW_GetIDList(psl
, ppidl
);
216 /* get command line arguments and display mode if available */
217 IShellLinkW_GetWorkingDirectory(psl
, wdir
, MAX_PATH
);
218 IShellLinkW_GetArguments(psl
, args
, MAX_PATH
);
219 IShellLinkW_GetShowCmd(psl
, pshowcmd
);
224 IShellLinkW_Release(psl
);
230 IShellFolder_Release(psf
);
236 /*************************************************************************
237 * SHELL_ExecuteA [Internal]
240 static UINT
SHELL_ExecuteA(char *lpCmd
, void *env
, const char* lpDir
, LPSHELLEXECUTEINFOA sei
, BOOL shWait
)
242 STARTUPINFOA startup
;
243 PROCESS_INFORMATION info
;
246 TRACE("Execute %s from directory %s\n", lpCmd
, lpDir
);
247 ZeroMemory(&startup
,sizeof(STARTUPINFOA
));
248 startup
.cb
= sizeof(STARTUPINFOA
);
249 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
250 startup
.wShowWindow
= sei
->nShow
;
251 if (CreateProcessA(NULL
, lpCmd
, NULL
, NULL
, FALSE
, 0,
252 env
, lpDir
, &startup
, &info
))
254 /* Give 30 seconds to the app to come up, if desired. Probably only needed
255 when starting app immediately before making a DDE connection. */
257 if (WaitForInputIdle( info
.hProcess
, 30000 ) == -1)
258 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
260 if(sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
261 sei
->hProcess
= info
.hProcess
;
263 CloseHandle( info
.hProcess
);
264 CloseHandle( info
.hThread
);
266 else if ((retval
= GetLastError()) >= 32)
268 FIXME("Strange error set by CreateProcess: %d\n", retval
);
269 retval
= ERROR_BAD_FORMAT
;
272 sei
->hInstApp
= (HINSTANCE
)retval
;
277 /***********************************************************************
280 * Build the environment for the new process, adding the specified
281 * path to the PATH variable. Returned pointer must be freed by caller.
283 static void *build_env( const char *path
)
285 char *strings
, *new_env
;
287 int total
= strlen(path
) + 1;
288 BOOL got_path
= FALSE
;
290 if (!(strings
= GetEnvironmentStringsA())) return NULL
;
294 int len
= strlen(p
) + 1;
295 if (!strncasecmp( p
, "PATH=", 5 )) got_path
= TRUE
;
299 if (!got_path
) total
+= 5; /* we need to create PATH */
300 total
++; /* terminating null */
302 if (!(new_env
= HeapAlloc( GetProcessHeap(), 0, total
)))
304 FreeEnvironmentStringsA( strings
);
311 int len
= strlen(p
) + 1;
312 memcpy( p2
, p
, len
);
313 if (!strncasecmp( p
, "PATH=", 5 ))
316 strcpy( p2
+ len
, path
);
317 p2
+= strlen(path
) + 1;
324 strcpy( p2
, "PATH=" );
326 p2
+= strlen(p2
) + 1;
329 FreeEnvironmentStringsA( strings
);
334 /***********************************************************************
337 * Helper function for SHELL_FindExecutable
338 * @param lpResult - pointer to a buffer of size MAX_PATH
339 * On entry: szName is a filename (probably without path separators).
340 * On exit: if szName found in "App Path", place full path in lpResult, and return true
342 static BOOL
SHELL_TryAppPath( LPCSTR szName
, LPSTR lpResult
, void**env
)
350 if (env
) *env
= NULL
;
351 sprintf(buffer
, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s", szName
);
352 res
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
356 res
= RegQueryValueA(hkApp
, NULL
, lpResult
, &len
);
362 DWORD count
= sizeof(buffer
);
363 if (!RegQueryValueExA(hkApp
, "Path", NULL
, NULL
, buffer
, &count
) && buffer
[0])
364 *env
= build_env( buffer
);
368 if (hkApp
) RegCloseKey(hkApp
);
372 static UINT
_FindExecutableByOperation(LPCSTR lpPath
, LPCSTR lpFile
, LPCSTR lpOperation
, LPSTR key
, LPSTR filetype
, LPSTR command
)
374 LONG commandlen
= 256; /* This is the most DOS can handle :) */
376 /* Looking for ...buffer\shell\<verb>\command */
377 strcat(filetype
, "\\shell\\");
378 strcat(filetype
, lpOperation
);
379 strcat(filetype
, "\\command");
381 if (RegQueryValueA(HKEY_CLASSES_ROOT
, filetype
, command
, &commandlen
) == ERROR_SUCCESS
)
383 if (key
) strcpy(key
, filetype
);
389 /* FIXME: it seems all Windows version don't behave the same here.
390 * the doc states that this ddeexec information can be found after
392 * on Win98, it doesn't appear, but I think it does on Win2k
394 /* Get the parameters needed by the application
395 from the associated ddeexec key */
396 tmp
= strstr(filetype
, "command");
398 strcat(filetype
, "ddeexec");
400 if (RegQueryValueA(HKEY_CLASSES_ROOT
, filetype
, param
, ¶mlen
) == ERROR_SUCCESS
)
402 strcat(command
, " ");
403 strcat(command
, param
);
404 commandlen
+= paramlen
;
408 command
[commandlen
] = '\0';
410 return 33; /* FIXME see SHELL_FindExecutable() */
413 return 31; /* default - 'No association was found' */
416 /*************************************************************************
417 * SHELL_FindExecutable [Internal]
419 * Utility for code sharing between FindExecutable and ShellExecute
421 * lpFile the name of a file
422 * lpOperation the operation on it (open)
424 * lpResult a buffer, big enough :-(, to store the command to do the
425 * operation on the file
426 * key a buffer, big enough, to get the key name to do actually the
427 * command (it'll be used afterwards for more information
430 UINT
SHELL_FindExecutable(LPCSTR lpPath
, LPCSTR lpFile
, LPCSTR lpOperation
,
431 LPSTR lpResult
, LPSTR key
, void **env
, LPITEMIDLIST pidl
, LPCSTR args
)
433 char *extension
= NULL
; /* pointer to file extension */
434 char tmpext
[5]; /* local copy to munge as we please */
435 char filetype
[256]; /* registry name for this filetype */
436 LONG filetypelen
= 256; /* length of above */
437 char command
[256]; /* command from registry */
438 char buffer
[256]; /* Used to GetProfileString */
439 UINT retval
= 31; /* default - 'No association was found' */
440 char *tok
; /* token pointer */
441 char xlpFile
[256] = ""; /* result of SearchPath */
442 DWORD attribs
; /* file attributes */
444 TRACE("%s\n", (lpFile
!= NULL
) ? lpFile
: "-");
446 lpResult
[0] = '\0'; /* Start off with an empty return string */
447 if (key
) *key
= '\0';
449 /* trap NULL parameters on entry */
450 if ((lpFile
== NULL
) || (lpResult
== NULL
))
452 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n", lpFile
, lpResult
);
453 return 2; /* File not found. Close enough, I guess. */
456 if (SHELL_TryAppPath( lpFile
, lpResult
, env
))
458 TRACE("found %s via App Paths\n", lpResult
);
462 if (SearchPathA(lpPath
, lpFile
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
464 TRACE("SearchPathA returned non-zero\n");
466 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
469 attribs
= GetFileAttributesA(lpFile
);
471 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
473 strcpy(filetype
, "Folder");
474 filetypelen
= 6; /* strlen("Folder") */
478 /* First thing we need is the file's extension */
479 extension
= PathFindExtensionA(xlpFile
); /* Assume last "." is the one; */
480 /* File->Run in progman uses */
482 TRACE("xlpFile=%s,extension=%s\n", xlpFile
, extension
);
484 if ((extension
== NULL
) || (extension
== &xlpFile
[strlen(xlpFile
)]))
486 WARN("Returning 31 - No association\n");
487 return 31; /* no association */
490 /* Make local copy & lowercase it for reg & 'programs=' lookup */
491 lstrcpynA(tmpext
, extension
, 5);
493 TRACE("%s file\n", tmpext
);
495 /* Three places to check: */
496 /* 1. win.ini, [windows], programs (NB no leading '.') */
497 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
498 /* 3. win.ini, [extensions], extension (NB no leading '.' */
499 /* All I know of the order is that registry is checked before */
500 /* extensions; however, it'd make sense to check the programs */
501 /* section first, so that's what happens here. */
503 /* See if it's a program - if GetProfileString fails, we skip this
504 * section. Actually, if GetProfileString fails, we've probably
505 * got a lot more to worry about than running a program... */
506 if (GetProfileStringA("windows", "programs", "exe pif bat cmd com",
507 buffer
, sizeof(buffer
)) > 0)
511 for (i
=0; i
<strlen(buffer
); i
++) buffer
[i
] = tolower(buffer
[i
]);
513 tok
= strtok(buffer
, " \t"); /* ? */
516 if (strcmp(tok
, &tmpext
[1]) == 0) /* have to skip the leading "." */
518 strcpy(lpResult
, xlpFile
);
519 /* Need to perhaps check that the file has a path
521 TRACE("found %s\n", lpResult
);
524 /* Greater than 32 to indicate success FIXME According to the
525 * docs, I should be returning a handle for the
526 * executable. Does this mean I'm supposed to open the
527 * executable file or something? More RTFM, I guess... */
529 tok
= strtok(NULL
, " \t");
534 if (RegQueryValueA(HKEY_CLASSES_ROOT
, tmpext
, filetype
, &filetypelen
) != ERROR_SUCCESS
)
542 /* pass the operation string to _FindExecutableByOperation() */
543 filetype
[filetypelen
] = '\0';
544 retval
= _FindExecutableByOperation(lpPath
, lpFile
, lpOperation
, key
, filetype
, command
);
548 char operation
[MAX_PATH
];
551 strcat(filetype
, "\\shell");
553 /* enumerate the operation subkeys in the registry and search for one with an associated command */
554 if (RegOpenKeyA(HKEY_CLASSES_ROOT
, filetype
, &hkey
) == ERROR_SUCCESS
)
559 if (RegEnumKeyA(hkey
, idx
, operation
, MAX_PATH
) != ERROR_SUCCESS
)
562 filetype
[filetypelen
] = '\0';
563 retval
= _FindExecutableByOperation(lpPath
, lpFile
, operation
, key
, filetype
, command
);
575 argify(lpResult
, sizeof(lpResult
), command
, xlpFile
, pidl
, args
);
577 /* Remove double quotation marks */
578 if (*lpResult
== '"')
581 while (*(p
+ 1) != '"' && *(p
+ 1) != ' ')
590 else if (extension
) /* Check win.ini */
592 /* Toss the leading dot */
594 if (GetProfileStringA("extensions", extension
, "", command
,
595 sizeof(command
)) > 0)
597 if (strlen(command
) != 0)
599 strcpy(lpResult
, command
);
600 tok
= strstr(lpResult
, "^"); /* should be ^.extension? */
605 strcat(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
606 tok
= strstr(command
, "^"); /* see above */
607 if ((tok
!= NULL
) && (strlen(tok
)>5))
608 strcat(lpResult
, &tok
[5]);
611 retval
= 33; /* FIXME - see above */
616 TRACE("returning %s\n", lpResult
);
620 /******************************************************************
623 * callback for the DDE connection. not really usefull
625 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
627 HDDEDATA hData
, DWORD dwData1
, DWORD dwData2
)
632 /******************************************************************
635 * ShellExecute helper. Used to do an operation with a DDE connection
637 * Handles both the direct connection (try #1), and if it fails,
638 * launching an application and trying (#2) to connect to it
641 static unsigned dde_connect(char* key
, char* start
, char* ddeexec
,
642 const char* lpFile
, const char* lpDir
, void *env
,
643 LPSHELLEXECUTEINFOA sei
, SHELL_ExecuteA1632 execfunc
,
644 LPCSTR szCommandline
, LPITEMIDLIST pidl
)
646 char* endkey
= key
+ strlen(key
);
647 char app
[256], topic
[256], ifexec
[256];
648 LONG applen
, topiclen
, ifexeclen
;
652 HSZ hszApp
, hszTopic
;
656 strcpy(endkey
, "\\application");
657 applen
= sizeof(app
);
658 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, app
, &applen
) != ERROR_SUCCESS
)
660 FIXME("default app name NIY %s\n", key
);
664 strcpy(endkey
, "\\topic");
665 topiclen
= sizeof(topic
);
666 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, topic
, &topiclen
) != ERROR_SUCCESS
)
668 strcpy(topic
, "System");
671 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
676 hszApp
= DdeCreateStringHandleA(ddeInst
, app
, CP_WINANSI
);
677 hszTopic
= DdeCreateStringHandleA(ddeInst
, topic
, CP_WINANSI
);
679 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
683 TRACE("Launching '%s'\n", start
);
684 ret
= execfunc(start
, env
, lpDir
, sei
, TRUE
);
687 TRACE("Couldn't launch\n");
690 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
693 TRACE("Couldn't connect. ret=%d\n", ret
);
694 ret
= 30; /* whatever */
697 strcpy(endkey
, "\\ifexec");
698 ifexeclen
= sizeof(ifexec
);
699 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
705 #if 0 /* argify has already been called. */
706 argify(res
, sizeof(res
), exec
, lpFile
, pidl
, szCommandline
);
707 TRACE("%s %s => %s\n", exec
, lpFile
, res
);
710 ret
= (DdeClientTransaction((LPBYTE
)szCommandline
, strlen(szCommandline
) + 1, hConv
, 0L, 0,
711 XTYP_EXECUTE
, 10000, &tid
) != DMLERR_NO_ERROR
) ? 31 : 33;
712 DdeDisconnect(hConv
);
714 DdeUninitialize(ddeInst
);
718 /*************************************************************************
719 * execute_from_key [Internal]
721 static UINT
execute_from_key(LPSTR key
, LPCSTR lpFile
, LPCSTR lpDir
, void *env
,
722 LPSHELLEXECUTEINFOA sei
, SHELL_ExecuteA1632 execfunc
,
723 LPCSTR szCommandline
, LPITEMIDLIST pidl
)
726 LONG cmdlen
= sizeof(cmd
);
729 /* Get the application for the registry */
730 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
733 char param
[256] = "";
736 /* Get the parameters needed by the application
737 from the associated ddeexec key */
738 tmp
= strstr(key
, "command");
740 strcpy(tmp
, "ddeexec");
742 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, param
, ¶mlen
) == ERROR_SUCCESS
)
744 TRACE("Got ddeexec %s => %s\n", key
, param
);
745 retval
= dde_connect(key
, cmd
, param
, lpFile
, lpDir
, env
, sei
, execfunc
, szCommandline
, pidl
);
749 #if 0 /* argify() has already been called. */
750 /* Is there a replace() function anywhere? */
752 argify(param
, sizeof(param
), cmd
, lpFile
, pidl
, szCommandline
);
754 strcpy(param
, szCommandline
);
756 retval
= execfunc(param
, env
, lpDir
, sei
, FALSE
);
759 else TRACE("ooch\n");
764 /*************************************************************************
765 * FindExecutableA [SHELL32.@]
767 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
769 UINT retval
= 31; /* default - 'No association was found' */
772 TRACE("File %s, Dir %s\n",
773 (lpFile
!= NULL
? lpFile
: "-"), (lpDirectory
!= NULL
? lpDirectory
: "-"));
775 lpResult
[0] = '\0'; /* Start off with an empty return string */
777 /* trap NULL parameters on entry */
778 if ((lpFile
== NULL
) || (lpResult
== NULL
))
780 /* FIXME - should throw a warning, perhaps! */
781 return (HINSTANCE
)2; /* File not found. Close enough, I guess. */
786 GetCurrentDirectoryA(sizeof(old_dir
), old_dir
);
787 SetCurrentDirectoryA(lpDirectory
);
790 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, "open", lpResult
, NULL
, NULL
, NULL
, NULL
);
792 TRACE("returning %s\n", lpResult
);
794 SetCurrentDirectoryA(old_dir
);
795 return (HINSTANCE
)retval
;
798 /*************************************************************************
799 * FindExecutableW [SHELL32.@]
801 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
803 FIXME("(%p,%p,%p): stub\n", lpFile
, lpDirectory
, lpResult
);
804 return (HINSTANCE
)31; /* default - 'No association was found' */
807 /*************************************************************************
808 * ShellExecuteExA32 [Internal]
810 * FIXME: use PathResolveA() to search for the fully qualified executable path
811 * use PathProcessCommandA() to processes the command line string
813 BOOL WINAPI
ShellExecuteExA32 (LPSHELLEXECUTEINFOA sei
, SHELL_ExecuteA1632 execfunc
)
815 CHAR szApplicationName
[MAX_PATH
+2], szCommandline
[MAX_PATH
], fileName
[MAX_PATH
], dir
[MAX_PATH
];
818 char lpstrProtocol
[256];
825 LPITEMIDLIST pidl
= sei
->lpIDList
;
826 LPITEMIDLIST tmpPidl
= NULL
;
828 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
829 sei
->fMask
, sei
->hwnd
, debugstr_a(sei
->lpVerb
),
830 debugstr_a(sei
->lpFile
), debugstr_a(sei
->lpParameters
),
831 debugstr_a(sei
->lpDirectory
), sei
->nShow
,
832 (sei
->fMask
& SEE_MASK_CLASSNAME
) ? debugstr_a(sei
->lpClass
) : "not used");
834 sei
->hProcess
= NULL
;
837 strcpy(szApplicationName
, sei
->lpFile
);
839 *szApplicationName
= '\0';
841 if (sei
->lpParameters
)
842 strcpy(szCommandline
, sei
->lpParameters
);
844 *szCommandline
= '\0';
846 if (sei
->lpDirectory
)
847 strcpy(dir
, sei
->lpDirectory
);
851 if (sei
->fMask
& (SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
852 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
853 SEE_MASK_DOENVSUBST
| SEE_MASK_FLAG_NO_UI
| SEE_MASK_UNICODE
|
854 SEE_MASK_NO_CONSOLE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
))
856 FIXME("flags ignored: 0x%08lx\n", sei
->fMask
);
859 /* process the IDList */
860 if (sei
->fMask
& SEE_MASK_INVOKEIDLIST
) /* 0x0c: includes SEE_MASK_IDLIST */
862 IShellExecuteHookA
* pSEH
;
864 HRESULT hr
= SHBindToParent(pidl
, &IID_IShellExecuteHookA
, (LPVOID
*)&pSEH
, NULL
);
867 hr
= IShellExecuteHookA_Execute(pSEH
, sei
);
869 IShellExecuteHookA_Release(pSEH
);
875 if (!SHGetPathFromIDListA(pidl
, szApplicationName
))
878 TRACE("-- idlist=%p (%s)\n", pidl
, szApplicationName
);
881 if (sei
->fMask
& (SEE_MASK_CLASSNAME
| SEE_MASK_CLASSKEY
))
883 /* launch a document by fileclass like 'WordPad.Document.1' */
884 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
885 /* FIXME: szCommandline should not be of a fixed size. Plus MAX_PATH is way too short! */
886 if (sei
->fMask
& SEE_MASK_CLASSKEY
)
887 HCR_GetExecuteCommandExA(sei
->hkeyClass
,
888 (sei
->fMask
& SEE_MASK_CLASSNAME
) ? sei
->lpClass
: NULL
,
889 (sei
->lpVerb
) ? sei
->lpVerb
: "open", szCommandline
, sizeof(szCommandline
));
890 else if (sei
->fMask
& SEE_MASK_CLASSNAME
)
891 HCR_GetExecuteCommandA(sei
->lpClass
, (sei
->lpVerb
) ? sei
->lpVerb
:
892 "open", szCommandline
, sizeof(szCommandline
));
894 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
895 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", szCommandline
, szApplicationName
);
898 done
= argify(cmd
, sizeof(cmd
), szCommandline
, szApplicationName
, pidl
, NULL
);
899 if (!done
&& szApplicationName
[0])
902 strcat(cmd
, szApplicationName
);
904 retval
= execfunc(cmd
, NULL
, dir
, sei
, FALSE
);
911 /* Else, try to execute the filename */
912 TRACE("execute:'%s','%s'\n", szApplicationName
, szCommandline
);
914 /* resolve shell shortcuts */
915 ext
= PathFindExtensionA(szApplicationName
);
916 if (ext
&& !strcasecmp(ext
, ".lnk")) { /* or check for: shell_attribs & SFGAO_LINK */
917 WCHAR cmd
[MAX_PATH
], args
[MAX_PATH
], wdir
[MAX_PATH
];
919 if (MultiByteToWideChar(CP_ACP
, 0, szApplicationName
, -1, cmd
, MAX_PATH
)) {
920 MultiByteToWideChar(CP_ACP
, 0, dir
, -1, wdir
, MAX_PATH
);
922 if (SUCCEEDED(_ResolveShortCut(cmd
, wdir
, args
, sei
->hwnd
, &sei
->nShow
, &tmpPidl
))) {
923 if (!*cmd
&& tmpPidl
) {
924 /* We got a PIDL instead of a file system path. */
925 if (SHGetPathFromIDListA(tmpPidl
, cmd
))
928 if (cmd
[0]==':' && cmd
[1]==':') {
929 /* open shell folder for the specified class GUID */
930 strcpy(szApplicationName
, "explorer.exe");
931 WideCharToMultiByte(CP_ACP
, 0, cmd
, -1, szCommandline
, MAX_PATH
, NULL
, NULL
);
934 } else if (HCR_GetExecuteCommandA("Folder", sei
->lpVerb
? sei
->lpVerb
: "open", szCommandline
, sizeof(szCommandline
))) {
937 /*FIXME: seems to have problems in some cases */
938 if (argify(res
, sizeof(res
), szCommandline
, NULL
, tmpPidl
, NULL
))
939 strcpy(szApplicationName
, res
);
941 szCommandline
[0] = '\0';
947 WideCharToMultiByte(CP_ACP
, 0, cmd
, -1, szApplicationName
, MAX_PATH
, NULL
, NULL
);
948 WideCharToMultiByte(CP_ACP
, 0, wdir
, -1, dir
, MAX_PATH
, NULL
, NULL
);
949 WideCharToMultiByte(CP_ACP
, 0, args
, -1, szCommandline
, MAX_PATH
, NULL
, NULL
);
952 /* If we prevoiusly had a PIDL, it is now resolved, so forget it. */
955 FIXME("We could not resolve the shell shortcut.\n");
959 /* The following code is needed for example to resolve a shortcut
960 to control panel applet "Keyboard", since this is accessed using
961 "rundll32.exe shell32.dll,Control_RunDLL %1,%*" with a command line
962 parameter received from ISF_ControlPanel_fnGetDisplayNameOf(). */
963 if (!*szCommandline
) {
964 /* If the executable path is quoted, handle the rest of the command line as parameters. */
965 if (*szApplicationName
== '"') {
966 LPSTR src
= szApplicationName
+ 1;
967 LPSTR dst
= fileName
;
970 /* copy the unquoted executabe path to 'fileName' */
971 while(*src
&& *src
!='"')
984 /* copy the paremeter string to 'szCommandline' */
985 strcpy(szCommandline
, src
);
987 /* terminate 'szApplicationName' after the quote character */
992 /* If the executrable name is not quoted, we have to use this search loop here,
993 that in CreateProcess() is not sufficient because it does not handle shell links. */
995 char buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
997 LPSTR beg
= szApplicationName
;
998 for(s
=beg
; (space
=strchr(s
, ' ')); s
=space
+1) {
999 int idx
= space
-szApplicationName
;
1000 strncpy(buffer
, szApplicationName
, idx
);
1003 if (SearchPathA(*dir
? dir
: NULL
, buffer
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
1005 /* separate out command from parameter string */
1006 LPCSTR p
= space
+ 1;
1011 strcpy(szCommandline
, p
);
1018 strcpy(fileName
, szApplicationName
);
1021 strcpy(fileName
, szApplicationName
);
1025 if (szCommandline
[0]) {
1026 strcat(szApplicationName
, " ");
1027 strcat(szApplicationName
, szCommandline
);
1030 retval
= execfunc(szApplicationName
, NULL
, dir
, sei
, FALSE
);
1033 /* Now, that we have successfully launched a process, we can free the PIDL.
1034 It may have been used before for %I command line options. */
1038 TRACE("execfunc: retval=%d sei->hInstApp=%p\n", retval
, sei
->hInstApp
);
1042 /* Else, try to find the executable */
1044 retval
= SHELL_FindExecutable(*dir
? dir
: NULL
, lpFile
, sei
->lpVerb
, cmd
, lpstrProtocol
, &env
, pidl
, szCommandline
);
1045 if (retval
> 32) /* Found */
1047 #if 0 /* SHELL_FindExecutable() already quoted by calling argify() */
1048 CHAR szQuotedCmd
[MAX_PATH
+2];
1049 /* Must quote to handle case where cmd contains spaces,
1050 * else security hole if malicious user creates executable file "C:\\Program"
1052 * FIXME: If we don't have set explicitly command line arguments, we must first
1053 * split executable path from optional command line arguments. Otherwise we would quote
1054 * the complete string with executable path _and_ arguments, which is not what we want.
1056 if (szCommandline
[0])
1057 sprintf(szQuotedCmd
, "\"%s\" %s", cmd
, szCommandline
);
1059 sprintf(szQuotedCmd
, "\"%s\"", cmd
);
1060 TRACE("%s/%s => %s/%s\n", szApplicationName
, sei
->lpVerb
?sei
->lpVerb
:"NULL", szQuotedCmd
, lpstrProtocol
);
1062 retval
= execute_from_key(lpstrProtocol
, lpFile
, env
, dir
, sei
, execfunc
, szCommandline
, pidl
);
1064 retval
= execfunc(szQuotedCmd
, env
, dir
, sei
, FALSE
);
1067 retval
= execute_from_key(lpstrProtocol
, lpFile
, env
, dir
, sei
, execfunc
, cmd
, pidl
);
1069 retval
= execfunc(cmd
, env
, dir
, sei
, FALSE
);
1071 if (env
) HeapFree( GetProcessHeap(), 0, env
);
1073 else if (PathIsURLA((LPSTR
)lpFile
)) /* File not found, check for URL */
1078 lpstrRes
= strchr(lpFile
, ':');
1080 iSize
= lpstrRes
- lpFile
;
1082 iSize
= strlen(lpFile
);
1084 TRACE("Got URL: %s\n", lpFile
);
1085 /* Looking for ...protocol\shell\<verb>\command */
1086 strncpy(lpstrProtocol
, lpFile
, iSize
);
1087 lpstrProtocol
[iSize
] = '\0';
1088 strcat(lpstrProtocol
, "\\shell\\");
1089 strcat(lpstrProtocol
, sei
->lpVerb
? sei
->lpVerb
: "open"); /*FIXME: enumerate registry subkeys - compare with the loop into SHELL_FindExecutable() */
1090 strcat(lpstrProtocol
, "\\command");
1092 /* Remove File Protocol from lpFile */
1093 /* In the case file://path/file */
1094 if (!strncasecmp(lpFile
, "file", iSize
))
1097 while (*lpFile
== ':') lpFile
++;
1099 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, dir
, sei
, execfunc
, szCommandline
, pidl
);
1101 /* Check if file specified is in the form www.??????.*** */
1102 else if (!strncasecmp(lpFile
, "www", 3))
1104 /* if so, append lpFile http:// and call ShellExecute */
1105 char lpstrTmpFile
[256] = "http://" ;
1106 strcat(lpstrTmpFile
, lpFile
);
1107 retval
= (UINT
)ShellExecuteA(sei
->hwnd
, sei
->lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1110 /* Now we can free the PIDL. It may have been used before for %I command line options. */
1114 TRACE("ShellExecuteExA32 retval=%d\n", retval
);
1118 sei
->hInstApp
= (HINSTANCE
)retval
;
1122 sei
->hInstApp
= (HINSTANCE
)33;
1126 /*************************************************************************
1127 * ShellExecuteA [SHELL32.290]
1129 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
1130 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
)
1132 SHELLEXECUTEINFOA sei
;
1133 HANDLE hProcess
= 0;
1136 sei
.cbSize
= sizeof(sei
);
1139 sei
.lpVerb
= lpOperation
;
1140 sei
.lpFile
= lpFile
;
1141 sei
.lpParameters
= lpParameters
;
1142 sei
.lpDirectory
= lpDirectory
;
1143 sei
.nShow
= iShowCmd
;
1148 sei
.hProcess
= hProcess
;
1150 ShellExecuteExA32 (&sei
, SHELL_ExecuteA
);
1151 return sei
.hInstApp
;
1154 /*************************************************************************
1155 * ShellExecuteEx [SHELL32.291]
1158 BOOL WINAPI
ShellExecuteExAW (LPVOID sei
)
1160 if (SHELL_OsIsUnicode())
1161 return ShellExecuteExW (sei
);
1162 return ShellExecuteExA32 (sei
, SHELL_ExecuteA
);
1165 /*************************************************************************
1166 * ShellExecuteExA [SHELL32.292]
1169 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1171 BOOL ret
= ShellExecuteExA32 (sei
, SHELL_ExecuteA
);
1173 TRACE("ShellExecuteExA(): ret=%d\n", ret
);
1178 /*************************************************************************
1179 * ShellExecuteExW [SHELL32.293]
1182 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1184 SHELLEXECUTEINFOA seiA
;
1189 memcpy(&seiA
, sei
, sizeof(SHELLEXECUTEINFOA
));
1192 seiA
.lpVerb
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpVerb
);
1195 seiA
.lpFile
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpFile
);
1197 if (sei
->lpParameters
)
1198 seiA
.lpParameters
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpParameters
);
1200 if (sei
->lpDirectory
)
1201 seiA
.lpDirectory
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpDirectory
);
1203 if ((sei
->fMask
& SEE_MASK_CLASSNAME
) && sei
->lpClass
)
1204 seiA
.lpClass
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpClass
);
1206 seiA
.lpClass
= NULL
;
1208 ret
= ShellExecuteExA(&seiA
);
1210 if (seiA
.lpVerb
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpVerb
);
1211 if (seiA
.lpFile
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpFile
);
1212 if (seiA
.lpParameters
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpParameters
);
1213 if (seiA
.lpDirectory
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpDirectory
);
1214 if (seiA
.lpClass
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpClass
);
1216 sei
->hInstApp
= seiA
.hInstApp
;
1218 TRACE("ShellExecuteExW(): ret=%d\n", ret
);
1223 /*************************************************************************
1224 * ShellExecuteW [SHELL32.294]
1226 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1227 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1229 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1230 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1232 SHELLEXECUTEINFOW sei
;
1233 HANDLE hProcess
= 0;
1237 sei
.cbSize
= sizeof(sei
);
1240 sei
.lpVerb
= lpOperation
;
1241 sei
.lpFile
= lpFile
;
1242 sei
.lpParameters
= lpParameters
;
1243 sei
.lpDirectory
= lpDirectory
;
1244 sei
.nShow
= nShowCmd
;
1249 sei
.hProcess
= hProcess
;
1251 ret
= ShellExecuteExW(&sei
);
1253 TRACE("ShellExecuteW(): ret=%d module=%p", ret
, sei
.hInstApp
);
1254 return sei
.hInstApp
;