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
24 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
26 static const WCHAR wszOpen
[] = L
"open";
27 static const WCHAR wszExe
[] = L
".exe";
29 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
31 static void ParseNoTildeEffect(PWSTR
&res
, LPCWSTR
&args
, DWORD
&len
, DWORD
&used
, int argNum
)
33 bool firstCharQuote
= false;
34 bool quotes_opened
= false;
35 bool backslash_encountered
= false;
37 for (int curArg
= 0; curArg
<= argNum
&& *args
; ++curArg
)
39 firstCharQuote
= false;
43 firstCharQuote
= true;
51 // if we found a backslash then flip the variable
52 backslash_encountered
= !backslash_encountered
;
54 else if (*args
== '"')
58 if (*(args
+ 1) != '"')
60 quotes_opened
= false;
74 backslash_encountered
= false;
78 backslash_encountered
= false;
79 if (*args
== ' ' && !firstCharQuote
)
98 static void ParseTildeEffect(PWSTR
&res
, LPCWSTR
&args
, DWORD
&len
, DWORD
&used
, int argNum
)
100 bool quotes_opened
= false;
101 bool backslash_encountered
= false;
103 for (int curArg
= 0; curArg
<= argNum
&& *args
; ++curArg
)
109 // if we found a backslash then flip the variable
110 backslash_encountered
= !backslash_encountered
;
112 else if (*args
== '"')
116 if (*(args
+ 1) != '"')
118 quotes_opened
= false;
127 quotes_opened
= true;
130 backslash_encountered
= false;
134 backslash_encountered
= false;
135 if (*args
== ' ' && !quotes_opened
&& curArg
!= argNum
)
139 if (curArg
== argNum
)
151 /***********************************************************************
152 * SHELL_ArgifyW [Internal]
154 * this function is supposed to expand the escape sequences found in the registry
155 * some diving reported that the following were used:
156 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
161 * %I address of a global item ID (explorer switch /idlist)
162 * %L seems to be %1 as long filename followed by the 8+3 variation
164 * %* all following parameters (see batfile)
166 * The way we parse the command line arguments was determined through extensive
167 * testing and can be summed up by the following rules"
170 * - if first letter is " break on first non literal " and include any white spaces
171 * - if first letter is NOT " break on first " or white space
172 * - if " is opened any pair of consecutive " results in ONE literal "
175 * - use rules from here http://www.autohotkey.net/~deleyd/parameters/parameters.htm
178 static BOOL
SHELL_ArgifyW(WCHAR
* out
, DWORD len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
, DWORD
* out_len
)
182 BOOL found_p1
= FALSE
;
186 bool tildeEffect
= false;
188 TRACE("Before parsing: %p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
189 debugstr_w(lpFile
), pidl
, args
);
250 ParseTildeEffect(res
, args
, len
, used
, *fmt
- '2');
255 ParseNoTildeEffect(res
, args
, len
, used
, *fmt
- '2');
262 if (!done
|| (*fmt
== '1'))
264 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
265 if (SearchPathW(NULL
, lpFile
, wszExe
, sizeof(xlpFile
) / sizeof(WCHAR
), xlpFile
, NULL
))
281 * IE uses this a lot for activating things such as windows media
282 * player. This is not verified to be fully correct but it appears
289 used
+= wcslen(lpFile
);
293 res
+= wcslen(lpFile
);
304 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
305 * allowing a buffer of 100 should more than exceed all needs */
308 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
309 pv
= SHLockShared(hmem
, 0);
310 chars
= swprintf(buf
, L
":%p", pv
);
312 if (chars
>= sizeof(buf
) / sizeof(WCHAR
))
313 ERR("pidl format buffer too small!\n");
329 * Check if this is an env-variable here...
332 /* Make sure that we have at least one more %.*/
333 if (strchrW(fmt
, '%'))
335 WCHAR tmpBuffer
[1024];
336 PWSTR tmpB
= tmpBuffer
;
337 WCHAR tmpEnvBuff
[MAX_PATH
];
344 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer
));
346 envRet
= GetEnvironmentVariableW(tmpBuffer
, tmpEnvBuff
, MAX_PATH
);
347 if (envRet
== 0 || envRet
> MAX_PATH
)
349 used
+= wcslen(tmpBuffer
);
352 wcscpy( res
, tmpBuffer
);
353 res
+= wcslen(tmpBuffer
);
358 used
+= wcslen(tmpEnvBuff
);
361 wcscpy( res
, tmpEnvBuff
);
362 res
+= wcslen(tmpEnvBuff
);
369 /* Don't skip past terminator (catch a single '%' at the end) */
386 TRACE("used %i of %i space\n", used
, len
);
390 TRACE("After parsing: %p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
391 debugstr_w(lpFile
), pidl
, args
);
396 static HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
399 IShellFolder
*desktop
;
401 HRESULT hr
= SHGetDesktopFolder(&desktop
);
405 hr
= desktop
->GetDisplayNameOf(pidl
, SHGDN_FORPARSING
, &strret
);
408 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
416 /*************************************************************************
417 * SHELL_ExecuteW [Internal]
420 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
421 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
423 STARTUPINFOW startup
;
424 PROCESS_INFORMATION info
;
425 UINT_PTR retval
= SE_ERR_NOASSOC
;
427 WCHAR curdir
[MAX_PATH
];
428 DWORD dwCreationFlags
;
429 const WCHAR
*lpDirectory
= NULL
;
431 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
433 /* make sure we don't fail the CreateProcess if the calling app passes in
434 * a bad working directory */
435 if (psei
->lpDirectory
&& psei
->lpDirectory
[0])
437 DWORD attr
= GetFileAttributesW(psei
->lpDirectory
);
438 if (attr
!= INVALID_FILE_ATTRIBUTES
&& attr
& FILE_ATTRIBUTE_DIRECTORY
)
439 lpDirectory
= psei
->lpDirectory
;
442 /* ShellExecute specifies the command from psei->lpDirectory
443 * if present. Not from the current dir as CreateProcess does */
445 if ((gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
446 if (!SetCurrentDirectoryW( lpDirectory
))
447 ERR("cannot set directory %s\n", debugstr_w(lpDirectory
));
449 ZeroMemory(&startup
, sizeof(STARTUPINFOW
));
450 startup
.cb
= sizeof(STARTUPINFOW
);
451 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
452 startup
.wShowWindow
= psei
->nShow
;
453 startup
.lpTitle
= (LPWSTR
)(psei
->fMask
& (SEE_MASK_HASLINKNAME
| SEE_MASK_HASTITLE
) ? psei
->lpClass
: NULL
);
455 if (psei
->fMask
& SEE_MASK_HASLINKNAME
)
456 startup
.dwFlags
|= STARTF_TITLEISLINKNAME
;
458 dwCreationFlags
= CREATE_UNICODE_ENVIRONMENT
;
460 if (psei
->fMask
& SEE_MASK_NO_CONSOLE
)
461 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
463 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, dwCreationFlags
, env
,
464 lpDirectory
, &startup
, &info
))
466 /* Give 30 seconds to the app to come up, if desired. Probably only needed
467 when starting app immediately before making a DDE connection. */
469 if (WaitForInputIdle(info
.hProcess
, 30000) == WAIT_FAILED
)
470 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
473 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
474 psei_out
->hProcess
= info
.hProcess
;
476 CloseHandle( info
.hProcess
);
477 CloseHandle( info
.hThread
);
479 else if ((retval
= GetLastError()) >= 32)
481 WARN("CreateProcess returned error %ld\n", retval
);
482 retval
= ERROR_BAD_FORMAT
;
485 TRACE("returning %lu\n", retval
);
487 psei_out
->hInstApp
= (HINSTANCE
)retval
;
490 if (!SetCurrentDirectoryW(curdir
))
491 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
497 /***********************************************************************
498 * SHELL_BuildEnvW [Internal]
500 * Build the environment for the new process, adding the specified
501 * path to the PATH variable. Returned pointer must be freed by caller.
503 static LPWSTR
SHELL_BuildEnvW( const WCHAR
*path
)
505 static const WCHAR wPath
[] = L
"PATH=";
506 WCHAR
*strings
, *new_env
;
508 int total
= wcslen(path
) + 1;
509 BOOL got_path
= FALSE
;
511 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
515 int len
= wcslen(p
) + 1;
516 if (!_wcsnicmp( p
, wPath
, 5 )) got_path
= TRUE
;
520 if (!got_path
) total
+= 5; /* we need to create PATH */
521 total
++; /* terminating null */
523 if (!(new_env
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, total
* sizeof(WCHAR
))))
525 FreeEnvironmentStringsW(strings
);
532 int len
= wcslen(p
) + 1;
533 memcpy(p2
, p
, len
* sizeof(WCHAR
));
534 if (!_wcsnicmp( p
, wPath
, 5 ))
537 wcscpy( p2
+ len
, path
);
538 p2
+= wcslen(path
) + 1;
547 p2
+= wcslen(p2
) + 1;
550 FreeEnvironmentStringsW(strings
);
555 /***********************************************************************
556 * SHELL_TryAppPathW [Internal]
558 * Helper function for SHELL_FindExecutable
559 * @param lpResult - pointer to a buffer of size MAX_PATH
560 * On entry: szName is a filename (probably without path separators).
561 * On exit: if szName found in "App Path", place full path in lpResult, and return true
563 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
571 if (env
) *env
= NULL
;
572 wcscpy(buffer
, L
"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
573 wcscat(buffer
, szName
);
574 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
577 len
= MAX_PATH
* sizeof(WCHAR
);
578 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
584 DWORD count
= sizeof(buffer
);
585 if (!RegQueryValueExW(hkApp
, L
"Path", NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
586 *env
= SHELL_BuildEnvW(buffer
);
590 if (hkApp
) RegCloseKey(hkApp
);
594 static UINT
SHELL_FindExecutableByOperation(LPCWSTR lpOperation
, LPWSTR key
, LPWSTR filetype
, LPWSTR command
, LONG commandlen
)
596 static const WCHAR wCommand
[] = L
"\\command";
598 WCHAR verb
[MAX_PATH
];
600 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, filetype
, 0, 0x02000000, &hkeyClass
))
601 return SE_ERR_NOASSOC
;
602 if (!HCR_GetDefaultVerbW(hkeyClass
, lpOperation
, verb
, sizeof(verb
) / sizeof(verb
[0])))
603 return SE_ERR_NOASSOC
;
604 RegCloseKey(hkeyClass
);
606 /* Looking for ...buffer\shell\<verb>\command */
607 wcscat(filetype
, L
"\\shell\\");
608 wcscat(filetype
, verb
);
609 wcscat(filetype
, wCommand
);
611 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, command
,
612 &commandlen
) == ERROR_SUCCESS
)
614 commandlen
/= sizeof(WCHAR
);
615 if (key
) wcscpy(key
, filetype
);
619 LONG paramlen
= sizeof(param
);
620 static const WCHAR wSpace
[] = {' ', 0};
622 /* FIXME: it seems all Windows version don't behave the same here.
623 * the doc states that this ddeexec information can be found after
625 * on Win98, it doesn't appear, but I think it does on Win2k
627 /* Get the parameters needed by the application
628 from the associated ddeexec key */
629 tmp
= strstrW(filetype
, wCommand
);
631 wcscat(filetype
, wDdeexec
);
632 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, param
,
633 ¶mlen
) == ERROR_SUCCESS
)
635 paramlen
/= sizeof(WCHAR
);
636 wcscat(command
, wSpace
);
637 wcscat(command
, param
);
638 commandlen
+= paramlen
;
642 command
[commandlen
] = '\0';
644 return 33; /* FIXME see SHELL_FindExecutable() */
647 return SE_ERR_NOASSOC
;
650 /*************************************************************************
651 * SHELL_FindExecutable [Internal]
653 * Utility for code sharing between FindExecutable and ShellExecute
655 * lpFile the name of a file
656 * lpOperation the operation on it (open)
658 * lpResult a buffer, big enough :-(, to store the command to do the
659 * operation on the file
660 * key a buffer, big enough, to get the key name to do actually the
661 * command (it'll be used afterwards for more information
664 static UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
,
665 LPWSTR lpResult
, DWORD resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
667 WCHAR
*extension
= NULL
; /* pointer to file extension */
668 WCHAR filetype
[256]; /* registry name for this filetype */
669 LONG filetypelen
= sizeof(filetype
); /* length of above */
670 WCHAR command
[1024]; /* command from registry */
671 WCHAR wBuffer
[256]; /* Used to GetProfileString */
672 UINT retval
= SE_ERR_NOASSOC
;
673 WCHAR
*tok
; /* token pointer */
674 WCHAR xlpFile
[256]; /* result of SearchPath */
675 DWORD attribs
; /* file attributes */
677 TRACE("%s\n", debugstr_w(lpFile
));
680 return ERROR_INVALID_PARAMETER
;
683 lpResult
[0] = '\0'; /* Start off with an empty return string */
684 if (key
) *key
= '\0';
686 /* trap NULL parameters on entry */
689 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
690 debugstr_w(lpFile
), debugstr_w(lpResult
));
691 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
694 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
696 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
700 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
) / sizeof(WCHAR
), xlpFile
, NULL
))
702 TRACE("SearchPathW returned non-zero\n");
704 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
707 attribs
= GetFileAttributesW(lpFile
);
708 if (attribs
!= INVALID_FILE_ATTRIBUTES
&& (attribs
& FILE_ATTRIBUTE_DIRECTORY
))
710 wcscpy(filetype
, L
"Folder");
711 filetypelen
= 6; /* strlen("Folder") */
715 /* Did we get something? Anything? */
718 TRACE("Returning SE_ERR_FNF\n");
721 /* First thing we need is the file's extension */
722 extension
= wcsrchr(xlpFile
, '.'); /* Assume last "." is the one; */
723 /* File->Run in progman uses */
725 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
727 if (extension
== NULL
|| extension
[1] == 0)
729 WARN("Returning SE_ERR_NOASSOC\n");
730 return SE_ERR_NOASSOC
;
733 /* Three places to check: */
734 /* 1. win.ini, [windows], programs (NB no leading '.') */
735 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
736 /* 3. win.ini, [extensions], extension (NB no leading '.' */
737 /* All I know of the order is that registry is checked before */
738 /* extensions; however, it'd make sense to check the programs */
739 /* section first, so that's what happens here. */
741 /* See if it's a program - if GetProfileString fails, we skip this
742 * section. Actually, if GetProfileString fails, we've probably
743 * got a lot more to worry about than running a program... */
744 if (GetProfileStringW(L
"windows", L
"programs", L
"exe pif bat cmd com", wBuffer
, sizeof(wBuffer
) / sizeof(WCHAR
)) > 0)
751 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
755 while (*p
== ' ' || *p
== '\t') p
++;
758 if (wcsicmp(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
760 wcscpy(lpResult
, xlpFile
);
761 /* Need to perhaps check that the file has a path
763 TRACE("found %s\n", debugstr_w(lpResult
));
765 /* Greater than 32 to indicate success */
772 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, filetype
,
773 &filetypelen
) == ERROR_SUCCESS
)
775 filetypelen
/= sizeof(WCHAR
);
776 if (filetypelen
== sizeof(filetype
) / sizeof(WCHAR
))
779 filetype
[filetypelen
] = '\0';
780 TRACE("File type: %s\n", debugstr_w(filetype
));
791 /* pass the operation string to SHELL_FindExecutableByOperation() */
792 filetype
[filetypelen
] = '\0';
793 retval
= SHELL_FindExecutableByOperation(lpOperation
, key
, filetype
, command
, sizeof(command
));
798 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
799 if (finishedLen
> resultLen
)
800 ERR("Argify buffer not large enough.. truncated\n");
801 /* Remove double quotation marks and command line arguments */
802 if (*lpResult
== '"')
805 while (*(p
+ 1) != '"')
814 /* Truncate on first space */
816 while (*p
!= ' ' && *p
!= '\0')
822 else /* Check win.ini */
824 /* Toss the leading dot */
826 if (GetProfileStringW(L
"extensions", extension
, L
"", command
, sizeof(command
) / sizeof(WCHAR
)) > 0)
828 if (wcslen(command
) != 0)
830 wcscpy(lpResult
, command
);
831 tok
= wcschr(lpResult
, '^'); /* should be ^.extension? */
835 wcscat(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
836 tok
= wcschr(command
, '^'); /* see above */
837 if ((tok
!= NULL
) && (wcslen(tok
) > 5))
839 wcscat(lpResult
, &tok
[5]);
842 retval
= 33; /* FIXME - see above */
847 TRACE("returning %s\n", debugstr_w(lpResult
));
851 /******************************************************************
854 * callback for the DDE connection. not really useful
856 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
857 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
858 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
860 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
861 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
865 /******************************************************************
868 * ShellExecute helper. Used to do an operation with a DDE connection
870 * Handles both the direct connection (try #1), and if it fails,
871 * launching an application and trying (#2) to connect to it
874 static unsigned dde_connect(const WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
875 const WCHAR
* lpFile
, WCHAR
*env
,
876 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
877 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
880 WCHAR
* endkey
= regkey
+ wcslen(key
);
881 WCHAR app
[256], topic
[256], ifexec
[256], res
[256];
882 LONG applen
, topiclen
, ifexeclen
;
887 HSZ hszApp
, hszTopic
;
890 unsigned ret
= SE_ERR_NOASSOC
;
891 BOOL unicode
= !(GetVersion() & 0x80000000);
894 wcscpy(endkey
, L
"\\application");
895 applen
= sizeof(app
);
896 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, app
, &applen
) != ERROR_SUCCESS
)
898 WCHAR command
[1024], fullpath
[MAX_PATH
];
899 static const WCHAR wSo
[] = L
".so";
900 DWORD sizeSo
= sizeof(wSo
) / sizeof(WCHAR
);
904 /* Get application command from start string and find filename of application */
907 wcscpy(command
, start
+ 1);
908 if ((ptr
= wcschr(command
, '"')))
910 ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
);
915 for (p
= (LPWSTR
)start
; (space
= const_cast<LPWSTR
>(strchrW(p
, ' '))); p
= space
+ 1)
917 int idx
= space
- start
;
918 memcpy(command
, start
, idx
* sizeof(WCHAR
));
920 if ((ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
)))
924 ret
= SearchPathW(NULL
, start
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
);
929 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
930 return ERROR_ACCESS_DENIED
;
934 /* Remove extensions (including .so) */
935 ptr
= app
+ wcslen(app
) - (sizeSo
- 1);
936 if (wcslen(app
) >= sizeSo
&&
940 ptr
= const_cast<LPWSTR
>(strrchrW(app
, '.'));
945 wcscpy(endkey
, L
"\\topic");
946 topiclen
= sizeof(topic
);
947 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, topic
, &topiclen
) != ERROR_SUCCESS
)
949 wcscpy(topic
, L
"System");
954 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
959 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
963 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
964 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
966 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
970 TRACE("Launching %s\n", debugstr_w(start
));
971 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
974 TRACE("Couldn't launch\n");
977 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
980 TRACE("Couldn't connect. ret=%d\n", ret
);
981 DdeUninitialize(ddeInst
);
982 SetLastError(ERROR_DDE_FAIL
);
983 return 30; /* whatever */
985 strcpyW(endkey
, L
"\\ifexec");
986 ifexeclen
= sizeof(ifexec
);
987 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
993 SHELL_ArgifyW(res
, sizeof(res
) / sizeof(WCHAR
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
994 if (resultLen
> sizeof(res
) / sizeof(WCHAR
))
995 ERR("Argify buffer not large enough, truncated\n");
996 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
998 /* It's documented in the KB 330337 that IE has a bug and returns
999 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
1002 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0, XTYP_EXECUTE
, 30000, &tid
);
1005 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
1006 char *resA
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, lenA
);
1007 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
1008 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
1009 XTYP_EXECUTE
, 10000, &tid
);
1010 HeapFree(GetProcessHeap(), 0, resA
);
1013 DdeFreeDataHandle(hDdeData
);
1015 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
1018 DdeDisconnect(hConv
);
1021 DdeUninitialize(ddeInst
);
1026 /*************************************************************************
1027 * execute_from_key [Internal]
1029 static UINT_PTR
execute_from_key(LPCWSTR key
, LPCWSTR lpFile
, WCHAR
*env
,
1030 LPCWSTR szCommandline
, LPCWSTR executable_name
,
1031 SHELL_ExecuteW32 execfunc
,
1032 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
1034 WCHAR cmd
[256], param
[1024], ddeexec
[256];
1035 DWORD cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
1036 UINT_PTR retval
= SE_ERR_NOASSOC
;
1040 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
1041 debugstr_w(szCommandline
), debugstr_w(executable_name
));
1046 /* Get the application from the registry */
1047 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, (LONG
*)&cmdlen
) == ERROR_SUCCESS
)
1049 TRACE("got cmd: %s\n", debugstr_w(cmd
));
1051 /* Is there a replace() function anywhere? */
1052 cmdlen
/= sizeof(WCHAR
);
1053 if (cmdlen
>= sizeof(cmd
) / sizeof(WCHAR
))
1054 cmdlen
= sizeof(cmd
) / sizeof(WCHAR
) - 1;
1056 SHELL_ArgifyW(param
, sizeof(param
) / sizeof(WCHAR
), cmd
, lpFile
, (LPITEMIDLIST
)psei
->lpIDList
, szCommandline
, &resultLen
);
1057 if (resultLen
> sizeof(param
) / sizeof(WCHAR
))
1058 ERR("Argify buffer not large enough, truncating\n");
1061 /* Get the parameters needed by the application
1062 from the associated ddeexec key */
1063 tmp
= const_cast<LPWSTR
>(strstrW(key
, L
"command"));
1065 wcscpy(tmp
, L
"ddeexec");
1067 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, (LONG
*)&ddeexeclen
) == ERROR_SUCCESS
)
1069 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
1070 if (!param
[0]) strcpyW(param
, executable_name
);
1071 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, (LPITEMIDLIST
)psei
->lpIDList
, execfunc
, psei
, psei_out
);
1075 TRACE("executing: %s\n", debugstr_w(param
));
1076 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
1079 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
1084 /*************************************************************************
1085 * FindExecutableA [SHELL32.@]
1087 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
1090 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
1091 WCHAR wResult
[MAX_PATH
];
1093 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
1094 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
1096 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
1097 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
1101 TRACE("returning %s\n", lpResult
);
1105 /*************************************************************************
1106 * FindExecutableW [SHELL32.@]
1108 * This function returns the executable associated with the specified file
1109 * for the default verb.
1112 * lpFile [I] The file to find the association for. This must refer to
1113 * an existing file otherwise FindExecutable fails and returns
1115 * lpResult [O] Points to a buffer into which the executable path is
1116 * copied. This parameter must not be NULL otherwise
1117 * FindExecutable() segfaults. The buffer must be of size at
1118 * least MAX_PATH characters.
1121 * A value greater than 32 on success, less than or equal to 32 otherwise.
1122 * See the SE_ERR_* constants.
1125 * On Windows XP and 2003, FindExecutable() seems to first convert the
1126 * filename into 8.3 format, thus taking into account only the first three
1127 * characters of the extension, and expects to find an association for those.
1128 * However other Windows versions behave sanely.
1130 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
1132 UINT_PTR retval
= SE_ERR_NOASSOC
;
1133 WCHAR old_dir
[1024];
1135 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
1137 lpResult
[0] = '\0'; /* Start off with an empty return string */
1139 return (HINSTANCE
)SE_ERR_FNF
;
1143 GetCurrentDirectoryW(sizeof(old_dir
) / sizeof(WCHAR
), old_dir
);
1144 SetCurrentDirectoryW(lpDirectory
);
1147 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, lpResult
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1149 TRACE("returning %s\n", debugstr_w(lpResult
));
1151 SetCurrentDirectoryW(old_dir
);
1152 return (HINSTANCE
)retval
;
1155 /* FIXME: is this already implemented somewhere else? */
1156 static HKEY
ShellExecute_GetClassKey(const SHELLEXECUTEINFOW
*sei
)
1158 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1160 DWORD type
= 0, sz
= 0;
1164 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1165 return sei
->hkeyClass
;
1167 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1168 lpClass
= sei
->lpClass
;
1171 ext
= PathFindExtensionW(sei
->lpFile
);
1172 TRACE("ext = %s\n", debugstr_w(ext
));
1176 r
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
1177 if (r
!= ERROR_SUCCESS
)
1180 r
= RegQueryValueExW(hkey
, NULL
, 0, &type
, NULL
, &sz
);
1181 if (r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1183 sz
+= sizeof (WCHAR
);
1184 cls
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sz
);
1186 RegQueryValueExW(hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1189 RegCloseKey( hkey
);
1193 TRACE("class = %s\n", debugstr_w(lpClass
));
1197 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1199 HeapFree(GetProcessHeap(), 0, cls
);
1204 static IDataObject
*shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
)
1206 LPCITEMIDLIST pidllast
= NULL
;
1207 IDataObject
*dataobj
= NULL
;
1208 IShellFolder
*shf
= NULL
;
1209 LPITEMIDLIST pidl
= NULL
;
1212 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1213 pidl
= (LPITEMIDLIST
)sei
->lpIDList
;
1216 WCHAR fullpath
[MAX_PATH
];
1220 ret
= GetFullPathNameW(sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1224 pidl
= ILCreateFromPathW(fullpath
);
1227 r
= SHBindToParent(pidl
, IID_PPV_ARG(IShellFolder
, &shf
), &pidllast
);
1231 shf
->GetUIObjectOf(NULL
, 1, &pidllast
, IID_NULL_PPV_ARG(IDataObject
, &dataobj
));
1234 if (pidl
!= sei
->lpIDList
)
1241 static HRESULT
shellex_run_context_menu_default(IShellExtInit
*obj
,
1242 LPSHELLEXECUTEINFOW sei
)
1244 IContextMenu
*cm
= NULL
;
1245 CMINVOKECOMMANDINFOEX ici
;
1252 TRACE("%p %p\n", obj
, sei
);
1254 r
= obj
->QueryInterface(IID_PPV_ARG(IContextMenu
, &cm
));
1258 hmenu
= CreateMenu();
1262 /* the number of the last menu added is returned in r */
1263 r
= cm
->QueryContextMenu(hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1267 n
= GetMenuItemCount(hmenu
);
1268 for (i
= 0; i
< n
; i
++)
1270 memset(&info
, 0, sizeof(info
));
1271 info
.cbSize
= sizeof info
;
1272 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1273 info
.dwTypeData
= string
;
1274 info
.cch
= sizeof string
;
1276 GetMenuItemInfoW(hmenu
, i
, TRUE
, &info
);
1278 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1279 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1280 if ((!sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
)) ||
1281 (sei
->lpVerb
&& !lstrcmpiW(sei
->lpVerb
, string
)))
1292 memset(&ici
, 0, sizeof ici
);
1293 ici
.cbSize
= sizeof ici
;
1294 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NOASYNC
| SEE_MASK_ASYNCOK
| SEE_MASK_FLAG_NO_UI
));
1295 ici
.nShow
= sei
->nShow
;
1296 ici
.lpVerb
= MAKEINTRESOURCEA(def
);
1297 ici
.hwnd
= sei
->hwnd
;
1298 ici
.lpParametersW
= sei
->lpParameters
;
1300 r
= cm
->InvokeCommand((LPCMINVOKECOMMANDINFO
)&ici
);
1302 TRACE("invoke command returned %08x\n", r
);
1306 DestroyMenu( hmenu
);
1312 static HRESULT
shellex_load_object_and_run(HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1314 IDataObject
*dataobj
= NULL
;
1315 IObjectWithSite
*ows
= NULL
;
1316 IShellExtInit
*obj
= NULL
;
1319 TRACE("%p %s %p\n", hkey
, debugstr_guid(guid
), sei
);
1321 r
= CoInitialize(NULL
);
1325 r
= CoCreateInstance(*guid
, NULL
, CLSCTX_INPROC_SERVER
,
1326 IID_PPV_ARG(IShellExtInit
, &obj
));
1329 ERR("failed %08x\n", r
);
1333 dataobj
= shellex_get_dataobj(sei
);
1336 ERR("failed to get data object\n");
1340 r
= obj
->Initialize(NULL
, dataobj
, hkey
);
1344 r
= obj
->QueryInterface(IID_PPV_ARG(IObjectWithSite
, &ows
));
1350 r
= shellex_run_context_menu_default(obj
, sei
);
1364 /*************************************************************************
1365 * ShellExecute_FromContextMenu [Internal]
1367 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1369 HKEY hkey
, hkeycm
= 0;
1376 TRACE("%s\n", debugstr_w(sei
->lpFile
));
1378 hkey
= ShellExecute_GetClassKey(sei
);
1380 return ERROR_FUNCTION_FAILED
;
1382 r
= RegOpenKeyW(hkey
, L
"shellex\\ContextMenuHandlers", &hkeycm
);
1383 if (r
== ERROR_SUCCESS
)
1388 r
= RegEnumKeyW(hkeycm
, i
++, szguid
, sizeof(szguid
) / sizeof(szguid
[0]));
1389 if (r
!= ERROR_SUCCESS
)
1392 hr
= CLSIDFromString(szguid
, &guid
);
1395 /* stop at the first one that succeeds in running */
1396 hr
= shellex_load_object_and_run(hkey
, &guid
, sei
);
1401 RegCloseKey(hkeycm
);
1404 if (hkey
!= sei
->hkeyClass
)
1409 static UINT_PTR
SHELL_execute_class(LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1411 WCHAR execCmd
[1024], wcmd
[1024];
1412 /* launch a document by fileclass like 'WordPad.Document.1' */
1413 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1414 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1415 ULONG cmask
= (psei
->fMask
& SEE_MASK_CLASSALL
);
1419 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1420 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1422 execCmd
, sizeof(execCmd
));
1424 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1425 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1428 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
) / sizeof(WCHAR
), execCmd
, wszApplicationName
, (LPITEMIDLIST
)psei
->lpIDList
, NULL
, &resultLen
);
1429 if (!done
&& wszApplicationName
[0])
1431 strcatW(wcmd
, L
" ");
1432 strcatW(wcmd
, wszApplicationName
);
1434 if (resultLen
> sizeof(wcmd
) / sizeof(WCHAR
))
1435 ERR("Argify buffer not large enough... truncating\n");
1436 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1439 static BOOL
SHELL_translate_idlist(LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1441 static const WCHAR wExplorer
[] = L
"explorer.exe";
1442 WCHAR buffer
[MAX_PATH
];
1443 BOOL appKnownSingular
= FALSE
;
1445 /* last chance to translate IDList: now also allow CLSID paths */
1446 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST
)sei
->lpIDList
, buffer
, sizeof(buffer
)))) {
1447 if (buffer
[0] == ':' && buffer
[1] == ':') {
1448 /* open shell folder for the specified class GUID */
1449 if (strlenW(buffer
) + 1 > parametersLen
)
1450 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1451 lstrlenW(buffer
) + 1, parametersLen
);
1452 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1453 if (strlenW(wExplorer
) > dwApplicationNameLen
)
1454 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1455 lstrlenW(wExplorer
) + 1, dwApplicationNameLen
);
1456 lstrcpynW(wszApplicationName
, wExplorer
, dwApplicationNameLen
);
1457 appKnownSingular
= TRUE
;
1459 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1461 WCHAR target
[MAX_PATH
];
1464 /* Check if we're executing a directory and if so use the
1465 handler for the Folder class */
1466 strcpyW(target
, buffer
);
1467 attribs
= GetFileAttributesW(buffer
);
1468 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1469 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1470 HCR_GetExecuteCommandW(0, L
"Folder",
1472 buffer
, sizeof(buffer
))) {
1473 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1474 buffer
, target
, (LPITEMIDLIST
)sei
->lpIDList
, NULL
, &resultLen
);
1475 if (resultLen
> dwApplicationNameLen
)
1476 ERR("Argify buffer not large enough... truncating\n");
1477 appKnownSingular
= FALSE
;
1479 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1482 return appKnownSingular
;
1485 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
)
1489 WCHAR
*wszQuotedCmd
;
1491 /* Length of quotes plus length of command plus NULL terminator */
1492 len
= 2 + lstrlenW(wcmd
) + 1;
1493 if (wszParameters
[0])
1495 /* Length of space plus length of parameters */
1496 len
+= 1 + lstrlenW(wszParameters
);
1498 wszQuotedCmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1499 /* Must quote to handle case where cmd contains spaces,
1500 * else security hole if malicious user creates executable file "C:\\Program"
1502 strcpyW(wszQuotedCmd
, L
"\"");
1503 strcatW(wszQuotedCmd
, wcmd
);
1504 strcatW(wszQuotedCmd
, L
"\"");
1505 if (wszParameters
[0])
1507 strcatW(wszQuotedCmd
, L
" ");
1508 strcatW(wszQuotedCmd
, wszParameters
);
1511 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(lpstrProtocol
));
1514 retval
= execute_from_key(lpstrProtocol
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1516 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1517 HeapFree(GetProcessHeap(), 0, wszQuotedCmd
);
1521 static UINT_PTR
SHELL_execute_url(LPCWSTR lpFile
, LPCWSTR wFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1523 static const WCHAR wShell
[] = L
"\\shell\\";
1524 static const WCHAR wCommand
[] = L
"\\command";
1526 WCHAR
*lpstrProtocol
;
1531 lpstrRes
= strchrW(lpFile
, ':');
1533 iSize
= lpstrRes
- lpFile
;
1535 iSize
= strlenW(lpFile
);
1537 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1538 /* Looking for ...protocol\shell\lpOperation\command */
1539 len
= iSize
+ lstrlenW(wShell
) + lstrlenW(wCommand
) + 1;
1541 len
+= lstrlenW(psei
->lpVerb
);
1543 len
+= lstrlenW(wszOpen
);
1544 lpstrProtocol
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1545 memcpy(lpstrProtocol
, lpFile
, iSize
* sizeof(WCHAR
));
1546 lpstrProtocol
[iSize
] = '\0';
1547 strcatW(lpstrProtocol
, wShell
);
1548 strcatW(lpstrProtocol
, psei
->lpVerb
? psei
->lpVerb
: wszOpen
);
1549 strcatW(lpstrProtocol
, wCommand
);
1551 /* Remove File Protocol from lpFile */
1552 /* In the case file://path/file */
1553 if (!strncmpiW(lpFile
, wFile
, iSize
))
1556 while (*lpFile
== ':') lpFile
++;
1558 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1559 wcmd
, execfunc
, psei
, psei_out
);
1560 HeapFree(GetProcessHeap(), 0, lpstrProtocol
);
1564 void do_error_dialog(UINT_PTR retval
, HWND hwnd
, WCHAR
* filename
)
1567 DWORD_PTR msgArguments
[3] = { (DWORD_PTR
)filename
, 0, 0 };
1570 error_code
= GetLastError();
1572 if (retval
== SE_ERR_NOASSOC
)
1573 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, sizeof(msg
) / sizeof(WCHAR
));
1575 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ARGUMENT_ARRAY
,
1580 sizeof(msg
) / sizeof(WCHAR
),
1581 (va_list*)msgArguments
);
1583 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1586 /*************************************************************************
1587 * SHELL_execute [Internal]
1589 BOOL
SHELL_execute(LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1591 static const DWORD unsupportedFlags
=
1592 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1593 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1594 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1596 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1597 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
;
1598 DWORD dwApplicationNameLen
= MAX_PATH
+ 2;
1599 DWORD parametersLen
= sizeof(parametersBuffer
) / sizeof(WCHAR
);
1600 DWORD dirLen
= sizeof(dirBuffer
) / sizeof(WCHAR
);
1601 DWORD wcmdLen
= sizeof(wcmdBuffer
) / sizeof(WCHAR
);
1603 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1604 WCHAR wfileName
[MAX_PATH
];
1606 WCHAR lpstrProtocol
[256];
1608 UINT_PTR retval
= SE_ERR_NOASSOC
;
1609 BOOL appKnownSingular
= FALSE
;
1611 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1614 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1615 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1616 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1617 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1618 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1619 debugstr_w(sei_tmp
.lpClass
) : "not used");
1621 sei
->hProcess
= NULL
;
1623 /* make copies of all path/command strings */
1624 if (!sei_tmp
.lpFile
)
1626 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1627 *wszApplicationName
= '\0';
1629 else if (*sei_tmp
.lpFile
== '\"')
1631 DWORD l
= strlenW(sei_tmp
.lpFile
+ 1);
1632 if(l
>= dwApplicationNameLen
)
1633 dwApplicationNameLen
= l
+ 1;
1635 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1636 memcpy(wszApplicationName
, sei_tmp
.lpFile
+ 1, (l
+ 1)*sizeof(WCHAR
));
1638 if (wszApplicationName
[l
-1] == L
'\"')
1639 wszApplicationName
[l
-1] = L
'\0';
1640 appKnownSingular
= TRUE
;
1642 TRACE("wszApplicationName=%s\n", debugstr_w(wszApplicationName
));
1646 DWORD l
= strlenW(sei_tmp
.lpFile
) + 1;
1647 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+ 1;
1648 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1649 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
* sizeof(WCHAR
));
1652 wszParameters
= parametersBuffer
;
1653 if (sei_tmp
.lpParameters
)
1655 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1656 if (len
> parametersLen
)
1658 wszParameters
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1659 parametersLen
= len
;
1661 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1664 *wszParameters
= L
'\0';
1667 if (sei_tmp
.lpDirectory
)
1669 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1672 wszDir
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1675 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1680 /* adjust string pointers to point to the new buffers */
1681 sei_tmp
.lpFile
= wszApplicationName
;
1682 sei_tmp
.lpParameters
= wszParameters
;
1683 sei_tmp
.lpDirectory
= wszDir
;
1685 if (sei_tmp
.fMask
& unsupportedFlags
)
1687 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1690 /* process the IDList */
1691 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1693 IShellExecuteHookW
* pSEH
;
1695 HRESULT hr
= SHBindToParent((LPCITEMIDLIST
)sei_tmp
.lpIDList
, IID_PPV_ARG(IShellExecuteHookW
, &pSEH
), NULL
);
1699 hr
= pSEH
->Execute(&sei_tmp
);
1705 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1706 if (wszParameters
!= parametersBuffer
)
1707 HeapFree(GetProcessHeap(), 0, wszParameters
);
1708 if (wszDir
!= dirBuffer
)
1709 HeapFree(GetProcessHeap(), 0, wszDir
);
1714 SHGetPathFromIDListW((LPCITEMIDLIST
)sei_tmp
.lpIDList
, wszApplicationName
);
1715 appKnownSingular
= TRUE
;
1716 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1719 if (ERROR_SUCCESS
== ShellExecute_FromContextMenu(&sei_tmp
))
1721 sei
->hInstApp
= (HINSTANCE
) 33;
1722 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1723 if (wszParameters
!= parametersBuffer
)
1724 HeapFree(GetProcessHeap(), 0, wszParameters
);
1725 if (wszDir
!= dirBuffer
)
1726 HeapFree(GetProcessHeap(), 0, wszDir
);
1730 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1732 retval
= SHELL_execute_class(wszApplicationName
, &sei_tmp
, sei
, execfunc
);
1733 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1740 Info
.pcszFile
= wszApplicationName
;
1741 Info
.pcszClass
= NULL
;
1742 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1744 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1745 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1747 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1748 if (wszParameters
!= parametersBuffer
)
1749 HeapFree(GetProcessHeap(), 0, wszParameters
);
1750 if (wszDir
!= dirBuffer
)
1751 HeapFree(GetProcessHeap(), 0, wszDir
);
1755 /* Has the IDList not yet been translated? */
1756 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1758 appKnownSingular
= SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1761 dwApplicationNameLen
);
1764 /* expand environment strings */
1765 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1769 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1771 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1772 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1773 dwApplicationNameLen
= len
+ 1;
1774 wszApplicationName
= buf
;
1775 /* appKnownSingular unmodified */
1777 sei_tmp
.lpFile
= wszApplicationName
;
1780 if (*sei_tmp
.lpParameters
)
1782 len
= ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, NULL
, 0);
1787 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1788 ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buf
, len
);
1789 if (wszParameters
!= parametersBuffer
)
1790 HeapFree(GetProcessHeap(), 0, wszParameters
);
1791 wszParameters
= buf
;
1792 parametersLen
= len
;
1793 sei_tmp
.lpParameters
= wszParameters
;
1797 if (*sei_tmp
.lpDirectory
)
1799 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1804 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1805 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1806 if (wszDir
!= dirBuffer
)
1807 HeapFree(GetProcessHeap(), 0, wszDir
);
1809 sei_tmp
.lpDirectory
= wszDir
;
1813 /* Else, try to execute the filename */
1814 TRACE("execute: %s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1816 /* separate out command line arguments from executable file name */
1817 if (!*sei_tmp
.lpParameters
&& !appKnownSingular
)
1819 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1820 if (sei_tmp
.lpFile
[0] == L
'"')
1822 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1823 LPWSTR dst
= wfileName
;
1826 /* copy the unquoted executable path to 'wfileName' */
1827 while(*src
&& *src
!= L
'"')
1836 while(isspace(*src
))
1842 /* copy the parameter string to 'wszParameters' */
1843 strcpyW(wszParameters
, src
);
1845 /* terminate previous command string after the quote character */
1850 /* If the executable name is not quoted, we have to use this search loop here,
1851 that in CreateProcess() is not sufficient because it does not handle shell links. */
1852 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1855 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1856 for(s
= beg
; (space
= const_cast<LPWSTR
>(strchrW(s
, L
' '))); s
= space
+ 1)
1858 int idx
= space
- sei_tmp
.lpFile
;
1859 memcpy(buffer
, sei_tmp
.lpFile
, idx
* sizeof(WCHAR
));
1862 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1863 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
) / sizeof(xlpFile
[0]), xlpFile
, NULL
))
1865 /* separate out command from parameter string */
1866 LPCWSTR p
= space
+ 1;
1871 strcpyW(wszParameters
, p
);
1878 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1882 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1887 len
= lstrlenW(wszApplicationName
) + 3;
1888 if (sei_tmp
.lpParameters
[0])
1889 len
+= 1 + lstrlenW(wszParameters
);
1892 wcmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1895 swprintf(wcmd
, L
"\"%s\"", wszApplicationName
);
1896 if (sei_tmp
.lpParameters
[0])
1898 strcatW(wcmd
, L
" ");
1899 strcatW(wcmd
, wszParameters
);
1902 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1905 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1906 if (wszParameters
!= parametersBuffer
)
1907 HeapFree(GetProcessHeap(), 0, wszParameters
);
1908 if (wszDir
!= dirBuffer
)
1909 HeapFree(GetProcessHeap(), 0, wszDir
);
1910 if (wcmd
!= wcmdBuffer
)
1911 HeapFree(GetProcessHeap(), 0, wcmd
);
1915 /* Else, try to find the executable */
1917 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, lpstrProtocol
, &env
, (LPITEMIDLIST
)sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1918 if (retval
> 32) /* Found */
1920 retval
= SHELL_quote_and_execute(wcmd
, wszParameters
, lpstrProtocol
,
1921 wszApplicationName
, env
, &sei_tmp
,
1923 HeapFree(GetProcessHeap(), 0, env
);
1925 else if (PathIsDirectoryW(lpFile
))
1927 WCHAR wExec
[MAX_PATH
];
1928 WCHAR
* lpQuotedFile
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (strlenW(lpFile
) + 3));
1932 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, L
"explorer",
1933 wszOpen
, wExec
, MAX_PATH
,
1934 NULL
, &env
, NULL
, NULL
);
1937 swprintf(lpQuotedFile
, L
"\"%s\"", lpFile
);
1938 retval
= SHELL_quote_and_execute(wExec
, lpQuotedFile
,
1940 wszApplicationName
, env
,
1941 &sei_tmp
, sei
, execfunc
);
1942 HeapFree(GetProcessHeap(), 0, env
);
1944 HeapFree(GetProcessHeap(), 0, lpQuotedFile
);
1947 retval
= 0; /* Out of memory */
1949 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1951 retval
= SHELL_execute_url(lpFile
, L
"file", wcmd
, &sei_tmp
, sei
, execfunc
);
1953 /* Check if file specified is in the form www.??????.*** */
1954 else if (!strncmpiW(lpFile
, L
"www", 3))
1956 /* if so, append lpFile http:// and call ShellExecute */
1957 WCHAR lpstrTmpFile
[256];
1958 strcpyW(lpstrTmpFile
, L
"http://");
1959 strcatW(lpstrTmpFile
, lpFile
);
1960 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1963 TRACE("retval %lu\n", retval
);
1965 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1972 Info
.pcszFile
= wszApplicationName
;
1973 Info
.pcszClass
= NULL
;
1974 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1976 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1977 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1980 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1981 if (wszParameters
!= parametersBuffer
)
1982 HeapFree(GetProcessHeap(), 0, wszParameters
);
1983 if (wszDir
!= dirBuffer
)
1984 HeapFree(GetProcessHeap(), 0, wszDir
);
1985 if (wcmd
!= wcmdBuffer
)
1986 HeapFree(GetProcessHeap(), 0, wcmd
);
1988 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1993 /*************************************************************************
1994 * ShellExecuteA [SHELL32.290]
1996 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
1997 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1999 SHELLEXECUTEINFOA sei
;
2001 TRACE("%p,%s,%s,%s,%s,%d\n",
2002 hWnd
, debugstr_a(lpOperation
), debugstr_a(lpFile
),
2003 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
2005 sei
.cbSize
= sizeof(sei
);
2006 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2008 sei
.lpVerb
= lpOperation
;
2009 sei
.lpFile
= lpFile
;
2010 sei
.lpParameters
= lpParameters
;
2011 sei
.lpDirectory
= lpDirectory
;
2012 sei
.nShow
= iShowCmd
;
2019 ShellExecuteExA(&sei
);
2020 return sei
.hInstApp
;
2023 /*************************************************************************
2024 * ShellExecuteExA [SHELL32.292]
2030 ShellExecuteExA(LPSHELLEXECUTEINFOA sei
)
2032 SHELLEXECUTEINFOW seiW
;
2034 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
2038 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
2041 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
2044 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
2046 if (sei
->lpParameters
)
2047 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
2049 if (sei
->lpDirectory
)
2050 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
2052 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
2053 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
2055 seiW
.lpClass
= NULL
;
2057 ret
= SHELL_execute(&seiW
, SHELL_ExecuteW
);
2059 sei
->hInstApp
= seiW
.hInstApp
;
2061 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
2062 sei
->hProcess
= seiW
.hProcess
;
2066 SHFree(wParameters
);
2073 /*************************************************************************
2074 * ShellExecuteExW [SHELL32.293]
2080 ShellExecuteExW(LPSHELLEXECUTEINFOW sei
)
2082 return SHELL_execute(sei
, SHELL_ExecuteW
);
2085 /*************************************************************************
2086 * ShellExecuteW [SHELL32.294]
2088 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
2089 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
2091 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
2092 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
2094 SHELLEXECUTEINFOW sei
;
2097 sei
.cbSize
= sizeof(sei
);
2098 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2100 sei
.lpVerb
= lpOperation
;
2101 sei
.lpFile
= lpFile
;
2102 sei
.lpParameters
= lpParameters
;
2103 sei
.lpDirectory
= lpDirectory
;
2104 sei
.nShow
= nShowCmd
;
2111 SHELL_execute(&sei
, SHELL_ExecuteW
);
2112 return sei
.hInstApp
;
2115 /*************************************************************************
2116 * WOWShellExecute [SHELL32.@]
2118 * FIXME: the callback function most likely doesn't work the same way on Windows.
2120 EXTERN_C HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
2121 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
2123 SHELLEXECUTEINFOW seiW
;
2124 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
2125 HANDLE hProcess
= 0;
2127 seiW
.lpVerb
= lpOperation
? __SHCloneStrAtoW(&wVerb
, lpOperation
) : NULL
;
2128 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
2129 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
2130 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
2132 seiW
.cbSize
= sizeof(seiW
);
2135 seiW
.nShow
= iShowCmd
;
2140 seiW
.hProcess
= hProcess
;
2142 SHELL_execute(&seiW
, (SHELL_ExecuteW32
)callback
);
2146 SHFree(wParameters
);
2148 return seiW
.hInstApp
;
2151 /*************************************************************************
2152 * OpenAs_RunDLLA [SHELL32.@]
2154 EXTERN_C
void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2156 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2159 /*************************************************************************
2160 * OpenAs_RunDLLW [SHELL32.@]
2162 EXTERN_C
void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2164 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);