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 * FIXME: use PathResolveA() to search for the fully qualified executable path
803 * use PathProcessCommandA() to processes the command line string
805 BOOL WINAPI
ShellExecuteExA32 (LPSHELLEXECUTEINFOA sei
, SHELL_ExecuteA1632 execfunc
)
807 CHAR szApplicationName
[MAX_PATH
+2], szCommandline
[MAX_PATH
], fileName
[MAX_PATH
], dir
[MAX_PATH
];
810 char lpstrProtocol
[256];
817 LPITEMIDLIST pidl
= sei
->lpIDList
;
818 LPITEMIDLIST tmpPidl
= NULL
;
820 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
821 sei
->fMask
, sei
->hwnd
, debugstr_a(sei
->lpVerb
),
822 debugstr_a(sei
->lpFile
), debugstr_a(sei
->lpParameters
),
823 debugstr_a(sei
->lpDirectory
), sei
->nShow
,
824 (sei
->fMask
& SEE_MASK_CLASSNAME
) ? debugstr_a(sei
->lpClass
) : "not used");
826 sei
->hProcess
= NULL
;
829 strcpy(szApplicationName
, sei
->lpFile
);
831 *szApplicationName
= '\0';
833 if (sei
->lpParameters
)
834 strcpy(szCommandline
, sei
->lpParameters
);
836 *szCommandline
= '\0';
838 if (sei
->lpDirectory
)
839 strcpy(dir
, sei
->lpDirectory
);
843 if (sei
->fMask
& (SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
844 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
845 SEE_MASK_DOENVSUBST
| SEE_MASK_FLAG_NO_UI
| SEE_MASK_UNICODE
|
846 SEE_MASK_NO_CONSOLE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
))
848 FIXME("flags ignored: 0x%08lx\n", sei
->fMask
);
851 /* process the IDList */
852 if (sei
->fMask
& SEE_MASK_INVOKEIDLIST
) /* 0x0c: includes SEE_MASK_IDLIST */
854 IShellExecuteHookA
* pSEH
;
856 HRESULT hr
= SHBindToParent(pidl
, &IID_IShellExecuteHookA
, (LPVOID
*)&pSEH
, NULL
);
859 hr
= IShellExecuteHookA_Execute(pSEH
, sei
);
861 IShellExecuteHookA_Release(pSEH
);
867 if (!SHGetPathFromIDListA(pidl
, szApplicationName
))
870 TRACE("-- idlist=%p (%s)\n", pidl
, szApplicationName
);
873 if (sei
->fMask
& (SEE_MASK_CLASSNAME
| SEE_MASK_CLASSKEY
))
875 /* launch a document by fileclass like 'WordPad.Document.1' */
876 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
877 /* FIXME: szCommandline should not be of a fixed size. Plus MAX_PATH is way too short! */
878 if (sei
->fMask
& SEE_MASK_CLASSKEY
)
879 HCR_GetExecuteCommandExA(sei
->hkeyClass
,
880 (sei
->fMask
& SEE_MASK_CLASSNAME
) ? sei
->lpClass
: NULL
,
881 (sei
->lpVerb
) ? sei
->lpVerb
: "open", szCommandline
, sizeof(szCommandline
));
882 else if (sei
->fMask
& SEE_MASK_CLASSNAME
)
883 HCR_GetExecuteCommandA(sei
->lpClass
, (sei
->lpVerb
) ? sei
->lpVerb
:
884 "open", szCommandline
, sizeof(szCommandline
));
886 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
887 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", szCommandline
, szApplicationName
);
890 done
= argify(cmd
, sizeof(cmd
), szCommandline
, szApplicationName
, pidl
, NULL
);
891 if (!done
&& szApplicationName
[0])
894 strcat(cmd
, szApplicationName
);
896 retval
= execfunc(cmd
, NULL
, dir
, sei
, FALSE
);
903 /* Else, try to execute the filename */
904 TRACE("execute:'%s','%s'\n", szApplicationName
, szCommandline
);
906 /* resolve shell shortcuts */
907 ext
= PathFindExtensionA(szApplicationName
);
908 if (ext
&& !strcasecmp(ext
, ".lnk")) { /* or check for: shell_attribs & SFGAO_LINK */
909 WCHAR cmd
[MAX_PATH
], args
[MAX_PATH
], wdir
[MAX_PATH
];
911 if (MultiByteToWideChar(CP_ACP
, 0, szApplicationName
, -1, cmd
, MAX_PATH
)) {
912 MultiByteToWideChar(CP_ACP
, 0, dir
, -1, wdir
, MAX_PATH
);
914 if (SUCCEEDED(_ResolveShortCut(cmd
, wdir
, args
, sei
->hwnd
, &sei
->nShow
, &tmpPidl
))) {
915 if (!*cmd
&& tmpPidl
) {
916 /* We got a PIDL instead of a file system path. */
917 IShellFolder
* desktop
;
920 HRESULT hr
= SHGetDesktopFolder(&desktop
);
923 hr
= IShellFolder_GetDisplayNameOf(desktop
, tmpPidl
, SHGDN_FORPARSING
, &str
);
926 hr
= StrRetToStrNW(cmd
, MAX_PATH
, &str
, tmpPidl
);
930 IShellFolder_Release(desktop
);
933 if (cmd
[0]==':' && cmd
[1]==':') {
934 /* open shell folder for the specified class GUID */
935 strcpy(szApplicationName
, "explorer.exe");
936 WideCharToMultiByte(CP_ACP
, 0, cmd
, -1, szCommandline
, MAX_PATH
, NULL
, NULL
);
939 } else if (HCR_GetExecuteCommandA("Folder", sei
->lpVerb
? sei
->lpVerb
: "open", szCommandline
, sizeof(szCommandline
))) {
942 /*FIXME: seems to have problems in some cases */
943 if (argify(res
, sizeof(res
), szCommandline
, NULL
, tmpPidl
, NULL
))
944 strcpy(szApplicationName
, res
);
946 szCommandline
[0] = '\0';
952 WideCharToMultiByte(CP_ACP
, 0, cmd
, -1, szApplicationName
, MAX_PATH
, NULL
, NULL
);
953 WideCharToMultiByte(CP_ACP
, 0, wdir
, -1, dir
, MAX_PATH
, NULL
, NULL
);
954 WideCharToMultiByte(CP_ACP
, 0, args
, -1, szCommandline
, MAX_PATH
, NULL
, NULL
);
957 /* If we prevoiusly had a PIDL, it is now resolved, so forget it. */
960 FIXME("We could not resolve the shell shortcut.\n");
964 /* The following code is needed for example to resolve a shortcut
965 to control panel applet "Keyboard", since this is accessed using
966 "rundll32.exe shell32.dll,Control_RunDLL %1,%*" with a command line
967 parameter received from ISF_ControlPanel_fnGetDisplayNameOf(). */
968 if (!*szCommandline
) {
969 if (*szApplicationName
== '"') {
970 LPCSTR src
= szApplicationName
+ 1;
971 LPSTR dst
= fileName
;
973 while(*src
&& *src
!='"')
979 for(++src
; isspace(*src
); )
982 strcpy(szCommandline
, src
);
986 /* We have to use this search loop here, that in CreateProcess()
987 is not sufficent because it does not handle shell links. */
989 char buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
991 LPSTR beg
= szApplicationName
;
992 for(s
=beg
; (space
=strchr(s
, ' ')); s
=space
+1) {
993 int idx
= space
-szApplicationName
;
994 strncpy(buffer
, szApplicationName
, idx
);
997 if (SearchPathA(*dir
? dir
: NULL
, buffer
, ".exe", sizeof(xlpFile
), xlpFile
, NULL
))
999 /* separate out command from parameter string */
1000 LPCSTR p
= space
+ 1;
1005 strcpy(szCommandline
, p
);
1012 strcpy(fileName
, szApplicationName
);
1015 strcpy(fileName
, szApplicationName
);
1019 if (szCommandline
[0]) {
1020 strcat(szApplicationName
, " ");
1021 strcat(szApplicationName
, szCommandline
);
1024 retval
= execfunc(szApplicationName
, NULL
, dir
, sei
, FALSE
);
1027 /* Now, that we have successfully launched a process, we can free the PIDL.
1028 It may have been used before for %I command line options. */
1032 TRACE("execfunc: retval=%d sei->hInstApp=%p\n", retval
, sei
->hInstApp
);
1036 /* Else, try to find the executable */
1038 retval
= SHELL_FindExecutable(*dir
? dir
: NULL
, lpFile
, sei
->lpVerb
, cmd
, lpstrProtocol
, &env
, pidl
, szCommandline
);
1039 if (retval
> 32) /* Found */
1041 CHAR szQuotedCmd
[MAX_PATH
+2];
1042 /* Must quote to handle case where cmd contains spaces,
1043 * else security hole if malicious user creates executable file "C:\\Program"
1045 * FIXME: If we don't have set explicitly command line arguments, we must first
1046 * split executable path from optional command line arguments. Otherwise we would quote
1047 * the complete string with executable path _and_ arguments, which is not what we want.
1049 if (szCommandline
[0])
1050 sprintf(szQuotedCmd
, "\"%s\" %s", cmd
, szCommandline
);
1052 sprintf(szQuotedCmd
, "\"%s\"", cmd
);
1053 TRACE("%s/%s => %s/%s\n", szApplicationName
, sei
->lpVerb
?sei
->lpVerb
:"NULL", szQuotedCmd
, lpstrProtocol
);
1055 retval
= execute_from_key(lpstrProtocol
, lpFile
, env
, dir
, sei
, execfunc
, szCommandline
, pidl
);
1057 retval
= execfunc(szQuotedCmd
, env
, dir
, sei
, FALSE
);
1058 if (env
) HeapFree( GetProcessHeap(), 0, env
);
1060 else if (PathIsURLA((LPSTR
)lpFile
)) /* File not found, check for URL */
1065 lpstrRes
= strchr(lpFile
, ':');
1067 iSize
= lpstrRes
- lpFile
;
1069 iSize
= strlen(lpFile
);
1071 TRACE("Got URL: %s\n", lpFile
);
1072 /* Looking for ...protocol\shell\<verb>\command */
1073 strncpy(lpstrProtocol
, lpFile
, iSize
);
1074 lpstrProtocol
[iSize
] = '\0';
1075 strcat(lpstrProtocol
, "\\shell\\");
1076 strcat(lpstrProtocol
, sei
->lpVerb
? sei
->lpVerb
: "open"); /*FIXME: enumerate registry subkeys - compare with the loop into SHELL_FindExecutable() */
1077 strcat(lpstrProtocol
, "\\command");
1079 /* Remove File Protocol from lpFile */
1080 /* In the case file://path/file */
1081 if (!strncasecmp(lpFile
, "file", iSize
))
1084 while (*lpFile
== ':') lpFile
++;
1086 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, dir
, sei
, execfunc
, szCommandline
, pidl
);
1088 /* Check if file specified is in the form www.??????.*** */
1089 else if (!strncasecmp(lpFile
, "www", 3))
1091 /* if so, append lpFile http:// and call ShellExecute */
1092 char lpstrTmpFile
[256] = "http://" ;
1093 strcat(lpstrTmpFile
, lpFile
);
1094 retval
= (UINT
)ShellExecuteA(sei
->hwnd
, sei
->lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1097 /* Now we can free the PIDL. It may have been used before for %I command line options. */
1101 TRACE("ShellExecuteExA32 retval=%d\n", retval
);
1105 sei
->hInstApp
= (HINSTANCE
)retval
;
1109 sei
->hInstApp
= (HINSTANCE
)33;
1113 /*************************************************************************
1114 * ShellExecuteA [SHELL32.290]
1116 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
1117 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
)
1119 SHELLEXECUTEINFOA sei
;
1120 HANDLE hProcess
= 0;
1123 sei
.cbSize
= sizeof(sei
);
1126 sei
.lpVerb
= lpOperation
;
1127 sei
.lpFile
= lpFile
;
1128 sei
.lpParameters
= lpParameters
;
1129 sei
.lpDirectory
= lpDirectory
;
1130 sei
.nShow
= iShowCmd
;
1135 sei
.hProcess
= hProcess
;
1137 ShellExecuteExA32 (&sei
, SHELL_ExecuteA
);
1138 return sei
.hInstApp
;
1141 /*************************************************************************
1142 * ShellExecuteEx [SHELL32.291]
1145 BOOL WINAPI
ShellExecuteExAW (LPVOID sei
)
1147 if (SHELL_OsIsUnicode())
1148 return ShellExecuteExW (sei
);
1149 return ShellExecuteExA32 (sei
, SHELL_ExecuteA
);
1152 /*************************************************************************
1153 * ShellExecuteExA [SHELL32.292]
1156 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1158 BOOL ret
= ShellExecuteExA32 (sei
, SHELL_ExecuteA
);
1160 TRACE("ShellExecuteExA(): ret=%d\n", ret
);
1165 /*************************************************************************
1166 * ShellExecuteExW [SHELL32.293]
1169 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1171 SHELLEXECUTEINFOA seiA
;
1176 memcpy(&seiA
, sei
, sizeof(SHELLEXECUTEINFOA
));
1179 seiA
.lpVerb
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpVerb
);
1182 seiA
.lpFile
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpFile
);
1184 if (sei
->lpParameters
)
1185 seiA
.lpParameters
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpParameters
);
1187 if (sei
->lpDirectory
)
1188 seiA
.lpDirectory
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpDirectory
);
1190 if ((sei
->fMask
& SEE_MASK_CLASSNAME
) && sei
->lpClass
)
1191 seiA
.lpClass
= HEAP_strdupWtoA( GetProcessHeap(), 0, sei
->lpClass
);
1193 seiA
.lpClass
= NULL
;
1195 ret
= ShellExecuteExA(&seiA
);
1197 if (seiA
.lpVerb
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpVerb
);
1198 if (seiA
.lpFile
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpFile
);
1199 if (seiA
.lpParameters
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpParameters
);
1200 if (seiA
.lpDirectory
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpDirectory
);
1201 if (seiA
.lpClass
) HeapFree( GetProcessHeap(), 0, (LPSTR
) seiA
.lpClass
);
1203 sei
->hInstApp
= seiA
.hInstApp
;
1205 TRACE("ShellExecuteExW(): ret=%d\n", ret
);
1210 /*************************************************************************
1211 * ShellExecuteW [SHELL32.294]
1213 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1214 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1216 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1217 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1219 SHELLEXECUTEINFOW sei
;
1220 HANDLE hProcess
= 0;
1224 sei
.cbSize
= sizeof(sei
);
1227 sei
.lpVerb
= lpOperation
;
1228 sei
.lpFile
= lpFile
;
1229 sei
.lpParameters
= lpParameters
;
1230 sei
.lpDirectory
= lpDirectory
;
1231 sei
.nShow
= nShowCmd
;
1236 sei
.hProcess
= hProcess
;
1238 ret
= ShellExecuteExW(&sei
);
1240 TRACE("ShellExecuteW(): ret=%d module=%p", ret
, sei
.hInstApp
);
1241 return sei
.hInstApp
;