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
,
1232 IID_IDataObject
, NULL
, (LPVOID
*) &dataobj
);
1235 if (pidl
!= sei
->lpIDList
)
1242 static HRESULT
shellex_run_context_menu_default(IShellExtInit
*obj
,
1243 LPSHELLEXECUTEINFOW sei
)
1245 IContextMenu
*cm
= NULL
;
1246 CMINVOKECOMMANDINFOEX ici
;
1253 TRACE("%p %p\n", obj
, sei
);
1255 r
= obj
->QueryInterface(IID_IContextMenu
, (LPVOID
*) &cm
);
1259 hmenu
= CreateMenu();
1263 /* the number of the last menu added is returned in r */
1264 r
= cm
->QueryContextMenu(hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1268 n
= GetMenuItemCount(hmenu
);
1269 for (i
= 0; i
< n
; i
++)
1271 memset(&info
, 0, sizeof(info
));
1272 info
.cbSize
= sizeof info
;
1273 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1274 info
.dwTypeData
= string
;
1275 info
.cch
= sizeof string
;
1277 GetMenuItemInfoW(hmenu
, i
, TRUE
, &info
);
1279 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1280 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1281 if ((!sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
)) ||
1282 (sei
->lpVerb
&& !lstrcmpiW(sei
->lpVerb
, string
)))
1293 memset(&ici
, 0, sizeof ici
);
1294 ici
.cbSize
= sizeof ici
;
1295 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NOASYNC
| SEE_MASK_ASYNCOK
| SEE_MASK_FLAG_NO_UI
));
1296 ici
.nShow
= sei
->nShow
;
1297 ici
.lpVerb
= MAKEINTRESOURCEA(def
);
1298 ici
.hwnd
= sei
->hwnd
;
1299 ici
.lpParametersW
= sei
->lpParameters
;
1301 r
= cm
->InvokeCommand((LPCMINVOKECOMMANDINFO
)&ici
);
1303 TRACE("invoke command returned %08x\n", r
);
1307 DestroyMenu( hmenu
);
1313 static HRESULT
shellex_load_object_and_run(HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1315 IDataObject
*dataobj
= NULL
;
1316 IObjectWithSite
*ows
= NULL
;
1317 IShellExtInit
*obj
= NULL
;
1320 TRACE("%p %s %p\n", hkey
, debugstr_guid(guid
), sei
);
1322 r
= CoInitialize(NULL
);
1326 r
= CoCreateInstance(*guid
, NULL
, CLSCTX_INPROC_SERVER
,
1327 IID_PPV_ARG(IShellExtInit
, &obj
));
1330 ERR("failed %08x\n", r
);
1334 dataobj
= shellex_get_dataobj(sei
);
1337 ERR("failed to get data object\n");
1341 r
= obj
->Initialize(NULL
, dataobj
, hkey
);
1345 r
= obj
->QueryInterface(IID_IObjectWithSite
, (LPVOID
*) &ows
);
1351 r
= shellex_run_context_menu_default(obj
, sei
);
1365 /*************************************************************************
1366 * ShellExecute_FromContextMenu [Internal]
1368 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1370 HKEY hkey
, hkeycm
= 0;
1377 TRACE("%s\n", debugstr_w(sei
->lpFile
));
1379 hkey
= ShellExecute_GetClassKey(sei
);
1381 return ERROR_FUNCTION_FAILED
;
1383 r
= RegOpenKeyW(hkey
, L
"shellex\\ContextMenuHandlers", &hkeycm
);
1384 if (r
== ERROR_SUCCESS
)
1389 r
= RegEnumKeyW(hkeycm
, i
++, szguid
, sizeof(szguid
) / sizeof(szguid
[0]));
1390 if (r
!= ERROR_SUCCESS
)
1393 hr
= CLSIDFromString(szguid
, &guid
);
1396 /* stop at the first one that succeeds in running */
1397 hr
= shellex_load_object_and_run(hkey
, &guid
, sei
);
1402 RegCloseKey(hkeycm
);
1405 if (hkey
!= sei
->hkeyClass
)
1410 static UINT_PTR
SHELL_execute_class(LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1412 WCHAR execCmd
[1024], wcmd
[1024];
1413 /* launch a document by fileclass like 'WordPad.Document.1' */
1414 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1415 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1416 ULONG cmask
= (psei
->fMask
& SEE_MASK_CLASSALL
);
1420 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1421 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1423 execCmd
, sizeof(execCmd
));
1425 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1426 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1429 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
) / sizeof(WCHAR
), execCmd
, wszApplicationName
, (LPITEMIDLIST
)psei
->lpIDList
, NULL
, &resultLen
);
1430 if (!done
&& wszApplicationName
[0])
1432 strcatW(wcmd
, L
" ");
1433 strcatW(wcmd
, wszApplicationName
);
1435 if (resultLen
> sizeof(wcmd
) / sizeof(WCHAR
))
1436 ERR("Argify buffer not large enough... truncating\n");
1437 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1440 static BOOL
SHELL_translate_idlist(LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1442 static const WCHAR wExplorer
[] = L
"explorer.exe";
1443 WCHAR buffer
[MAX_PATH
];
1444 BOOL appKnownSingular
= FALSE
;
1446 /* last chance to translate IDList: now also allow CLSID paths */
1447 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST
)sei
->lpIDList
, buffer
, sizeof(buffer
)))) {
1448 if (buffer
[0] == ':' && buffer
[1] == ':') {
1449 /* open shell folder for the specified class GUID */
1450 if (strlenW(buffer
) + 1 > parametersLen
)
1451 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1452 lstrlenW(buffer
) + 1, parametersLen
);
1453 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1454 if (strlenW(wExplorer
) > dwApplicationNameLen
)
1455 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1456 lstrlenW(wExplorer
) + 1, dwApplicationNameLen
);
1457 lstrcpynW(wszApplicationName
, wExplorer
, dwApplicationNameLen
);
1458 appKnownSingular
= TRUE
;
1460 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1462 WCHAR target
[MAX_PATH
];
1465 /* Check if we're executing a directory and if so use the
1466 handler for the Folder class */
1467 strcpyW(target
, buffer
);
1468 attribs
= GetFileAttributesW(buffer
);
1469 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1470 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1471 HCR_GetExecuteCommandW(0, L
"Folder",
1473 buffer
, sizeof(buffer
))) {
1474 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1475 buffer
, target
, (LPITEMIDLIST
)sei
->lpIDList
, NULL
, &resultLen
);
1476 if (resultLen
> dwApplicationNameLen
)
1477 ERR("Argify buffer not large enough... truncating\n");
1478 appKnownSingular
= FALSE
;
1480 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1483 return appKnownSingular
;
1486 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
)
1490 WCHAR
*wszQuotedCmd
;
1492 /* Length of quotes plus length of command plus NULL terminator */
1493 len
= 2 + lstrlenW(wcmd
) + 1;
1494 if (wszParameters
[0])
1496 /* Length of space plus length of parameters */
1497 len
+= 1 + lstrlenW(wszParameters
);
1499 wszQuotedCmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1500 /* Must quote to handle case where cmd contains spaces,
1501 * else security hole if malicious user creates executable file "C:\\Program"
1503 strcpyW(wszQuotedCmd
, L
"\"");
1504 strcatW(wszQuotedCmd
, wcmd
);
1505 strcatW(wszQuotedCmd
, L
"\"");
1506 if (wszParameters
[0])
1508 strcatW(wszQuotedCmd
, L
" ");
1509 strcatW(wszQuotedCmd
, wszParameters
);
1512 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(lpstrProtocol
));
1515 retval
= execute_from_key(lpstrProtocol
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1517 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1518 HeapFree(GetProcessHeap(), 0, wszQuotedCmd
);
1522 static UINT_PTR
SHELL_execute_url(LPCWSTR lpFile
, LPCWSTR wFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1524 static const WCHAR wShell
[] = L
"\\shell\\";
1525 static const WCHAR wCommand
[] = L
"\\command";
1527 WCHAR
*lpstrProtocol
;
1532 lpstrRes
= strchrW(lpFile
, ':');
1534 iSize
= lpstrRes
- lpFile
;
1536 iSize
= strlenW(lpFile
);
1538 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1539 /* Looking for ...protocol\shell\lpOperation\command */
1540 len
= iSize
+ lstrlenW(wShell
) + lstrlenW(wCommand
) + 1;
1542 len
+= lstrlenW(psei
->lpVerb
);
1544 len
+= lstrlenW(wszOpen
);
1545 lpstrProtocol
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1546 memcpy(lpstrProtocol
, lpFile
, iSize
* sizeof(WCHAR
));
1547 lpstrProtocol
[iSize
] = '\0';
1548 strcatW(lpstrProtocol
, wShell
);
1549 strcatW(lpstrProtocol
, psei
->lpVerb
? psei
->lpVerb
: wszOpen
);
1550 strcatW(lpstrProtocol
, wCommand
);
1552 /* Remove File Protocol from lpFile */
1553 /* In the case file://path/file */
1554 if (!strncmpiW(lpFile
, wFile
, iSize
))
1557 while (*lpFile
== ':') lpFile
++;
1559 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1560 wcmd
, execfunc
, psei
, psei_out
);
1561 HeapFree(GetProcessHeap(), 0, lpstrProtocol
);
1565 void do_error_dialog(UINT_PTR retval
, HWND hwnd
, WCHAR
* filename
)
1568 DWORD_PTR msgArguments
[3] = { (DWORD_PTR
)filename
, 0, 0 };
1571 error_code
= GetLastError();
1573 if (retval
== SE_ERR_NOASSOC
)
1574 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, sizeof(msg
) / sizeof(WCHAR
));
1576 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ARGUMENT_ARRAY
,
1581 sizeof(msg
) / sizeof(WCHAR
),
1582 (va_list*)msgArguments
);
1584 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1587 /*************************************************************************
1588 * SHELL_execute [Internal]
1590 BOOL
SHELL_execute(LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1592 static const DWORD unsupportedFlags
=
1593 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1594 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1595 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1597 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1598 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
;
1599 DWORD dwApplicationNameLen
= MAX_PATH
+ 2;
1600 DWORD parametersLen
= sizeof(parametersBuffer
) / sizeof(WCHAR
);
1601 DWORD dirLen
= sizeof(dirBuffer
) / sizeof(WCHAR
);
1602 DWORD wcmdLen
= sizeof(wcmdBuffer
) / sizeof(WCHAR
);
1604 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1605 WCHAR wfileName
[MAX_PATH
];
1607 WCHAR lpstrProtocol
[256];
1609 UINT_PTR retval
= SE_ERR_NOASSOC
;
1610 BOOL appKnownSingular
= FALSE
;
1612 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1615 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1616 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1617 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1618 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1619 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1620 debugstr_w(sei_tmp
.lpClass
) : "not used");
1622 sei
->hProcess
= NULL
;
1624 /* make copies of all path/command strings */
1625 if (!sei_tmp
.lpFile
)
1627 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1628 *wszApplicationName
= '\0';
1630 else if (*sei_tmp
.lpFile
== '\"')
1632 DWORD l
= strlenW(sei_tmp
.lpFile
+ 1);
1633 if(l
>= dwApplicationNameLen
)
1634 dwApplicationNameLen
= l
+ 1;
1636 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1637 memcpy(wszApplicationName
, sei_tmp
.lpFile
+ 1, (l
+ 1)*sizeof(WCHAR
));
1639 if (wszApplicationName
[l
-1] == L
'\"')
1640 wszApplicationName
[l
-1] = L
'\0';
1641 appKnownSingular
= TRUE
;
1643 TRACE("wszApplicationName=%s\n", debugstr_w(wszApplicationName
));
1647 DWORD l
= strlenW(sei_tmp
.lpFile
) + 1;
1648 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+ 1;
1649 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1650 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
* sizeof(WCHAR
));
1653 wszParameters
= parametersBuffer
;
1654 if (sei_tmp
.lpParameters
)
1656 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1657 if (len
> parametersLen
)
1659 wszParameters
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1660 parametersLen
= len
;
1662 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1665 *wszParameters
= L
'\0';
1668 if (sei_tmp
.lpDirectory
)
1670 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1673 wszDir
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1676 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1681 /* adjust string pointers to point to the new buffers */
1682 sei_tmp
.lpFile
= wszApplicationName
;
1683 sei_tmp
.lpParameters
= wszParameters
;
1684 sei_tmp
.lpDirectory
= wszDir
;
1686 if (sei_tmp
.fMask
& unsupportedFlags
)
1688 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1691 /* process the IDList */
1692 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1694 IShellExecuteHookW
* pSEH
;
1696 HRESULT hr
= SHBindToParent((LPCITEMIDLIST
)sei_tmp
.lpIDList
, IID_PPV_ARG(IShellExecuteHookW
, &pSEH
), NULL
);
1700 hr
= pSEH
->Execute(&sei_tmp
);
1706 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1707 if (wszParameters
!= parametersBuffer
)
1708 HeapFree(GetProcessHeap(), 0, wszParameters
);
1709 if (wszDir
!= dirBuffer
)
1710 HeapFree(GetProcessHeap(), 0, wszDir
);
1715 SHGetPathFromIDListW((LPCITEMIDLIST
)sei_tmp
.lpIDList
, wszApplicationName
);
1716 appKnownSingular
= TRUE
;
1717 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1720 if (ERROR_SUCCESS
== ShellExecute_FromContextMenu(&sei_tmp
))
1722 sei
->hInstApp
= (HINSTANCE
) 33;
1723 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1724 if (wszParameters
!= parametersBuffer
)
1725 HeapFree(GetProcessHeap(), 0, wszParameters
);
1726 if (wszDir
!= dirBuffer
)
1727 HeapFree(GetProcessHeap(), 0, wszDir
);
1731 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1733 retval
= SHELL_execute_class(wszApplicationName
, &sei_tmp
, sei
, execfunc
);
1734 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1741 Info
.pcszFile
= wszApplicationName
;
1742 Info
.pcszClass
= NULL
;
1743 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1745 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1746 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1748 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1749 if (wszParameters
!= parametersBuffer
)
1750 HeapFree(GetProcessHeap(), 0, wszParameters
);
1751 if (wszDir
!= dirBuffer
)
1752 HeapFree(GetProcessHeap(), 0, wszDir
);
1756 /* Has the IDList not yet been translated? */
1757 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1759 appKnownSingular
= SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1762 dwApplicationNameLen
);
1765 /* expand environment strings */
1766 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1770 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1772 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1773 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1774 dwApplicationNameLen
= len
+ 1;
1775 wszApplicationName
= buf
;
1776 /* appKnownSingular unmodified */
1778 sei_tmp
.lpFile
= wszApplicationName
;
1781 if (*sei_tmp
.lpParameters
)
1783 len
= ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, NULL
, 0);
1788 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1789 ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buf
, len
);
1790 if (wszParameters
!= parametersBuffer
)
1791 HeapFree(GetProcessHeap(), 0, wszParameters
);
1792 wszParameters
= buf
;
1793 parametersLen
= len
;
1794 sei_tmp
.lpParameters
= wszParameters
;
1798 if (*sei_tmp
.lpDirectory
)
1800 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1805 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1806 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1807 if (wszDir
!= dirBuffer
)
1808 HeapFree(GetProcessHeap(), 0, wszDir
);
1810 sei_tmp
.lpDirectory
= wszDir
;
1814 /* Else, try to execute the filename */
1815 TRACE("execute: %s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1817 /* separate out command line arguments from executable file name */
1818 if (!*sei_tmp
.lpParameters
&& !appKnownSingular
)
1820 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1821 if (sei_tmp
.lpFile
[0] == L
'"')
1823 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1824 LPWSTR dst
= wfileName
;
1827 /* copy the unquoted executable path to 'wfileName' */
1828 while(*src
&& *src
!= L
'"')
1837 while(isspace(*src
))
1843 /* copy the parameter string to 'wszParameters' */
1844 strcpyW(wszParameters
, src
);
1846 /* terminate previous command string after the quote character */
1851 /* If the executable name is not quoted, we have to use this search loop here,
1852 that in CreateProcess() is not sufficient because it does not handle shell links. */
1853 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1856 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1857 for(s
= beg
; (space
= const_cast<LPWSTR
>(strchrW(s
, L
' '))); s
= space
+ 1)
1859 int idx
= space
- sei_tmp
.lpFile
;
1860 memcpy(buffer
, sei_tmp
.lpFile
, idx
* sizeof(WCHAR
));
1863 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1864 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
) / sizeof(xlpFile
[0]), xlpFile
, NULL
))
1866 /* separate out command from parameter string */
1867 LPCWSTR p
= space
+ 1;
1872 strcpyW(wszParameters
, p
);
1879 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1883 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1888 len
= lstrlenW(wszApplicationName
) + 3;
1889 if (sei_tmp
.lpParameters
[0])
1890 len
+= 1 + lstrlenW(wszParameters
);
1893 wcmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1896 swprintf(wcmd
, L
"\"%s\"", wszApplicationName
);
1897 if (sei_tmp
.lpParameters
[0])
1899 strcatW(wcmd
, L
" ");
1900 strcatW(wcmd
, wszParameters
);
1903 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1906 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1907 if (wszParameters
!= parametersBuffer
)
1908 HeapFree(GetProcessHeap(), 0, wszParameters
);
1909 if (wszDir
!= dirBuffer
)
1910 HeapFree(GetProcessHeap(), 0, wszDir
);
1911 if (wcmd
!= wcmdBuffer
)
1912 HeapFree(GetProcessHeap(), 0, wcmd
);
1916 /* Else, try to find the executable */
1918 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, lpstrProtocol
, &env
, (LPITEMIDLIST
)sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1919 if (retval
> 32) /* Found */
1921 retval
= SHELL_quote_and_execute(wcmd
, wszParameters
, lpstrProtocol
,
1922 wszApplicationName
, env
, &sei_tmp
,
1924 HeapFree(GetProcessHeap(), 0, env
);
1926 else if (PathIsDirectoryW(lpFile
))
1928 WCHAR wExec
[MAX_PATH
];
1929 WCHAR
* lpQuotedFile
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (strlenW(lpFile
) + 3));
1933 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, L
"explorer",
1934 wszOpen
, wExec
, MAX_PATH
,
1935 NULL
, &env
, NULL
, NULL
);
1938 swprintf(lpQuotedFile
, L
"\"%s\"", lpFile
);
1939 retval
= SHELL_quote_and_execute(wExec
, lpQuotedFile
,
1941 wszApplicationName
, env
,
1942 &sei_tmp
, sei
, execfunc
);
1943 HeapFree(GetProcessHeap(), 0, env
);
1945 HeapFree(GetProcessHeap(), 0, lpQuotedFile
);
1948 retval
= 0; /* Out of memory */
1950 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1952 retval
= SHELL_execute_url(lpFile
, L
"file", wcmd
, &sei_tmp
, sei
, execfunc
);
1954 /* Check if file specified is in the form www.??????.*** */
1955 else if (!strncmpiW(lpFile
, L
"www", 3))
1957 /* if so, append lpFile http:// and call ShellExecute */
1958 WCHAR lpstrTmpFile
[256];
1959 strcpyW(lpstrTmpFile
, L
"http://");
1960 strcatW(lpstrTmpFile
, lpFile
);
1961 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1964 TRACE("retval %lu\n", retval
);
1966 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1973 Info
.pcszFile
= wszApplicationName
;
1974 Info
.pcszClass
= NULL
;
1975 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1977 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1978 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1981 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1982 if (wszParameters
!= parametersBuffer
)
1983 HeapFree(GetProcessHeap(), 0, wszParameters
);
1984 if (wszDir
!= dirBuffer
)
1985 HeapFree(GetProcessHeap(), 0, wszDir
);
1986 if (wcmd
!= wcmdBuffer
)
1987 HeapFree(GetProcessHeap(), 0, wcmd
);
1989 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1994 /*************************************************************************
1995 * ShellExecuteA [SHELL32.290]
1997 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
1998 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
2000 SHELLEXECUTEINFOA sei
;
2002 TRACE("%p,%s,%s,%s,%s,%d\n",
2003 hWnd
, debugstr_a(lpOperation
), debugstr_a(lpFile
),
2004 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
2006 sei
.cbSize
= sizeof(sei
);
2007 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2009 sei
.lpVerb
= lpOperation
;
2010 sei
.lpFile
= lpFile
;
2011 sei
.lpParameters
= lpParameters
;
2012 sei
.lpDirectory
= lpDirectory
;
2013 sei
.nShow
= iShowCmd
;
2020 ShellExecuteExA(&sei
);
2021 return sei
.hInstApp
;
2024 /*************************************************************************
2025 * ShellExecuteExA [SHELL32.292]
2028 BOOL WINAPI
ShellExecuteExA(LPSHELLEXECUTEINFOA sei
)
2030 SHELLEXECUTEINFOW seiW
;
2032 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
2036 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
2039 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
2042 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
2044 if (sei
->lpParameters
)
2045 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
2047 if (sei
->lpDirectory
)
2048 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
2050 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
2051 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
2053 seiW
.lpClass
= NULL
;
2055 ret
= SHELL_execute(&seiW
, SHELL_ExecuteW
);
2057 sei
->hInstApp
= seiW
.hInstApp
;
2059 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
2060 sei
->hProcess
= seiW
.hProcess
;
2064 SHFree(wParameters
);
2071 /*************************************************************************
2072 * ShellExecuteExW [SHELL32.293]
2075 BOOL WINAPI
ShellExecuteExW(LPSHELLEXECUTEINFOW sei
)
2077 return SHELL_execute(sei
, SHELL_ExecuteW
);
2080 /*************************************************************************
2081 * ShellExecuteW [SHELL32.294]
2083 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
2084 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
2086 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
2087 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
2089 SHELLEXECUTEINFOW sei
;
2092 sei
.cbSize
= sizeof(sei
);
2093 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2095 sei
.lpVerb
= lpOperation
;
2096 sei
.lpFile
= lpFile
;
2097 sei
.lpParameters
= lpParameters
;
2098 sei
.lpDirectory
= lpDirectory
;
2099 sei
.nShow
= nShowCmd
;
2106 SHELL_execute(&sei
, SHELL_ExecuteW
);
2107 return sei
.hInstApp
;
2110 /*************************************************************************
2111 * WOWShellExecute [SHELL32.@]
2113 * FIXME: the callback function most likely doesn't work the same way on Windows.
2115 EXTERN_C HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
2116 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
2118 SHELLEXECUTEINFOW seiW
;
2119 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
2120 HANDLE hProcess
= 0;
2122 seiW
.lpVerb
= lpOperation
? __SHCloneStrAtoW(&wVerb
, lpOperation
) : NULL
;
2123 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
2124 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
2125 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
2127 seiW
.cbSize
= sizeof(seiW
);
2130 seiW
.nShow
= iShowCmd
;
2135 seiW
.hProcess
= hProcess
;
2137 SHELL_execute(&seiW
, (SHELL_ExecuteW32
)callback
);
2141 SHFree(wParameters
);
2143 return seiW
.hInstApp
;
2146 /*************************************************************************
2147 * OpenAs_RunDLLA [SHELL32.@]
2149 EXTERN_C
void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2151 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2154 /*************************************************************************
2155 * OpenAs_RunDLLW [SHELL32.@]
2157 EXTERN_C
void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2159 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);