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
)
107 while(*args
&& !isspace(*args
))
110 while(isspace(*args
))
117 if (!done
|| (*fmt
== '1'))
119 /*FIXME Is SearchPath() really needed? We already have separated out the parameter string in args. */
120 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) == '"')
146 * IE uses this alot for activating things such as windows media
147 * player. This is not verified to be fully correct but it appears
154 res
+= strlen(lpFile
);
161 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
162 pv
= SHLockShared(hmem
, 0);
163 res
+= sprintf(res
, ":%p", pv
);
168 default: FIXME("Unknown escape sequence %%%c\n", *fmt
);
180 /*************************************************************************
181 * _ResolveShortCut [Internal]
184 static HRESULT
_ResolveShortCut(LPWSTR path
, LPWSTR wdir
, LPWSTR args
, HWND hwnd
, int* pshowcmd
, LPITEMIDLIST
* ppidl
)
188 HRESULT hr
= SHGetDesktopFolder(&psf
);
196 hr
= IShellFolder_ParseDisplayName(psf
, 0, 0, path
, &l
, &pidl
, 0);
201 hr
= IShellFolder_GetUIObjectOf(psf
, NULL
, 1, (LPCITEMIDLIST
*)&pidl
, &IID_IShellLinkW
, NULL
, (LPVOID
*)&psl
);
204 hr
= IShellLinkW_Resolve(psl
, hwnd
, SLR_NO_UI
);
207 hr
= IShellLinkW_GetPath(psl
, path
, MAX_PATH
, NULL
, SLGP_UNCPRIORITY
);
211 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
212 hr
= IShellLinkW_GetIDList(psl
, ppidl
);
215 /* get command line arguments and display mode if available */
216 IShellLinkW_GetWorkingDirectory(psl
, wdir
, MAX_PATH
);
217 IShellLinkW_GetArguments(psl
, args
, MAX_PATH
);
218 IShellLinkW_GetShowCmd(psl
, pshowcmd
);
223 IShellLinkW_Release(psl
);
229 IShellFolder_Release(psf
);
235 /*************************************************************************
236 * SHELL_ExecuteA [Internal]
239 static UINT
SHELL_ExecuteA(char *lpCmd
, void *env
, const char* lpDir
, LPSHELLEXECUTEINFOA sei
, BOOL shWait
)
241 STARTUPINFOA startup
;
242 PROCESS_INFORMATION info
;
245 TRACE("Execute %s from directory %s\n", lpCmd
, lpDir
);
246 ZeroMemory(&startup
,sizeof(STARTUPINFOA
));
247 startup
.cb
= sizeof(STARTUPINFOA
);
248 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
249 startup
.wShowWindow
= sei
->nShow
;
250 if (CreateProcessA(NULL
, lpCmd
, NULL
, NULL
, FALSE
, 0,
251 env
, lpDir
, &startup
, &info
))
253 /* Give 30 seconds to the app to come up, if desired. Probably only needed
254 when starting app immediately before making a DDE connection. */
256 if (WaitForInputIdle( info
.hProcess
, 30000 ) == -1)
257 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
259 if(sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
260 sei
->hProcess
= info
.hProcess
;
262 CloseHandle( info
.hProcess
);
263 CloseHandle( info
.hThread
);
265 else if ((retval
= GetLastError()) >= 32)
267 FIXME("Strange error set by CreateProcess: %d\n", retval
);
268 retval
= ERROR_BAD_FORMAT
;
271 sei
->hInstApp
= (HINSTANCE
)retval
;
276 /***********************************************************************
279 * Build the environment for the new process, adding the specified
280 * path to the PATH variable. Returned pointer must be freed by caller.
282 static void *build_env( const char *path
)
284 char *strings
, *new_env
;
286 int total
= strlen(path
) + 1;
287 BOOL got_path
= FALSE
;
289 if (!(strings
= GetEnvironmentStringsA())) return NULL
;
293 int len
= strlen(p
) + 1;
294 if (!strncasecmp( p
, "PATH=", 5 )) got_path
= TRUE
;
298 if (!got_path
) total
+= 5; /* we need to create PATH */
299 total
++; /* terminating null */
301 if (!(new_env
= HeapAlloc( GetProcessHeap(), 0, total
)))
303 FreeEnvironmentStringsA( strings
);
310 int len
= strlen(p
) + 1;
311 memcpy( p2
, p
, len
);
312 if (!strncasecmp( p
, "PATH=", 5 ))
315 strcpy( p2
+ len
, path
);
316 p2
+= strlen(path
) + 1;
323 strcpy( p2
, "PATH=" );
325 p2
+= strlen(p2
) + 1;
328 FreeEnvironmentStringsA( strings
);
333 /***********************************************************************
336 * Helper function for SHELL_FindExecutable
337 * @param lpResult - pointer to a buffer of size MAX_PATH
338 * On entry: szName is a filename (probably without path separators).
339 * On exit: if szName found in "App Path", place full path in lpResult, and return true
341 static BOOL
SHELL_TryAppPath( LPCSTR szName
, LPSTR lpResult
, void**env
)
349 if (env
) *env
= NULL
;
350 sprintf(buffer
, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s", szName
);
351 res
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
355 res
= RegQueryValueA(hkApp
, NULL
, lpResult
, &len
);
361 DWORD count
= sizeof(buffer
);
362 if (!RegQueryValueExA(hkApp
, "Path", NULL
, NULL
, buffer
, &count
) && buffer
[0])
363 *env
= build_env( buffer
);
367 if (hkApp
) RegCloseKey(hkApp
);
371 static UINT
_FindExecutableByOperation(LPCSTR lpPath
, LPCSTR lpFile
, LPCSTR lpOperation
, LPSTR key
, LPSTR filetype
, LPSTR command
)
373 LONG commandlen
= 256; /* This is the most DOS can handle :) */
375 /* Looking for ...buffer\shell\<verb>\command */
376 strcat(filetype
, "\\shell\\");
377 strcat(filetype
, lpOperation
);
378 strcat(filetype
, "\\command");
380 if (RegQueryValueA(HKEY_CLASSES_ROOT
, filetype
, command
, &commandlen
) == ERROR_SUCCESS
)
382 if (key
) strcpy(key
, filetype
);
388 /* FIXME: it seems all Windows version don't behave the same here.
389 * the doc states that this ddeexec information can be found after
391 * on Win98, it doesn't appear, but I think it does on Win2k
393 /* Get the parameters needed by the application
394 from the associated ddeexec key */
395 tmp
= strstr(filetype
, "command");
397 strcat(filetype
, "ddeexec");
399 if (RegQueryValueA(HKEY_CLASSES_ROOT
, filetype
, param
, ¶mlen
) == ERROR_SUCCESS
)
401 strcat(command
, " ");
402 strcat(command
, param
);
403 commandlen
+= paramlen
;
407 command
[commandlen
] = '\0';
409 return 33; /* FIXME see SHELL_FindExecutable() */
412 return 31; /* default - 'No association was found' */
415 /*************************************************************************
416 * SHELL_FindExecutable [Internal]
418 * Utility for code sharing between FindExecutable and ShellExecute
420 * lpFile the name of a file
421 * lpOperation the operation on it (open)
423 * lpResult a buffer, big enough :-(, to store the command to do the
424 * operation on the file
425 * key a buffer, big enough, to get the key name to do actually the
426 * command (it'll be used afterwards for more information
429 UINT
SHELL_FindExecutable(LPCSTR lpPath
, LPCSTR lpFile
, LPCSTR lpOperation
,
430 LPSTR lpResult
, LPSTR key
, void **env
, LPITEMIDLIST pidl
, LPCSTR args
)
432 char *extension
= NULL
; /* pointer to file extension */
433 char tmpext
[5]; /* local copy to mung as we please */
434 char filetype
[256]; /* registry name for this filetype */
435 LONG filetypelen
= 256; /* length of above */
436 char command
[256]; /* command from registry */
437 char buffer
[256]; /* Used to GetProfileString */
438 UINT retval
= 31; /* default - 'No association was found' */
439 char *tok
; /* token pointer */
440 char xlpFile
[256] = ""; /* result of SearchPath */
441 DWORD attribs
; /* file attributes */
443 TRACE("%s\n", (lpFile
!= NULL
) ? lpFile
: "-");
445 lpResult
[0] = '\0'; /* Start off with an empty return string */
446 if (key
) *key
= '\0';
448 /* trap NULL parameters on entry */
449 if ((lpFile
== NULL
) || (lpResult
== NULL
))
451 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n", lpFile
, lpResult
);
452 return 2; /* File not found. Close enough, I guess. */
455 if (SHELL_TryAppPath( lpFile
, lpResult
, env
))
457 TRACE("found %s via App Paths\n", lpResult
);
461 if (SearchPathA(lpPath
, lpFile
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
463 TRACE("SearchPathA returned non-zero\n");
465 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
468 attribs
= GetFileAttributesA(lpFile
);
470 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
472 strcpy(filetype
, "Folder");
473 filetypelen
= 6; /* strlen("Folder") */
477 /* First thing we need is the file's extension */
478 extension
= PathFindExtensionA(xlpFile
); /* Assume last "." is the one; */
479 /* File->Run in progman uses */
481 TRACE("xlpFile=%s,extension=%s\n", xlpFile
, extension
);
483 if ((extension
== NULL
) || (extension
== &xlpFile
[strlen(xlpFile
)]))
485 WARN("Returning 31 - No association\n");
486 return 31; /* no association */
489 /* Make local copy & lowercase it for reg & 'programs=' lookup */
490 lstrcpynA(tmpext
, extension
, 5);
492 TRACE("%s file\n", tmpext
);
494 /* Three places to check: */
495 /* 1. win.ini, [windows], programs (NB no leading '.') */
496 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
497 /* 3. win.ini, [extensions], extension (NB no leading '.' */
498 /* All I know of the order is that registry is checked before */
499 /* extensions; however, it'd make sense to check the programs */
500 /* section first, so that's what happens here. */
502 /* See if it's a program - if GetProfileString fails, we skip this
503 * section. Actually, if GetProfileString fails, we've probably
504 * got a lot more to worry about than running a program... */
505 if (GetProfileStringA("windows", "programs", "exe pif bat cmd com",
506 buffer
, sizeof(buffer
)) > 0)
510 for (i
= 0;i
<strlen(buffer
); i
++) buffer
[i
] = tolower(buffer
[i
]);
512 tok
= strtok(buffer
, " \t"); /* ? */
515 if (strcmp(tok
, &tmpext
[1]) == 0) /* have to skip the leading "." */
517 strcpy(lpResult
, xlpFile
);
518 /* Need to perhaps check that the file has a path
520 TRACE("found %s\n", lpResult
);
523 /* Greater than 32 to indicate success FIXME According to the
524 * docs, I should be returning a handle for the
525 * executable. Does this mean I'm supposed to open the
526 * executable file or something? More RTFM, I guess... */
528 tok
= strtok(NULL
, " \t");
533 if (RegQueryValueA(HKEY_CLASSES_ROOT
, tmpext
, filetype
, &filetypelen
) != ERROR_SUCCESS
)
541 /* pass the operation string to _FindExecutableByOperation() */
542 filetype
[filetypelen
] = '\0';
543 retval
= _FindExecutableByOperation(lpPath
, lpFile
, lpOperation
, key
, filetype
, command
);
547 char operation
[MAX_PATH
];
550 strcat(filetype
, "\\shell");
552 /* enumerate the operation subkeys in the registry and search for one with an associated command */
553 if (RegOpenKeyA(HKEY_CLASSES_ROOT
, filetype
, &hkey
) == ERROR_SUCCESS
)
558 if (RegEnumKeyA(hkey
, idx
, operation
, MAX_PATH
) != ERROR_SUCCESS
)
561 filetype
[filetypelen
] = '\0';
562 retval
= _FindExecutableByOperation(lpPath
, lpFile
, operation
, key
, filetype
, command
);
574 argify(lpResult
, sizeof(lpResult
), command
, xlpFile
, pidl
, args
);
575 /* Remove double quotation marks */
576 if (*lpResult
== '"')
579 while (*(p
+ 1) != '"' && *(p
+ 1) != ' ')
588 else if (extension
) /* Check win.ini */
590 /* Toss the leading dot */
592 if (GetProfileStringA("extensions", extension
, "", command
,
593 sizeof(command
)) > 0)
595 if (strlen(command
) != 0)
597 strcpy(lpResult
, command
);
598 tok
= strstr(lpResult
, "^"); /* should be ^.extension? */
602 strcat(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
603 tok
= strstr(command
, "^"); /* see above */
604 if ((tok
!= NULL
) && (strlen(tok
)>5))
606 strcat(lpResult
, &tok
[5]);
609 retval
= 33; /* FIXME - see above */
614 TRACE("returning %s\n", lpResult
);
618 /******************************************************************
621 * callback for the DDE connection. not really usefull
623 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
625 HDDEDATA hData
, DWORD dwData1
, DWORD dwData2
)
630 /******************************************************************
633 * ShellExecute helper. Used to do an operation with a DDE connection
635 * Handles both the direct connection (try #1), and if it fails,
636 * launching an application and trying (#2) to connect to it
639 static unsigned dde_connect(char* key
, char* start
, char* ddeexec
,
640 const char* lpFile
, const char* lpDir
, void *env
,
641 LPSHELLEXECUTEINFOA sei
, SHELL_ExecuteA1632 execfunc
,
642 LPCSTR szCommandline
, LPITEMIDLIST pidl
)
644 char* endkey
= key
+ strlen(key
);
645 char app
[256], topic
[256], ifexec
[256], res
[256];
646 LONG applen
, topiclen
, ifexeclen
;
650 HSZ hszApp
, hszTopic
;
654 strcpy(endkey
, "\\application");
655 applen
= sizeof(app
);
656 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, app
, &applen
) != ERROR_SUCCESS
)
658 FIXME("default app name NIY %s\n", key
);
662 strcpy(endkey
, "\\topic");
663 topiclen
= sizeof(topic
);
664 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, topic
, &topiclen
) != ERROR_SUCCESS
)
666 strcpy(topic
, "System");
669 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
674 hszApp
= DdeCreateStringHandleA(ddeInst
, app
, CP_WINANSI
);
675 hszTopic
= DdeCreateStringHandleA(ddeInst
, topic
, CP_WINANSI
);
677 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
681 TRACE("Launching '%s'\n", start
);
682 ret
= execfunc(start
, env
, lpDir
, sei
, TRUE
);
685 TRACE("Couldn't launch\n");
688 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
691 TRACE("Couldn't connect. ret=%d\n", ret
);
692 ret
= 30; /* whatever */
695 strcpy(endkey
, "\\ifexec");
696 ifexeclen
= sizeof(ifexec
);
697 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
703 argify(res
, sizeof(res
), exec
, lpFile
, pidl
, szCommandline
);
704 TRACE("%s %s => %s\n", exec
, lpFile
, res
);
706 ret
= (DdeClientTransaction(res
, strlen(res
) + 1, hConv
, 0L, 0,
707 XTYP_EXECUTE
, 10000, &tid
) != DMLERR_NO_ERROR
) ? 31 : 33;
708 DdeDisconnect(hConv
);
710 DdeUninitialize(ddeInst
);
714 /*************************************************************************
715 * execute_from_key [Internal]
717 static UINT
execute_from_key(LPSTR key
, LPCSTR lpFile
, LPCSTR lpDir
, void *env
,
718 LPSHELLEXECUTEINFOA sei
, SHELL_ExecuteA1632 execfunc
,
719 LPCSTR szCommandline
, LPITEMIDLIST pidl
)
722 LONG cmdlen
= sizeof(cmd
);
725 /* Get the application for the registry */
726 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
729 char param
[256] = "";
732 /* Get the parameters needed by the application
733 from the associated ddeexec key */
734 tmp
= strstr(key
, "command");
736 strcpy(tmp
, "ddeexec");
738 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, param
, ¶mlen
) == ERROR_SUCCESS
)
740 TRACE("Got ddeexec %s => %s\n", key
, param
);
741 retval
= dde_connect(key
, cmd
, param
, lpFile
, lpDir
, env
, sei
, execfunc
, szCommandline
, pidl
);
745 /* Is there a replace() function anywhere? */
747 argify(param
, sizeof(param
), cmd
, lpFile
, pidl
, szCommandline
);
748 retval
= execfunc(param
, env
, lpDir
, sei
, FALSE
);
751 else TRACE("ooch\n");
756 /*************************************************************************
757 * FindExecutableA [SHELL32.@]
759 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
761 UINT retval
= 31; /* default - 'No association was found' */
764 TRACE("File %s, Dir %s\n",
765 (lpFile
!= NULL
? lpFile
: "-"), (lpDirectory
!= NULL
? lpDirectory
: "-"));
767 lpResult
[0] = '\0'; /* Start off with an empty return string */
769 /* trap NULL parameters on entry */
770 if ((lpFile
== NULL
) || (lpResult
== NULL
))
772 /* FIXME - should throw a warning, perhaps! */
773 return (HINSTANCE
)2; /* File not found. Close enough, I guess. */
778 GetCurrentDirectoryA(sizeof(old_dir
), old_dir
);
779 SetCurrentDirectoryA(lpDirectory
);
782 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, "open", lpResult
, NULL
, NULL
, NULL
, NULL
);
784 TRACE("returning %s\n", lpResult
);
786 SetCurrentDirectoryA(old_dir
);
787 return (HINSTANCE
)retval
;
790 /*************************************************************************
791 * FindExecutableW [SHELL32.@]
793 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
795 FIXME("(%p,%p,%p): stub\n", lpFile
, lpDirectory
, lpResult
);
796 return (HINSTANCE
)31; /* default - 'No association was found' */
799 /*************************************************************************
800 * ShellExecuteExA32 [Internal]
802 BOOL WINAPI
ShellExecuteExA32 (LPSHELLEXECUTEINFOA sei
, SHELL_ExecuteA1632 execfunc
)
804 CHAR szApplicationName
[MAX_PATH
+2], szCommandline
[MAX_PATH
], fileName
[MAX_PATH
], dir
[MAX_PATH
];
807 char lpstrProtocol
[256];
814 LPITEMIDLIST pidl
= sei
->lpIDList
;
815 LPITEMIDLIST tmpPidl
= NULL
;
817 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
818 sei
->fMask
, sei
->hwnd
, debugstr_a(sei
->lpVerb
),
819 debugstr_a(sei
->lpFile
), debugstr_a(sei
->lpParameters
),
820 debugstr_a(sei
->lpDirectory
), sei
->nShow
,
821 (sei
->fMask
& SEE_MASK_CLASSNAME
) ? debugstr_a(sei
->lpClass
) : "not used");
823 sei
->hProcess
= NULL
;
826 strcpy(szApplicationName
, sei
->lpFile
);
828 *szApplicationName
= '\0';
830 if (sei
->lpParameters
)
831 strcpy(szCommandline
, sei
->lpParameters
);
833 *szCommandline
= '\0';
835 if (sei
->lpDirectory
)
836 strcpy(dir
, sei
->lpDirectory
);
840 if (sei
->fMask
& (SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
841 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
842 SEE_MASK_DOENVSUBST
| SEE_MASK_FLAG_NO_UI
| SEE_MASK_UNICODE
|
843 SEE_MASK_NO_CONSOLE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
))
845 FIXME("flags ignored: 0x%08lx\n", sei
->fMask
);
848 /* process the IDList */
849 if (sei
->fMask
& SEE_MASK_INVOKEIDLIST
) /* 0x0c: includes SEE_MASK_IDLIST */
851 if (!SHGetPathFromIDListA(pidl
, szApplicationName
))
854 TRACE("-- idlist=%p (%s)\n", pidl
, szApplicationName
);
857 if (sei
->fMask
& (SEE_MASK_CLASSNAME
| SEE_MASK_CLASSKEY
))
859 /* launch a document by fileclass like 'WordPad.Document.1' */
860 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
861 /* FIXME: szCommandline should not be of a fixed size. Plus MAX_PATH is way too short! */
862 if (sei
->fMask
& SEE_MASK_CLASSKEY
)
863 HCR_GetExecuteCommandExA(sei
->hkeyClass
,
864 (sei
->fMask
& SEE_MASK_CLASSNAME
) ? sei
->lpClass
: NULL
,
865 (sei
->lpVerb
) ? sei
->lpVerb
: "open", szCommandline
, sizeof(szCommandline
));
866 else if (sei
->fMask
& SEE_MASK_CLASSNAME
)
867 HCR_GetExecuteCommandA(sei
->lpClass
, (sei
->lpVerb
) ? sei
->lpVerb
:
868 "open", szCommandline
, sizeof(szCommandline
));
870 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
871 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", szCommandline
, szApplicationName
);
874 done
= argify(cmd
, sizeof(cmd
), szCommandline
, szApplicationName
, pidl
, NULL
);
875 if (!done
&& szApplicationName
[0])
878 strcat(cmd
, szApplicationName
);
880 retval
= execfunc(cmd
, NULL
, dir
, sei
, FALSE
);
887 /* Else, try to execute the filename */
888 TRACE("execute:'%s','%s'\n", szApplicationName
, szCommandline
);
890 /* resolve shell shortcuts */
891 ext
= PathFindExtensionA(szApplicationName
);
892 if (ext
&& !strcasecmp(ext
, ".lnk")) { /* or check for: shell_attribs & SFGAO_LINK */
893 WCHAR cmd
[MAX_PATH
], args
[MAX_PATH
], wdir
[MAX_PATH
];
895 if (MultiByteToWideChar(CP_ACP
, 0, szApplicationName
, -1, cmd
, MAX_PATH
)) {
896 MultiByteToWideChar(CP_ACP
, 0, dir
, -1, wdir
, MAX_PATH
);
898 if (SUCCEEDED(_ResolveShortCut(cmd
, wdir
, args
, sei
->hwnd
, &sei
->nShow
, &tmpPidl
))) {
899 if (!*cmd
&& tmpPidl
) {
900 /* We got a PIDL instead of a file system path. */
901 IShellFolder
* desktop
;
904 HRESULT hr
= SHGetDesktopFolder(&desktop
);
907 hr
= IShellFolder_GetDisplayNameOf(desktop
, tmpPidl
, SHGDN_FORPARSING
, &str
);
910 hr
= StrRetToStrNW(cmd
, MAX_PATH
, &str
, tmpPidl
);
914 IShellFolder_Release(desktop
);
917 if (cmd
[0]==':' && cmd
[1]==':') {
918 /* open shell folder for the specified class GUID */
919 strcpy(szApplicationName
, "explorer.exe");
920 WideCharToMultiByte(CP_ACP
, 0, cmd
, -1, szCommandline
, MAX_PATH
, NULL
, NULL
);
923 } else if (HCR_GetExecuteCommandA("Folder", sei
->lpVerb
? sei
->lpVerb
: "open", szCommandline
, sizeof(szCommandline
))) {
926 /*FIXME: seems to have problems in some cases */
927 if (argify(res
, sizeof(res
), szCommandline
, NULL
, tmpPidl
, NULL
))
928 strcpy(szApplicationName
, res
);
930 szCommandline
[0] = '\0';
936 WideCharToMultiByte(CP_ACP
, 0, cmd
, -1, szApplicationName
, MAX_PATH
, NULL
, NULL
);
937 WideCharToMultiByte(CP_ACP
, 0, wdir
, -1, dir
, MAX_PATH
, NULL
, NULL
);
938 WideCharToMultiByte(CP_ACP
, 0, args
, -1, szCommandline
, MAX_PATH
, NULL
, NULL
);
941 /* If we prevoiusly had a PIDL, it is now resolved, so forget it. */
944 FIXME("We could not resolve the shell shortcut.\n");
948 /* The following code is needed for example to resolve a shortcut
949 to control panel applet "Keyboard", since this is accessed using
950 "rundll32.exe shell32.dll,Control_RunDLL %1,%*" with a command line
951 parameter received from ISF_ControlPanel_fnGetDisplayNameOf(). */
952 if (!*szCommandline
) {
953 if (*szApplicationName
== '"') {
954 LPCSTR src
= szApplicationName
+ 1;
955 LPSTR dst
= fileName
;
957 while(*src
&& *src
!='"')
963 for(++src
; isspace(*src
); )
966 strcpy(szCommandline
, src
);
971 char buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
973 LPSTR beg
= szApplicationName
;
974 for(s
=beg
; space
=strchr(s
, ' '); s
=space
+1) {
975 int idx
= space
-szApplicationName
;
976 strncpy(buffer
, szApplicationName
, idx
);
979 if (SearchPathA(*dir
? dir
: NULL
, buffer
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
981 /* separate out command from parameter string */
982 LPCSTR p
= space
+ 1;
987 strcpy(szCommandline
, p
);
994 strcpy(fileName
, szApplicationName
);
997 strcpy(fileName
, szApplicationName
);
1001 if (szCommandline
[0]) {
1002 strcat(szApplicationName
, " ");
1003 strcat(szApplicationName
, szCommandline
);
1006 retval
= execfunc(szApplicationName
, NULL
, dir
, sei
, FALSE
);
1009 /* Now, that we have successfully launched a process, we can free the PIDL.
1010 It may have been used before for %I command line options. */
1014 TRACE("execfunc: retval=%d sei->hInstApp=%p\n", retval
, sei
->hInstApp
);
1018 /* Else, try to find the executable */
1020 retval
= SHELL_FindExecutable(*dir
? dir
: NULL
, lpFile
, sei
->lpVerb
, cmd
, lpstrProtocol
, &env
, pidl
, szCommandline
);
1021 if (retval
> 32) /* Found */
1023 CHAR szQuotedCmd
[MAX_PATH
+2];
1024 /* Must quote to handle case where cmd contains spaces,
1025 * else security hole if malicious user creates executable file "C:\\Program"
1027 * FIXME: If we don't have set explicitly command line arguments, we must first
1028 * split executable path from optional command line arguments. Otherwise we would quote
1029 * the complete string with executable path _and_ arguments, which is not what we want.
1031 if (szCommandline
[0])
1032 sprintf(szQuotedCmd
, "\"%s\" %s", cmd
, szCommandline
);
1034 sprintf(szQuotedCmd
, "\"%s\"", cmd
);
1035 TRACE("%s/%s => %s/%s\n", szApplicationName
, sei
->lpVerb
?sei
->lpVerb
:"NULL", szQuotedCmd
, lpstrProtocol
);
1037 retval
= execute_from_key(lpstrProtocol
, lpFile
, env
, dir
, sei
, execfunc
, szCommandline
, pidl
);
1039 retval
= execfunc(szQuotedCmd
, env
, dir
, sei
, FALSE
);
1040 if (env
) HeapFree( GetProcessHeap(), 0, env
);
1042 else if (PathIsURLA((LPSTR
)lpFile
)) /* File not found, check for URL */
1047 lpstrRes
= strchr(lpFile
, ':');
1049 iSize
= lpstrRes
- lpFile
;
1051 iSize
= strlen(lpFile
);
1053 TRACE("Got URL: %s\n", lpFile
);
1054 /* Looking for ...protocol\shell\<verb>\command */
1055 strncpy(lpstrProtocol
, lpFile
, iSize
);
1056 lpstrProtocol
[iSize
] = '\0';
1057 strcat(lpstrProtocol
, "\\shell\\");
1058 strcat(lpstrProtocol
, sei
->lpVerb
? sei
->lpVerb
: "open"); /*FIXME: enumerate registry subkeys - compare with the loop into SHELL_FindExecutable() */
1059 strcat(lpstrProtocol
, "\\command");
1061 /* Remove File Protocol from lpFile */
1062 /* In the case file://path/file */
1063 if (!strncasecmp(lpFile
, "file", iSize
))
1066 while (*lpFile
== ':') lpFile
++;
1068 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, dir
, sei
, execfunc
, szCommandline
, pidl
);
1070 /* Check if file specified is in the form www.??????.*** */
1071 else if (!strncasecmp(lpFile
, "www", 3))
1073 /* if so, append lpFile http:// and call ShellExecute */
1074 char lpstrTmpFile
[256] = "http://" ;
1075 strcat(lpstrTmpFile
, lpFile
);
1076 retval
= (UINT
)ShellExecuteA(sei
->hwnd
, sei
->lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1079 /* Now we can free the PIDL. It may have been used before for %I command line options. */
1083 TRACE("ShellExecuteExA32 retval=%d\n", retval
);
1087 sei
->hInstApp
= (HINSTANCE
)retval
;
1091 sei
->hInstApp
= (HINSTANCE
)33;
1095 /*************************************************************************
1096 * ShellExecuteA [SHELL32.290]
1098 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
1099 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
)
1101 SHELLEXECUTEINFOA sei
;
1102 HANDLE hProcess
= 0;
1105 sei
.cbSize
= sizeof(sei
);
1108 sei
.lpVerb
= lpOperation
;
1109 sei
.lpFile
= lpFile
;
1110 sei
.lpParameters
= lpParameters
;
1111 sei
.lpDirectory
= lpDirectory
;
1112 sei
.nShow
= iShowCmd
;
1117 sei
.hProcess
= hProcess
;
1119 ShellExecuteExA32 (&sei
, SHELL_ExecuteA
);
1120 return sei
.hInstApp
;
1123 /*************************************************************************
1124 * ShellExecuteEx [SHELL32.291]
1127 BOOL WINAPI
ShellExecuteExAW (LPVOID sei
)
1129 if (SHELL_OsIsUnicode())
1130 return ShellExecuteExW (sei
);
1131 return ShellExecuteExA32 (sei
, SHELL_ExecuteA
);
1134 /*************************************************************************
1135 * ShellExecuteExA [SHELL32.292]
1138 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1140 BOOL ret
= ShellExecuteExA32 (sei
, SHELL_ExecuteA
);
1142 TRACE("ShellExecuteExA(): ret=%d\n", ret
);
1147 /*************************************************************************
1148 * ShellExecuteExW [SHELL32.293]
1151 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1153 SHELLEXECUTEINFOA seiA
;
1158 memcpy(&seiA
, sei
, sizeof(SHELLEXECUTEINFOA
));
1161 seiA
.lpVerb
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpVerb
);
1164 seiA
.lpFile
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpFile
);
1166 if (sei
->lpParameters
)
1167 seiA
.lpParameters
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpParameters
);
1169 if (sei
->lpDirectory
)
1170 seiA
.lpDirectory
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpDirectory
);
1172 if ((sei
->fMask
& SEE_MASK_CLASSNAME
) && sei
->lpClass
)
1173 seiA
.lpClass
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpClass
);
1175 seiA
.lpClass
= NULL
;
1177 ret
= ShellExecuteExA(&seiA
);
1179 if (seiA
.lpVerb
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpVerb
);
1180 if (seiA
.lpFile
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpFile
);
1181 if (seiA
.lpParameters
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpParameters
);
1182 if (seiA
.lpDirectory
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpDirectory
);
1183 if (seiA
.lpClass
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpClass
);
1185 sei
->hInstApp
= seiA
.hInstApp
;
1187 TRACE("ShellExecuteExW(): ret=%d\n", ret
);
1192 /*************************************************************************
1193 * ShellExecuteW [SHELL32.294]
1195 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1196 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1198 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1199 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1201 SHELLEXECUTEINFOW sei
;
1202 HANDLE hProcess
= 0;
1206 sei
.cbSize
= sizeof(sei
);
1209 sei
.lpVerb
= lpOperation
;
1210 sei
.lpFile
= lpFile
;
1211 sei
.lpParameters
= lpParameters
;
1212 sei
.lpDirectory
= lpDirectory
;
1213 sei
.nShow
= nShowCmd
;
1218 sei
.hProcess
= hProcess
;
1220 ret
= ShellExecuteExW(&sei
);
1222 TRACE("ShellExecuteW(): ret=%d module=%p", ret
, sei
.hInstApp
);
1223 return sei
.hInstApp
;