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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
39 #include "shell32_main.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
47 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
49 typedef UINT_PTR (*SHELL_ExecuteW32
)(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
50 const SHELLEXECUTEINFOW
*sei
, LPSHELLEXECUTEINFOW sei_out
);
52 static inline BOOL
isSpace(WCHAR c
)
54 return c
== ' ' || c
== '\f' || c
== '\n' || c
== '\r' || c
== '\t' || c
== '\v';
57 /***********************************************************************
58 * SHELL_ArgifyW [Internal]
60 * this function is supposed to expand the escape sequences found in the registry
61 * some diving reported that the following were used:
62 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
67 * %I address of a global item ID (explorer switch /idlist)
68 * %L seems to be %1 as long filename followed by the 8+3 variation
70 * %* all following parameters (see batfile)
73 static BOOL
SHELL_ArgifyW(WCHAR
* out
, int len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
, DWORD
* out_len
)
77 BOOL found_p1
= FALSE
;
82 TRACE("%p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
83 debugstr_w(lpFile
), pidl
, args
);
125 while(*args
&& !isSpace(*args
))
134 while(isSpace(*args
))
139 /* else fall through */
141 if (!done
|| (*fmt
== '1'))
143 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
144 if (SearchPathW(NULL
, lpFile
, L
".exe", ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
149 used
+= lstrlenW(cmd
);
153 res
+= lstrlenW(cmd
);
160 * IE uses this a lot for activating things such as windows media
161 * player. This is not verified to be fully correct but it appears
167 used
+= lstrlenW(lpFile
);
170 lstrcpyW(res
, lpFile
);
171 res
+= lstrlenW(lpFile
);
181 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
182 * allowing a buffer of 100 should more than exceed all needs */
185 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
186 pv
= SHLockShared(hmem
, 0);
187 chars
= swprintf(buf
, ARRAY_SIZE(buf
), L
":%p", pv
);
188 if (chars
>= ARRAY_SIZE(buf
))
189 ERR("pidl format buffer too small!\n");
203 * Check if this is an env-variable here...
206 /* Make sure that we have at least one more %.*/
207 if (wcschr(fmt
, '%'))
209 WCHAR tmpBuffer
[1024];
210 PWSTR tmpB
= tmpBuffer
;
211 WCHAR tmpEnvBuff
[MAX_PATH
];
218 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer
));
220 envRet
= GetEnvironmentVariableW(tmpBuffer
, tmpEnvBuff
, MAX_PATH
);
221 if (envRet
== 0 || envRet
> MAX_PATH
)
223 used
+= lstrlenW(tmpBuffer
);
226 lstrcpyW( res
, tmpBuffer
);
227 res
+= lstrlenW(tmpBuffer
);
232 used
+= lstrlenW(tmpEnvBuff
);
235 lstrcpyW( res
, tmpEnvBuff
);
236 res
+= lstrlenW(tmpEnvBuff
);
243 /* Don't skip past terminator (catch a single '%' at the end) */
265 TRACE("used %li of %i space\n",used
,len
);
272 static HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
275 IShellFolder
* desktop
;
277 HRESULT hr
= SHGetDesktopFolder(&desktop
);
280 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
283 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
285 IShellFolder_Release(desktop
);
291 /*************************************************************************
292 * SHELL_ExecuteW [Internal]
295 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
296 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
298 STARTUPINFOW startup
;
299 PROCESS_INFORMATION info
;
300 UINT_PTR retval
= SE_ERR_NOASSOC
;
302 WCHAR curdir
[MAX_PATH
];
303 DWORD dwCreationFlags
;
305 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
307 /* make sure we don't fail the CreateProcess if the calling app passes in
308 * a bad working directory */
309 if (psei
->lpDirectory
&& psei
->lpDirectory
[0])
311 /* ShellExecute specifies the command from psei->lpDirectory
312 * if present. Not from the current dir as CreateProcess does */
313 if( ( gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
314 if( !SetCurrentDirectoryW( psei
->lpDirectory
))
315 ERR("cannot set directory %s\n", debugstr_w(psei
->lpDirectory
));
318 ZeroMemory(&startup
,sizeof(STARTUPINFOW
));
319 startup
.cb
= sizeof(STARTUPINFOW
);
320 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
321 startup
.wShowWindow
= psei
->nShow
;
322 dwCreationFlags
= CREATE_UNICODE_ENVIRONMENT
;
323 if (!(psei
->fMask
& SEE_MASK_NO_CONSOLE
))
324 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
325 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, dwCreationFlags
, env
,
326 NULL
, &startup
, &info
))
328 /* Give 30 seconds to the app to come up, if desired. Probably only needed
329 when starting app immediately before making a DDE connection. */
331 if (WaitForInputIdle( info
.hProcess
, 30000 ) == WAIT_FAILED
)
332 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
334 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
335 psei_out
->hProcess
= info
.hProcess
;
337 CloseHandle( info
.hProcess
);
338 CloseHandle( info
.hThread
);
340 else if ((retval
= GetLastError()) >= 32)
342 TRACE("CreateProcess returned error %Id\n", retval
);
343 retval
= ERROR_BAD_FORMAT
;
346 TRACE("returning %Iu\n", retval
);
348 psei_out
->hInstApp
= (HINSTANCE
)retval
;
350 if( !SetCurrentDirectoryW( curdir
))
351 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
357 /***********************************************************************
358 * SHELL_BuildEnvW [Internal]
360 * Build the environment for the new process, adding the specified
361 * path to the PATH variable. Returned pointer must be freed by caller.
363 static void *SHELL_BuildEnvW( const WCHAR
*path
)
365 WCHAR
*strings
, *new_env
;
367 int total
= lstrlenW(path
) + 1;
368 BOOL got_path
= FALSE
;
370 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
374 int len
= lstrlenW(p
) + 1;
375 if (!wcsnicmp( p
, L
"PATH=", 5 )) got_path
= TRUE
;
379 if (!got_path
) total
+= 5; /* we need to create PATH */
380 total
++; /* terminating null */
382 if (!(new_env
= malloc( total
* sizeof(WCHAR
) )))
384 FreeEnvironmentStringsW( strings
);
391 int len
= lstrlenW(p
) + 1;
392 memcpy( p2
, p
, len
* sizeof(WCHAR
) );
393 if (!wcsnicmp( p
, L
"PATH=", 5 ))
396 lstrcpyW( p2
+ len
, path
);
397 p2
+= lstrlenW(path
) + 1;
404 lstrcpyW( p2
, L
"PATH=" );
405 lstrcatW( p2
, path
);
406 p2
+= lstrlenW(p2
) + 1;
409 FreeEnvironmentStringsW( strings
);
414 /***********************************************************************
415 * SHELL_TryAppPathW [Internal]
417 * Helper function for SHELL_FindExecutable
418 * @param lpResult - pointer to a buffer of size MAX_PATH
419 * On entry: szName is a filename (probably without path separators).
420 * On exit: if szName found in "App Path", place full path in lpResult, and return true
422 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
430 if (env
) *env
= NULL
;
431 wcscpy(buffer
, L
"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
432 if (wcslen(buffer
) + wcslen(szName
) + 1 > ARRAY_SIZE(buffer
))
434 WARN("Name is too big.\n");
438 wcscat(buffer
, szName
);
439 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
442 len
= MAX_PATH
*sizeof(WCHAR
);
443 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
449 DWORD count
= sizeof(buffer
);
450 if (!RegQueryValueExW(hkApp
, L
"Path", NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
451 *env
= SHELL_BuildEnvW( buffer
);
455 if (hkApp
) RegCloseKey(hkApp
);
459 /*************************************************************************
460 * SHELL_FindExecutableByVerb [Internal]
462 * called from SHELL_FindExecutable or SHELL_execute_class
464 * classname a buffer, big enough, to get the key name to do actually the
465 * command "WordPad.Document.1\\shell\\open\\command"
466 * passed as "WordPad.Document.1"
468 * lpVerb the operation on it (open)
469 * commandlen the size of command buffer (in bytes)
471 * command a buffer, to store the command to do the
472 * operation on the file
473 * key a buffer, big enough, to get the key name to do actually the
474 * command "WordPad.Document.1\\shell\\open\\command"
477 static UINT
SHELL_FindExecutableByVerb(LPCWSTR lpVerb
, LPWSTR key
, LPWSTR classname
, LPWSTR command
, LONG commandlen
)
480 WCHAR verb
[MAX_PATH
];
482 if (*classname
== '.')
484 /* Extension, default key value holds class name. Extension may also have
485 * empty class name and shell\verb\command subkey. */
486 WCHAR
class[MAX_PATH
];
490 if (!RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, class, &len
) && *class)
491 wcscpy(classname
, class);
494 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, classname
, 0, 0x02000000, &hkeyClass
))
495 return SE_ERR_NOASSOC
;
496 if (!HCR_GetDefaultVerbW(hkeyClass
, lpVerb
, verb
, ARRAY_SIZE(verb
)))
497 return SE_ERR_NOASSOC
;
498 RegCloseKey(hkeyClass
);
500 /* Looking for ...buffer\shell\<verb>\command */
501 lstrcatW(classname
, L
"\\shell\\");
502 lstrcatW(classname
, verb
);
503 lstrcatW(classname
, L
"\\command");
505 if (RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, command
,
506 &commandlen
) == ERROR_SUCCESS
)
508 commandlen
/= sizeof(WCHAR
);
509 if (key
) lstrcpyW(key
, classname
);
513 LONG paramlen
= sizeof(param
);
515 /* FIXME: it seems all Windows version don't behave the same here.
516 * the doc states that this ddeexec information can be found after
518 * on Win98, it doesn't appear, but I think it does on Win2k
520 /* Get the parameters needed by the application
521 from the associated ddeexec key */
522 tmp
= wcsstr(classname
, L
"\\command");
524 lstrcatW(classname
, wDdeexec
);
525 if (RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, param
,
526 ¶mlen
) == ERROR_SUCCESS
)
528 paramlen
/= sizeof(WCHAR
);
529 lstrcatW(command
, L
" ");
530 lstrcatW(command
, param
);
531 commandlen
+= paramlen
;
535 command
[commandlen
] = '\0';
537 return 33; /* FIXME see SHELL_FindExecutable() */
540 return SE_ERR_NOASSOC
;
543 /*************************************************************************
544 * SHELL_FindExecutable [Internal]
546 * Utility for code sharing between FindExecutable and ShellExecute
548 * lpFile the name of a file
549 * lpVerb the operation on it (open)
551 * lpResult a buffer, big enough :-(, to store the command to do the
552 * operation on the file
553 * key a buffer, big enough, to get the key name to do actually the
554 * command (it'll be used afterwards for more information
557 static UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpVerb
,
558 LPWSTR lpResult
, int resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
560 WCHAR
*extension
= NULL
; /* pointer to file extension */
561 WCHAR classname
[256]; /* registry name for this file type */
562 LONG classnamelen
= sizeof(classname
); /* length of above */
563 WCHAR command
[1024]; /* command from registry */
564 WCHAR wBuffer
[256]; /* Used to GetProfileString */
565 UINT retval
= SE_ERR_NOASSOC
;
566 WCHAR
*tok
; /* token pointer */
567 WCHAR xlpFile
[256]; /* result of SearchPath */
568 DWORD attribs
; /* file attributes */
570 TRACE("%s\n", debugstr_w(lpFile
));
573 return ERROR_INVALID_PARAMETER
;
576 lpResult
[0] = '\0'; /* Start off with an empty return string */
577 if (key
) *key
= '\0';
579 /* trap NULL parameters on entry */
582 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
583 debugstr_w(lpFile
), debugstr_w(lpResult
));
584 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
587 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
589 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
593 if (SearchPathW(lpPath
, lpFile
, L
".exe", ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
595 TRACE("SearchPathW returned non-zero\n");
597 /* The file was found in the application-supplied default directory (or the system search path) */
599 else if (lpPath
&& SearchPathW(NULL
, lpFile
, L
".exe", ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
601 TRACE("SearchPathW returned non-zero\n");
603 /* The file was found in one of the directories in the system-wide search path */
606 attribs
= GetFileAttributesW(lpFile
);
607 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
609 lstrcpyW(classname
, L
"Folder");
613 /* Did we get something? Anything? */
616 TRACE("Returning SE_ERR_FNF\n");
619 /* First thing we need is the file's extension */
620 extension
= wcsrchr(xlpFile
, '.'); /* Assume last "." is the one; */
621 /* File->Run in progman uses */
623 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
625 if (extension
== NULL
|| extension
[1]==0)
627 WARN("Returning SE_ERR_NOASSOC\n");
628 return SE_ERR_NOASSOC
;
631 /* Three places to check: */
632 /* 1. win.ini, [windows], programs (NB no leading '.') */
633 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
634 /* 3. win.ini, [extensions], extension (NB no leading '.' */
635 /* All I know of the order is that registry is checked before */
636 /* extensions; however, it'd make sense to check the programs */
637 /* section first, so that's what happens here. */
639 /* See if it's a program - if GetProfileString fails, we skip this
640 * section. Actually, if GetProfileString fails, we've probably
641 * got a lot more to worry about than running a program... */
642 if (GetProfileStringW(L
"windows", L
"programs", L
"exe pif bat cmd com", wBuffer
, ARRAY_SIZE(wBuffer
)) > 0)
649 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
653 while (*p
== ' ' || *p
== '\t') p
++;
656 if (wcsicmp(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
658 lstrcpyW(lpResult
, xlpFile
);
659 /* Need to perhaps check that the file has a path
661 TRACE("found %s\n", debugstr_w(lpResult
));
663 /* Greater than 32 to indicate success */
670 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, classname
,
671 &classnamelen
) == ERROR_SUCCESS
)
673 classnamelen
/= sizeof(WCHAR
);
674 if (classnamelen
== ARRAY_SIZE(classname
))
676 classname
[classnamelen
] = '\0';
677 TRACE("File type: %s\n", debugstr_w(classname
));
687 /* pass the verb string to SHELL_FindExecutableByVerb() */
688 retval
= SHELL_FindExecutableByVerb(lpVerb
, key
, classname
, command
, sizeof(command
));
693 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
694 if (finishedLen
> resultLen
)
695 ERR("Argify buffer not large enough.. truncated\n");
697 /* Remove double quotation marks and command line arguments */
698 if (*lpResult
== '"')
701 while (*(p
+ 1) != '"')
710 /* Truncate on first space */
712 while (*p
!= ' ' && *p
!= '\0')
718 else /* Check win.ini */
720 /* Toss the leading dot */
722 if (GetProfileStringW(L
"extensions", extension
, L
"", command
, ARRAY_SIZE(command
)) > 0)
726 lstrcpyW(lpResult
, command
);
727 tok
= wcschr(lpResult
, '^'); /* should be ^.extension? */
731 lstrcatW(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
732 tok
= wcschr(command
, '^'); /* see above */
733 if ((tok
!= NULL
) && (lstrlenW(tok
)>5))
735 lstrcatW(lpResult
, &tok
[5]);
738 retval
= 33; /* FIXME - see above */
743 TRACE("returning %s\n", debugstr_w(lpResult
));
747 /******************************************************************
750 * callback for the DDE connection. not really useful
752 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
753 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
754 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
756 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08Ix, %08Ix\n",
757 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
761 /******************************************************************
764 * ShellExecute helper. Used to do an operation with a DDE connection
766 * Handles both the direct connection (try #1), and if it fails,
767 * launching an application and trying (#2) to connect to it
770 static unsigned dde_connect(const WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
771 const WCHAR
* lpFile
, WCHAR
*env
,
772 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
773 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
776 WCHAR
* endkey
= regkey
+ lstrlenW(key
);
777 WCHAR app
[256], topic
[256], ifexec
[256], static_res
[256];
778 WCHAR
* dynamic_res
=NULL
;
780 LONG applen
, topiclen
, ifexeclen
;
784 DWORD resultLen
, endkeyLen
;
785 HSZ hszApp
, hszTopic
;
788 unsigned ret
= SE_ERR_NOASSOC
;
789 BOOL unicode
= !(GetVersion() & 0x80000000);
791 if (lstrlenW(key
) + 1 > ARRAY_SIZE(regkey
))
793 FIXME("input parameter %s larger than buffer\n", debugstr_w(key
));
796 lstrcpyW(regkey
, key
);
797 endkeyLen
= ARRAY_SIZE(regkey
) - (endkey
- regkey
);
798 if (lstrlenW(L
"\\application") + 1 > endkeyLen
)
800 FIXME("endkey overruns buffer\n");
803 lstrcpyW(endkey
, L
"\\application");
804 applen
= sizeof(app
);
805 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, app
, &applen
) != ERROR_SUCCESS
)
807 WCHAR command
[1024], fullpath
[MAX_PATH
];
811 /* Get application command from start string and find filename of application */
814 if (lstrlenW(start
+ 1) + 1 > ARRAY_SIZE(command
))
816 FIXME("size of input parameter %s larger than buffer\n",
817 debugstr_w(start
+ 1));
820 lstrcpyW(command
, start
+1);
821 if ((ptr
= wcschr(command
, '"')))
823 ret
= SearchPathW(NULL
, command
, L
".exe", ARRAY_SIZE(fullpath
), fullpath
, &ptr
);
829 for (p
=start
; (space
=wcschr(p
, ' ')); p
=space
+1)
831 int idx
= space
-start
;
832 memcpy(command
, start
, idx
*sizeof(WCHAR
));
834 if ((ret
= SearchPathW(NULL
, command
, L
".exe", ARRAY_SIZE(fullpath
), fullpath
, &ptr
)))
838 ret
= SearchPathW(NULL
, start
, L
".exe", ARRAY_SIZE(fullpath
), fullpath
, &ptr
);
843 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
844 return ERROR_ACCESS_DENIED
;
846 if (lstrlenW(ptr
) + 1 > ARRAY_SIZE(app
))
848 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr
));
853 /* Remove extensions (including .so) */
854 ptr
= app
+ lstrlenW(app
) - 3;
855 if (ptr
> app
&& !wcscmp(ptr
, L
".so"))
858 ptr
= wcsrchr(app
, '.');
863 if (lstrlenW(L
"\\topic") + 1 > endkeyLen
)
865 FIXME("endkey overruns buffer\n");
868 lstrcpyW(endkey
, L
"\\topic");
869 topiclen
= sizeof(topic
);
870 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, topic
, &topiclen
) != ERROR_SUCCESS
)
871 lstrcpyW(topic
, L
"System");
875 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
880 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
884 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
885 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
887 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
891 TRACE("Launching %s\n", debugstr_w(start
));
892 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
895 TRACE("Couldn't launch\n");
898 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
901 TRACE("Couldn't connect. ret=%d\n", ret
);
902 DdeUninitialize(ddeInst
);
903 SetLastError(ERROR_DDE_FAIL
);
904 return 30; /* whatever */
906 if (lstrlenW(L
"\\ifexec") + 1 > endkeyLen
)
908 FIXME("endkey overruns buffer\n");
911 lstrcpyW(endkey
, L
"\\ifexec");
912 ifexeclen
= sizeof(ifexec
);
913 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
919 SHELL_ArgifyW(static_res
, ARRAY_SIZE(static_res
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
920 if (resultLen
> ARRAY_SIZE(static_res
))
922 res
= dynamic_res
= malloc(resultLen
* sizeof(WCHAR
));
923 SHELL_ArgifyW(dynamic_res
, resultLen
, exec
, lpFile
, pidl
, szCommandline
, NULL
);
927 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
929 /* It's documented in the KB 330337 that IE has a bug and returns
930 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
933 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (lstrlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0,
934 XTYP_EXECUTE
, 30000, &tid
);
937 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
938 char *resA
= malloc(lenA
);
939 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
940 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
941 XTYP_EXECUTE
, 10000, &tid
);
945 DdeFreeDataHandle(hDdeData
);
947 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
952 DdeDisconnect(hConv
);
955 DdeUninitialize(ddeInst
);
960 /*************************************************************************
961 * execute_from_key [Internal]
963 static UINT_PTR
execute_from_key(LPCWSTR key
, LPCWSTR lpFile
, WCHAR
*env
, LPCWSTR szCommandline
,
964 LPCWSTR executable_name
,
965 SHELL_ExecuteW32 execfunc
,
966 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
968 WCHAR cmd
[256], parambuffer
[1024], ddeexec
[256], *param
= parambuffer
;
969 LONG cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
970 UINT_PTR retval
= SE_ERR_NOASSOC
;
974 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
975 debugstr_w(szCommandline
), debugstr_w(executable_name
));
980 /* Get the application from the registry */
981 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
983 TRACE("got cmd: %s\n", debugstr_w(cmd
));
985 /* Is there a replace() function anywhere? */
986 cmdlen
/= sizeof(WCHAR
);
987 if (cmdlen
>= ARRAY_SIZE(cmd
))
988 cmdlen
= ARRAY_SIZE(cmd
) - 1;
990 SHELL_ArgifyW(param
, ARRAY_SIZE(parambuffer
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
, &resultLen
);
991 if (resultLen
> ARRAY_SIZE(parambuffer
))
993 if ((param
= malloc(resultLen
* sizeof(*param
))))
994 SHELL_ArgifyW(param
, resultLen
, cmd
, lpFile
, psei
->lpIDList
, szCommandline
, &resultLen
);
998 /* Get the parameters needed by the application
999 from the associated ddeexec key */
1000 tmp
= wcsstr(key
, L
"command");
1002 lstrcpyW(tmp
, L
"ddeexec");
1004 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, &ddeexeclen
) == ERROR_SUCCESS
)
1006 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
1007 if (!param
[0]) lstrcpyW(param
, executable_name
);
1008 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, psei
->lpIDList
, execfunc
, psei
, psei_out
);
1012 TRACE("executing: %s\n", debugstr_w(param
));
1013 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
1016 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
1018 if (param
!= parambuffer
) free(param
);
1022 /*************************************************************************
1023 * FindExecutableA [SHELL32.@]
1025 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
1028 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
1029 WCHAR wResult
[MAX_PATH
];
1031 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
1032 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
1034 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
1035 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
1037 SHFree( wDirectory
);
1039 TRACE("returning %s\n", lpResult
);
1043 /*************************************************************************
1044 * FindExecutableW [SHELL32.@]
1046 * This function returns the executable associated with the specified file
1047 * for the default verb.
1050 * lpFile [I] The file to find the association for. This must refer to
1051 * an existing file otherwise FindExecutable fails and returns
1053 * lpResult [O] Points to a buffer into which the executable path is
1054 * copied. This parameter must not be NULL otherwise
1055 * FindExecutable() segfaults. The buffer must be of size at
1056 * least MAX_PATH characters.
1059 * A value greater than 32 on success, less than or equal to 32 otherwise.
1060 * See the SE_ERR_* constants.
1063 * On Windows XP and 2003, FindExecutable() seems to first convert the
1064 * filename into 8.3 format, thus taking into account only the first three
1065 * characters of the extension, and expects to find an association for those.
1066 * However other Windows versions behave sanely.
1068 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
1070 UINT_PTR retval
= SE_ERR_NOASSOC
;
1071 WCHAR old_dir
[1024];
1072 WCHAR res
[MAX_PATH
];
1074 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
1076 lpResult
[0] = '\0'; /* Start off with an empty return string */
1078 return (HINSTANCE
)SE_ERR_FNF
;
1082 GetCurrentDirectoryW(ARRAY_SIZE(old_dir
), old_dir
);
1083 SetCurrentDirectoryW(lpDirectory
);
1086 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, L
"open", res
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1089 lstrcpyW(lpResult
, res
);
1091 TRACE("returning %s\n", debugstr_w(lpResult
));
1093 SetCurrentDirectoryW(old_dir
);
1094 return (HINSTANCE
)retval
;
1097 /* FIXME: is this already implemented somewhere else? */
1098 static HKEY
ShellExecute_GetClassKey( const SHELLEXECUTEINFOW
*sei
)
1100 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1102 DWORD type
= 0, sz
= 0;
1106 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1107 return sei
->hkeyClass
;
1109 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1110 lpClass
= sei
->lpClass
;
1113 ext
= PathFindExtensionW( sei
->lpFile
);
1114 TRACE("ext = %s\n", debugstr_w( ext
) );
1118 r
= RegOpenKeyW( HKEY_CLASSES_ROOT
, ext
, &hkey
);
1119 if (r
!= ERROR_SUCCESS
)
1122 r
= RegQueryValueExW( hkey
, NULL
, 0, &type
, NULL
, &sz
);
1123 if ( r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1125 sz
+= sizeof (WCHAR
);
1128 RegQueryValueExW( hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1131 RegCloseKey( hkey
);
1135 TRACE("class = %s\n", debugstr_w(lpClass
) );
1139 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1146 static HRESULT
shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
, IDataObject
**dataobj
)
1148 LPCITEMIDLIST pidllast
= NULL
;
1149 IShellFolder
*shf
= NULL
;
1150 LPITEMIDLIST pidl
= NULL
;
1151 HRESULT r
= SE_ERR_DLLNOTFOUND
;
1153 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1154 pidl
= sei
->lpIDList
;
1157 WCHAR fullpath
[MAX_PATH
];
1161 ret
= GetFullPathNameW( sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1165 pidl
= ILCreateFromPathW( fullpath
);
1168 r
= SHBindToParent( pidl
, &IID_IShellFolder
, (LPVOID
*)&shf
, &pidllast
);
1172 r
= IShellFolder_GetUIObjectOf( shf
, NULL
, 1, &pidllast
,
1173 &IID_IDataObject
, NULL
, (void**)dataobj
);
1176 if ( pidl
!= sei
->lpIDList
)
1179 IShellFolder_Release( shf
);
1183 static HRESULT
shellex_run_context_menu_default( IShellExtInit
*obj
,
1184 LPSHELLEXECUTEINFOW sei
)
1186 IContextMenu
*cm
= NULL
;
1187 CMINVOKECOMMANDINFOEX ici
;
1194 TRACE("%p %p\n", obj
, sei
);
1196 r
= IShellExtInit_QueryInterface( obj
, &IID_IContextMenu
, (LPVOID
*) &cm
);
1200 hmenu
= CreateMenu();
1204 /* the number of the last menu added is returned in r */
1205 r
= IContextMenu_QueryContextMenu( cm
, hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1209 n
= GetMenuItemCount( hmenu
);
1210 for ( i
= 0; i
< n
; i
++ )
1212 memset( &info
, 0, sizeof info
);
1213 info
.cbSize
= sizeof info
;
1214 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1215 info
.dwTypeData
= string
;
1216 info
.cch
= ARRAY_SIZE(string
);
1218 GetMenuItemInfoW( hmenu
, i
, TRUE
, &info
);
1220 TRACE("menu %d %s %08x %08Ix %08x %08x\n", i
, debugstr_w(string
),
1221 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1222 if ( ( !sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
) ) ||
1223 ( sei
->lpVerb
&& !lstrcmpiW( sei
->lpVerb
, string
) ) )
1234 memset( &ici
, 0, sizeof ici
);
1235 ici
.cbSize
= sizeof ici
;
1236 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NO_CONSOLE
|SEE_MASK_NOASYNC
|SEE_MASK_ASYNCOK
|SEE_MASK_FLAG_NO_UI
));
1237 ici
.nShow
= sei
->nShow
;
1238 ici
.lpVerb
= MAKEINTRESOURCEA( def
);
1239 ici
.hwnd
= sei
->hwnd
;
1240 ici
.lpParametersW
= sei
->lpParameters
;
1242 r
= IContextMenu_InvokeCommand( cm
, (LPCMINVOKECOMMANDINFO
) &ici
);
1244 TRACE("invoke command returned %08lx\n", r
);
1248 DestroyMenu( hmenu
);
1250 IContextMenu_Release( cm
);
1254 static HRESULT
shellex_load_object_and_run( HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1256 IDataObject
*dataobj
= NULL
;
1257 IObjectWithSite
*ows
= NULL
;
1258 IShellExtInit
*obj
= NULL
;
1261 TRACE("%p %s %p\n", hkey
, debugstr_guid( guid
), sei
);
1263 r
= CoInitialize( NULL
);
1267 r
= CoCreateInstance( guid
, NULL
, CLSCTX_INPROC_SERVER
,
1268 &IID_IShellExtInit
, (LPVOID
*)&obj
);
1271 ERR("failed %08lx\n", r
);
1275 r
= shellex_get_dataobj( sei
, &dataobj
);
1278 ERR("failed to get data object\n");
1282 r
= IShellExtInit_Initialize( obj
, NULL
, dataobj
, hkey
);
1286 r
= IShellExtInit_QueryInterface( obj
, &IID_IObjectWithSite
, (LPVOID
*) &ows
);
1290 IObjectWithSite_SetSite( ows
, NULL
);
1292 r
= shellex_run_context_menu_default( obj
, sei
);
1296 IObjectWithSite_Release( ows
);
1298 IDataObject_Release( dataobj
);
1300 IShellExtInit_Release( obj
);
1306 /*************************************************************************
1307 * ShellExecute_FromContextMenu [Internal]
1309 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1311 HKEY hkey
, hkeycm
= 0;
1318 TRACE("%s\n", debugstr_w(sei
->lpFile
) );
1320 hkey
= ShellExecute_GetClassKey( sei
);
1322 return ERROR_FUNCTION_FAILED
;
1324 r
= RegOpenKeyW( hkey
, L
"shellex\\ContextMenuHandlers", &hkeycm
);
1325 if ( r
== ERROR_SUCCESS
)
1330 r
= RegEnumKeyW( hkeycm
, i
++, szguid
, ARRAY_SIZE(szguid
));
1331 if ( r
!= ERROR_SUCCESS
)
1334 hr
= CLSIDFromString( szguid
, &guid
);
1337 /* stop at the first one that succeeds in running */
1338 hr
= shellex_load_object_and_run( hkey
, &guid
, sei
);
1339 if ( SUCCEEDED( hr
) )
1343 RegCloseKey( hkeycm
);
1346 if ( hkey
!= sei
->hkeyClass
)
1347 RegCloseKey( hkey
);
1351 static UINT_PTR
SHELL_quote_and_execute( LPCWSTR wcmd
, LPCWSTR wszParameters
, LPCWSTR lpstrProtocol
, LPCWSTR wszApplicationName
, LPWSTR env
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
);
1353 static UINT_PTR
SHELL_execute_class( LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1355 WCHAR execCmd
[1024], classname
[1024];
1356 /* launch a document by fileclass like 'WordPad.Document.1' */
1357 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1358 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1359 ULONG cmask
=(psei
->fMask
& SEE_MASK_CLASSALL
);
1364 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1365 if (cmask
!= SEE_MASK_CLASSNAME
)
1368 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1369 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1371 execCmd
, sizeof(execCmd
));
1373 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1374 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1377 done
= SHELL_ArgifyW(wcmd
, ARRAY_SIZE(wcmd
), execCmd
, wszApplicationName
, psei
->lpIDList
, NULL
, &resultLen
);
1378 if (!done
&& wszApplicationName
[0])
1380 lstrcatW(wcmd
, L
" ");
1381 if (*wszApplicationName
!= '"')
1383 lstrcatW(wcmd
, L
"\"");
1384 lstrcatW(wcmd
, wszApplicationName
);
1385 lstrcatW(wcmd
, L
"\"");
1388 lstrcatW(wcmd
, wszApplicationName
);
1390 if (resultLen
> ARRAY_SIZE(wcmd
))
1391 ERR("Argify buffer not large enough... truncating\n");
1392 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1395 lstrcpyW(classname
, psei
->lpClass
);
1396 rslt
= SHELL_FindExecutableByVerb(psei
->lpVerb
, NULL
, classname
, execCmd
, sizeof(execCmd
));
1398 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt
, debugstr_w(classname
), debugstr_w(execCmd
));
1401 rslt
= SHELL_quote_and_execute( execCmd
, L
"", classname
,
1402 wszApplicationName
, NULL
, psei
,
1403 psei_out
, execfunc
);
1407 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1409 WCHAR buffer
[MAX_PATH
];
1411 /* last chance to translate IDList: now also allow CLSID paths */
1412 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei
->lpIDList
, buffer
, ARRAY_SIZE(buffer
)))) {
1413 if (buffer
[0]==':' && buffer
[1]==':') {
1414 /* open shell folder for the specified class GUID */
1415 if (lstrlenW(buffer
) + 1 > parametersLen
)
1416 ERR("parameters len exceeds buffer size (%i > %li), truncating\n",
1417 lstrlenW(buffer
) + 1, parametersLen
);
1418 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1419 if (lstrlenW(L
"explorer.exe") > dwApplicationNameLen
)
1420 ERR("application len exceeds buffer size (%li), truncating\n",
1421 dwApplicationNameLen
);
1422 lstrcpynW(wszApplicationName
, L
"explorer.exe", dwApplicationNameLen
);
1424 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1426 WCHAR target
[MAX_PATH
];
1429 /* Check if we're executing a directory and if so use the
1430 handler for the Folder class */
1431 lstrcpyW(target
, buffer
);
1432 attribs
= GetFileAttributesW(buffer
);
1433 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1434 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1435 HCR_GetExecuteCommandW(0, L
"Folder",
1437 buffer
, sizeof(buffer
))) {
1438 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1439 buffer
, target
, sei
->lpIDList
, NULL
, &resultLen
);
1440 if (resultLen
> dwApplicationNameLen
)
1441 ERR("Argify buffer not large enough... truncating\n");
1443 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1448 static UINT_PTR
SHELL_quote_and_execute( LPCWSTR wcmd
, LPCWSTR wszParameters
, LPCWSTR wszKeyname
, LPCWSTR wszApplicationName
, LPWSTR env
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1452 WCHAR
*wszQuotedCmd
;
1454 /* Length of quotes plus length of command plus NULL terminator */
1455 len
= 2 + lstrlenW(wcmd
) + 1;
1456 if (wszParameters
[0])
1458 /* Length of space plus length of parameters */
1459 len
+= 1 + lstrlenW(wszParameters
);
1461 wszQuotedCmd
= malloc(len
* sizeof(WCHAR
));
1462 /* Must quote to handle case where cmd contains spaces,
1463 * else security hole if malicious user creates executable file "C:\\Program"
1465 lstrcpyW(wszQuotedCmd
, L
"\"");
1466 lstrcatW(wszQuotedCmd
, wcmd
);
1467 lstrcatW(wszQuotedCmd
, L
"\"");
1468 if (wszParameters
[0]) {
1469 lstrcatW(wszQuotedCmd
, L
" ");
1470 lstrcatW(wszQuotedCmd
, wszParameters
);
1472 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(wszKeyname
));
1474 retval
= execute_from_key(wszKeyname
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1476 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1481 static UINT_PTR
SHELL_execute_url( LPCWSTR lpFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1484 WCHAR
*lpstrProtocol
;
1489 lpstrRes
= wcschr(lpFile
, ':');
1491 iSize
= lpstrRes
- lpFile
;
1493 iSize
= lstrlenW(lpFile
);
1495 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1496 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1497 len
= iSize
+ lstrlenW(L
"\\shell\\") + lstrlenW(L
"\\command") + 1;
1498 if (psei
->lpVerb
&& *psei
->lpVerb
)
1499 len
+= lstrlenW(psei
->lpVerb
);
1501 len
+= lstrlenW(L
"open");
1502 lpstrProtocol
= malloc(len
* sizeof(WCHAR
));
1503 memcpy(lpstrProtocol
, lpFile
, iSize
*sizeof(WCHAR
));
1504 lpstrProtocol
[iSize
] = '\0';
1505 lstrcatW(lpstrProtocol
, L
"\\shell\\");
1506 lstrcatW(lpstrProtocol
, psei
->lpVerb
&& *psei
->lpVerb
? psei
->lpVerb
: L
"open");
1507 lstrcatW(lpstrProtocol
, L
"\\command");
1509 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1510 wcmd
, execfunc
, psei
, psei_out
);
1511 free(lpstrProtocol
);
1515 static void do_error_dialog( UINT_PTR retval
, HWND hwnd
)
1518 int error_code
=GetLastError();
1520 if (retval
== SE_ERR_NOASSOC
)
1521 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, ARRAY_SIZE(msg
));
1523 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error_code
, 0, msg
, ARRAY_SIZE(msg
), NULL
);
1525 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1528 static WCHAR
*expand_environment( const WCHAR
*str
)
1533 len
= ExpandEnvironmentStringsW(str
, NULL
, 0);
1534 if (!len
) return NULL
;
1536 buf
= malloc(len
* sizeof(WCHAR
));
1537 if (!buf
) return NULL
;
1539 len
= ExpandEnvironmentStringsW(str
, buf
, len
);
1548 /*************************************************************************
1549 * SHELL_execute [Internal]
1551 static BOOL
SHELL_execute( LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1553 static const DWORD unsupportedFlags
=
1554 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1555 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1556 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1558 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1559 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
= NULL
;
1560 DWORD dwApplicationNameLen
= MAX_PATH
+2;
1561 DWORD parametersLen
= ARRAY_SIZE(parametersBuffer
);
1562 DWORD wcmdLen
= ARRAY_SIZE(wcmdBuffer
);
1564 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1566 WCHAR wszKeyname
[256];
1568 UINT_PTR retval
= SE_ERR_NOASSOC
;
1570 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1573 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1574 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1575 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1576 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1577 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1578 debugstr_w(sei_tmp
.lpClass
) : "not used");
1580 sei
->hProcess
= NULL
;
1582 /* make copies of all path/command strings */
1583 if (!sei_tmp
.lpFile
)
1585 wszApplicationName
= malloc(dwApplicationNameLen
* sizeof(WCHAR
));
1586 *wszApplicationName
= '\0';
1588 else if (*sei_tmp
.lpFile
== '\"' && sei_tmp
.lpFile
[(len
= lstrlenW(sei_tmp
.lpFile
))-1] == '\"')
1590 if(len
-1 >= dwApplicationNameLen
) dwApplicationNameLen
= len
;
1591 wszApplicationName
= malloc(dwApplicationNameLen
* sizeof(WCHAR
));
1592 memcpy(wszApplicationName
, sei_tmp
.lpFile
+1, len
*sizeof(WCHAR
));
1594 wszApplicationName
[len
-2] = '\0';
1595 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName
));
1597 DWORD l
= lstrlenW(sei_tmp
.lpFile
)+1;
1598 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1599 wszApplicationName
= malloc(dwApplicationNameLen
* sizeof(WCHAR
));
1600 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
*sizeof(WCHAR
));
1603 wszParameters
= parametersBuffer
;
1604 if (sei_tmp
.lpParameters
)
1606 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1607 if (len
> parametersLen
)
1609 wszParameters
= malloc(len
* sizeof(WCHAR
));
1610 parametersLen
= len
;
1612 lstrcpyW(wszParameters
, sei_tmp
.lpParameters
);
1615 *wszParameters
= '\0';
1618 if (sei_tmp
.lpDirectory
)
1620 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1621 if (len
> ARRAY_SIZE(dirBuffer
))
1622 wszDir
= malloc(len
* sizeof(WCHAR
));
1623 lstrcpyW(wszDir
, sei_tmp
.lpDirectory
);
1628 /* adjust string pointers to point to the new buffers */
1629 sei_tmp
.lpFile
= wszApplicationName
;
1630 sei_tmp
.lpParameters
= wszParameters
;
1631 sei_tmp
.lpDirectory
= wszDir
;
1633 if (sei_tmp
.fMask
& unsupportedFlags
)
1635 FIXME("flags ignored: 0x%08lx\n", sei_tmp
.fMask
& unsupportedFlags
);
1638 /* process the IDList */
1639 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1641 IShellExecuteHookW
* pSEH
;
1643 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1647 hr
= IShellExecuteHookW_Execute(pSEH
, &sei_tmp
);
1649 IShellExecuteHookW_Release(pSEH
);
1652 free(wszApplicationName
);
1653 if (wszParameters
!= parametersBuffer
)
1654 free(wszParameters
);
1655 if (wszDir
!= dirBuffer
)
1661 SHGetPathFromIDListW(sei_tmp
.lpIDList
, wszApplicationName
);
1662 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1665 if (sei_tmp
.fMask
& SEE_MASK_DOENVSUBST
)
1669 tmp
= expand_environment(sei_tmp
.lpFile
);
1672 retval
= SE_ERR_OOM
;
1675 free(wszApplicationName
);
1676 sei_tmp
.lpFile
= wszApplicationName
= tmp
;
1678 tmp
= expand_environment(sei_tmp
.lpDirectory
);
1681 retval
= SE_ERR_OOM
;
1684 if (wszDir
!= dirBuffer
)
1686 sei_tmp
.lpDirectory
= wszDir
= tmp
;
1689 if ( ERROR_SUCCESS
== ShellExecute_FromContextMenu( &sei_tmp
) )
1691 sei
->hInstApp
= (HINSTANCE
) 33;
1692 free(wszApplicationName
);
1693 if (wszParameters
!= parametersBuffer
)
1694 free(wszParameters
);
1695 if (wszDir
!= dirBuffer
)
1700 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1702 retval
= SHELL_execute_class( wszApplicationName
, &sei_tmp
, sei
,
1704 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1705 do_error_dialog(retval
, sei_tmp
.hwnd
);
1706 free(wszApplicationName
);
1707 if (wszParameters
!= parametersBuffer
)
1708 free(wszParameters
);
1709 if (wszDir
!= dirBuffer
)
1714 /* Has the IDList not yet been translated? */
1715 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1717 SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1720 dwApplicationNameLen
);
1723 /* convert file URLs */
1724 if (UrlIsFileUrlW(sei_tmp
.lpFile
))
1730 buf
= malloc(size
* sizeof(WCHAR
));
1731 if (!buf
|| FAILED(PathCreateFromUrlW(sei_tmp
.lpFile
, buf
, &size
, 0))) {
1733 retval
= SE_ERR_OOM
;
1737 free(wszApplicationName
);
1738 wszApplicationName
= buf
;
1739 sei_tmp
.lpFile
= wszApplicationName
;
1741 else /* or expand environment strings (not both!) */
1743 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1747 buf
= malloc((len
+ 1) * sizeof(WCHAR
));
1749 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1750 free(wszApplicationName
);
1751 wszApplicationName
= buf
;
1753 sei_tmp
.lpFile
= wszApplicationName
;
1757 if (*sei_tmp
.lpDirectory
)
1759 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1764 buf
= malloc(len
* sizeof(WCHAR
));
1765 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1766 if (wszDir
!= dirBuffer
)
1769 sei_tmp
.lpDirectory
= wszDir
;
1773 /* Else, try to execute the filename */
1774 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1775 lpFile
= sei_tmp
.lpFile
;
1777 len
= lstrlenW(wszApplicationName
) + 3;
1778 if (sei_tmp
.lpParameters
[0])
1779 len
+= 1 + lstrlenW(wszParameters
);
1782 wcmd
= malloc(len
* sizeof(WCHAR
));
1785 lstrcpyW(wcmd
, wszApplicationName
);
1786 if (sei_tmp
.lpDirectory
)
1788 LPCWSTR searchPath
[] = {
1789 sei_tmp
.lpDirectory
,
1792 PathFindOnPathW(wcmd
, searchPath
);
1794 retval
= SHELL_quote_and_execute( wcmd
, wszParameters
, L
"",
1795 wszApplicationName
, NULL
, &sei_tmp
,
1798 free(wszApplicationName
);
1799 if (wszParameters
!= parametersBuffer
)
1800 free(wszParameters
);
1801 if (wszDir
!= dirBuffer
)
1803 if (wcmd
!= wcmdBuffer
)
1808 /* Else, try to find the executable */
1810 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, wszKeyname
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1811 if (retval
> 32) /* Found */
1813 retval
= SHELL_quote_and_execute( wcmd
, wszParameters
, wszKeyname
,
1814 wszApplicationName
, env
, &sei_tmp
,
1818 else if (PathIsDirectoryW(lpFile
))
1820 WCHAR wExec
[MAX_PATH
];
1821 WCHAR
* lpQuotedFile
= malloc( sizeof(WCHAR
) * (lstrlenW(lpFile
) + 3) );
1825 retval
= SHELL_FindExecutable( sei_tmp
.lpDirectory
, L
"explorer",
1826 L
"open", wExec
, MAX_PATH
,
1827 NULL
, &env
, NULL
, NULL
);
1830 lstrcpyW(lpQuotedFile
, L
"\"");
1831 lstrcatW(lpQuotedFile
, lpFile
);
1832 lstrcatW(lpQuotedFile
, L
"\"");
1833 retval
= SHELL_quote_and_execute( wExec
, lpQuotedFile
,
1835 wszApplicationName
, env
,
1836 &sei_tmp
, sei
, execfunc
);
1839 free( lpQuotedFile
);
1842 retval
= 0; /* Out of memory */
1844 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1846 retval
= SHELL_execute_url( lpFile
, wcmd
, &sei_tmp
, sei
, execfunc
);
1848 /* Check if file specified is in the form www.??????.*** */
1849 else if (!wcsnicmp(lpFile
, L
"www", 3))
1851 /* if so, prefix lpFile with http:// and call ShellExecute */
1852 WCHAR lpstrTmpFile
[256];
1853 lstrcpyW(lpstrTmpFile
, L
"http://");
1854 lstrcatW(lpstrTmpFile
, lpFile
);
1855 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1859 TRACE("retval %Iu\n", retval
);
1861 free(wszApplicationName
);
1862 if (wszParameters
!= parametersBuffer
)
1863 free(wszParameters
);
1864 if (wszDir
!= dirBuffer
)
1866 if (wcmd
!= wcmdBuffer
)
1869 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1871 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1872 do_error_dialog(retval
, sei_tmp
.hwnd
);
1876 /*************************************************************************
1877 * ShellExecuteA [SHELL32.290]
1879 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpVerb
, LPCSTR lpFile
,
1880 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1882 SHELLEXECUTEINFOA sei
;
1884 TRACE("%p,%s,%s,%s,%s,%d\n",
1885 hWnd
, debugstr_a(lpVerb
), debugstr_a(lpFile
),
1886 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
1888 sei
.cbSize
= sizeof(sei
);
1889 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1891 sei
.lpVerb
= lpVerb
;
1892 sei
.lpFile
= lpFile
;
1893 sei
.lpParameters
= lpParameters
;
1894 sei
.lpDirectory
= lpDirectory
;
1895 sei
.nShow
= iShowCmd
;
1902 ShellExecuteExA (&sei
);
1903 return sei
.hInstApp
;
1906 /*************************************************************************
1907 * ShellExecuteExA [SHELL32.292]
1910 BOOL WINAPI DECLSPEC_HOTPATCH
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1912 SHELLEXECUTEINFOW seiW
;
1914 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1918 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1921 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1924 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1926 if (sei
->lpParameters
)
1927 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1929 if (sei
->lpDirectory
)
1930 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1932 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
1933 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1935 seiW
.lpClass
= NULL
;
1937 ret
= SHELL_execute( &seiW
, SHELL_ExecuteW
);
1939 sei
->hInstApp
= seiW
.hInstApp
;
1941 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
1942 sei
->hProcess
= seiW
.hProcess
;
1946 SHFree(wParameters
);
1953 /*************************************************************************
1954 * ShellExecuteExW [SHELL32.293]
1957 BOOL WINAPI DECLSPEC_HOTPATCH
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1959 return SHELL_execute( sei
, SHELL_ExecuteW
);
1962 /*************************************************************************
1963 * ShellExecuteW [SHELL32.294]
1965 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1966 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1968 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpVerb
, LPCWSTR lpFile
,
1969 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1971 SHELLEXECUTEINFOW sei
;
1974 sei
.cbSize
= sizeof(sei
);
1975 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1977 sei
.lpVerb
= lpVerb
;
1978 sei
.lpFile
= lpFile
;
1979 sei
.lpParameters
= lpParameters
;
1980 sei
.lpDirectory
= lpDirectory
;
1981 sei
.nShow
= nShowCmd
;
1988 SHELL_execute( &sei
, SHELL_ExecuteW
);
1989 return sei
.hInstApp
;
1992 /*************************************************************************
1993 * WOWShellExecute [SHELL32.@]
1995 * FIXME: the callback function most likely doesn't work the same way on Windows.
1997 HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpVerb
,LPCSTR lpFile
,
1998 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
2000 SHELLEXECUTEINFOW seiW
;
2001 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
2002 HANDLE hProcess
= 0;
2004 seiW
.lpVerb
= lpVerb
? __SHCloneStrAtoW(&wVerb
, lpVerb
) : NULL
;
2005 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
2006 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
2007 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
2009 seiW
.cbSize
= sizeof(seiW
);
2012 seiW
.nShow
= iShowCmd
;
2017 seiW
.hProcess
= hProcess
;
2019 SHELL_execute( &seiW
, callback
);
2023 SHFree(wParameters
);
2025 return seiW
.hInstApp
;
2028 /*************************************************************************
2029 * ShellExec_RunDLLW [SHELL32.@]
2031 void WINAPI
ShellExec_RunDLLW(HWND hwnd
, HINSTANCE instance
, WCHAR
*cmdline
, int cmdshow
)
2033 BOOL in_single_quotes
= FALSE
, in_double_quotes
= FALSE
;
2036 TRACE("%p, %p, %s, %d\n", hwnd
, instance
, debugstr_w(cmdline
), cmdshow
);
2038 /* Replace the first whitespace character in the command line string with a
2039 null terminator to separate the program name from the program arguments */
2040 for (args
= cmdline
; *args
; args
++)
2045 args
++; /* skip the next character */
2048 if (!in_double_quotes
)
2049 in_single_quotes
= !in_single_quotes
;
2052 if (!in_single_quotes
)
2053 in_double_quotes
= !in_double_quotes
;
2057 if (!in_single_quotes
&& !in_double_quotes
)
2067 ShellExecuteW(hwnd
, NULL
, cmdline
, args
, NULL
, cmdshow
);
2070 /*************************************************************************
2071 * ShellExec_RunDLLA [SHELL32.@]
2073 void WINAPI
ShellExec_RunDLLA(HWND hwnd
, HINSTANCE instance
, CHAR
*cmdline
, int cmdshow
)
2076 ShellExec_RunDLLW(hwnd
, instance
, __SHCloneStrAtoW(&cmdlineW
, cmdline
), cmdshow
);
2080 /*************************************************************************
2081 * OpenAs_RunDLLA [SHELL32.@]
2083 void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2085 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2088 /*************************************************************************
2089 * OpenAs_RunDLLW [SHELL32.@]
2091 void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2093 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);
2096 /*************************************************************************
2097 * RegenerateUserEnvironment [SHELL32.@]
2099 BOOL WINAPI
RegenerateUserEnvironment(WCHAR
*wunknown
, BOOL bunknown
)
2101 FIXME("stub: %p, %d\n", wunknown
, bunknown
);