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* res
, int len
, const char* fmt
, const char* lpFile
, LPITEMIDLIST pidl
, LPCSTR args
)
105 while(*args
&& !isspace(*args
))
108 while(isspace(*args
))
115 if (!done
|| (*fmt
== '1'))
117 /*FIXME Is SearchPath() really needed? We already have separated out the parameter string in args. */
118 if (SearchPathA(NULL
, lpFile
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
121 strcpy(res
, xlpFile
);
122 res
+= strlen(xlpFile
);
129 res
+= strlen(lpFile
);
137 * IE uses this alot for activating things such as windows media
138 * player. This is not verified to be fully correct but it appears
145 res
+= strlen(lpFile
);
152 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
153 pv
= SHLockShared(hmem
, 0);
154 res
+= sprintf(res
, ":%p", pv
);
159 default: FIXME("Unknown escape sequence %%%c\n", *fmt
);
171 /*************************************************************************
172 * _ResolveShortCut [Internal]
175 static HRESULT
_ResolveShortCut(LPWSTR path
, LPWSTR args
, HWND hwnd
, int* pshowcmd
, LPITEMIDLIST
* ppidl
)
179 HRESULT hr
= SHGetDesktopFolder(&psf
);
187 hr
= IShellFolder_ParseDisplayName(psf
, 0, 0, path
, &l
, &pidl
, 0);
192 hr
= IShellFolder_GetUIObjectOf(psf
, NULL
, 1, (LPCITEMIDLIST
*)&pidl
, &IID_IShellLinkW
, NULL
, (LPVOID
*)&psl
);
195 hr
= IShellLinkW_Resolve(psl
, hwnd
, SLR_NO_UI
);
198 hr
= IShellLinkW_GetPath(psl
, path
, MAX_PATH
, NULL
, SLGP_UNCPRIORITY
);
202 /* We could not translate the PIDL in the shell link into a valid file system path - so return the PIDL instead. */
203 hr
= IShellLinkW_GetIDList(psl
, ppidl
);
206 /* get command line arguments and display mode if available */
207 IShellLinkW_GetArguments(psl
, args
, MAX_PATH
);
208 IShellLinkW_GetShowCmd(psl
, pshowcmd
);
213 IShellLinkW_Release(psl
);
219 IShellFolder_Release(psf
);
225 /*************************************************************************
226 * SHELL_ExecuteA [Internal]
229 static UINT
SHELL_ExecuteA(char *lpCmd
, void *env
, LPSHELLEXECUTEINFOA sei
, BOOL shWait
)
231 STARTUPINFOA startup
;
232 PROCESS_INFORMATION info
;
235 TRACE("Execute %s from directory %s\n", lpCmd
, sei
->lpDirectory
);
236 ZeroMemory(&startup
,sizeof(STARTUPINFOA
));
237 startup
.cb
= sizeof(STARTUPINFOA
);
238 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
239 startup
.wShowWindow
= sei
->nShow
;
240 if (CreateProcessA(NULL
, lpCmd
, NULL
, NULL
, FALSE
, 0,
241 env
, sei
->lpDirectory
, &startup
, &info
))
243 /* Give 30 seconds to the app to come up, if desired. Probably only needed
244 when starting app immediately before making a DDE connection. */
246 if (WaitForInputIdle( info
.hProcess
, 30000 ) == -1)
247 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
249 if(sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
250 sei
->hProcess
= info
.hProcess
;
252 CloseHandle( info
.hProcess
);
253 CloseHandle( info
.hThread
);
255 else if ((retval
= GetLastError()) >= 32)
257 FIXME("Strange error set by CreateProcess: %d\n", retval
);
258 retval
= ERROR_BAD_FORMAT
;
261 sei
->hInstApp
= (HINSTANCE
)retval
;
266 /***********************************************************************
269 * Build the environment for the new process, adding the specified
270 * path to the PATH variable. Returned pointer must be freed by caller.
272 static void *build_env( const char *path
)
274 char *strings
, *new_env
;
276 int total
= strlen(path
) + 1;
277 BOOL got_path
= FALSE
;
279 if (!(strings
= GetEnvironmentStringsA())) return NULL
;
283 int len
= strlen(p
) + 1;
284 if (!strncasecmp( p
, "PATH=", 5 )) got_path
= TRUE
;
288 if (!got_path
) total
+= 5; /* we need to create PATH */
289 total
++; /* terminating null */
291 if (!(new_env
= HeapAlloc( GetProcessHeap(), 0, total
)))
293 FreeEnvironmentStringsA( strings
);
300 int len
= strlen(p
) + 1;
301 memcpy( p2
, p
, len
);
302 if (!strncasecmp( p
, "PATH=", 5 ))
305 strcpy( p2
+ len
, path
);
306 p2
+= strlen(path
) + 1;
313 strcpy( p2
, "PATH=" );
315 p2
+= strlen(p2
) + 1;
318 FreeEnvironmentStringsA( strings
);
323 /***********************************************************************
326 * Helper function for SHELL_FindExecutable
327 * @param lpResult - pointer to a buffer of size MAX_PATH
328 * On entry: szName is a filename (probably without path separators).
329 * On exit: if szName found in "App Path", place full path in lpResult, and return true
331 static BOOL
SHELL_TryAppPath( LPCSTR szName
, LPSTR lpResult
, void**env
)
339 if (env
) *env
= NULL
;
340 sprintf(buffer
, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\%s", szName
);
341 res
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
345 res
= RegQueryValueA(hkApp
, NULL
, lpResult
, &len
);
351 DWORD count
= sizeof(buffer
);
352 if (!RegQueryValueExA(hkApp
, "Path", NULL
, NULL
, buffer
, &count
) && buffer
[0])
353 *env
= build_env( buffer
);
357 if (hkApp
) RegCloseKey(hkApp
);
361 static UINT
_FindExecutableByOperation(LPCSTR lpPath
, LPCSTR lpFile
, LPCSTR lpOperation
, LPSTR key
, LPSTR filetype
, LPSTR command
)
363 LONG commandlen
= 256; /* This is the most DOS can handle :) */
365 /* Looking for ...buffer\shell\<verb>\command */
366 strcat(filetype
, "\\shell\\");
367 strcat(filetype
, lpOperation
);
368 strcat(filetype
, "\\command");
370 if (RegQueryValueA(HKEY_CLASSES_ROOT
, filetype
, command
, &commandlen
) == ERROR_SUCCESS
)
372 if (key
) strcpy(key
, filetype
);
378 /* FIXME: it seems all Windows version don't behave the same here.
379 * the doc states that this ddeexec information can be found after
381 * on Win98, it doesn't appear, but I think it does on Win2k
383 /* Get the parameters needed by the application
384 from the associated ddeexec key */
385 tmp
= strstr(filetype
, "command");
387 strcat(filetype
, "ddeexec");
389 if (RegQueryValueA(HKEY_CLASSES_ROOT
, filetype
, param
, ¶mlen
) == ERROR_SUCCESS
)
391 strcat(command
, " ");
392 strcat(command
, param
);
393 commandlen
+= paramlen
;
397 command
[commandlen
] = '\0';
399 return 33; /* FIXME see SHELL_FindExecutable() */
402 return 31; /* default - 'No association was found' */
405 /*************************************************************************
406 * SHELL_FindExecutable [Internal]
408 * Utility for code sharing between FindExecutable and ShellExecute
410 * lpFile the name of a file
411 * lpOperation the operation on it (open)
413 * lpResult a buffer, big enough :-(, to store the command to do the
414 * operation on the file
415 * key a buffer, big enough, to get the key name to do actually the
416 * command (it'll be used afterwards for more information
419 UINT
SHELL_FindExecutable(LPCSTR lpPath
, LPCSTR lpFile
, LPCSTR lpOperation
,
420 LPSTR lpResult
, LPSTR key
, void **env
, LPITEMIDLIST pidl
, LPCSTR args
)
422 char *extension
= NULL
; /* pointer to file extension */
423 char tmpext
[5]; /* local copy to mung as we please */
424 char filetype
[256]; /* registry name for this filetype */
425 LONG filetypelen
= 256; /* length of above */
426 char command
[256]; /* command from registry */
427 char buffer
[256]; /* Used to GetProfileString */
428 UINT retval
= 31; /* default - 'No association was found' */
429 char *tok
; /* token pointer */
430 char xlpFile
[256] = ""; /* result of SearchPath */
431 DWORD attribs
; /* file attributes */
433 TRACE("%s\n", (lpFile
!= NULL
) ? lpFile
: "-");
435 lpResult
[0] = '\0'; /* Start off with an empty return string */
436 if (key
) *key
= '\0';
438 /* trap NULL parameters on entry */
439 if ((lpFile
== NULL
) || (lpResult
== NULL
))
441 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n", lpFile
, lpResult
);
442 return 2; /* File not found. Close enough, I guess. */
445 if (SHELL_TryAppPath( lpFile
, lpResult
, env
))
447 TRACE("found %s via App Paths\n", lpResult
);
451 if (SearchPathA(lpPath
, lpFile
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
453 TRACE("SearchPathA returned non-zero\n");
455 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
458 attribs
= GetFileAttributesA(lpFile
);
460 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
462 strcpy(filetype
, "Folder");
463 filetypelen
= 6; /* strlen("Folder") */
467 /* First thing we need is the file's extension */
468 extension
= PathFindExtensionA(xlpFile
); /* Assume last "." is the one; */
469 /* File->Run in progman uses */
471 TRACE("xlpFile=%s,extension=%s\n", xlpFile
, extension
);
473 if ((extension
== NULL
) || (extension
== &xlpFile
[strlen(xlpFile
)]))
475 WARN("Returning 31 - No association\n");
476 return 31; /* no association */
479 /* Make local copy & lowercase it for reg & 'programs=' lookup */
480 lstrcpynA(tmpext
, extension
, 5);
482 TRACE("%s file\n", tmpext
);
484 /* Three places to check: */
485 /* 1. win.ini, [windows], programs (NB no leading '.') */
486 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
487 /* 3. win.ini, [extensions], extension (NB no leading '.' */
488 /* All I know of the order is that registry is checked before */
489 /* extensions; however, it'd make sense to check the programs */
490 /* section first, so that's what happens here. */
492 /* See if it's a program - if GetProfileString fails, we skip this
493 * section. Actually, if GetProfileString fails, we've probably
494 * got a lot more to worry about than running a program... */
495 if (GetProfileStringA("windows", "programs", "exe pif bat cmd com",
496 buffer
, sizeof(buffer
)) > 0)
500 for (i
= 0;i
<strlen(buffer
); i
++) buffer
[i
] = tolower(buffer
[i
]);
502 tok
= strtok(buffer
, " \t"); /* ? */
505 if (strcmp(tok
, &tmpext
[1]) == 0) /* have to skip the leading "." */
507 strcpy(lpResult
, xlpFile
);
508 /* Need to perhaps check that the file has a path
510 TRACE("found %s\n", lpResult
);
513 /* Greater than 32 to indicate success FIXME According to the
514 * docs, I should be returning a handle for the
515 * executable. Does this mean I'm supposed to open the
516 * executable file or something? More RTFM, I guess... */
518 tok
= strtok(NULL
, " \t");
523 if (RegQueryValueA(HKEY_CLASSES_ROOT
, tmpext
, filetype
, &filetypelen
) != ERROR_SUCCESS
)
531 /* pass the operation string to _FindExecutableByOperation() */
532 filetype
[filetypelen
] = '\0';
533 retval
= _FindExecutableByOperation(lpPath
, lpFile
, lpOperation
, key
, filetype
, command
);
537 char operation
[MAX_PATH
];
540 strcat(filetype
, "\\shell");
542 /* enumerate the operation subkeys in the registry and search for one with an associated command */
543 if (RegOpenKeyA(HKEY_CLASSES_ROOT
, filetype
, &hkey
) == ERROR_SUCCESS
)
548 if (RegEnumKeyA(hkey
, idx
, operation
, MAX_PATH
) != ERROR_SUCCESS
)
551 filetype
[filetypelen
] = '\0';
552 retval
= _FindExecutableByOperation(lpPath
, lpFile
, operation
, key
, filetype
, command
);
564 argify(lpResult
, sizeof(lpResult
), command
, xlpFile
, pidl
, args
);
565 /* Remove double quotation marks */
566 if (*lpResult
== '"')
569 while (*(p
+ 1) != '"' && *(p
+ 1) != ' ')
578 else if (extension
) /* Check win.ini */
580 /* Toss the leading dot */
582 if (GetProfileStringA("extensions", extension
, "", command
,
583 sizeof(command
)) > 0)
585 if (strlen(command
) != 0)
587 strcpy(lpResult
, command
);
588 tok
= strstr(lpResult
, "^"); /* should be ^.extension? */
592 strcat(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
593 tok
= strstr(command
, "^"); /* see above */
594 if ((tok
!= NULL
) && (strlen(tok
)>5))
596 strcat(lpResult
, &tok
[5]);
599 retval
= 33; /* FIXME - see above */
604 TRACE("returning %s\n", lpResult
);
608 /******************************************************************
611 * callback for the DDE connection. not really usefull
613 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
615 HDDEDATA hData
, DWORD dwData1
, DWORD dwData2
)
620 /******************************************************************
623 * ShellExecute helper. Used to do an operation with a DDE connection
625 * Handles both the direct connection (try #1), and if it fails,
626 * launching an application and trying (#2) to connect to it
629 static unsigned dde_connect(char* key
, char* start
, char* ddeexec
,
630 const char* lpFile
, void *env
,
631 LPSHELLEXECUTEINFOA sei
, SHELL_ExecuteA1632 execfunc
,
632 LPCSTR szCommandline
, LPITEMIDLIST pidl
)
634 char* endkey
= key
+ strlen(key
);
635 char app
[256], topic
[256], ifexec
[256], res
[256];
636 LONG applen
, topiclen
, ifexeclen
;
640 HSZ hszApp
, hszTopic
;
644 strcpy(endkey
, "\\application");
645 applen
= sizeof(app
);
646 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, app
, &applen
) != ERROR_SUCCESS
)
648 FIXME("default app name NIY %s\n", key
);
652 strcpy(endkey
, "\\topic");
653 topiclen
= sizeof(topic
);
654 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, topic
, &topiclen
) != ERROR_SUCCESS
)
656 strcpy(topic
, "System");
659 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
664 hszApp
= DdeCreateStringHandleA(ddeInst
, app
, CP_WINANSI
);
665 hszTopic
= DdeCreateStringHandleA(ddeInst
, topic
, CP_WINANSI
);
667 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
671 TRACE("Launching '%s'\n", start
);
672 ret
= execfunc(start
, env
, sei
, TRUE
);
675 TRACE("Couldn't launch\n");
678 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
681 TRACE("Couldn't connect. ret=%d\n", ret
);
682 ret
= 30; /* whatever */
685 strcpy(endkey
, "\\ifexec");
686 ifexeclen
= sizeof(ifexec
);
687 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
693 argify(res
, sizeof(res
), exec
, lpFile
, pidl
, szCommandline
);
694 TRACE("%s %s => %s\n", exec
, lpFile
, res
);
696 ret
= (DdeClientTransaction(res
, strlen(res
) + 1, hConv
, 0L, 0,
697 XTYP_EXECUTE
, 10000, &tid
) != DMLERR_NO_ERROR
) ? 31 : 33;
698 DdeDisconnect(hConv
);
700 DdeUninitialize(ddeInst
);
704 /*************************************************************************
705 * execute_from_key [Internal]
707 static UINT
execute_from_key(LPSTR key
, LPCSTR lpFile
, void *env
,
708 LPSHELLEXECUTEINFOA sei
, SHELL_ExecuteA1632 execfunc
,
709 LPCSTR szCommandline
, LPITEMIDLIST pidl
)
712 LONG cmdlen
= sizeof(cmd
);
715 /* Get the application for the registry */
716 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
719 char param
[256] = "";
722 /* Get the parameters needed by the application
723 from the associated ddeexec key */
724 tmp
= strstr(key
, "command");
726 strcpy(tmp
, "ddeexec");
728 if (RegQueryValueA(HKEY_CLASSES_ROOT
, key
, param
, ¶mlen
) == ERROR_SUCCESS
)
730 TRACE("Got ddeexec %s => %s\n", key
, param
);
731 retval
= dde_connect(key
, cmd
, param
, lpFile
, env
, sei
, execfunc
, szCommandline
, pidl
);
735 /* Is there a replace() function anywhere? */
737 argify(param
, sizeof(param
), cmd
, lpFile
, pidl
, szCommandline
);
738 retval
= execfunc(param
, env
, sei
, FALSE
);
741 else TRACE("ooch\n");
746 /*************************************************************************
747 * FindExecutableA [SHELL32.@]
749 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
751 UINT retval
= 31; /* default - 'No association was found' */
754 TRACE("File %s, Dir %s\n",
755 (lpFile
!= NULL
? lpFile
: "-"), (lpDirectory
!= NULL
? lpDirectory
: "-"));
757 lpResult
[0] = '\0'; /* Start off with an empty return string */
759 /* trap NULL parameters on entry */
760 if ((lpFile
== NULL
) || (lpResult
== NULL
))
762 /* FIXME - should throw a warning, perhaps! */
763 return (HINSTANCE
)2; /* File not found. Close enough, I guess. */
768 GetCurrentDirectoryA(sizeof(old_dir
), old_dir
);
769 SetCurrentDirectoryA(lpDirectory
);
772 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, "open", lpResult
, NULL
, NULL
, NULL
, NULL
);
774 TRACE("returning %s\n", lpResult
);
776 SetCurrentDirectoryA(old_dir
);
777 return (HINSTANCE
)retval
;
780 /*************************************************************************
781 * FindExecutableW [SHELL32.@]
783 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
785 FIXME("(%p,%p,%p): stub\n", lpFile
, lpDirectory
, lpResult
);
786 return (HINSTANCE
)31; /* default - 'No association was found' */
789 /*************************************************************************
790 * ShellExecuteExA32 [Internal]
792 BOOL WINAPI
ShellExecuteExA32 (LPSHELLEXECUTEINFOA sei
, SHELL_ExecuteA1632 execfunc
)
794 CHAR szApplicationName
[MAX_PATH
+2], szCommandline
[MAX_PATH
], fileName
[MAX_PATH
];
797 char lpstrProtocol
[256];
804 LPITEMIDLIST pidl
= sei
->lpIDList
;
805 LPITEMIDLIST tmpPidl
= NULL
;
807 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
808 sei
->fMask
, sei
->hwnd
, debugstr_a(sei
->lpVerb
),
809 debugstr_a(sei
->lpFile
), debugstr_a(sei
->lpParameters
),
810 debugstr_a(sei
->lpDirectory
), sei
->nShow
,
811 (sei
->fMask
& SEE_MASK_CLASSNAME
) ? debugstr_a(sei
->lpClass
) : "not used");
813 sei
->hProcess
= NULL
;
814 ZeroMemory(szApplicationName
,MAX_PATH
);
816 strcpy(szApplicationName
, sei
->lpFile
);
818 ZeroMemory(szCommandline
,MAX_PATH
);
819 if (sei
->lpParameters
)
820 strcpy(szCommandline
, sei
->lpParameters
);
822 if (sei
->fMask
& (SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
823 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
824 SEE_MASK_DOENVSUBST
| SEE_MASK_FLAG_NO_UI
| SEE_MASK_UNICODE
|
825 SEE_MASK_NO_CONSOLE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
))
827 FIXME("flags ignored: 0x%08lx\n", sei
->fMask
);
830 /* process the IDList */
831 if (sei
->fMask
& SEE_MASK_INVOKEIDLIST
) /* 0x0c: includes SEE_MASK_IDLIST */
833 if (!SHGetPathFromIDListA(pidl
, szApplicationName
))
836 TRACE("-- idlist=%p (%s)\n", pidl
, szApplicationName
);
839 if (sei
->fMask
& (SEE_MASK_CLASSNAME
| SEE_MASK_CLASSKEY
))
841 /* launch a document by fileclass like 'WordPad.Document.1' */
842 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
843 /* FIXME: szCommandline should not be of a fixed size. Plus MAX_PATH is way too short! */
844 if (sei
->fMask
& SEE_MASK_CLASSKEY
)
845 HCR_GetExecuteCommandExA(sei
->hkeyClass
,
846 (sei
->fMask
& SEE_MASK_CLASSNAME
) ? sei
->lpClass
: NULL
,
847 (sei
->lpVerb
) ? sei
->lpVerb
: "open", szCommandline
, sizeof(szCommandline
));
848 else if (sei
->fMask
& SEE_MASK_CLASSNAME
)
849 HCR_GetExecuteCommandA(sei
->lpClass
, (sei
->lpVerb
) ? sei
->lpVerb
:
850 "open", szCommandline
, sizeof(szCommandline
));
852 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
853 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", szCommandline
, szApplicationName
);
856 done
= argify(cmd
, sizeof(cmd
), szCommandline
, szApplicationName
, pidl
, NULL
);
857 if (!done
&& szApplicationName
[0])
860 strcat(cmd
, szApplicationName
);
862 retval
= execfunc(cmd
, NULL
, sei
, FALSE
);
869 /* Else, try to execute the filename */
870 TRACE("execute:'%s','%s'\n", szApplicationName
, szCommandline
);
872 /* resolve shell shortcuts */
873 ext
= PathFindExtensionA(szApplicationName
);
874 if (ext
&& !strcasecmp(ext
, ".lnk")) { /* or check for: shell_attribs & SFGAO_LINK */
875 WCHAR cmd
[MAX_PATH
], args
[MAX_PATH
];
877 if (MultiByteToWideChar(CP_ACP
, 0, szApplicationName
, -1, cmd
, MAX_PATH
)) {
878 if (SUCCEEDED(_ResolveShortCut(cmd
, args
, sei
->hwnd
, &sei
->nShow
, &tmpPidl
))) {
879 if (!*cmd
&& tmpPidl
) {
880 /* We got a PIDL instead of a file system path. */
881 IShellFolder
* desktop
;
884 HRESULT hr
= SHGetDesktopFolder(&desktop
);
887 hr
= IShellFolder_GetDisplayNameOf(desktop
, tmpPidl
, SHGDN_FORPARSING
, &str
);
890 hr
= StrRetToStrNW(cmd
, MAX_PATH
, &str
, tmpPidl
);
894 IShellFolder_Release(desktop
);
897 if (cmd
[0]==':' && cmd
[1]==':') {
898 /* open shell folder for the specified class GUID */
899 strcpy(szApplicationName
, "explorer.exe");
900 WideCharToMultiByte(CP_ACP
, 0, cmd
, -1, szCommandline
, MAX_PATH
, NULL
, NULL
);
903 } else if (HCR_GetExecuteCommandA("Folder", sei
->lpVerb
? sei
->lpVerb
: "open", szCommandline
, sizeof(szCommandline
))) {
906 /*FIXME: seems to have problems in some cases */
907 if (argify(res
, sizeof(res
), szCommandline
, NULL
, tmpPidl
, NULL
))
908 strcpy(szApplicationName
, res
);
910 szCommandline
[0] = '\0';
916 WideCharToMultiByte(CP_ACP
, 0, cmd
, -1, szApplicationName
, MAX_PATH
, NULL
, NULL
);
917 WideCharToMultiByte(CP_ACP
, 0, args
, -1, szCommandline
, MAX_PATH
, NULL
, NULL
);
920 /* If we prevoiusly had a PIDL, it is now resolved, so forget it. */
923 FIXME("We could not resolve the shell shortcut.\n");
927 /* The following code is needed for example to resolve a shortcut
928 to control panel applet "Keyboard", since this is accessed using
929 "rundll32.exe shell32.dll,Control_RunDLL %1,%*" with a command line
930 parameter received from ISF_ControlPanel_fnGetDisplayNameOf(). */
931 if (!*szCommandline
) {
932 if (*szApplicationName
== '"') {
933 LPCSTR src
= szApplicationName
+ 1;
934 LPSTR dst
= fileName
;
936 while(*src
&& *src
!='"')
942 for(++src
; isspace(*src
); )
945 strcpy(szCommandline
, src
);
950 char buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
952 LPSTR beg
= szApplicationName
;
953 for(s
=beg
; space
=strchr(s
, ' '); s
=space
+1) {
954 int idx
= space
-szApplicationName
;
955 strncpy(buffer
, szApplicationName
, idx
);
958 if (SearchPathA(sei
->lpDirectory
, buffer
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
960 /* separate out command from parameter string */
961 LPCSTR p
= space
+ 1;
966 strcpy(szCommandline
, p
);
973 strcpy(fileName
, szApplicationName
);
976 strcpy(fileName
, szApplicationName
);
980 if (szCommandline
[0]) {
981 strcat(szApplicationName
, " ");
982 strcat(szApplicationName
, szCommandline
);
985 retval
= execfunc(szApplicationName
, NULL
, sei
, FALSE
);
988 /* Now, that we have successfully launched a process, we can free the PIDL.
989 It may have been used before for %I command line options. */
993 TRACE("execfunc: retval=%d sei->hInstApp=%p\n", retval
, sei
->hInstApp
);
997 /* Else, try to find the executable */
999 retval
= SHELL_FindExecutable(sei
->lpDirectory
, lpFile
, sei
->lpVerb
, cmd
, lpstrProtocol
, &env
, pidl
, szCommandline
);
1000 if (retval
> 32) /* Found */
1002 CHAR szQuotedCmd
[MAX_PATH
+2];
1003 /* Must quote to handle case where cmd contains spaces,
1004 * else security hole if malicious user creates executable file "C:\\Program"
1006 * FIXME: If we don't have set explicitly command line arguments, we must first
1007 * split executable path from optional command line arguments. Otherwise we would quote
1008 * the complete string with executable path _and_ arguments, which is not what we want.
1010 if (szCommandline
[0])
1011 sprintf(szQuotedCmd
, "\"%s\" %s", cmd
, szCommandline
);
1013 sprintf(szQuotedCmd
, "\"%s\"", cmd
);
1014 TRACE("%s/%s => %s/%s\n", szApplicationName
, sei
->lpVerb
?sei
->lpVerb
:"NULL", szQuotedCmd
, lpstrProtocol
);
1016 retval
= execute_from_key(lpstrProtocol
, lpFile
, env
, sei
, execfunc
, szCommandline
, pidl
);
1018 retval
= execfunc(szQuotedCmd
, env
, sei
, FALSE
);
1019 if (env
) HeapFree( GetProcessHeap(), 0, env
);
1021 else if (PathIsURLA((LPSTR
)lpFile
)) /* File not found, check for URL */
1026 lpstrRes
= strchr(lpFile
, ':');
1028 iSize
= lpstrRes
- lpFile
;
1030 iSize
= strlen(lpFile
);
1032 TRACE("Got URL: %s\n", lpFile
);
1033 /* Looking for ...protocol\shell\<verb>\command */
1034 strncpy(lpstrProtocol
, lpFile
, iSize
);
1035 lpstrProtocol
[iSize
] = '\0';
1036 strcat(lpstrProtocol
, "\\shell\\");
1037 strcat(lpstrProtocol
, sei
->lpVerb
? sei
->lpVerb
: "open"); /*FIXME: enumerate registry subkeys - compare with the loop into SHELL_FindExecutable() */
1038 strcat(lpstrProtocol
, "\\command");
1040 /* Remove File Protocol from lpFile */
1041 /* In the case file://path/file */
1042 if (!strncasecmp(lpFile
, "file", iSize
))
1045 while (*lpFile
== ':') lpFile
++;
1047 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, sei
, execfunc
, szCommandline
, pidl
);
1049 /* Check if file specified is in the form www.??????.*** */
1050 else if (!strncasecmp(lpFile
, "www", 3))
1052 /* if so, append lpFile http:// and call ShellExecute */
1053 char lpstrTmpFile
[256] = "http://" ;
1054 strcat(lpstrTmpFile
, lpFile
);
1055 retval
= (UINT
)ShellExecuteA(sei
->hwnd
, sei
->lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1058 /* Now we can free the PIDL. It may have been used before for %I command line options. */
1062 TRACE("ShellExecuteExA32 retval=%d\n", retval
);
1066 sei
->hInstApp
= (HINSTANCE
)retval
;
1070 sei
->hInstApp
= (HINSTANCE
)33;
1074 /*************************************************************************
1075 * ShellExecuteA [SHELL32.290]
1077 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
1078 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
)
1080 SHELLEXECUTEINFOA sei
;
1081 HANDLE hProcess
= 0;
1084 sei
.cbSize
= sizeof(sei
);
1087 sei
.lpVerb
= lpOperation
;
1088 sei
.lpFile
= lpFile
;
1089 sei
.lpParameters
= lpParameters
;
1090 sei
.lpDirectory
= lpDirectory
;
1091 sei
.nShow
= iShowCmd
;
1096 sei
.hProcess
= hProcess
;
1098 ShellExecuteExA32 (&sei
, SHELL_ExecuteA
);
1099 return sei
.hInstApp
;
1102 /*************************************************************************
1103 * ShellExecuteEx [SHELL32.291]
1106 BOOL WINAPI
ShellExecuteExAW (LPVOID sei
)
1108 if (SHELL_OsIsUnicode())
1109 return ShellExecuteExW (sei
);
1110 return ShellExecuteExA32 (sei
, SHELL_ExecuteA
);
1113 /*************************************************************************
1114 * ShellExecuteExA [SHELL32.292]
1117 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1119 BOOL ret
= ShellExecuteExA32 (sei
, SHELL_ExecuteA
);
1121 TRACE("ShellExecuteExA(): ret=%d\n", ret
);
1126 /*************************************************************************
1127 * ShellExecuteExW [SHELL32.293]
1130 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1132 SHELLEXECUTEINFOA seiA
;
1137 memcpy(&seiA
, sei
, sizeof(SHELLEXECUTEINFOA
));
1140 seiA
.lpVerb
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpVerb
);
1143 seiA
.lpFile
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpFile
);
1145 if (sei
->lpParameters
)
1146 seiA
.lpParameters
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpParameters
);
1148 if (sei
->lpDirectory
)
1149 seiA
.lpDirectory
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpDirectory
);
1151 if ((sei
->fMask
& SEE_MASK_CLASSNAME
) && sei
->lpClass
)
1152 seiA
.lpClass
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpClass
);
1154 seiA
.lpClass
= NULL
;
1156 ret
= ShellExecuteExA(&seiA
);
1158 if (seiA
.lpVerb
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpVerb
);
1159 if (seiA
.lpFile
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpFile
);
1160 if (seiA
.lpParameters
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpParameters
);
1161 if (seiA
.lpDirectory
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpDirectory
);
1162 if (seiA
.lpClass
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpClass
);
1164 sei
->hInstApp
= seiA
.hInstApp
;
1166 TRACE("ShellExecuteExW(): ret=%d\n", ret
);
1171 /*************************************************************************
1172 * ShellExecuteW [SHELL32.294]
1174 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1175 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1177 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1178 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1180 SHELLEXECUTEINFOW sei
;
1181 HANDLE hProcess
= 0;
1185 sei
.cbSize
= sizeof(sei
);
1188 sei
.lpVerb
= lpOperation
;
1189 sei
.lpFile
= lpFile
;
1190 sei
.lpParameters
= lpParameters
;
1191 sei
.lpDirectory
= lpDirectory
;
1192 sei
.nShow
= nShowCmd
;
1197 sei
.hProcess
= hProcess
;
1199 ret
= ShellExecuteExW(&sei
);
1201 TRACE("ShellExecuteW(): ret=%d module=%p", ret
, sei
.hInstApp
);
1202 return sei
.hInstApp
;