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
25 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
27 static const WCHAR wszOpen
[] = L
"open";
28 static const WCHAR wszExe
[] = L
".exe";
29 static const WCHAR wszShell
[] = L
"\\shell\\";
30 static const WCHAR wszFolder
[] = L
"Folder";
32 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
34 static void ParseNoTildeEffect(PWSTR
&res
, LPCWSTR
&args
, DWORD
&len
, DWORD
&used
, int argNum
)
36 bool firstCharQuote
= false;
37 bool quotes_opened
= false;
38 bool backslash_encountered
= false;
40 for (int curArg
= 0; curArg
<= argNum
&& *args
; ++curArg
)
42 firstCharQuote
= false;
46 firstCharQuote
= true;
54 // if we found a backslash then flip the variable
55 backslash_encountered
= !backslash_encountered
;
57 else if (*args
== '"')
61 if (*(args
+ 1) != '"')
63 quotes_opened
= false;
77 backslash_encountered
= false;
81 backslash_encountered
= false;
82 if (*args
== ' ' && !firstCharQuote
)
101 static void ParseTildeEffect(PWSTR
&res
, LPCWSTR
&args
, DWORD
&len
, DWORD
&used
, int argNum
)
103 bool quotes_opened
= false;
104 bool backslash_encountered
= false;
106 for (int curArg
= 0; curArg
<= argNum
&& *args
; ++curArg
)
112 // if we found a backslash then flip the variable
113 backslash_encountered
= !backslash_encountered
;
115 else if (*args
== '"')
119 if (*(args
+ 1) != '"')
121 quotes_opened
= false;
130 quotes_opened
= true;
133 backslash_encountered
= false;
137 backslash_encountered
= false;
138 if (*args
== ' ' && !quotes_opened
&& curArg
!= argNum
)
142 if (curArg
== argNum
)
154 /***********************************************************************
155 * SHELL_ArgifyW [Internal]
157 * this function is supposed to expand the escape sequences found in the registry
158 * some diving reported that the following were used:
159 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
164 * %I address of a global item ID (explorer switch /idlist)
165 * %L seems to be %1 as long filename followed by the 8+3 variation
167 * %* all following parameters (see batfile)
169 * The way we parse the command line arguments was determined through extensive
170 * testing and can be summed up by the following rules"
173 * - if first letter is " break on first non literal " and include any white spaces
174 * - if first letter is NOT " break on first " or white space
175 * - if " is opened any pair of consecutive " results in ONE literal "
178 * - use rules from here http://www.autohotkey.net/~deleyd/parameters/parameters.htm
181 static BOOL
SHELL_ArgifyW(WCHAR
* out
, DWORD len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
, DWORD
* out_len
)
185 BOOL found_p1
= FALSE
;
189 bool tildeEffect
= false;
191 TRACE("Before parsing: %p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
192 debugstr_w(lpFile
), pidl
, args
);
253 ParseTildeEffect(res
, args
, len
, used
, *fmt
- '2');
258 ParseNoTildeEffect(res
, args
, len
, used
, *fmt
- '2');
265 if (!done
|| (*fmt
== '1'))
267 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
268 if (SearchPathW(NULL
, lpFile
, wszExe
, sizeof(xlpFile
) / sizeof(WCHAR
), xlpFile
, NULL
))
284 * IE uses this a lot for activating things such as windows media
285 * player. This is not verified to be fully correct but it appears
292 used
+= wcslen(lpFile
);
296 res
+= wcslen(lpFile
);
307 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
308 * allowing a buffer of 100 should more than exceed all needs */
311 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
312 pv
= SHLockShared(hmem
, 0);
313 chars
= swprintf(buf
, L
":%p", pv
);
315 if (chars
>= sizeof(buf
) / sizeof(WCHAR
))
316 ERR("pidl format buffer too small!\n");
332 * Check if this is an env-variable here...
335 /* Make sure that we have at least one more %.*/
336 if (strchrW(fmt
, '%'))
338 WCHAR tmpBuffer
[1024];
339 PWSTR tmpB
= tmpBuffer
;
340 WCHAR tmpEnvBuff
[MAX_PATH
];
347 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer
));
349 envRet
= GetEnvironmentVariableW(tmpBuffer
, tmpEnvBuff
, MAX_PATH
);
350 if (envRet
== 0 || envRet
> MAX_PATH
)
352 used
+= wcslen(tmpBuffer
);
355 wcscpy( res
, tmpBuffer
);
356 res
+= wcslen(tmpBuffer
);
361 used
+= wcslen(tmpEnvBuff
);
364 wcscpy( res
, tmpEnvBuff
);
365 res
+= wcslen(tmpEnvBuff
);
372 /* Don't skip past terminator (catch a single '%' at the end) */
389 TRACE("used %i of %i space\n", used
, len
);
393 TRACE("After parsing: %p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
394 debugstr_w(lpFile
), pidl
, args
);
399 static HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
402 IShellFolder
*desktop
;
404 HRESULT hr
= SHGetDesktopFolder(&desktop
);
408 hr
= desktop
->GetDisplayNameOf(pidl
, SHGDN_FORPARSING
, &strret
);
411 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
419 /*************************************************************************
420 * SHELL_ExecuteW [Internal]
423 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
424 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
426 STARTUPINFOW startup
;
427 PROCESS_INFORMATION info
;
428 UINT_PTR retval
= SE_ERR_NOASSOC
;
430 WCHAR curdir
[MAX_PATH
];
431 DWORD dwCreationFlags
;
432 const WCHAR
*lpDirectory
= NULL
;
434 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
436 /* make sure we don't fail the CreateProcess if the calling app passes in
437 * a bad working directory */
438 if (psei
->lpDirectory
&& psei
->lpDirectory
[0])
440 DWORD attr
= GetFileAttributesW(psei
->lpDirectory
);
441 if (attr
!= INVALID_FILE_ATTRIBUTES
&& attr
& FILE_ATTRIBUTE_DIRECTORY
)
442 lpDirectory
= psei
->lpDirectory
;
445 /* ShellExecute specifies the command from psei->lpDirectory
446 * if present. Not from the current dir as CreateProcess does */
448 if ((gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
449 if (!SetCurrentDirectoryW( lpDirectory
))
450 ERR("cannot set directory %s\n", debugstr_w(lpDirectory
));
452 ZeroMemory(&startup
, sizeof(STARTUPINFOW
));
453 startup
.cb
= sizeof(STARTUPINFOW
);
454 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
455 startup
.wShowWindow
= psei
->nShow
;
456 dwCreationFlags
= CREATE_UNICODE_ENVIRONMENT
;
458 if (psei
->fMask
& SEE_MASK_NO_CONSOLE
)
459 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
461 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, dwCreationFlags
, env
,
462 lpDirectory
, &startup
, &info
))
464 /* Give 30 seconds to the app to come up, if desired. Probably only needed
465 when starting app immediately before making a DDE connection. */
467 if (WaitForInputIdle(info
.hProcess
, 30000) == WAIT_FAILED
)
468 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
471 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
472 psei_out
->hProcess
= info
.hProcess
;
474 CloseHandle( info
.hProcess
);
475 CloseHandle( info
.hThread
);
477 else if ((retval
= GetLastError()) >= 32)
479 WARN("CreateProcess returned error %ld\n", retval
);
480 retval
= ERROR_BAD_FORMAT
;
483 TRACE("returning %lu\n", retval
);
485 psei_out
->hInstApp
= (HINSTANCE
)retval
;
488 if (!SetCurrentDirectoryW(curdir
))
489 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
495 /***********************************************************************
496 * SHELL_BuildEnvW [Internal]
498 * Build the environment for the new process, adding the specified
499 * path to the PATH variable. Returned pointer must be freed by caller.
501 static LPWSTR
SHELL_BuildEnvW( const WCHAR
*path
)
503 static const WCHAR wPath
[] = L
"PATH=";
504 WCHAR
*strings
, *new_env
;
506 int total
= wcslen(path
) + 1;
507 BOOL got_path
= FALSE
;
509 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
513 int len
= wcslen(p
) + 1;
514 if (!_wcsnicmp( p
, wPath
, 5 )) got_path
= TRUE
;
518 if (!got_path
) total
+= 5; /* we need to create PATH */
519 total
++; /* terminating null */
521 if (!(new_env
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, total
* sizeof(WCHAR
))))
523 FreeEnvironmentStringsW(strings
);
530 int len
= wcslen(p
) + 1;
531 memcpy(p2
, p
, len
* sizeof(WCHAR
));
532 if (!_wcsnicmp( p
, wPath
, 5 ))
535 wcscpy( p2
+ len
, path
);
536 p2
+= wcslen(path
) + 1;
545 p2
+= wcslen(p2
) + 1;
548 FreeEnvironmentStringsW(strings
);
553 /***********************************************************************
554 * SHELL_TryAppPathW [Internal]
556 * Helper function for SHELL_FindExecutable
557 * @param lpResult - pointer to a buffer of size MAX_PATH
558 * On entry: szName is a filename (probably without path separators).
559 * On exit: if szName found in "App Path", place full path in lpResult, and return true
561 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
569 if (env
) *env
= NULL
;
570 wcscpy(buffer
, L
"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
571 wcscat(buffer
, szName
);
572 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
575 len
= MAX_PATH
* sizeof(WCHAR
);
576 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
582 DWORD count
= sizeof(buffer
);
583 if (!RegQueryValueExW(hkApp
, L
"Path", NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
584 *env
= SHELL_BuildEnvW(buffer
);
588 if (hkApp
) RegCloseKey(hkApp
);
592 static UINT
SHELL_FindExecutableByOperation(LPCWSTR lpOperation
, LPWSTR key
, LPWSTR filetype
, LPWSTR command
, LONG commandlen
)
594 static const WCHAR wCommand
[] = L
"\\command";
596 WCHAR verb
[MAX_PATH
];
598 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, filetype
, 0, 0x02000000, &hkeyClass
))
599 return SE_ERR_NOASSOC
;
600 if (!HCR_GetDefaultVerbW(hkeyClass
, lpOperation
, verb
, sizeof(verb
) / sizeof(verb
[0])))
601 return SE_ERR_NOASSOC
;
602 RegCloseKey(hkeyClass
);
604 /* Looking for ...buffer\shell\<verb>\command */
605 wcscat(filetype
, wszShell
);
606 wcscat(filetype
, verb
);
607 wcscat(filetype
, wCommand
);
609 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, command
,
610 &commandlen
) == ERROR_SUCCESS
)
612 commandlen
/= sizeof(WCHAR
);
613 if (key
) wcscpy(key
, filetype
);
617 LONG paramlen
= sizeof(param
);
618 static const WCHAR wSpace
[] = {' ', 0};
620 /* FIXME: it seems all Windows version don't behave the same here.
621 * the doc states that this ddeexec information can be found after
623 * on Win98, it doesn't appear, but I think it does on Win2k
625 /* Get the parameters needed by the application
626 from the associated ddeexec key */
627 tmp
= strstrW(filetype
, wCommand
);
629 wcscat(filetype
, wDdeexec
);
630 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, param
,
631 ¶mlen
) == ERROR_SUCCESS
)
633 paramlen
/= sizeof(WCHAR
);
634 wcscat(command
, wSpace
);
635 wcscat(command
, param
);
636 commandlen
+= paramlen
;
640 command
[commandlen
] = '\0';
642 return 33; /* FIXME see SHELL_FindExecutable() */
645 return SE_ERR_NOASSOC
;
648 /*************************************************************************
649 * SHELL_FindExecutable [Internal]
651 * Utility for code sharing between FindExecutable and ShellExecute
653 * lpFile the name of a file
654 * lpOperation the operation on it (open)
656 * lpResult a buffer, big enough :-(, to store the command to do the
657 * operation on the file
658 * key a buffer, big enough, to get the key name to do actually the
659 * command (it'll be used afterwards for more information
662 static UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
,
663 LPWSTR lpResult
, DWORD resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
665 WCHAR
*extension
= NULL
; /* pointer to file extension */
666 WCHAR filetype
[256]; /* registry name for this filetype */
667 LONG filetypelen
= sizeof(filetype
); /* length of above */
668 WCHAR command
[1024]; /* command from registry */
669 WCHAR wBuffer
[256]; /* Used to GetProfileString */
670 UINT retval
= SE_ERR_NOASSOC
;
671 WCHAR
*tok
; /* token pointer */
672 WCHAR xlpFile
[256]; /* result of SearchPath */
673 DWORD attribs
; /* file attributes */
675 TRACE("%s\n", debugstr_w(lpFile
));
678 return ERROR_INVALID_PARAMETER
;
681 lpResult
[0] = '\0'; /* Start off with an empty return string */
682 if (key
) *key
= '\0';
684 /* trap NULL parameters on entry */
687 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
688 debugstr_w(lpFile
), debugstr_w(lpResult
));
689 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
692 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
694 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
698 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
) / sizeof(WCHAR
), xlpFile
, NULL
))
700 TRACE("SearchPathW returned non-zero\n");
702 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
705 attribs
= GetFileAttributesW(lpFile
);
706 if (attribs
!= INVALID_FILE_ATTRIBUTES
&& (attribs
& FILE_ATTRIBUTE_DIRECTORY
))
708 wcscpy(filetype
, wszFolder
);
709 filetypelen
= 6; /* strlen("Folder") */
713 /* Did we get something? Anything? */
716 TRACE("Returning SE_ERR_FNF\n");
719 /* First thing we need is the file's extension */
720 extension
= wcsrchr(xlpFile
, '.'); /* Assume last "." is the one; */
721 /* File->Run in progman uses */
723 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
725 if (extension
== NULL
|| extension
[1] == 0)
727 WARN("Returning SE_ERR_NOASSOC\n");
728 return SE_ERR_NOASSOC
;
731 /* Three places to check: */
732 /* 1. win.ini, [windows], programs (NB no leading '.') */
733 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
734 /* 3. win.ini, [extensions], extension (NB no leading '.' */
735 /* All I know of the order is that registry is checked before */
736 /* extensions; however, it'd make sense to check the programs */
737 /* section first, so that's what happens here. */
739 /* See if it's a program - if GetProfileString fails, we skip this
740 * section. Actually, if GetProfileString fails, we've probably
741 * got a lot more to worry about than running a program... */
742 if (GetProfileStringW(L
"windows", L
"programs", L
"exe pif bat cmd com", wBuffer
, sizeof(wBuffer
) / sizeof(WCHAR
)) > 0)
749 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
753 while (*p
== ' ' || *p
== '\t') p
++;
756 if (wcsicmp(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
758 wcscpy(lpResult
, xlpFile
);
759 /* Need to perhaps check that the file has a path
761 TRACE("found %s\n", debugstr_w(lpResult
));
763 /* Greater than 32 to indicate success */
770 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, filetype
,
771 &filetypelen
) == ERROR_SUCCESS
)
773 filetypelen
/= sizeof(WCHAR
);
774 if (filetypelen
== sizeof(filetype
) / sizeof(WCHAR
))
777 filetype
[filetypelen
] = '\0';
778 TRACE("File type: %s\n", debugstr_w(filetype
));
789 /* pass the operation string to SHELL_FindExecutableByOperation() */
790 filetype
[filetypelen
] = '\0';
791 retval
= SHELL_FindExecutableByOperation(lpOperation
, key
, filetype
, command
, sizeof(command
));
796 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
797 if (finishedLen
> resultLen
)
798 ERR("Argify buffer not large enough.. truncated\n");
799 /* Remove double quotation marks and command line arguments */
800 if (*lpResult
== '"')
803 while (*(p
+ 1) != '"')
812 /* Truncate on first space */
814 while (*p
!= ' ' && *p
!= '\0')
820 else /* Check win.ini */
822 /* Toss the leading dot */
824 if (GetProfileStringW(L
"extesions", extension
, L
"", command
, sizeof(command
) / sizeof(WCHAR
)) > 0)
826 if (wcslen(command
) != 0)
828 wcscpy(lpResult
, command
);
829 tok
= wcschr(lpResult
, '^'); /* should be ^.extension? */
833 wcscat(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
834 tok
= wcschr(command
, '^'); /* see above */
835 if ((tok
!= NULL
) && (wcslen(tok
) > 5))
837 wcscat(lpResult
, &tok
[5]);
840 retval
= 33; /* FIXME - see above */
845 TRACE("returning %s\n", debugstr_w(lpResult
));
849 /******************************************************************
852 * callback for the DDE connection. not really useful
854 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
855 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
856 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
858 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
859 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
863 /******************************************************************
866 * ShellExecute helper. Used to do an operation with a DDE connection
868 * Handles both the direct connection (try #1), and if it fails,
869 * launching an application and trying (#2) to connect to it
872 static unsigned dde_connect(const WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
873 const WCHAR
* lpFile
, WCHAR
*env
,
874 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
875 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
878 WCHAR
* endkey
= regkey
+ wcslen(key
);
879 WCHAR app
[256], topic
[256], ifexec
[256], res
[256];
880 LONG applen
, topiclen
, ifexeclen
;
885 HSZ hszApp
, hszTopic
;
888 unsigned ret
= SE_ERR_NOASSOC
;
889 BOOL unicode
= !(GetVersion() & 0x80000000);
892 wcscpy(endkey
, L
"application");
893 applen
= sizeof(app
);
894 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, app
, &applen
) != ERROR_SUCCESS
)
896 WCHAR command
[1024], fullpath
[MAX_PATH
];
897 static const WCHAR wSo
[] = L
".so";
898 DWORD sizeSo
= sizeof(wSo
) / sizeof(WCHAR
);
902 /* Get application command from start string and find filename of application */
905 wcscpy(command
, start
+ 1);
906 if ((ptr
= wcschr(command
, '"')))
908 ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
);
913 for (p
= (LPWSTR
)start
; (space
= const_cast<LPWSTR
>(strchrW(p
, ' '))); p
= space
+ 1)
915 int idx
= space
- start
;
916 memcpy(command
, start
, idx
* sizeof(WCHAR
));
918 if ((ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
)))
922 ret
= SearchPathW(NULL
, start
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
);
927 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
928 return ERROR_ACCESS_DENIED
;
932 /* Remove extensions (including .so) */
933 ptr
= app
+ wcslen(app
) - (sizeSo
- 1);
934 if (wcslen(app
) >= sizeSo
&&
938 ptr
= const_cast<LPWSTR
>(strrchrW(app
, '.'));
943 wcscpy(endkey
, L
"\\topic");
944 topiclen
= sizeof(topic
);
945 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, topic
, &topiclen
) != ERROR_SUCCESS
)
947 wcscpy(topic
, L
"System");
952 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
957 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
961 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
962 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
964 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
968 TRACE("Launching %s\n", debugstr_w(start
));
969 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
972 TRACE("Couldn't launch\n");
975 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
978 TRACE("Couldn't connect. ret=%d\n", ret
);
979 DdeUninitialize(ddeInst
);
980 SetLastError(ERROR_DDE_FAIL
);
981 return 30; /* whatever */
983 strcpyW(endkey
, L
"\\ifexec");
984 ifexeclen
= sizeof(ifexec
);
985 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
991 SHELL_ArgifyW(res
, sizeof(res
) / sizeof(WCHAR
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
992 if (resultLen
> sizeof(res
) / sizeof(WCHAR
))
993 ERR("Argify buffer not large enough, truncated\n");
994 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
996 /* It's documented in the KB 330337 that IE has a bug and returns
997 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
1000 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0, XTYP_EXECUTE
, 30000, &tid
);
1003 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
1004 char *resA
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, lenA
);
1005 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
1006 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
1007 XTYP_EXECUTE
, 10000, &tid
);
1008 HeapFree(GetProcessHeap(), 0, resA
);
1011 DdeFreeDataHandle(hDdeData
);
1013 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
1016 DdeDisconnect(hConv
);
1019 DdeUninitialize(ddeInst
);
1024 /*************************************************************************
1025 * execute_from_key [Internal]
1027 static UINT_PTR
execute_from_key(LPCWSTR key
, LPCWSTR lpFile
, WCHAR
*env
,
1028 LPCWSTR szCommandline
, LPCWSTR executable_name
,
1029 SHELL_ExecuteW32 execfunc
,
1030 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
1032 WCHAR cmd
[256], param
[1024], ddeexec
[256];
1033 DWORD cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
1034 UINT_PTR retval
= SE_ERR_NOASSOC
;
1038 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
1039 debugstr_w(szCommandline
), debugstr_w(executable_name
));
1044 /* Get the application from the registry */
1045 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, (LONG
*)&cmdlen
) == ERROR_SUCCESS
)
1047 TRACE("got cmd: %s\n", debugstr_w(cmd
));
1049 /* Is there a replace() function anywhere? */
1050 cmdlen
/= sizeof(WCHAR
);
1051 if (cmdlen
>= sizeof(cmd
) / sizeof(WCHAR
))
1052 cmdlen
= sizeof(cmd
) / sizeof(WCHAR
) - 1;
1054 SHELL_ArgifyW(param
, sizeof(param
) / sizeof(WCHAR
), cmd
, lpFile
, (LPITEMIDLIST
)psei
->lpIDList
, szCommandline
, &resultLen
);
1055 if (resultLen
> sizeof(param
) / sizeof(WCHAR
))
1056 ERR("Argify buffer not large enough, truncating\n");
1059 /* Get the parameters needed by the application
1060 from the associated ddeexec key */
1061 tmp
= const_cast<LPWSTR
>(strstrW(key
, L
"command"));
1063 wcscpy(tmp
, L
"ddeexec");
1065 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, (LONG
*)&ddeexeclen
) == ERROR_SUCCESS
)
1067 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
1068 if (!param
[0]) strcpyW(param
, executable_name
);
1069 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, (LPITEMIDLIST
)psei
->lpIDList
, execfunc
, psei
, psei_out
);
1073 TRACE("executing: %s\n", debugstr_w(param
));
1074 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
1077 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
1082 /*************************************************************************
1083 * FindExecutableA [SHELL32.@]
1085 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
1088 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
1089 WCHAR wResult
[MAX_PATH
];
1091 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
1092 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
1094 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
1095 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
1099 TRACE("returning %s\n", lpResult
);
1103 /*************************************************************************
1104 * FindExecutableW [SHELL32.@]
1106 * This function returns the executable associated with the specified file
1107 * for the default verb.
1110 * lpFile [I] The file to find the association for. This must refer to
1111 * an existing file otherwise FindExecutable fails and returns
1113 * lpResult [O] Points to a buffer into which the executable path is
1114 * copied. This parameter must not be NULL otherwise
1115 * FindExecutable() segfaults. The buffer must be of size at
1116 * least MAX_PATH characters.
1119 * A value greater than 32 on success, less than or equal to 32 otherwise.
1120 * See the SE_ERR_* constants.
1123 * On Windows XP and 2003, FindExecutable() seems to first convert the
1124 * filename into 8.3 format, thus taking into account only the first three
1125 * characters of the extension, and expects to find an association for those.
1126 * However other Windows versions behave sanely.
1128 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
1130 UINT_PTR retval
= SE_ERR_NOASSOC
;
1131 WCHAR old_dir
[1024];
1133 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
1135 lpResult
[0] = '\0'; /* Start off with an empty return string */
1137 return (HINSTANCE
)SE_ERR_FNF
;
1141 GetCurrentDirectoryW(sizeof(old_dir
) / sizeof(WCHAR
), old_dir
);
1142 SetCurrentDirectoryW(lpDirectory
);
1145 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, lpResult
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1147 TRACE("returning %s\n", debugstr_w(lpResult
));
1149 SetCurrentDirectoryW(old_dir
);
1150 return (HINSTANCE
)retval
;
1153 /* FIXME: is this already implemented somewhere else? */
1154 static HKEY
ShellExecute_GetClassKey(const SHELLEXECUTEINFOW
*sei
)
1156 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1158 DWORD type
= 0, sz
= 0;
1162 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1163 return sei
->hkeyClass
;
1165 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1166 lpClass
= sei
->lpClass
;
1169 ext
= PathFindExtensionW(sei
->lpFile
);
1170 TRACE("ext = %s\n", debugstr_w(ext
));
1174 r
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
1175 if (r
!= ERROR_SUCCESS
)
1178 r
= RegQueryValueExW(hkey
, NULL
, 0, &type
, NULL
, &sz
);
1179 if (r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1181 sz
+= sizeof (WCHAR
);
1182 cls
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sz
);
1184 RegQueryValueExW(hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1187 RegCloseKey( hkey
);
1191 TRACE("class = %s\n", debugstr_w(lpClass
));
1195 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1197 HeapFree(GetProcessHeap(), 0, cls
);
1202 static IDataObject
*shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
)
1204 LPCITEMIDLIST pidllast
= NULL
;
1205 IDataObject
*dataobj
= NULL
;
1206 IShellFolder
*shf
= NULL
;
1207 LPITEMIDLIST pidl
= NULL
;
1210 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1211 pidl
= (LPITEMIDLIST
)sei
->lpIDList
;
1214 WCHAR fullpath
[MAX_PATH
];
1218 ret
= GetFullPathNameW(sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1222 pidl
= ILCreateFromPathW(fullpath
);
1225 r
= SHBindToParent(pidl
, IID_IShellFolder
, (LPVOID
*)&shf
, &pidllast
);
1229 shf
->GetUIObjectOf(NULL
, 1, &pidllast
,
1230 IID_IDataObject
, NULL
, (LPVOID
*) &dataobj
);
1233 if (pidl
!= sei
->lpIDList
)
1240 static HRESULT
shellex_run_context_menu_default(IShellExtInit
*obj
,
1241 LPSHELLEXECUTEINFOW sei
)
1243 IContextMenu
*cm
= NULL
;
1244 CMINVOKECOMMANDINFOEX ici
;
1251 TRACE("%p %p\n", obj
, sei
);
1253 r
= obj
->QueryInterface(IID_IContextMenu
, (LPVOID
*) &cm
);
1257 hmenu
= CreateMenu();
1261 /* the number of the last menu added is returned in r */
1262 r
= cm
->QueryContextMenu(hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1266 n
= GetMenuItemCount(hmenu
);
1267 for (i
= 0; i
< n
; i
++)
1269 memset(&info
, 0, sizeof(info
));
1270 info
.cbSize
= sizeof info
;
1271 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1272 info
.dwTypeData
= string
;
1273 info
.cch
= sizeof string
;
1275 GetMenuItemInfoW(hmenu
, i
, TRUE
, &info
);
1277 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1278 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1279 if ((!sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
)) ||
1280 (sei
->lpVerb
&& !lstrcmpiW(sei
->lpVerb
, string
)))
1291 memset(&ici
, 0, sizeof ici
);
1292 ici
.cbSize
= sizeof ici
;
1293 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NOASYNC
| SEE_MASK_ASYNCOK
| SEE_MASK_FLAG_NO_UI
));
1294 ici
.nShow
= sei
->nShow
;
1295 ici
.lpVerb
= MAKEINTRESOURCEA(def
);
1296 ici
.hwnd
= sei
->hwnd
;
1297 ici
.lpParametersW
= sei
->lpParameters
;
1299 r
= cm
->InvokeCommand((LPCMINVOKECOMMANDINFO
)&ici
);
1301 TRACE("invoke command returned %08x\n", r
);
1305 DestroyMenu( hmenu
);
1311 static HRESULT
shellex_load_object_and_run(HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1313 IDataObject
*dataobj
= NULL
;
1314 IObjectWithSite
*ows
= NULL
;
1315 IShellExtInit
*obj
= NULL
;
1318 TRACE("%p %s %p\n", hkey
, debugstr_guid(guid
), sei
);
1320 r
= CoInitialize(NULL
);
1324 r
= CoCreateInstance(*guid
, NULL
, CLSCTX_INPROC_SERVER
,
1325 IID_IShellExtInit
, (LPVOID
*)&obj
);
1328 ERR("failed %08x\n", r
);
1332 dataobj
= shellex_get_dataobj(sei
);
1335 ERR("failed to get data object\n");
1339 r
= obj
->Initialize(NULL
, dataobj
, hkey
);
1343 r
= obj
->QueryInterface(IID_IObjectWithSite
, (LPVOID
*) &ows
);
1349 r
= shellex_run_context_menu_default(obj
, sei
);
1363 /*************************************************************************
1364 * ShellExecute_FromContextMenu [Internal]
1366 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1368 HKEY hkey
, hkeycm
= 0;
1375 TRACE("%s\n", debugstr_w(sei
->lpFile
));
1377 hkey
= ShellExecute_GetClassKey(sei
);
1379 return ERROR_FUNCTION_FAILED
;
1381 r
= RegOpenKeyW(hkey
, L
"shellex\\ContextMenuHandlers", &hkeycm
);
1382 if (r
== ERROR_SUCCESS
)
1387 r
= RegEnumKeyW(hkeycm
, i
++, szguid
, sizeof(szguid
) / sizeof(szguid
[0]));
1388 if (r
!= ERROR_SUCCESS
)
1391 hr
= CLSIDFromString(szguid
, &guid
);
1394 /* stop at the first one that succeeds in running */
1395 hr
= shellex_load_object_and_run(hkey
, &guid
, sei
);
1400 RegCloseKey(hkeycm
);
1403 if (hkey
!= sei
->hkeyClass
)
1408 static UINT_PTR
SHELL_execute_class(LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1410 WCHAR execCmd
[1024], wcmd
[1024];
1411 /* launch a document by fileclass like 'WordPad.Document.1' */
1412 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1413 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1414 ULONG cmask
= (psei
->fMask
& SEE_MASK_CLASSALL
);
1418 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1419 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1421 execCmd
, sizeof(execCmd
));
1423 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1424 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1427 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
) / sizeof(WCHAR
), execCmd
, wszApplicationName
, (LPITEMIDLIST
)psei
->lpIDList
, NULL
, &resultLen
);
1428 if (!done
&& wszApplicationName
[0])
1430 strcatW(wcmd
, L
" ");
1431 strcatW(wcmd
, wszApplicationName
);
1433 if (resultLen
> sizeof(wcmd
) / sizeof(WCHAR
))
1434 ERR("Argify buffer not large enough... truncating\n");
1435 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1438 static BOOL
SHELL_translate_idlist(LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1440 static const WCHAR wExplorer
[] = L
"explorer.exe";
1441 WCHAR buffer
[MAX_PATH
];
1442 BOOL appKnownSingular
= FALSE
;
1444 /* last chance to translate IDList: now also allow CLSID paths */
1445 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST
)sei
->lpIDList
, buffer
, sizeof(buffer
)))) {
1446 if (buffer
[0] == ':' && buffer
[1] == ':') {
1447 /* open shell folder for the specified class GUID */
1448 if (strlenW(buffer
) + 1 > parametersLen
)
1449 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1450 lstrlenW(buffer
) + 1, parametersLen
);
1451 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1452 if (strlenW(wExplorer
) > dwApplicationNameLen
)
1453 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1454 lstrlenW(wExplorer
) + 1, dwApplicationNameLen
);
1455 lstrcpynW(wszApplicationName
, wExplorer
, dwApplicationNameLen
);
1456 appKnownSingular
= TRUE
;
1458 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1460 WCHAR target
[MAX_PATH
];
1463 /* Check if we're executing a directory and if so use the
1464 handler for the Folder class */
1465 strcpyW(target
, buffer
);
1466 attribs
= GetFileAttributesW(buffer
);
1467 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1468 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1469 HCR_GetExecuteCommandW(0, wszFolder
,
1471 buffer
, sizeof(buffer
))) {
1472 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1473 buffer
, target
, (LPITEMIDLIST
)sei
->lpIDList
, NULL
, &resultLen
);
1474 if (resultLen
> dwApplicationNameLen
)
1475 ERR("Argify buffer not large enough... truncating\n");
1476 appKnownSingular
= FALSE
;
1478 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1481 return appKnownSingular
;
1484 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
)
1488 WCHAR
*wszQuotedCmd
;
1490 /* Length of quotes plus length of command plus NULL terminator */
1491 len
= 2 + lstrlenW(wcmd
) + 1;
1492 if (wszParameters
[0])
1494 /* Length of space plus length of parameters */
1495 len
+= 1 + lstrlenW(wszParameters
);
1497 wszQuotedCmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1498 /* Must quote to handle case where cmd contains spaces,
1499 * else security hole if malicious user creates executable file "C:\\Program"
1501 strcpyW(wszQuotedCmd
, L
"\"");
1502 strcatW(wszQuotedCmd
, wcmd
);
1503 strcatW(wszQuotedCmd
, L
"\"");
1504 if (wszParameters
[0])
1506 strcatW(wszQuotedCmd
, " ");
1507 strcatW(wszQuotedCmd
, wszParameters
);
1510 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(lpstrProtocol
));
1513 retval
= execute_from_key(lpstrProtocol
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1515 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1516 HeapFree(GetProcessHeap(), 0, wszQuotedCmd
);
1520 static UINT_PTR
SHELL_execute_url(LPCWSTR lpFile
, LPCWSTR wFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1522 static const WCHAR wShell
[] = L
"\\shell\\";
1523 static const WCHAR wCommand
[] = L
"\\command";
1525 WCHAR
*lpstrProtocol
;
1530 lpstrRes
= strchrW(lpFile
, ':');
1532 iSize
= lpstrRes
- lpFile
;
1534 iSize
= strlenW(lpFile
);
1536 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1537 /* Looking for ...protocol\shell\lpOperation\command */
1538 len
= iSize
+ lstrlenW(wShell
) + lstrlenW(wCommand
) + 1;
1540 len
+= lstrlenW(psei
->lpVerb
);
1542 len
+= lstrlenW(wszOpen
);
1543 lpstrProtocol
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1544 memcpy(lpstrProtocol
, lpFile
, iSize
* sizeof(WCHAR
));
1545 lpstrProtocol
[iSize
] = '\0';
1546 strcatW(lpstrProtocol
, wShell
);
1547 strcatW(lpstrProtocol
, psei
->lpVerb
? psei
->lpVerb
: wszOpen
);
1548 strcatW(lpstrProtocol
, wCommand
);
1550 /* Remove File Protocol from lpFile */
1551 /* In the case file://path/file */
1552 if (!strncmpiW(lpFile
, wFile
, iSize
))
1555 while (*lpFile
== ':') lpFile
++;
1557 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1558 wcmd
, execfunc
, psei
, psei_out
);
1559 HeapFree(GetProcessHeap(), 0, lpstrProtocol
);
1563 void do_error_dialog(UINT_PTR retval
, HWND hwnd
, WCHAR
* filename
)
1566 DWORD_PTR msgArguments
[3] = { (DWORD_PTR
)filename
, 0, 0 };
1569 error_code
= GetLastError();
1571 if (retval
== SE_ERR_NOASSOC
)
1572 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, sizeof(msg
) / sizeof(WCHAR
));
1574 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ARGUMENT_ARRAY
,
1579 sizeof(msg
) / sizeof(WCHAR
),
1580 (va_list*)msgArguments
);
1582 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1585 /*************************************************************************
1586 * SHELL_execute [Internal]
1588 BOOL
SHELL_execute(LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1590 static const DWORD unsupportedFlags
=
1591 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1592 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1593 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1595 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1596 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
;
1597 DWORD dwApplicationNameLen
= MAX_PATH
+ 2;
1598 DWORD parametersLen
= sizeof(parametersBuffer
) / sizeof(WCHAR
);
1599 DWORD dirLen
= sizeof(dirBuffer
) / sizeof(WCHAR
);
1600 DWORD wcmdLen
= sizeof(wcmdBuffer
) / sizeof(WCHAR
);
1602 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1603 WCHAR wfileName
[MAX_PATH
];
1605 WCHAR lpstrProtocol
[256];
1607 UINT_PTR retval
= SE_ERR_NOASSOC
;
1608 BOOL appKnownSingular
= FALSE
;
1610 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1613 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1614 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1615 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1616 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1617 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1618 debugstr_w(sei_tmp
.lpClass
) : "not used");
1620 sei
->hProcess
= NULL
;
1622 /* make copies of all path/command strings */
1623 if (!sei_tmp
.lpFile
)
1625 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1626 *wszApplicationName
= '\0';
1628 else if (*sei_tmp
.lpFile
== '\"')
1630 DWORD l
= strlenW(sei_tmp
.lpFile
+ 1);
1631 if(l
>= dwApplicationNameLen
)
1632 dwApplicationNameLen
= l
+ 1;
1634 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1635 memcpy(wszApplicationName
, sei_tmp
.lpFile
+ 1, (l
+ 1)*sizeof(WCHAR
));
1637 if (wszApplicationName
[l
-1] == L
'\"')
1638 wszApplicationName
[l
-1] = L
'\0';
1639 appKnownSingular
= TRUE
;
1641 TRACE("wszApplicationName=%s\n", debugstr_w(wszApplicationName
));
1645 DWORD l
= strlenW(sei_tmp
.lpFile
) + 1;
1646 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+ 1;
1647 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1648 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
* sizeof(WCHAR
));
1651 wszParameters
= parametersBuffer
;
1652 if (sei_tmp
.lpParameters
)
1654 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1655 if (len
> parametersLen
)
1657 wszParameters
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1658 parametersLen
= len
;
1660 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1663 *wszParameters
= L
'\0';
1666 if (sei_tmp
.lpDirectory
)
1668 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1671 wszDir
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1674 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1679 /* adjust string pointers to point to the new buffers */
1680 sei_tmp
.lpFile
= wszApplicationName
;
1681 sei_tmp
.lpParameters
= wszParameters
;
1682 sei_tmp
.lpDirectory
= wszDir
;
1684 if (sei_tmp
.fMask
& unsupportedFlags
)
1686 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1689 /* process the IDList */
1690 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1692 IShellExecuteHookW
* pSEH
;
1694 HRESULT hr
= SHBindToParent((LPCITEMIDLIST
)sei_tmp
.lpIDList
, IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1698 hr
= pSEH
->Execute(&sei_tmp
);
1704 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1705 if (wszParameters
!= parametersBuffer
)
1706 HeapFree(GetProcessHeap(), 0, wszParameters
);
1707 if (wszDir
!= dirBuffer
)
1708 HeapFree(GetProcessHeap(), 0, wszDir
);
1713 SHGetPathFromIDListW((LPCITEMIDLIST
)sei_tmp
.lpIDList
, wszApplicationName
);
1714 appKnownSingular
= TRUE
;
1715 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1718 if (ERROR_SUCCESS
== ShellExecute_FromContextMenu(&sei_tmp
))
1720 sei
->hInstApp
= (HINSTANCE
) 33;
1721 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1722 if (wszParameters
!= parametersBuffer
)
1723 HeapFree(GetProcessHeap(), 0, wszParameters
);
1724 if (wszDir
!= dirBuffer
)
1725 HeapFree(GetProcessHeap(), 0, wszDir
);
1729 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1731 retval
= SHELL_execute_class(wszApplicationName
, &sei_tmp
, sei
, execfunc
);
1732 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1739 Info
.pcszFile
= wszApplicationName
;
1740 Info
.pcszClass
= NULL
;
1741 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1743 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1744 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1746 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1747 if (wszParameters
!= parametersBuffer
)
1748 HeapFree(GetProcessHeap(), 0, wszParameters
);
1749 if (wszDir
!= dirBuffer
)
1750 HeapFree(GetProcessHeap(), 0, wszDir
);
1754 /* Has the IDList not yet been translated? */
1755 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1757 appKnownSingular
= SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1760 dwApplicationNameLen
);
1763 /* expand environment strings */
1764 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1768 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1770 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1771 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1772 dwApplicationNameLen
= len
+ 1;
1773 wszApplicationName
= buf
;
1774 /* appKnownSingular unmodified */
1776 sei_tmp
.lpFile
= wszApplicationName
;
1779 if (*sei_tmp
.lpParameters
)
1781 len
= ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, NULL
, 0);
1786 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1787 ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buf
, len
);
1788 if (wszParameters
!= parametersBuffer
)
1789 HeapFree(GetProcessHeap(), 0, wszParameters
);
1790 wszParameters
= buf
;
1791 parametersLen
= len
;
1792 sei_tmp
.lpParameters
= wszParameters
;
1796 if (*sei_tmp
.lpDirectory
)
1798 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1803 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1804 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1805 if (wszDir
!= dirBuffer
)
1806 HeapFree(GetProcessHeap(), 0, wszDir
);
1808 sei_tmp
.lpDirectory
= wszDir
;
1812 /* Else, try to execute the filename */
1813 TRACE("execute: %s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1815 /* separate out command line arguments from executable file name */
1816 if (!*sei_tmp
.lpParameters
&& !appKnownSingular
)
1818 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1819 if (sei_tmp
.lpFile
[0] == L
'"')
1821 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1822 LPWSTR dst
= wfileName
;
1825 /* copy the unquoted executable path to 'wfileName' */
1826 while(*src
&& *src
!= L
'"')
1835 while(isspace(*src
))
1841 /* copy the parameter string to 'wszParameters' */
1842 strcpyW(wszParameters
, src
);
1844 /* terminate previous command string after the quote character */
1849 /* If the executable name is not quoted, we have to use this search loop here,
1850 that in CreateProcess() is not sufficient because it does not handle shell links. */
1851 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1854 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1855 for(s
= beg
; (space
= const_cast<LPWSTR
>(strchrW(s
, L
' '))); s
= space
+ 1)
1857 int idx
= space
- sei_tmp
.lpFile
;
1858 memcpy(buffer
, sei_tmp
.lpFile
, idx
* sizeof(WCHAR
));
1861 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1862 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
) / sizeof(xlpFile
[0]), xlpFile
, NULL
))
1864 /* separate out command from parameter string */
1865 LPCWSTR p
= space
+ 1;
1870 strcpyW(wszParameters
, p
);
1877 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1881 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1886 len
= lstrlenW(wszApplicationName
) + 3;
1887 if (sei_tmp
.lpParameters
[0])
1888 len
+= 1 + lstrlenW(wszParameters
);
1891 wcmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1894 swprintf(wcmd
, L
"\"%s\"", wszApplicationName
);
1895 if (sei_tmp
.lpParameters
[0])
1897 strcatW(wcmd
, L
" ");
1898 strcatW(wcmd
, wszParameters
);
1901 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1904 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1905 if (wszParameters
!= parametersBuffer
)
1906 HeapFree(GetProcessHeap(), 0, wszParameters
);
1907 if (wszDir
!= dirBuffer
)
1908 HeapFree(GetProcessHeap(), 0, wszDir
);
1909 if (wcmd
!= wcmdBuffer
)
1910 HeapFree(GetProcessHeap(), 0, wcmd
);
1914 /* Else, try to find the executable */
1916 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, lpstrProtocol
, &env
, (LPITEMIDLIST
)sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1917 if (retval
> 32) /* Found */
1919 retval
= SHELL_quote_and_execute(wcmd
, wszParameters
, lpstrProtocol
,
1920 wszApplicationName
, env
, &sei_tmp
,
1922 HeapFree(GetProcessHeap(), 0, env
);
1924 else if (PathIsDirectoryW(lpFile
))
1926 WCHAR wExec
[MAX_PATH
];
1927 WCHAR
* lpQuotedFile
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (strlenW(lpFile
) + 3));
1931 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, L
"explorer",
1932 wszOpen
, wExec
, MAX_PATH
,
1933 NULL
, &env
, NULL
, NULL
);
1936 swprintf(lpQuotedFile
, L
"\"%s\"", lpFile
);
1937 retval
= SHELL_quote_and_execute(wExec
, lpQuotedFile
,
1939 wszApplicationName
, env
,
1940 &sei_tmp
, sei
, execfunc
);
1941 HeapFree(GetProcessHeap(), 0, env
);
1943 HeapFree(GetProcessHeap(), 0, lpQuotedFile
);
1946 retval
= 0; /* Out of memory */
1948 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1950 retval
= SHELL_execute_url(lpFile
, L
"file", wcmd
, &sei_tmp
, sei
, execfunc
);
1952 /* Check if file specified is in the form www.??????.*** */
1953 else if (!strncmpiW(lpFile
, L
"www", 3))
1955 /* if so, append lpFile http:// and call ShellExecute */
1956 WCHAR lpstrTmpFile
[256];
1957 strcpyW(lpstrTmpFile
, L
"http://");
1958 strcatW(lpstrTmpFile
, lpFile
);
1959 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1962 TRACE("retval %lu\n", retval
);
1964 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1971 Info
.pcszFile
= wszApplicationName
;
1972 Info
.pcszClass
= NULL
;
1973 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1975 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1976 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1979 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1980 if (wszParameters
!= parametersBuffer
)
1981 HeapFree(GetProcessHeap(), 0, wszParameters
);
1982 if (wszDir
!= dirBuffer
)
1983 HeapFree(GetProcessHeap(), 0, wszDir
);
1984 if (wcmd
!= wcmdBuffer
)
1985 HeapFree(GetProcessHeap(), 0, wcmd
);
1987 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1992 /*************************************************************************
1993 * ShellExecuteA [SHELL32.290]
1995 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
1996 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1998 SHELLEXECUTEINFOA sei
;
2000 TRACE("%p,%s,%s,%s,%s,%d\n",
2001 hWnd
, debugstr_a(lpOperation
), debugstr_a(lpFile
),
2002 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
2004 sei
.cbSize
= sizeof(sei
);
2005 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2007 sei
.lpVerb
= lpOperation
;
2008 sei
.lpFile
= lpFile
;
2009 sei
.lpParameters
= lpParameters
;
2010 sei
.lpDirectory
= lpDirectory
;
2011 sei
.nShow
= iShowCmd
;
2018 ShellExecuteExA(&sei
);
2019 return sei
.hInstApp
;
2022 /*************************************************************************
2023 * ShellExecuteExA [SHELL32.292]
2026 BOOL WINAPI
ShellExecuteExA(LPSHELLEXECUTEINFOA sei
)
2028 SHELLEXECUTEINFOW seiW
;
2030 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
2034 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
2037 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
2040 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
2042 if (sei
->lpParameters
)
2043 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
2045 if (sei
->lpDirectory
)
2046 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
2048 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
2049 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
2051 seiW
.lpClass
= NULL
;
2053 ret
= SHELL_execute(&seiW
, SHELL_ExecuteW
);
2055 sei
->hInstApp
= seiW
.hInstApp
;
2057 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
2058 sei
->hProcess
= seiW
.hProcess
;
2062 SHFree(wParameters
);
2069 /*************************************************************************
2070 * ShellExecuteExW [SHELL32.293]
2073 BOOL WINAPI
ShellExecuteExW(LPSHELLEXECUTEINFOW sei
)
2075 return SHELL_execute(sei
, SHELL_ExecuteW
);
2078 /*************************************************************************
2079 * ShellExecuteW [SHELL32.294]
2081 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
2082 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
2084 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
2085 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
2087 SHELLEXECUTEINFOW sei
;
2090 sei
.cbSize
= sizeof(sei
);
2091 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2093 sei
.lpVerb
= lpOperation
;
2094 sei
.lpFile
= lpFile
;
2095 sei
.lpParameters
= lpParameters
;
2096 sei
.lpDirectory
= lpDirectory
;
2097 sei
.nShow
= nShowCmd
;
2104 SHELL_execute(&sei
, SHELL_ExecuteW
);
2105 return sei
.hInstApp
;
2108 /*************************************************************************
2109 * WOWShellExecute [SHELL32.@]
2111 * FIXME: the callback function most likely doesn't work the same way on Windows.
2113 EXTERN_C HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
2114 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
2116 SHELLEXECUTEINFOW seiW
;
2117 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
2118 HANDLE hProcess
= 0;
2120 seiW
.lpVerb
= lpOperation
? __SHCloneStrAtoW(&wVerb
, lpOperation
) : NULL
;
2121 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
2122 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
2123 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
2125 seiW
.cbSize
= sizeof(seiW
);
2128 seiW
.nShow
= iShowCmd
;
2133 seiW
.hProcess
= hProcess
;
2135 SHELL_execute(&seiW
, (SHELL_ExecuteW32
)callback
);
2139 SHFree(wParameters
);
2141 return seiW
.hInstApp
;
2144 /*************************************************************************
2145 * OpenAs_RunDLLA [SHELL32.@]
2147 EXTERN_C
void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2149 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2152 /*************************************************************************
2153 * OpenAs_RunDLLW [SHELL32.@]
2155 EXTERN_C
void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2157 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);