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";
30 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
32 static void ParseNoTildeEffect(PWSTR
&res
, LPCWSTR
&args
, DWORD
&len
, DWORD
&used
, int argNum
)
34 bool firstCharQuote
= false;
35 bool quotes_opened
= false;
36 bool backslash_encountered
= false;
38 for (int curArg
= 0; curArg
<= argNum
&& *args
; ++curArg
)
40 firstCharQuote
= false;
44 firstCharQuote
= true;
52 // if we found a backslash then flip the variable
53 backslash_encountered
= !backslash_encountered
;
55 else if (*args
== '"')
59 if (*(args
+ 1) != '"')
61 quotes_opened
= false;
75 backslash_encountered
= false;
79 backslash_encountered
= false;
80 if (*args
== ' ' && !firstCharQuote
)
99 static void ParseTildeEffect(PWSTR
&res
, LPCWSTR
&args
, DWORD
&len
, DWORD
&used
, int argNum
)
101 bool quotes_opened
= false;
102 bool backslash_encountered
= false;
104 for (int curArg
= 0; curArg
<= argNum
&& *args
; ++curArg
)
110 // if we found a backslash then flip the variable
111 backslash_encountered
= !backslash_encountered
;
113 else if (*args
== '"')
117 if (*(args
+ 1) != '"')
119 quotes_opened
= false;
128 quotes_opened
= true;
131 backslash_encountered
= false;
135 backslash_encountered
= false;
136 if (*args
== ' ' && !quotes_opened
&& curArg
!= argNum
)
140 if (curArg
== argNum
)
152 /***********************************************************************
153 * SHELL_ArgifyW [Internal]
155 * this function is supposed to expand the escape sequences found in the registry
156 * some diving reported that the following were used:
157 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
162 * %I address of a global item ID (explorer switch /idlist)
163 * %L seems to be %1 as long filename followed by the 8+3 variation
165 * %* all following parameters (see batfile)
167 * The way we parse the command line arguments was determined through extensive
168 * testing and can be summed up by the following rules"
171 * - if first letter is " break on first non literal " and include any white spaces
172 * - if first letter is NOT " break on first " or white space
173 * - if " is opened any pair of consecutive " results in ONE literal "
176 * - use rules from here http://www.autohotkey.net/~deleyd/parameters/parameters.htm
179 static BOOL
SHELL_ArgifyW(WCHAR
* out
, DWORD len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
, DWORD
* out_len
)
183 BOOL found_p1
= FALSE
;
187 bool tildeEffect
= false;
189 TRACE("Before parsing: %p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
190 debugstr_w(lpFile
), pidl
, args
);
251 ParseTildeEffect(res
, args
, len
, used
, *fmt
- '2');
256 ParseNoTildeEffect(res
, args
, len
, used
, *fmt
- '2');
263 if (!done
|| (*fmt
== '1'))
265 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
266 if (SearchPathW(NULL
, lpFile
, wszExe
, sizeof(xlpFile
) / sizeof(WCHAR
), xlpFile
, NULL
))
282 * IE uses this a lot for activating things such as windows media
283 * player. This is not verified to be fully correct but it appears
290 used
+= wcslen(lpFile
);
294 res
+= wcslen(lpFile
);
305 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
306 * allowing a buffer of 100 should more than exceed all needs */
309 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
310 pv
= SHLockShared(hmem
, 0);
311 chars
= swprintf(buf
, L
":%p", pv
);
313 if (chars
>= sizeof(buf
) / sizeof(WCHAR
))
314 ERR("pidl format buffer too small!\n");
330 * Check if this is an env-variable here...
333 /* Make sure that we have at least one more %.*/
334 if (strchrW(fmt
, '%'))
336 WCHAR tmpBuffer
[1024];
337 PWSTR tmpB
= tmpBuffer
;
338 WCHAR tmpEnvBuff
[MAX_PATH
];
345 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer
));
347 envRet
= GetEnvironmentVariableW(tmpBuffer
, tmpEnvBuff
, MAX_PATH
);
348 if (envRet
== 0 || envRet
> MAX_PATH
)
350 used
+= wcslen(tmpBuffer
);
353 wcscpy( res
, tmpBuffer
);
354 res
+= wcslen(tmpBuffer
);
359 used
+= wcslen(tmpEnvBuff
);
362 wcscpy( res
, tmpEnvBuff
);
363 res
+= wcslen(tmpEnvBuff
);
370 /* Don't skip past terminator (catch a single '%' at the end) */
387 TRACE("used %i of %i space\n", used
, len
);
391 TRACE("After parsing: %p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
392 debugstr_w(lpFile
), pidl
, args
);
397 static HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
400 IShellFolder
*desktop
;
402 HRESULT hr
= SHGetDesktopFolder(&desktop
);
406 hr
= desktop
->GetDisplayNameOf(pidl
, SHGDN_FORPARSING
, &strret
);
409 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
417 /*************************************************************************
418 * SHELL_ExecuteW [Internal]
421 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
422 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
424 STARTUPINFOW startup
;
425 PROCESS_INFORMATION info
;
426 UINT_PTR retval
= SE_ERR_NOASSOC
;
428 WCHAR curdir
[MAX_PATH
];
429 DWORD dwCreationFlags
;
430 const WCHAR
*lpDirectory
= NULL
;
432 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
434 /* make sure we don't fail the CreateProcess if the calling app passes in
435 * a bad working directory */
436 if (psei
->lpDirectory
&& psei
->lpDirectory
[0])
438 DWORD attr
= GetFileAttributesW(psei
->lpDirectory
);
439 if (attr
!= INVALID_FILE_ATTRIBUTES
&& attr
& FILE_ATTRIBUTE_DIRECTORY
)
440 lpDirectory
= psei
->lpDirectory
;
443 /* ShellExecute specifies the command from psei->lpDirectory
444 * if present. Not from the current dir as CreateProcess does */
446 if ((gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
447 if (!SetCurrentDirectoryW( lpDirectory
))
448 ERR("cannot set directory %s\n", debugstr_w(lpDirectory
));
450 ZeroMemory(&startup
, sizeof(STARTUPINFOW
));
451 startup
.cb
= sizeof(STARTUPINFOW
);
452 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
453 startup
.wShowWindow
= psei
->nShow
;
454 dwCreationFlags
= CREATE_UNICODE_ENVIRONMENT
;
456 if (psei
->fMask
& SEE_MASK_NO_CONSOLE
)
457 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
459 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, dwCreationFlags
, env
,
460 lpDirectory
, &startup
, &info
))
462 /* Give 30 seconds to the app to come up, if desired. Probably only needed
463 when starting app immediately before making a DDE connection. */
465 if (WaitForInputIdle(info
.hProcess
, 30000) == WAIT_FAILED
)
466 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
469 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
470 psei_out
->hProcess
= info
.hProcess
;
472 CloseHandle( info
.hProcess
);
473 CloseHandle( info
.hThread
);
475 else if ((retval
= GetLastError()) >= 32)
477 WARN("CreateProcess returned error %ld\n", retval
);
478 retval
= ERROR_BAD_FORMAT
;
481 TRACE("returning %lu\n", retval
);
483 psei_out
->hInstApp
= (HINSTANCE
)retval
;
486 if (!SetCurrentDirectoryW(curdir
))
487 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
493 /***********************************************************************
494 * SHELL_BuildEnvW [Internal]
496 * Build the environment for the new process, adding the specified
497 * path to the PATH variable. Returned pointer must be freed by caller.
499 static LPWSTR
SHELL_BuildEnvW( const WCHAR
*path
)
501 static const WCHAR wPath
[] = L
"PATH=";
502 WCHAR
*strings
, *new_env
;
504 int total
= wcslen(path
) + 1;
505 BOOL got_path
= FALSE
;
507 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
511 int len
= wcslen(p
) + 1;
512 if (!_wcsnicmp( p
, wPath
, 5 )) got_path
= TRUE
;
516 if (!got_path
) total
+= 5; /* we need to create PATH */
517 total
++; /* terminating null */
519 if (!(new_env
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, total
* sizeof(WCHAR
))))
521 FreeEnvironmentStringsW(strings
);
528 int len
= wcslen(p
) + 1;
529 memcpy(p2
, p
, len
* sizeof(WCHAR
));
530 if (!_wcsnicmp( p
, wPath
, 5 ))
533 wcscpy( p2
+ len
, path
);
534 p2
+= wcslen(path
) + 1;
543 p2
+= wcslen(p2
) + 1;
546 FreeEnvironmentStringsW(strings
);
551 /***********************************************************************
552 * SHELL_TryAppPathW [Internal]
554 * Helper function for SHELL_FindExecutable
555 * @param lpResult - pointer to a buffer of size MAX_PATH
556 * On entry: szName is a filename (probably without path separators).
557 * On exit: if szName found in "App Path", place full path in lpResult, and return true
559 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
567 if (env
) *env
= NULL
;
568 wcscpy(buffer
, L
"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
569 wcscat(buffer
, szName
);
570 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
573 len
= MAX_PATH
* sizeof(WCHAR
);
574 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
580 DWORD count
= sizeof(buffer
);
581 if (!RegQueryValueExW(hkApp
, L
"Path", NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
582 *env
= SHELL_BuildEnvW(buffer
);
586 if (hkApp
) RegCloseKey(hkApp
);
590 static UINT
SHELL_FindExecutableByOperation(LPCWSTR lpOperation
, LPWSTR key
, LPWSTR filetype
, LPWSTR command
, LONG commandlen
)
592 static const WCHAR wCommand
[] = L
"\\command";
594 WCHAR verb
[MAX_PATH
];
596 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, filetype
, 0, 0x02000000, &hkeyClass
))
597 return SE_ERR_NOASSOC
;
598 if (!HCR_GetDefaultVerbW(hkeyClass
, lpOperation
, verb
, sizeof(verb
) / sizeof(verb
[0])))
599 return SE_ERR_NOASSOC
;
600 RegCloseKey(hkeyClass
);
602 /* Looking for ...buffer\shell\<verb>\command */
603 wcscat(filetype
, L
"\\shell\\");
604 wcscat(filetype
, verb
);
605 wcscat(filetype
, wCommand
);
607 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, command
,
608 &commandlen
) == ERROR_SUCCESS
)
610 commandlen
/= sizeof(WCHAR
);
611 if (key
) wcscpy(key
, filetype
);
615 LONG paramlen
= sizeof(param
);
616 static const WCHAR wSpace
[] = {' ', 0};
618 /* FIXME: it seems all Windows version don't behave the same here.
619 * the doc states that this ddeexec information can be found after
621 * on Win98, it doesn't appear, but I think it does on Win2k
623 /* Get the parameters needed by the application
624 from the associated ddeexec key */
625 tmp
= strstrW(filetype
, wCommand
);
627 wcscat(filetype
, wDdeexec
);
628 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, param
,
629 ¶mlen
) == ERROR_SUCCESS
)
631 paramlen
/= sizeof(WCHAR
);
632 wcscat(command
, wSpace
);
633 wcscat(command
, param
);
634 commandlen
+= paramlen
;
638 command
[commandlen
] = '\0';
640 return 33; /* FIXME see SHELL_FindExecutable() */
643 return SE_ERR_NOASSOC
;
646 /*************************************************************************
647 * SHELL_FindExecutable [Internal]
649 * Utility for code sharing between FindExecutable and ShellExecute
651 * lpFile the name of a file
652 * lpOperation the operation on it (open)
654 * lpResult a buffer, big enough :-(, to store the command to do the
655 * operation on the file
656 * key a buffer, big enough, to get the key name to do actually the
657 * command (it'll be used afterwards for more information
660 static UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
,
661 LPWSTR lpResult
, DWORD resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
663 WCHAR
*extension
= NULL
; /* pointer to file extension */
664 WCHAR filetype
[256]; /* registry name for this filetype */
665 LONG filetypelen
= sizeof(filetype
); /* length of above */
666 WCHAR command
[1024]; /* command from registry */
667 WCHAR wBuffer
[256]; /* Used to GetProfileString */
668 UINT retval
= SE_ERR_NOASSOC
;
669 WCHAR
*tok
; /* token pointer */
670 WCHAR xlpFile
[256]; /* result of SearchPath */
671 DWORD attribs
; /* file attributes */
673 TRACE("%s\n", debugstr_w(lpFile
));
676 return ERROR_INVALID_PARAMETER
;
679 lpResult
[0] = '\0'; /* Start off with an empty return string */
680 if (key
) *key
= '\0';
682 /* trap NULL parameters on entry */
685 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
686 debugstr_w(lpFile
), debugstr_w(lpResult
));
687 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
690 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
692 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
696 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
) / sizeof(WCHAR
), xlpFile
, NULL
))
698 TRACE("SearchPathW returned non-zero\n");
700 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
703 attribs
= GetFileAttributesW(lpFile
);
704 if (attribs
!= INVALID_FILE_ATTRIBUTES
&& (attribs
& FILE_ATTRIBUTE_DIRECTORY
))
706 wcscpy(filetype
, L
"Folder");
707 filetypelen
= 6; /* strlen("Folder") */
711 /* Did we get something? Anything? */
714 TRACE("Returning SE_ERR_FNF\n");
717 /* First thing we need is the file's extension */
718 extension
= wcsrchr(xlpFile
, '.'); /* Assume last "." is the one; */
719 /* File->Run in progman uses */
721 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
723 if (extension
== NULL
|| extension
[1] == 0)
725 WARN("Returning SE_ERR_NOASSOC\n");
726 return SE_ERR_NOASSOC
;
729 /* Three places to check: */
730 /* 1. win.ini, [windows], programs (NB no leading '.') */
731 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
732 /* 3. win.ini, [extensions], extension (NB no leading '.' */
733 /* All I know of the order is that registry is checked before */
734 /* extensions; however, it'd make sense to check the programs */
735 /* section first, so that's what happens here. */
737 /* See if it's a program - if GetProfileString fails, we skip this
738 * section. Actually, if GetProfileString fails, we've probably
739 * got a lot more to worry about than running a program... */
740 if (GetProfileStringW(L
"windows", L
"programs", L
"exe pif bat cmd com", wBuffer
, sizeof(wBuffer
) / sizeof(WCHAR
)) > 0)
747 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
751 while (*p
== ' ' || *p
== '\t') p
++;
754 if (wcsicmp(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
756 wcscpy(lpResult
, xlpFile
);
757 /* Need to perhaps check that the file has a path
759 TRACE("found %s\n", debugstr_w(lpResult
));
761 /* Greater than 32 to indicate success */
768 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, filetype
,
769 &filetypelen
) == ERROR_SUCCESS
)
771 filetypelen
/= sizeof(WCHAR
);
772 if (filetypelen
== sizeof(filetype
) / sizeof(WCHAR
))
775 filetype
[filetypelen
] = '\0';
776 TRACE("File type: %s\n", debugstr_w(filetype
));
787 /* pass the operation string to SHELL_FindExecutableByOperation() */
788 filetype
[filetypelen
] = '\0';
789 retval
= SHELL_FindExecutableByOperation(lpOperation
, key
, filetype
, command
, sizeof(command
));
794 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
795 if (finishedLen
> resultLen
)
796 ERR("Argify buffer not large enough.. truncated\n");
797 /* Remove double quotation marks and command line arguments */
798 if (*lpResult
== '"')
801 while (*(p
+ 1) != '"')
810 /* Truncate on first space */
812 while (*p
!= ' ' && *p
!= '\0')
818 else /* Check win.ini */
820 /* Toss the leading dot */
822 if (GetProfileStringW(L
"extensions", extension
, L
"", command
, sizeof(command
) / sizeof(WCHAR
)) > 0)
824 if (wcslen(command
) != 0)
826 wcscpy(lpResult
, command
);
827 tok
= wcschr(lpResult
, '^'); /* should be ^.extension? */
831 wcscat(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
832 tok
= wcschr(command
, '^'); /* see above */
833 if ((tok
!= NULL
) && (wcslen(tok
) > 5))
835 wcscat(lpResult
, &tok
[5]);
838 retval
= 33; /* FIXME - see above */
843 TRACE("returning %s\n", debugstr_w(lpResult
));
847 /******************************************************************
850 * callback for the DDE connection. not really useful
852 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
853 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
854 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
856 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
857 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
861 /******************************************************************
864 * ShellExecute helper. Used to do an operation with a DDE connection
866 * Handles both the direct connection (try #1), and if it fails,
867 * launching an application and trying (#2) to connect to it
870 static unsigned dde_connect(const WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
871 const WCHAR
* lpFile
, WCHAR
*env
,
872 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
873 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
876 WCHAR
* endkey
= regkey
+ wcslen(key
);
877 WCHAR app
[256], topic
[256], ifexec
[256], res
[256];
878 LONG applen
, topiclen
, ifexeclen
;
883 HSZ hszApp
, hszTopic
;
886 unsigned ret
= SE_ERR_NOASSOC
;
887 BOOL unicode
= !(GetVersion() & 0x80000000);
890 wcscpy(endkey
, L
"\\application");
891 applen
= sizeof(app
);
892 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, app
, &applen
) != ERROR_SUCCESS
)
894 WCHAR command
[1024], fullpath
[MAX_PATH
];
895 static const WCHAR wSo
[] = L
".so";
896 DWORD sizeSo
= sizeof(wSo
) / sizeof(WCHAR
);
900 /* Get application command from start string and find filename of application */
903 wcscpy(command
, start
+ 1);
904 if ((ptr
= wcschr(command
, '"')))
906 ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
);
911 for (p
= (LPWSTR
)start
; (space
= const_cast<LPWSTR
>(strchrW(p
, ' '))); p
= space
+ 1)
913 int idx
= space
- start
;
914 memcpy(command
, start
, idx
* sizeof(WCHAR
));
916 if ((ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
)))
920 ret
= SearchPathW(NULL
, start
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
);
925 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
926 return ERROR_ACCESS_DENIED
;
930 /* Remove extensions (including .so) */
931 ptr
= app
+ wcslen(app
) - (sizeSo
- 1);
932 if (wcslen(app
) >= sizeSo
&&
936 ptr
= const_cast<LPWSTR
>(strrchrW(app
, '.'));
941 wcscpy(endkey
, L
"\\topic");
942 topiclen
= sizeof(topic
);
943 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, topic
, &topiclen
) != ERROR_SUCCESS
)
945 wcscpy(topic
, L
"System");
950 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
955 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
959 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
960 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
962 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
966 TRACE("Launching %s\n", debugstr_w(start
));
967 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
970 TRACE("Couldn't launch\n");
973 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
976 TRACE("Couldn't connect. ret=%d\n", ret
);
977 DdeUninitialize(ddeInst
);
978 SetLastError(ERROR_DDE_FAIL
);
979 return 30; /* whatever */
981 strcpyW(endkey
, L
"\\ifexec");
982 ifexeclen
= sizeof(ifexec
);
983 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
989 SHELL_ArgifyW(res
, sizeof(res
) / sizeof(WCHAR
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
990 if (resultLen
> sizeof(res
) / sizeof(WCHAR
))
991 ERR("Argify buffer not large enough, truncated\n");
992 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
994 /* It's documented in the KB 330337 that IE has a bug and returns
995 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
998 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0, XTYP_EXECUTE
, 30000, &tid
);
1001 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
1002 char *resA
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, lenA
);
1003 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
1004 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
1005 XTYP_EXECUTE
, 10000, &tid
);
1006 HeapFree(GetProcessHeap(), 0, resA
);
1009 DdeFreeDataHandle(hDdeData
);
1011 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
1014 DdeDisconnect(hConv
);
1017 DdeUninitialize(ddeInst
);
1022 /*************************************************************************
1023 * execute_from_key [Internal]
1025 static UINT_PTR
execute_from_key(LPCWSTR key
, LPCWSTR lpFile
, WCHAR
*env
,
1026 LPCWSTR szCommandline
, LPCWSTR executable_name
,
1027 SHELL_ExecuteW32 execfunc
,
1028 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
1030 WCHAR cmd
[256], param
[1024], ddeexec
[256];
1031 DWORD cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
1032 UINT_PTR retval
= SE_ERR_NOASSOC
;
1036 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
1037 debugstr_w(szCommandline
), debugstr_w(executable_name
));
1042 /* Get the application from the registry */
1043 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, (LONG
*)&cmdlen
) == ERROR_SUCCESS
)
1045 TRACE("got cmd: %s\n", debugstr_w(cmd
));
1047 /* Is there a replace() function anywhere? */
1048 cmdlen
/= sizeof(WCHAR
);
1049 if (cmdlen
>= sizeof(cmd
) / sizeof(WCHAR
))
1050 cmdlen
= sizeof(cmd
) / sizeof(WCHAR
) - 1;
1052 SHELL_ArgifyW(param
, sizeof(param
) / sizeof(WCHAR
), cmd
, lpFile
, (LPITEMIDLIST
)psei
->lpIDList
, szCommandline
, &resultLen
);
1053 if (resultLen
> sizeof(param
) / sizeof(WCHAR
))
1054 ERR("Argify buffer not large enough, truncating\n");
1057 /* Get the parameters needed by the application
1058 from the associated ddeexec key */
1059 tmp
= const_cast<LPWSTR
>(strstrW(key
, L
"command"));
1061 wcscpy(tmp
, L
"ddeexec");
1063 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, (LONG
*)&ddeexeclen
) == ERROR_SUCCESS
)
1065 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
1066 if (!param
[0]) strcpyW(param
, executable_name
);
1067 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, (LPITEMIDLIST
)psei
->lpIDList
, execfunc
, psei
, psei_out
);
1071 TRACE("executing: %s\n", debugstr_w(param
));
1072 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
1075 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
1080 /*************************************************************************
1081 * FindExecutableA [SHELL32.@]
1083 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
1086 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
1087 WCHAR wResult
[MAX_PATH
];
1089 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
1090 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
1092 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
1093 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
1097 TRACE("returning %s\n", lpResult
);
1101 /*************************************************************************
1102 * FindExecutableW [SHELL32.@]
1104 * This function returns the executable associated with the specified file
1105 * for the default verb.
1108 * lpFile [I] The file to find the association for. This must refer to
1109 * an existing file otherwise FindExecutable fails and returns
1111 * lpResult [O] Points to a buffer into which the executable path is
1112 * copied. This parameter must not be NULL otherwise
1113 * FindExecutable() segfaults. The buffer must be of size at
1114 * least MAX_PATH characters.
1117 * A value greater than 32 on success, less than or equal to 32 otherwise.
1118 * See the SE_ERR_* constants.
1121 * On Windows XP and 2003, FindExecutable() seems to first convert the
1122 * filename into 8.3 format, thus taking into account only the first three
1123 * characters of the extension, and expects to find an association for those.
1124 * However other Windows versions behave sanely.
1126 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
1128 UINT_PTR retval
= SE_ERR_NOASSOC
;
1129 WCHAR old_dir
[1024];
1131 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
1133 lpResult
[0] = '\0'; /* Start off with an empty return string */
1135 return (HINSTANCE
)SE_ERR_FNF
;
1139 GetCurrentDirectoryW(sizeof(old_dir
) / sizeof(WCHAR
), old_dir
);
1140 SetCurrentDirectoryW(lpDirectory
);
1143 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, lpResult
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1145 TRACE("returning %s\n", debugstr_w(lpResult
));
1147 SetCurrentDirectoryW(old_dir
);
1148 return (HINSTANCE
)retval
;
1151 /* FIXME: is this already implemented somewhere else? */
1152 static HKEY
ShellExecute_GetClassKey(const SHELLEXECUTEINFOW
*sei
)
1154 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1156 DWORD type
= 0, sz
= 0;
1160 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1161 return sei
->hkeyClass
;
1163 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1164 lpClass
= sei
->lpClass
;
1167 ext
= PathFindExtensionW(sei
->lpFile
);
1168 TRACE("ext = %s\n", debugstr_w(ext
));
1172 r
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
1173 if (r
!= ERROR_SUCCESS
)
1176 r
= RegQueryValueExW(hkey
, NULL
, 0, &type
, NULL
, &sz
);
1177 if (r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1179 sz
+= sizeof (WCHAR
);
1180 cls
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sz
);
1182 RegQueryValueExW(hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1185 RegCloseKey( hkey
);
1189 TRACE("class = %s\n", debugstr_w(lpClass
));
1193 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1195 HeapFree(GetProcessHeap(), 0, cls
);
1200 static IDataObject
*shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
)
1202 LPCITEMIDLIST pidllast
= NULL
;
1203 IDataObject
*dataobj
= NULL
;
1204 IShellFolder
*shf
= NULL
;
1205 LPITEMIDLIST pidl
= NULL
;
1208 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1209 pidl
= (LPITEMIDLIST
)sei
->lpIDList
;
1212 WCHAR fullpath
[MAX_PATH
];
1216 ret
= GetFullPathNameW(sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1220 pidl
= ILCreateFromPathW(fullpath
);
1223 r
= SHBindToParent(pidl
, IID_IShellFolder
, (LPVOID
*)&shf
, &pidllast
);
1227 shf
->GetUIObjectOf(NULL
, 1, &pidllast
,
1228 IID_IDataObject
, NULL
, (LPVOID
*) &dataobj
);
1231 if (pidl
!= sei
->lpIDList
)
1238 static HRESULT
shellex_run_context_menu_default(IShellExtInit
*obj
,
1239 LPSHELLEXECUTEINFOW sei
)
1241 IContextMenu
*cm
= NULL
;
1242 CMINVOKECOMMANDINFOEX ici
;
1249 TRACE("%p %p\n", obj
, sei
);
1251 r
= obj
->QueryInterface(IID_IContextMenu
, (LPVOID
*) &cm
);
1255 hmenu
= CreateMenu();
1259 /* the number of the last menu added is returned in r */
1260 r
= cm
->QueryContextMenu(hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1264 n
= GetMenuItemCount(hmenu
);
1265 for (i
= 0; i
< n
; i
++)
1267 memset(&info
, 0, sizeof(info
));
1268 info
.cbSize
= sizeof info
;
1269 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1270 info
.dwTypeData
= string
;
1271 info
.cch
= sizeof string
;
1273 GetMenuItemInfoW(hmenu
, i
, TRUE
, &info
);
1275 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1276 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1277 if ((!sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
)) ||
1278 (sei
->lpVerb
&& !lstrcmpiW(sei
->lpVerb
, string
)))
1289 memset(&ici
, 0, sizeof ici
);
1290 ici
.cbSize
= sizeof ici
;
1291 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NOASYNC
| SEE_MASK_ASYNCOK
| SEE_MASK_FLAG_NO_UI
));
1292 ici
.nShow
= sei
->nShow
;
1293 ici
.lpVerb
= MAKEINTRESOURCEA(def
);
1294 ici
.hwnd
= sei
->hwnd
;
1295 ici
.lpParametersW
= sei
->lpParameters
;
1297 r
= cm
->InvokeCommand((LPCMINVOKECOMMANDINFO
)&ici
);
1299 TRACE("invoke command returned %08x\n", r
);
1303 DestroyMenu( hmenu
);
1309 static HRESULT
shellex_load_object_and_run(HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1311 IDataObject
*dataobj
= NULL
;
1312 IObjectWithSite
*ows
= NULL
;
1313 IShellExtInit
*obj
= NULL
;
1316 TRACE("%p %s %p\n", hkey
, debugstr_guid(guid
), sei
);
1318 r
= CoInitialize(NULL
);
1322 r
= CoCreateInstance(*guid
, NULL
, CLSCTX_INPROC_SERVER
,
1323 IID_IShellExtInit
, (LPVOID
*)&obj
);
1326 ERR("failed %08x\n", r
);
1330 dataobj
= shellex_get_dataobj(sei
);
1333 ERR("failed to get data object\n");
1337 r
= obj
->Initialize(NULL
, dataobj
, hkey
);
1341 r
= obj
->QueryInterface(IID_IObjectWithSite
, (LPVOID
*) &ows
);
1347 r
= shellex_run_context_menu_default(obj
, sei
);
1361 /*************************************************************************
1362 * ShellExecute_FromContextMenu [Internal]
1364 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1366 HKEY hkey
, hkeycm
= 0;
1373 TRACE("%s\n", debugstr_w(sei
->lpFile
));
1375 hkey
= ShellExecute_GetClassKey(sei
);
1377 return ERROR_FUNCTION_FAILED
;
1379 r
= RegOpenKeyW(hkey
, L
"shellex\\ContextMenuHandlers", &hkeycm
);
1380 if (r
== ERROR_SUCCESS
)
1385 r
= RegEnumKeyW(hkeycm
, i
++, szguid
, sizeof(szguid
) / sizeof(szguid
[0]));
1386 if (r
!= ERROR_SUCCESS
)
1389 hr
= CLSIDFromString(szguid
, &guid
);
1392 /* stop at the first one that succeeds in running */
1393 hr
= shellex_load_object_and_run(hkey
, &guid
, sei
);
1398 RegCloseKey(hkeycm
);
1401 if (hkey
!= sei
->hkeyClass
)
1406 static UINT_PTR
SHELL_execute_class(LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1408 WCHAR execCmd
[1024], wcmd
[1024];
1409 /* launch a document by fileclass like 'WordPad.Document.1' */
1410 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1411 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1412 ULONG cmask
= (psei
->fMask
& SEE_MASK_CLASSALL
);
1416 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1417 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1419 execCmd
, sizeof(execCmd
));
1421 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1422 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1425 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
) / sizeof(WCHAR
), execCmd
, wszApplicationName
, (LPITEMIDLIST
)psei
->lpIDList
, NULL
, &resultLen
);
1426 if (!done
&& wszApplicationName
[0])
1428 strcatW(wcmd
, L
" ");
1429 strcatW(wcmd
, wszApplicationName
);
1431 if (resultLen
> sizeof(wcmd
) / sizeof(WCHAR
))
1432 ERR("Argify buffer not large enough... truncating\n");
1433 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1436 static BOOL
SHELL_translate_idlist(LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1438 static const WCHAR wExplorer
[] = L
"explorer.exe";
1439 WCHAR buffer
[MAX_PATH
];
1440 BOOL appKnownSingular
= FALSE
;
1442 /* last chance to translate IDList: now also allow CLSID paths */
1443 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST
)sei
->lpIDList
, buffer
, sizeof(buffer
)))) {
1444 if (buffer
[0] == ':' && buffer
[1] == ':') {
1445 /* open shell folder for the specified class GUID */
1446 if (strlenW(buffer
) + 1 > parametersLen
)
1447 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1448 lstrlenW(buffer
) + 1, parametersLen
);
1449 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1450 if (strlenW(wExplorer
) > dwApplicationNameLen
)
1451 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1452 lstrlenW(wExplorer
) + 1, dwApplicationNameLen
);
1453 lstrcpynW(wszApplicationName
, wExplorer
, dwApplicationNameLen
);
1454 appKnownSingular
= TRUE
;
1456 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1458 WCHAR target
[MAX_PATH
];
1461 /* Check if we're executing a directory and if so use the
1462 handler for the Folder class */
1463 strcpyW(target
, buffer
);
1464 attribs
= GetFileAttributesW(buffer
);
1465 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1466 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1467 HCR_GetExecuteCommandW(0, L
"Folder",
1469 buffer
, sizeof(buffer
))) {
1470 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1471 buffer
, target
, (LPITEMIDLIST
)sei
->lpIDList
, NULL
, &resultLen
);
1472 if (resultLen
> dwApplicationNameLen
)
1473 ERR("Argify buffer not large enough... truncating\n");
1474 appKnownSingular
= FALSE
;
1476 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1479 return appKnownSingular
;
1482 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
)
1486 WCHAR
*wszQuotedCmd
;
1488 /* Length of quotes plus length of command plus NULL terminator */
1489 len
= 2 + lstrlenW(wcmd
) + 1;
1490 if (wszParameters
[0])
1492 /* Length of space plus length of parameters */
1493 len
+= 1 + lstrlenW(wszParameters
);
1495 wszQuotedCmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1496 /* Must quote to handle case where cmd contains spaces,
1497 * else security hole if malicious user creates executable file "C:\\Program"
1499 strcpyW(wszQuotedCmd
, L
"\"");
1500 strcatW(wszQuotedCmd
, wcmd
);
1501 strcatW(wszQuotedCmd
, L
"\"");
1502 if (wszParameters
[0])
1504 strcatW(wszQuotedCmd
, L
" ");
1505 strcatW(wszQuotedCmd
, wszParameters
);
1508 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(lpstrProtocol
));
1511 retval
= execute_from_key(lpstrProtocol
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1513 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1514 HeapFree(GetProcessHeap(), 0, wszQuotedCmd
);
1518 static UINT_PTR
SHELL_execute_url(LPCWSTR lpFile
, LPCWSTR wFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1520 static const WCHAR wShell
[] = L
"\\shell\\";
1521 static const WCHAR wCommand
[] = L
"\\command";
1523 WCHAR
*lpstrProtocol
;
1528 lpstrRes
= strchrW(lpFile
, ':');
1530 iSize
= lpstrRes
- lpFile
;
1532 iSize
= strlenW(lpFile
);
1534 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1535 /* Looking for ...protocol\shell\lpOperation\command */
1536 len
= iSize
+ lstrlenW(wShell
) + lstrlenW(wCommand
) + 1;
1538 len
+= lstrlenW(psei
->lpVerb
);
1540 len
+= lstrlenW(wszOpen
);
1541 lpstrProtocol
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1542 memcpy(lpstrProtocol
, lpFile
, iSize
* sizeof(WCHAR
));
1543 lpstrProtocol
[iSize
] = '\0';
1544 strcatW(lpstrProtocol
, wShell
);
1545 strcatW(lpstrProtocol
, psei
->lpVerb
? psei
->lpVerb
: wszOpen
);
1546 strcatW(lpstrProtocol
, wCommand
);
1548 /* Remove File Protocol from lpFile */
1549 /* In the case file://path/file */
1550 if (!strncmpiW(lpFile
, wFile
, iSize
))
1553 while (*lpFile
== ':') lpFile
++;
1555 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1556 wcmd
, execfunc
, psei
, psei_out
);
1557 HeapFree(GetProcessHeap(), 0, lpstrProtocol
);
1561 void do_error_dialog(UINT_PTR retval
, HWND hwnd
, WCHAR
* filename
)
1564 DWORD_PTR msgArguments
[3] = { (DWORD_PTR
)filename
, 0, 0 };
1567 error_code
= GetLastError();
1569 if (retval
== SE_ERR_NOASSOC
)
1570 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, sizeof(msg
) / sizeof(WCHAR
));
1572 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ARGUMENT_ARRAY
,
1577 sizeof(msg
) / sizeof(WCHAR
),
1578 (va_list*)msgArguments
);
1580 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1583 /*************************************************************************
1584 * SHELL_execute [Internal]
1586 BOOL
SHELL_execute(LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1588 static const DWORD unsupportedFlags
=
1589 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1590 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1591 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1593 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1594 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
;
1595 DWORD dwApplicationNameLen
= MAX_PATH
+ 2;
1596 DWORD parametersLen
= sizeof(parametersBuffer
) / sizeof(WCHAR
);
1597 DWORD dirLen
= sizeof(dirBuffer
) / sizeof(WCHAR
);
1598 DWORD wcmdLen
= sizeof(wcmdBuffer
) / sizeof(WCHAR
);
1600 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1601 WCHAR wfileName
[MAX_PATH
];
1603 WCHAR lpstrProtocol
[256];
1605 UINT_PTR retval
= SE_ERR_NOASSOC
;
1606 BOOL appKnownSingular
= FALSE
;
1608 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1611 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1612 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1613 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1614 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1615 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1616 debugstr_w(sei_tmp
.lpClass
) : "not used");
1618 sei
->hProcess
= NULL
;
1620 /* make copies of all path/command strings */
1621 if (!sei_tmp
.lpFile
)
1623 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1624 *wszApplicationName
= '\0';
1626 else if (*sei_tmp
.lpFile
== '\"')
1628 DWORD l
= strlenW(sei_tmp
.lpFile
+ 1);
1629 if(l
>= dwApplicationNameLen
)
1630 dwApplicationNameLen
= l
+ 1;
1632 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1633 memcpy(wszApplicationName
, sei_tmp
.lpFile
+ 1, (l
+ 1)*sizeof(WCHAR
));
1635 if (wszApplicationName
[l
-1] == L
'\"')
1636 wszApplicationName
[l
-1] = L
'\0';
1637 appKnownSingular
= TRUE
;
1639 TRACE("wszApplicationName=%s\n", debugstr_w(wszApplicationName
));
1643 DWORD l
= strlenW(sei_tmp
.lpFile
) + 1;
1644 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+ 1;
1645 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1646 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
* sizeof(WCHAR
));
1649 wszParameters
= parametersBuffer
;
1650 if (sei_tmp
.lpParameters
)
1652 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1653 if (len
> parametersLen
)
1655 wszParameters
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1656 parametersLen
= len
;
1658 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1661 *wszParameters
= L
'\0';
1664 if (sei_tmp
.lpDirectory
)
1666 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1669 wszDir
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1672 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1677 /* adjust string pointers to point to the new buffers */
1678 sei_tmp
.lpFile
= wszApplicationName
;
1679 sei_tmp
.lpParameters
= wszParameters
;
1680 sei_tmp
.lpDirectory
= wszDir
;
1682 if (sei_tmp
.fMask
& unsupportedFlags
)
1684 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1687 /* process the IDList */
1688 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1690 IShellExecuteHookW
* pSEH
;
1692 HRESULT hr
= SHBindToParent((LPCITEMIDLIST
)sei_tmp
.lpIDList
, IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1696 hr
= pSEH
->Execute(&sei_tmp
);
1702 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1703 if (wszParameters
!= parametersBuffer
)
1704 HeapFree(GetProcessHeap(), 0, wszParameters
);
1705 if (wszDir
!= dirBuffer
)
1706 HeapFree(GetProcessHeap(), 0, wszDir
);
1711 SHGetPathFromIDListW((LPCITEMIDLIST
)sei_tmp
.lpIDList
, wszApplicationName
);
1712 appKnownSingular
= TRUE
;
1713 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1716 if (ERROR_SUCCESS
== ShellExecute_FromContextMenu(&sei_tmp
))
1718 sei
->hInstApp
= (HINSTANCE
) 33;
1719 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1720 if (wszParameters
!= parametersBuffer
)
1721 HeapFree(GetProcessHeap(), 0, wszParameters
);
1722 if (wszDir
!= dirBuffer
)
1723 HeapFree(GetProcessHeap(), 0, wszDir
);
1727 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1729 retval
= SHELL_execute_class(wszApplicationName
, &sei_tmp
, sei
, execfunc
);
1730 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1737 Info
.pcszFile
= wszApplicationName
;
1738 Info
.pcszClass
= NULL
;
1739 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1741 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1742 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1744 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1745 if (wszParameters
!= parametersBuffer
)
1746 HeapFree(GetProcessHeap(), 0, wszParameters
);
1747 if (wszDir
!= dirBuffer
)
1748 HeapFree(GetProcessHeap(), 0, wszDir
);
1752 /* Has the IDList not yet been translated? */
1753 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1755 appKnownSingular
= SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1758 dwApplicationNameLen
);
1761 /* expand environment strings */
1762 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1766 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1768 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1769 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1770 dwApplicationNameLen
= len
+ 1;
1771 wszApplicationName
= buf
;
1772 /* appKnownSingular unmodified */
1774 sei_tmp
.lpFile
= wszApplicationName
;
1777 if (*sei_tmp
.lpParameters
)
1779 len
= ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, NULL
, 0);
1784 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1785 ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buf
, len
);
1786 if (wszParameters
!= parametersBuffer
)
1787 HeapFree(GetProcessHeap(), 0, wszParameters
);
1788 wszParameters
= buf
;
1789 parametersLen
= len
;
1790 sei_tmp
.lpParameters
= wszParameters
;
1794 if (*sei_tmp
.lpDirectory
)
1796 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1801 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1802 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1803 if (wszDir
!= dirBuffer
)
1804 HeapFree(GetProcessHeap(), 0, wszDir
);
1806 sei_tmp
.lpDirectory
= wszDir
;
1810 /* Else, try to execute the filename */
1811 TRACE("execute: %s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1813 /* separate out command line arguments from executable file name */
1814 if (!*sei_tmp
.lpParameters
&& !appKnownSingular
)
1816 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1817 if (sei_tmp
.lpFile
[0] == L
'"')
1819 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1820 LPWSTR dst
= wfileName
;
1823 /* copy the unquoted executable path to 'wfileName' */
1824 while(*src
&& *src
!= L
'"')
1833 while(isspace(*src
))
1839 /* copy the parameter string to 'wszParameters' */
1840 strcpyW(wszParameters
, src
);
1842 /* terminate previous command string after the quote character */
1847 /* If the executable name is not quoted, we have to use this search loop here,
1848 that in CreateProcess() is not sufficient because it does not handle shell links. */
1849 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1852 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1853 for(s
= beg
; (space
= const_cast<LPWSTR
>(strchrW(s
, L
' '))); s
= space
+ 1)
1855 int idx
= space
- sei_tmp
.lpFile
;
1856 memcpy(buffer
, sei_tmp
.lpFile
, idx
* sizeof(WCHAR
));
1859 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1860 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
) / sizeof(xlpFile
[0]), xlpFile
, NULL
))
1862 /* separate out command from parameter string */
1863 LPCWSTR p
= space
+ 1;
1868 strcpyW(wszParameters
, p
);
1875 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1879 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1884 len
= lstrlenW(wszApplicationName
) + 3;
1885 if (sei_tmp
.lpParameters
[0])
1886 len
+= 1 + lstrlenW(wszParameters
);
1889 wcmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1892 swprintf(wcmd
, L
"\"%s\"", wszApplicationName
);
1893 if (sei_tmp
.lpParameters
[0])
1895 strcatW(wcmd
, L
" ");
1896 strcatW(wcmd
, wszParameters
);
1899 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1902 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1903 if (wszParameters
!= parametersBuffer
)
1904 HeapFree(GetProcessHeap(), 0, wszParameters
);
1905 if (wszDir
!= dirBuffer
)
1906 HeapFree(GetProcessHeap(), 0, wszDir
);
1907 if (wcmd
!= wcmdBuffer
)
1908 HeapFree(GetProcessHeap(), 0, wcmd
);
1912 /* Else, try to find the executable */
1914 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, lpstrProtocol
, &env
, (LPITEMIDLIST
)sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1915 if (retval
> 32) /* Found */
1917 retval
= SHELL_quote_and_execute(wcmd
, wszParameters
, lpstrProtocol
,
1918 wszApplicationName
, env
, &sei_tmp
,
1920 HeapFree(GetProcessHeap(), 0, env
);
1922 else if (PathIsDirectoryW(lpFile
))
1924 WCHAR wExec
[MAX_PATH
];
1925 WCHAR
* lpQuotedFile
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (strlenW(lpFile
) + 3));
1929 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, L
"explorer",
1930 wszOpen
, wExec
, MAX_PATH
,
1931 NULL
, &env
, NULL
, NULL
);
1934 swprintf(lpQuotedFile
, L
"\"%s\"", lpFile
);
1935 retval
= SHELL_quote_and_execute(wExec
, lpQuotedFile
,
1937 wszApplicationName
, env
,
1938 &sei_tmp
, sei
, execfunc
);
1939 HeapFree(GetProcessHeap(), 0, env
);
1941 HeapFree(GetProcessHeap(), 0, lpQuotedFile
);
1944 retval
= 0; /* Out of memory */
1946 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1948 retval
= SHELL_execute_url(lpFile
, L
"file", wcmd
, &sei_tmp
, sei
, execfunc
);
1950 /* Check if file specified is in the form www.??????.*** */
1951 else if (!strncmpiW(lpFile
, L
"www", 3))
1953 /* if so, append lpFile http:// and call ShellExecute */
1954 WCHAR lpstrTmpFile
[256];
1955 strcpyW(lpstrTmpFile
, L
"http://");
1956 strcatW(lpstrTmpFile
, lpFile
);
1957 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1960 TRACE("retval %lu\n", retval
);
1962 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1969 Info
.pcszFile
= wszApplicationName
;
1970 Info
.pcszClass
= NULL
;
1971 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1973 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1974 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1977 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1978 if (wszParameters
!= parametersBuffer
)
1979 HeapFree(GetProcessHeap(), 0, wszParameters
);
1980 if (wszDir
!= dirBuffer
)
1981 HeapFree(GetProcessHeap(), 0, wszDir
);
1982 if (wcmd
!= wcmdBuffer
)
1983 HeapFree(GetProcessHeap(), 0, wcmd
);
1985 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1990 /*************************************************************************
1991 * ShellExecuteA [SHELL32.290]
1993 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
1994 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1996 SHELLEXECUTEINFOA sei
;
1998 TRACE("%p,%s,%s,%s,%s,%d\n",
1999 hWnd
, debugstr_a(lpOperation
), debugstr_a(lpFile
),
2000 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
2002 sei
.cbSize
= sizeof(sei
);
2003 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2005 sei
.lpVerb
= lpOperation
;
2006 sei
.lpFile
= lpFile
;
2007 sei
.lpParameters
= lpParameters
;
2008 sei
.lpDirectory
= lpDirectory
;
2009 sei
.nShow
= iShowCmd
;
2016 ShellExecuteExA(&sei
);
2017 return sei
.hInstApp
;
2020 /*************************************************************************
2021 * ShellExecuteExA [SHELL32.292]
2024 BOOL WINAPI
ShellExecuteExA(LPSHELLEXECUTEINFOA sei
)
2026 SHELLEXECUTEINFOW seiW
;
2028 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
2032 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
2035 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
2038 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
2040 if (sei
->lpParameters
)
2041 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
2043 if (sei
->lpDirectory
)
2044 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
2046 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
2047 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
2049 seiW
.lpClass
= NULL
;
2051 ret
= SHELL_execute(&seiW
, SHELL_ExecuteW
);
2053 sei
->hInstApp
= seiW
.hInstApp
;
2055 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
2056 sei
->hProcess
= seiW
.hProcess
;
2060 SHFree(wParameters
);
2067 /*************************************************************************
2068 * ShellExecuteExW [SHELL32.293]
2071 BOOL WINAPI
ShellExecuteExW(LPSHELLEXECUTEINFOW sei
)
2073 return SHELL_execute(sei
, SHELL_ExecuteW
);
2076 /*************************************************************************
2077 * ShellExecuteW [SHELL32.294]
2079 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
2080 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
2082 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
2083 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
2085 SHELLEXECUTEINFOW sei
;
2088 sei
.cbSize
= sizeof(sei
);
2089 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2091 sei
.lpVerb
= lpOperation
;
2092 sei
.lpFile
= lpFile
;
2093 sei
.lpParameters
= lpParameters
;
2094 sei
.lpDirectory
= lpDirectory
;
2095 sei
.nShow
= nShowCmd
;
2102 SHELL_execute(&sei
, SHELL_ExecuteW
);
2103 return sei
.hInstApp
;
2106 /*************************************************************************
2107 * WOWShellExecute [SHELL32.@]
2109 * FIXME: the callback function most likely doesn't work the same way on Windows.
2111 EXTERN_C HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
2112 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
2114 SHELLEXECUTEINFOW seiW
;
2115 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
2116 HANDLE hProcess
= 0;
2118 seiW
.lpVerb
= lpOperation
? __SHCloneStrAtoW(&wVerb
, lpOperation
) : NULL
;
2119 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
2120 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
2121 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
2123 seiW
.cbSize
= sizeof(seiW
);
2126 seiW
.nShow
= iShowCmd
;
2131 seiW
.hProcess
= hProcess
;
2133 SHELL_execute(&seiW
, (SHELL_ExecuteW32
)callback
);
2137 SHFree(wParameters
);
2139 return seiW
.hInstApp
;
2142 /*************************************************************************
2143 * OpenAs_RunDLLA [SHELL32.@]
2145 EXTERN_C
void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2147 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2150 /*************************************************************************
2151 * OpenAs_RunDLLW [SHELL32.@]
2153 EXTERN_C
void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2155 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);