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 dwCreationFlags
= CREATE_UNICODE_ENVIRONMENT
;
454 if (psei
->fMask
& SEE_MASK_NO_CONSOLE
)
455 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
456 startup
.lpTitle
= (LPWSTR
)(psei
->fMask
& (SEE_MASK_HASLINKNAME
| SEE_MASK_HASTITLE
) ? psei
->lpClass
: NULL
);
458 if (psei
->fMask
& SEE_MASK_HASLINKNAME
)
459 startup
.dwFlags
|= STARTF_TITLEISLINKNAME
;
461 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, dwCreationFlags
, env
,
462 lpDirectory
, &startup
, &info
))
464 /* Give 30 seconds to the app to come up, if desired. Probably only needed
465 when starting app immediately before making a DDE connection. */
467 if (WaitForInputIdle(info
.hProcess
, 30000) == WAIT_FAILED
)
468 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
471 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
472 psei_out
->hProcess
= info
.hProcess
;
474 CloseHandle( info
.hProcess
);
475 CloseHandle( info
.hThread
);
477 else if ((retval
= GetLastError()) >= 32)
479 WARN("CreateProcess returned error %ld\n", retval
);
480 retval
= ERROR_BAD_FORMAT
;
483 TRACE("returning %lu\n", retval
);
485 psei_out
->hInstApp
= (HINSTANCE
)retval
;
488 if (!SetCurrentDirectoryW(curdir
))
489 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
495 /***********************************************************************
496 * SHELL_BuildEnvW [Internal]
498 * Build the environment for the new process, adding the specified
499 * path to the PATH variable. Returned pointer must be freed by caller.
501 static LPWSTR
SHELL_BuildEnvW( const WCHAR
*path
)
503 static const WCHAR wPath
[] = L
"PATH=";
504 WCHAR
*strings
, *new_env
;
506 int total
= wcslen(path
) + 1;
507 BOOL got_path
= FALSE
;
509 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
513 int len
= wcslen(p
) + 1;
514 if (!_wcsnicmp( p
, wPath
, 5 )) got_path
= TRUE
;
518 if (!got_path
) total
+= 5; /* we need to create PATH */
519 total
++; /* terminating null */
521 if (!(new_env
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, total
* sizeof(WCHAR
))))
523 FreeEnvironmentStringsW(strings
);
530 int len
= wcslen(p
) + 1;
531 memcpy(p2
, p
, len
* sizeof(WCHAR
));
532 if (!_wcsnicmp( p
, wPath
, 5 ))
535 wcscpy( p2
+ len
, path
);
536 p2
+= wcslen(path
) + 1;
545 p2
+= wcslen(p2
) + 1;
548 FreeEnvironmentStringsW(strings
);
553 /***********************************************************************
554 * SHELL_TryAppPathW [Internal]
556 * Helper function for SHELL_FindExecutable
557 * @param lpResult - pointer to a buffer of size MAX_PATH
558 * On entry: szName is a filename (probably without path separators).
559 * On exit: if szName found in "App Path", place full path in lpResult, and return true
561 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
569 if (env
) *env
= NULL
;
570 wcscpy(buffer
, L
"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
571 wcscat(buffer
, szName
);
572 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
575 len
= MAX_PATH
* sizeof(WCHAR
);
576 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
582 DWORD count
= sizeof(buffer
);
583 if (!RegQueryValueExW(hkApp
, L
"Path", NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
584 *env
= SHELL_BuildEnvW(buffer
);
588 if (hkApp
) RegCloseKey(hkApp
);
592 static UINT
SHELL_FindExecutableByOperation(LPCWSTR lpOperation
, LPWSTR key
, LPWSTR filetype
, LPWSTR command
, LONG commandlen
)
594 static const WCHAR wCommand
[] = L
"\\command";
596 WCHAR verb
[MAX_PATH
];
598 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, filetype
, 0, 0x02000000, &hkeyClass
))
599 return SE_ERR_NOASSOC
;
600 if (!HCR_GetDefaultVerbW(hkeyClass
, lpOperation
, verb
, sizeof(verb
) / sizeof(verb
[0])))
601 return SE_ERR_NOASSOC
;
602 RegCloseKey(hkeyClass
);
604 /* Looking for ...buffer\shell\<verb>\command */
605 wcscat(filetype
, L
"\\shell\\");
606 wcscat(filetype
, verb
);
607 wcscat(filetype
, wCommand
);
609 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, command
,
610 &commandlen
) == ERROR_SUCCESS
)
612 commandlen
/= sizeof(WCHAR
);
613 if (key
) wcscpy(key
, filetype
);
617 LONG paramlen
= sizeof(param
);
618 static const WCHAR wSpace
[] = {' ', 0};
620 /* FIXME: it seems all Windows version don't behave the same here.
621 * the doc states that this ddeexec information can be found after
623 * on Win98, it doesn't appear, but I think it does on Win2k
625 /* Get the parameters needed by the application
626 from the associated ddeexec key */
627 tmp
= strstrW(filetype
, wCommand
);
629 wcscat(filetype
, wDdeexec
);
630 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, param
,
631 ¶mlen
) == ERROR_SUCCESS
)
633 paramlen
/= sizeof(WCHAR
);
634 wcscat(command
, wSpace
);
635 wcscat(command
, param
);
636 commandlen
+= paramlen
;
640 command
[commandlen
] = '\0';
642 return 33; /* FIXME see SHELL_FindExecutable() */
645 return SE_ERR_NOASSOC
;
648 /*************************************************************************
649 * SHELL_FindExecutable [Internal]
651 * Utility for code sharing between FindExecutable and ShellExecute
653 * lpFile the name of a file
654 * lpOperation the operation on it (open)
656 * lpResult a buffer, big enough :-(, to store the command to do the
657 * operation on the file
658 * key a buffer, big enough, to get the key name to do actually the
659 * command (it'll be used afterwards for more information
662 static UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
,
663 LPWSTR lpResult
, DWORD resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
665 WCHAR
*extension
= NULL
; /* pointer to file extension */
666 WCHAR filetype
[256]; /* registry name for this filetype */
667 LONG filetypelen
= sizeof(filetype
); /* length of above */
668 WCHAR command
[1024]; /* command from registry */
669 WCHAR wBuffer
[256]; /* Used to GetProfileString */
670 UINT retval
= SE_ERR_NOASSOC
;
671 WCHAR
*tok
; /* token pointer */
672 WCHAR xlpFile
[256]; /* result of SearchPath */
673 DWORD attribs
; /* file attributes */
675 TRACE("%s\n", debugstr_w(lpFile
));
678 return ERROR_INVALID_PARAMETER
;
681 lpResult
[0] = '\0'; /* Start off with an empty return string */
682 if (key
) *key
= '\0';
684 /* trap NULL parameters on entry */
687 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
688 debugstr_w(lpFile
), debugstr_w(lpResult
));
689 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
692 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
694 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
698 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
) / sizeof(WCHAR
), xlpFile
, NULL
))
700 TRACE("SearchPathW returned non-zero\n");
702 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
704 else if (lpPath
&& SearchPathW(NULL
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
706 TRACE("SearchPathW returned non-zero\n");
708 /* The file was found in one of the directories in the system-wide search path */
711 attribs
= GetFileAttributesW(lpFile
);
712 if (attribs
!= INVALID_FILE_ATTRIBUTES
&& (attribs
& FILE_ATTRIBUTE_DIRECTORY
))
714 wcscpy(filetype
, L
"Folder");
715 filetypelen
= 6; /* strlen("Folder") */
719 /* Did we get something? Anything? */
722 TRACE("Returning SE_ERR_FNF\n");
725 /* First thing we need is the file's extension */
726 extension
= wcsrchr(xlpFile
, '.'); /* Assume last "." is the one; */
727 /* File->Run in progman uses */
729 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
731 if (extension
== NULL
|| extension
[1] == 0)
733 WARN("Returning SE_ERR_NOASSOC\n");
734 return SE_ERR_NOASSOC
;
737 /* Three places to check: */
738 /* 1. win.ini, [windows], programs (NB no leading '.') */
739 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
740 /* 3. win.ini, [extensions], extension (NB no leading '.' */
741 /* All I know of the order is that registry is checked before */
742 /* extensions; however, it'd make sense to check the programs */
743 /* section first, so that's what happens here. */
745 /* See if it's a program - if GetProfileString fails, we skip this
746 * section. Actually, if GetProfileString fails, we've probably
747 * got a lot more to worry about than running a program... */
748 if (GetProfileStringW(L
"windows", L
"programs", L
"exe pif bat cmd com", wBuffer
, sizeof(wBuffer
) / sizeof(WCHAR
)) > 0)
755 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
759 while (*p
== ' ' || *p
== '\t') p
++;
762 if (wcsicmp(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
764 wcscpy(lpResult
, xlpFile
);
765 /* Need to perhaps check that the file has a path
767 TRACE("found %s\n", debugstr_w(lpResult
));
769 /* Greater than 32 to indicate success */
776 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, filetype
,
777 &filetypelen
) == ERROR_SUCCESS
)
779 filetypelen
/= sizeof(WCHAR
);
780 if (filetypelen
== sizeof(filetype
) / sizeof(WCHAR
))
783 filetype
[filetypelen
] = '\0';
784 TRACE("File type: %s\n", debugstr_w(filetype
));
795 /* pass the operation string to SHELL_FindExecutableByOperation() */
796 filetype
[filetypelen
] = '\0';
797 retval
= SHELL_FindExecutableByOperation(lpOperation
, key
, filetype
, command
, sizeof(command
));
802 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
803 if (finishedLen
> resultLen
)
804 ERR("Argify buffer not large enough.. truncated\n");
805 /* Remove double quotation marks and command line arguments */
806 if (*lpResult
== '"')
809 while (*(p
+ 1) != '"')
818 /* Truncate on first space */
820 while (*p
!= ' ' && *p
!= '\0')
826 else /* Check win.ini */
828 /* Toss the leading dot */
830 if (GetProfileStringW(L
"extensions", extension
, L
"", command
, sizeof(command
) / sizeof(WCHAR
)) > 0)
832 if (wcslen(command
) != 0)
834 wcscpy(lpResult
, command
);
835 tok
= wcschr(lpResult
, '^'); /* should be ^.extension? */
839 wcscat(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
840 tok
= wcschr(command
, '^'); /* see above */
841 if ((tok
!= NULL
) && (wcslen(tok
) > 5))
843 wcscat(lpResult
, &tok
[5]);
846 retval
= 33; /* FIXME - see above */
851 TRACE("returning %s\n", debugstr_w(lpResult
));
855 /******************************************************************
858 * callback for the DDE connection. not really useful
860 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
861 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
862 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
864 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
865 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
869 /******************************************************************
872 * ShellExecute helper. Used to do an operation with a DDE connection
874 * Handles both the direct connection (try #1), and if it fails,
875 * launching an application and trying (#2) to connect to it
878 static unsigned dde_connect(const WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
879 const WCHAR
* lpFile
, WCHAR
*env
,
880 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
881 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
884 WCHAR
* endkey
= regkey
+ wcslen(key
);
885 WCHAR app
[256], topic
[256], ifexec
[256], res
[256];
886 LONG applen
, topiclen
, ifexeclen
;
891 HSZ hszApp
, hszTopic
;
894 unsigned ret
= SE_ERR_NOASSOC
;
895 BOOL unicode
= !(GetVersion() & 0x80000000);
898 wcscpy(endkey
, L
"\\application");
899 applen
= sizeof(app
);
900 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, app
, &applen
) != ERROR_SUCCESS
)
902 WCHAR command
[1024], fullpath
[MAX_PATH
];
903 static const WCHAR wSo
[] = L
".so";
904 DWORD sizeSo
= sizeof(wSo
) / sizeof(WCHAR
);
908 /* Get application command from start string and find filename of application */
911 wcscpy(command
, start
+ 1);
912 if ((ptr
= wcschr(command
, '"')))
914 ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
);
919 for (p
= (LPWSTR
)start
; (space
= const_cast<LPWSTR
>(strchrW(p
, ' '))); p
= space
+ 1)
921 int idx
= space
- start
;
922 memcpy(command
, start
, idx
* sizeof(WCHAR
));
924 if ((ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
)))
928 ret
= SearchPathW(NULL
, start
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
);
933 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
934 return ERROR_ACCESS_DENIED
;
938 /* Remove extensions (including .so) */
939 ptr
= app
+ wcslen(app
) - (sizeSo
- 1);
940 if (wcslen(app
) >= sizeSo
&&
944 ptr
= const_cast<LPWSTR
>(strrchrW(app
, '.'));
949 wcscpy(endkey
, L
"\\topic");
950 topiclen
= sizeof(topic
);
951 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, topic
, &topiclen
) != ERROR_SUCCESS
)
953 wcscpy(topic
, L
"System");
958 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
963 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
967 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
968 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
970 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
974 TRACE("Launching %s\n", debugstr_w(start
));
975 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
978 TRACE("Couldn't launch\n");
981 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
984 TRACE("Couldn't connect. ret=%d\n", ret
);
985 DdeUninitialize(ddeInst
);
986 SetLastError(ERROR_DDE_FAIL
);
987 return 30; /* whatever */
989 strcpyW(endkey
, L
"\\ifexec");
990 ifexeclen
= sizeof(ifexec
);
991 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
997 SHELL_ArgifyW(res
, sizeof(res
) / sizeof(WCHAR
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
998 if (resultLen
> sizeof(res
) / sizeof(WCHAR
))
999 ERR("Argify buffer not large enough, truncated\n");
1000 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
1002 /* It's documented in the KB 330337 that IE has a bug and returns
1003 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
1006 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0, XTYP_EXECUTE
, 30000, &tid
);
1009 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
1010 char *resA
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, lenA
);
1011 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
1012 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
1013 XTYP_EXECUTE
, 10000, &tid
);
1014 HeapFree(GetProcessHeap(), 0, resA
);
1017 DdeFreeDataHandle(hDdeData
);
1019 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
1022 DdeDisconnect(hConv
);
1025 DdeUninitialize(ddeInst
);
1030 /*************************************************************************
1031 * execute_from_key [Internal]
1033 static UINT_PTR
execute_from_key(LPCWSTR key
, LPCWSTR lpFile
, WCHAR
*env
,
1034 LPCWSTR szCommandline
, LPCWSTR executable_name
,
1035 SHELL_ExecuteW32 execfunc
,
1036 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
1038 WCHAR cmd
[256], param
[1024], ddeexec
[256];
1039 DWORD cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
1040 UINT_PTR retval
= SE_ERR_NOASSOC
;
1044 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
1045 debugstr_w(szCommandline
), debugstr_w(executable_name
));
1050 /* Get the application from the registry */
1051 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, (LONG
*)&cmdlen
) == ERROR_SUCCESS
)
1053 TRACE("got cmd: %s\n", debugstr_w(cmd
));
1055 /* Is there a replace() function anywhere? */
1056 cmdlen
/= sizeof(WCHAR
);
1057 if (cmdlen
>= sizeof(cmd
) / sizeof(WCHAR
))
1058 cmdlen
= sizeof(cmd
) / sizeof(WCHAR
) - 1;
1060 SHELL_ArgifyW(param
, sizeof(param
) / sizeof(WCHAR
), cmd
, lpFile
, (LPITEMIDLIST
)psei
->lpIDList
, szCommandline
, &resultLen
);
1061 if (resultLen
> sizeof(param
) / sizeof(WCHAR
))
1062 ERR("Argify buffer not large enough, truncating\n");
1065 /* Get the parameters needed by the application
1066 from the associated ddeexec key */
1067 tmp
= const_cast<LPWSTR
>(strstrW(key
, L
"command"));
1069 wcscpy(tmp
, L
"ddeexec");
1071 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, (LONG
*)&ddeexeclen
) == ERROR_SUCCESS
)
1073 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
1074 if (!param
[0]) strcpyW(param
, executable_name
);
1075 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, (LPITEMIDLIST
)psei
->lpIDList
, execfunc
, psei
, psei_out
);
1079 TRACE("executing: %s\n", debugstr_w(param
));
1080 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
1083 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
1088 /*************************************************************************
1089 * FindExecutableA [SHELL32.@]
1091 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
1094 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
1095 WCHAR wResult
[MAX_PATH
];
1097 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
1098 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
1100 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
1101 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
1105 TRACE("returning %s\n", lpResult
);
1109 /*************************************************************************
1110 * FindExecutableW [SHELL32.@]
1112 * This function returns the executable associated with the specified file
1113 * for the default verb.
1116 * lpFile [I] The file to find the association for. This must refer to
1117 * an existing file otherwise FindExecutable fails and returns
1119 * lpResult [O] Points to a buffer into which the executable path is
1120 * copied. This parameter must not be NULL otherwise
1121 * FindExecutable() segfaults. The buffer must be of size at
1122 * least MAX_PATH characters.
1125 * A value greater than 32 on success, less than or equal to 32 otherwise.
1126 * See the SE_ERR_* constants.
1129 * On Windows XP and 2003, FindExecutable() seems to first convert the
1130 * filename into 8.3 format, thus taking into account only the first three
1131 * characters of the extension, and expects to find an association for those.
1132 * However other Windows versions behave sanely.
1134 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
1136 UINT_PTR retval
= SE_ERR_NOASSOC
;
1137 WCHAR old_dir
[1024];
1139 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
1141 lpResult
[0] = '\0'; /* Start off with an empty return string */
1143 return (HINSTANCE
)SE_ERR_FNF
;
1147 GetCurrentDirectoryW(sizeof(old_dir
) / sizeof(WCHAR
), old_dir
);
1148 SetCurrentDirectoryW(lpDirectory
);
1151 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, lpResult
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1153 TRACE("returning %s\n", debugstr_w(lpResult
));
1155 SetCurrentDirectoryW(old_dir
);
1156 return (HINSTANCE
)retval
;
1159 /* FIXME: is this already implemented somewhere else? */
1160 static HKEY
ShellExecute_GetClassKey(const SHELLEXECUTEINFOW
*sei
)
1162 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1164 DWORD type
= 0, sz
= 0;
1168 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1169 return sei
->hkeyClass
;
1171 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1172 lpClass
= sei
->lpClass
;
1175 ext
= PathFindExtensionW(sei
->lpFile
);
1176 TRACE("ext = %s\n", debugstr_w(ext
));
1180 r
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
1181 if (r
!= ERROR_SUCCESS
)
1184 r
= RegQueryValueExW(hkey
, NULL
, 0, &type
, NULL
, &sz
);
1185 if (r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1187 sz
+= sizeof (WCHAR
);
1188 cls
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sz
);
1190 RegQueryValueExW(hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1193 RegCloseKey( hkey
);
1197 TRACE("class = %s\n", debugstr_w(lpClass
));
1201 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1203 HeapFree(GetProcessHeap(), 0, cls
);
1208 static IDataObject
*shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
)
1210 LPCITEMIDLIST pidllast
= NULL
;
1211 IDataObject
*dataobj
= NULL
;
1212 IShellFolder
*shf
= NULL
;
1213 LPITEMIDLIST pidl
= NULL
;
1216 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1217 pidl
= (LPITEMIDLIST
)sei
->lpIDList
;
1220 WCHAR fullpath
[MAX_PATH
];
1224 ret
= GetFullPathNameW(sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1228 pidl
= ILCreateFromPathW(fullpath
);
1231 r
= SHBindToParent(pidl
, IID_PPV_ARG(IShellFolder
, &shf
), &pidllast
);
1235 shf
->GetUIObjectOf(NULL
, 1, &pidllast
, IID_NULL_PPV_ARG(IDataObject
, &dataobj
));
1238 if (pidl
!= sei
->lpIDList
)
1245 static HRESULT
shellex_run_context_menu_default(IShellExtInit
*obj
,
1246 LPSHELLEXECUTEINFOW sei
)
1248 IContextMenu
*cm
= NULL
;
1249 CMINVOKECOMMANDINFOEX ici
;
1256 TRACE("%p %p\n", obj
, sei
);
1258 r
= obj
->QueryInterface(IID_PPV_ARG(IContextMenu
, &cm
));
1262 hmenu
= CreateMenu();
1266 /* the number of the last menu added is returned in r */
1267 r
= cm
->QueryContextMenu(hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1271 n
= GetMenuItemCount(hmenu
);
1272 for (i
= 0; i
< n
; i
++)
1274 memset(&info
, 0, sizeof(info
));
1275 info
.cbSize
= sizeof info
;
1276 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1277 info
.dwTypeData
= string
;
1278 info
.cch
= sizeof string
;
1280 GetMenuItemInfoW(hmenu
, i
, TRUE
, &info
);
1282 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1283 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1284 if ((!sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
)) ||
1285 (sei
->lpVerb
&& !lstrcmpiW(sei
->lpVerb
, string
)))
1296 memset(&ici
, 0, sizeof ici
);
1297 ici
.cbSize
= sizeof ici
;
1298 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NOASYNC
| SEE_MASK_ASYNCOK
| SEE_MASK_FLAG_NO_UI
));
1299 ici
.nShow
= sei
->nShow
;
1300 ici
.lpVerb
= MAKEINTRESOURCEA(def
);
1301 ici
.hwnd
= sei
->hwnd
;
1302 ici
.lpParametersW
= sei
->lpParameters
;
1304 r
= cm
->InvokeCommand((LPCMINVOKECOMMANDINFO
)&ici
);
1306 TRACE("invoke command returned %08x\n", r
);
1310 DestroyMenu( hmenu
);
1316 static HRESULT
shellex_load_object_and_run(HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1318 IDataObject
*dataobj
= NULL
;
1319 IObjectWithSite
*ows
= NULL
;
1320 IShellExtInit
*obj
= NULL
;
1323 TRACE("%p %s %p\n", hkey
, debugstr_guid(guid
), sei
);
1325 r
= CoInitialize(NULL
);
1329 r
= CoCreateInstance(*guid
, NULL
, CLSCTX_INPROC_SERVER
,
1330 IID_PPV_ARG(IShellExtInit
, &obj
));
1333 ERR("failed %08x\n", r
);
1337 dataobj
= shellex_get_dataobj(sei
);
1340 ERR("failed to get data object\n");
1344 r
= obj
->Initialize(NULL
, dataobj
, hkey
);
1348 r
= obj
->QueryInterface(IID_PPV_ARG(IObjectWithSite
, &ows
));
1354 r
= shellex_run_context_menu_default(obj
, sei
);
1368 /*************************************************************************
1369 * ShellExecute_FromContextMenu [Internal]
1371 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1373 HKEY hkey
, hkeycm
= 0;
1380 TRACE("%s\n", debugstr_w(sei
->lpFile
));
1382 hkey
= ShellExecute_GetClassKey(sei
);
1384 return ERROR_FUNCTION_FAILED
;
1386 r
= RegOpenKeyW(hkey
, L
"shellex\\ContextMenuHandlers", &hkeycm
);
1387 if (r
== ERROR_SUCCESS
)
1392 r
= RegEnumKeyW(hkeycm
, i
++, szguid
, sizeof(szguid
) / sizeof(szguid
[0]));
1393 if (r
!= ERROR_SUCCESS
)
1396 hr
= CLSIDFromString(szguid
, &guid
);
1399 /* stop at the first one that succeeds in running */
1400 hr
= shellex_load_object_and_run(hkey
, &guid
, sei
);
1405 RegCloseKey(hkeycm
);
1408 if (hkey
!= sei
->hkeyClass
)
1413 static UINT_PTR
SHELL_execute_class(LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1415 WCHAR execCmd
[1024], wcmd
[1024];
1416 /* launch a document by fileclass like 'WordPad.Document.1' */
1417 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1418 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1419 ULONG cmask
= (psei
->fMask
& SEE_MASK_CLASSALL
);
1423 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1424 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1426 execCmd
, sizeof(execCmd
));
1428 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1429 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1432 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
) / sizeof(WCHAR
), execCmd
, wszApplicationName
, (LPITEMIDLIST
)psei
->lpIDList
, NULL
, &resultLen
);
1433 if (!done
&& wszApplicationName
[0])
1435 strcatW(wcmd
, L
" ");
1436 strcatW(wcmd
, wszApplicationName
);
1438 if (resultLen
> sizeof(wcmd
) / sizeof(WCHAR
))
1439 ERR("Argify buffer not large enough... truncating\n");
1440 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1443 static BOOL
SHELL_translate_idlist(LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1445 static const WCHAR wExplorer
[] = L
"explorer.exe";
1446 WCHAR buffer
[MAX_PATH
];
1447 BOOL appKnownSingular
= FALSE
;
1449 /* last chance to translate IDList: now also allow CLSID paths */
1450 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST
)sei
->lpIDList
, buffer
, sizeof(buffer
)))) {
1451 if (buffer
[0] == ':' && buffer
[1] == ':') {
1452 /* open shell folder for the specified class GUID */
1453 if (strlenW(buffer
) + 1 > parametersLen
)
1454 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1455 lstrlenW(buffer
) + 1, parametersLen
);
1456 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1457 if (strlenW(wExplorer
) > dwApplicationNameLen
)
1458 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1459 lstrlenW(wExplorer
) + 1, dwApplicationNameLen
);
1460 lstrcpynW(wszApplicationName
, wExplorer
, dwApplicationNameLen
);
1461 appKnownSingular
= TRUE
;
1463 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1465 WCHAR target
[MAX_PATH
];
1468 /* Check if we're executing a directory and if so use the
1469 handler for the Folder class */
1470 strcpyW(target
, buffer
);
1471 attribs
= GetFileAttributesW(buffer
);
1472 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1473 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1474 HCR_GetExecuteCommandW(0, L
"Folder",
1476 buffer
, sizeof(buffer
))) {
1477 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1478 buffer
, target
, (LPITEMIDLIST
)sei
->lpIDList
, NULL
, &resultLen
);
1479 if (resultLen
> dwApplicationNameLen
)
1480 ERR("Argify buffer not large enough... truncating\n");
1481 appKnownSingular
= FALSE
;
1483 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1486 return appKnownSingular
;
1489 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
)
1493 WCHAR
*wszQuotedCmd
;
1495 /* Length of quotes plus length of command plus NULL terminator */
1496 len
= 2 + lstrlenW(wcmd
) + 1;
1497 if (wszParameters
[0])
1499 /* Length of space plus length of parameters */
1500 len
+= 1 + lstrlenW(wszParameters
);
1502 wszQuotedCmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1503 /* Must quote to handle case where cmd contains spaces,
1504 * else security hole if malicious user creates executable file "C:\\Program"
1506 strcpyW(wszQuotedCmd
, L
"\"");
1507 strcatW(wszQuotedCmd
, wcmd
);
1508 strcatW(wszQuotedCmd
, L
"\"");
1509 if (wszParameters
[0])
1511 strcatW(wszQuotedCmd
, L
" ");
1512 strcatW(wszQuotedCmd
, wszParameters
);
1515 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(lpstrProtocol
));
1518 retval
= execute_from_key(lpstrProtocol
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1520 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1521 HeapFree(GetProcessHeap(), 0, wszQuotedCmd
);
1525 static UINT_PTR
SHELL_execute_url(LPCWSTR lpFile
, LPCWSTR wFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1527 static const WCHAR wShell
[] = L
"\\shell\\";
1528 static const WCHAR wCommand
[] = L
"\\command";
1530 WCHAR
*lpstrProtocol
;
1535 lpstrRes
= strchrW(lpFile
, ':');
1537 iSize
= lpstrRes
- lpFile
;
1539 iSize
= strlenW(lpFile
);
1541 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1542 /* Looking for ...protocol\shell\lpOperation\command */
1543 len
= iSize
+ lstrlenW(wShell
) + lstrlenW(wCommand
) + 1;
1545 len
+= lstrlenW(psei
->lpVerb
);
1547 len
+= lstrlenW(wszOpen
);
1548 lpstrProtocol
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1549 memcpy(lpstrProtocol
, lpFile
, iSize
* sizeof(WCHAR
));
1550 lpstrProtocol
[iSize
] = '\0';
1551 strcatW(lpstrProtocol
, wShell
);
1552 strcatW(lpstrProtocol
, psei
->lpVerb
? psei
->lpVerb
: wszOpen
);
1553 strcatW(lpstrProtocol
, wCommand
);
1555 /* Remove File Protocol from lpFile */
1556 /* In the case file://path/file */
1557 if (!strncmpiW(lpFile
, wFile
, iSize
))
1560 while (*lpFile
== ':') lpFile
++;
1562 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1563 wcmd
, execfunc
, psei
, psei_out
);
1564 HeapFree(GetProcessHeap(), 0, lpstrProtocol
);
1568 void do_error_dialog(UINT_PTR retval
, HWND hwnd
, WCHAR
* filename
)
1571 DWORD_PTR msgArguments
[3] = { (DWORD_PTR
)filename
, 0, 0 };
1574 error_code
= GetLastError();
1576 if (retval
== SE_ERR_NOASSOC
)
1577 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, sizeof(msg
) / sizeof(WCHAR
));
1579 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ARGUMENT_ARRAY
,
1584 sizeof(msg
) / sizeof(WCHAR
),
1585 (va_list*)msgArguments
);
1587 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1590 /*************************************************************************
1591 * SHELL_execute [Internal]
1593 BOOL
SHELL_execute(LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1595 static const DWORD unsupportedFlags
=
1596 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1597 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1598 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1600 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1601 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
;
1602 DWORD dwApplicationNameLen
= MAX_PATH
+ 2;
1603 DWORD parametersLen
= sizeof(parametersBuffer
) / sizeof(WCHAR
);
1604 DWORD dirLen
= sizeof(dirBuffer
) / sizeof(WCHAR
);
1605 DWORD wcmdLen
= sizeof(wcmdBuffer
) / sizeof(WCHAR
);
1607 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1608 WCHAR wfileName
[MAX_PATH
];
1610 WCHAR lpstrProtocol
[256];
1612 UINT_PTR retval
= SE_ERR_NOASSOC
;
1613 BOOL appKnownSingular
= FALSE
;
1615 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1618 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1619 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1620 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1621 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1622 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1623 debugstr_w(sei_tmp
.lpClass
) : "not used");
1625 sei
->hProcess
= NULL
;
1627 /* make copies of all path/command strings */
1628 if (!sei_tmp
.lpFile
)
1630 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1631 *wszApplicationName
= '\0';
1633 else if (*sei_tmp
.lpFile
== '\"')
1635 DWORD l
= strlenW(sei_tmp
.lpFile
+ 1);
1636 if(l
>= dwApplicationNameLen
)
1637 dwApplicationNameLen
= l
+ 1;
1639 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1640 memcpy(wszApplicationName
, sei_tmp
.lpFile
+ 1, (l
+ 1)*sizeof(WCHAR
));
1642 if (wszApplicationName
[l
-1] == L
'\"')
1643 wszApplicationName
[l
-1] = L
'\0';
1644 appKnownSingular
= TRUE
;
1646 TRACE("wszApplicationName=%s\n", debugstr_w(wszApplicationName
));
1650 DWORD l
= strlenW(sei_tmp
.lpFile
) + 1;
1651 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+ 1;
1652 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1653 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
* sizeof(WCHAR
));
1656 wszParameters
= parametersBuffer
;
1657 if (sei_tmp
.lpParameters
)
1659 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1660 if (len
> parametersLen
)
1662 wszParameters
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1663 parametersLen
= len
;
1665 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1668 *wszParameters
= L
'\0';
1671 if (sei_tmp
.lpDirectory
)
1673 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1676 wszDir
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1679 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1684 /* adjust string pointers to point to the new buffers */
1685 sei_tmp
.lpFile
= wszApplicationName
;
1686 sei_tmp
.lpParameters
= wszParameters
;
1687 sei_tmp
.lpDirectory
= wszDir
;
1689 if (sei_tmp
.fMask
& unsupportedFlags
)
1691 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1694 /* process the IDList */
1695 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1697 IShellExecuteHookW
* pSEH
;
1699 HRESULT hr
= SHBindToParent((LPCITEMIDLIST
)sei_tmp
.lpIDList
, IID_PPV_ARG(IShellExecuteHookW
, &pSEH
), NULL
);
1703 hr
= pSEH
->Execute(&sei_tmp
);
1709 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1710 if (wszParameters
!= parametersBuffer
)
1711 HeapFree(GetProcessHeap(), 0, wszParameters
);
1712 if (wszDir
!= dirBuffer
)
1713 HeapFree(GetProcessHeap(), 0, wszDir
);
1718 SHGetPathFromIDListW((LPCITEMIDLIST
)sei_tmp
.lpIDList
, wszApplicationName
);
1719 appKnownSingular
= TRUE
;
1720 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1723 if (ERROR_SUCCESS
== ShellExecute_FromContextMenu(&sei_tmp
))
1725 sei
->hInstApp
= (HINSTANCE
) 33;
1726 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1727 if (wszParameters
!= parametersBuffer
)
1728 HeapFree(GetProcessHeap(), 0, wszParameters
);
1729 if (wszDir
!= dirBuffer
)
1730 HeapFree(GetProcessHeap(), 0, wszDir
);
1734 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1736 retval
= SHELL_execute_class(wszApplicationName
, &sei_tmp
, sei
, execfunc
);
1737 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1744 Info
.pcszFile
= wszApplicationName
;
1745 Info
.pcszClass
= NULL
;
1746 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1748 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1749 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1751 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1752 if (wszParameters
!= parametersBuffer
)
1753 HeapFree(GetProcessHeap(), 0, wszParameters
);
1754 if (wszDir
!= dirBuffer
)
1755 HeapFree(GetProcessHeap(), 0, wszDir
);
1759 /* Has the IDList not yet been translated? */
1760 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1762 appKnownSingular
= SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1765 dwApplicationNameLen
);
1768 /* expand environment strings */
1769 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1773 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1775 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1776 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1777 dwApplicationNameLen
= len
+ 1;
1778 wszApplicationName
= buf
;
1779 /* appKnownSingular unmodified */
1781 sei_tmp
.lpFile
= wszApplicationName
;
1784 if (*sei_tmp
.lpParameters
)
1786 len
= ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, NULL
, 0);
1791 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1792 ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buf
, len
);
1793 if (wszParameters
!= parametersBuffer
)
1794 HeapFree(GetProcessHeap(), 0, wszParameters
);
1795 wszParameters
= buf
;
1796 parametersLen
= len
;
1797 sei_tmp
.lpParameters
= wszParameters
;
1801 if (*sei_tmp
.lpDirectory
)
1803 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1808 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1809 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1810 if (wszDir
!= dirBuffer
)
1811 HeapFree(GetProcessHeap(), 0, wszDir
);
1813 sei_tmp
.lpDirectory
= wszDir
;
1817 /* Else, try to execute the filename */
1818 TRACE("execute: %s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1820 /* separate out command line arguments from executable file name */
1821 if (!*sei_tmp
.lpParameters
&& !appKnownSingular
)
1823 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1824 if (sei_tmp
.lpFile
[0] == L
'"')
1826 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1827 LPWSTR dst
= wfileName
;
1830 /* copy the unquoted executable path to 'wfileName' */
1831 while(*src
&& *src
!= L
'"')
1840 while(isspace(*src
))
1846 /* copy the parameter string to 'wszParameters' */
1847 strcpyW(wszParameters
, src
);
1849 /* terminate previous command string after the quote character */
1854 /* If the executable name is not quoted, we have to use this search loop here,
1855 that in CreateProcess() is not sufficient because it does not handle shell links. */
1856 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1859 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1860 for(s
= beg
; (space
= const_cast<LPWSTR
>(strchrW(s
, L
' '))); s
= space
+ 1)
1862 int idx
= space
- sei_tmp
.lpFile
;
1863 memcpy(buffer
, sei_tmp
.lpFile
, idx
* sizeof(WCHAR
));
1866 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1867 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
) / sizeof(xlpFile
[0]), xlpFile
, NULL
))
1869 /* separate out command from parameter string */
1870 LPCWSTR p
= space
+ 1;
1875 strcpyW(wszParameters
, p
);
1882 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1886 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1891 len
= lstrlenW(wszApplicationName
) + 3;
1892 if (sei_tmp
.lpParameters
[0])
1893 len
+= 1 + lstrlenW(wszParameters
);
1896 wcmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1899 swprintf(wcmd
, L
"\"%s\"", wszApplicationName
);
1900 if (sei_tmp
.lpParameters
[0])
1902 strcatW(wcmd
, L
" ");
1903 strcatW(wcmd
, wszParameters
);
1906 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1909 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1910 if (wszParameters
!= parametersBuffer
)
1911 HeapFree(GetProcessHeap(), 0, wszParameters
);
1912 if (wszDir
!= dirBuffer
)
1913 HeapFree(GetProcessHeap(), 0, wszDir
);
1914 if (wcmd
!= wcmdBuffer
)
1915 HeapFree(GetProcessHeap(), 0, wcmd
);
1919 /* Else, try to find the executable */
1921 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, lpstrProtocol
, &env
, (LPITEMIDLIST
)sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1922 if (retval
> 32) /* Found */
1924 retval
= SHELL_quote_and_execute(wcmd
, wszParameters
, lpstrProtocol
,
1925 wszApplicationName
, env
, &sei_tmp
,
1927 HeapFree(GetProcessHeap(), 0, env
);
1929 else if (PathIsDirectoryW(lpFile
))
1931 WCHAR wExec
[MAX_PATH
];
1932 WCHAR
* lpQuotedFile
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (strlenW(lpFile
) + 3));
1936 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, L
"explorer",
1937 wszOpen
, wExec
, MAX_PATH
,
1938 NULL
, &env
, NULL
, NULL
);
1941 swprintf(lpQuotedFile
, L
"\"%s\"", lpFile
);
1942 retval
= SHELL_quote_and_execute(wExec
, lpQuotedFile
,
1944 wszApplicationName
, env
,
1945 &sei_tmp
, sei
, execfunc
);
1946 HeapFree(GetProcessHeap(), 0, env
);
1948 HeapFree(GetProcessHeap(), 0, lpQuotedFile
);
1951 retval
= 0; /* Out of memory */
1953 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1955 retval
= SHELL_execute_url(lpFile
, L
"file", wcmd
, &sei_tmp
, sei
, execfunc
);
1957 /* Check if file specified is in the form www.??????.*** */
1958 else if (!strncmpiW(lpFile
, L
"www", 3))
1960 /* if so, append lpFile http:// and call ShellExecute */
1961 WCHAR lpstrTmpFile
[256];
1962 strcpyW(lpstrTmpFile
, L
"http://");
1963 strcatW(lpstrTmpFile
, lpFile
);
1964 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1967 TRACE("retval %lu\n", retval
);
1969 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1976 Info
.pcszFile
= wszApplicationName
;
1977 Info
.pcszClass
= NULL
;
1978 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1980 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1981 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1984 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1985 if (wszParameters
!= parametersBuffer
)
1986 HeapFree(GetProcessHeap(), 0, wszParameters
);
1987 if (wszDir
!= dirBuffer
)
1988 HeapFree(GetProcessHeap(), 0, wszDir
);
1989 if (wcmd
!= wcmdBuffer
)
1990 HeapFree(GetProcessHeap(), 0, wcmd
);
1992 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1997 /*************************************************************************
1998 * ShellExecuteA [SHELL32.290]
2000 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
2001 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
2003 SHELLEXECUTEINFOA sei
;
2005 TRACE("%p,%s,%s,%s,%s,%d\n",
2006 hWnd
, debugstr_a(lpOperation
), debugstr_a(lpFile
),
2007 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
2009 sei
.cbSize
= sizeof(sei
);
2010 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2012 sei
.lpVerb
= lpOperation
;
2013 sei
.lpFile
= lpFile
;
2014 sei
.lpParameters
= lpParameters
;
2015 sei
.lpDirectory
= lpDirectory
;
2016 sei
.nShow
= iShowCmd
;
2023 ShellExecuteExA(&sei
);
2024 return sei
.hInstApp
;
2027 /*************************************************************************
2028 * ShellExecuteExA [SHELL32.292]
2034 ShellExecuteExA(LPSHELLEXECUTEINFOA sei
)
2036 SHELLEXECUTEINFOW seiW
;
2038 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
2042 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
2045 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
2048 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
2050 if (sei
->lpParameters
)
2051 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
2053 if (sei
->lpDirectory
)
2054 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
2056 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
2057 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
2059 seiW
.lpClass
= NULL
;
2061 ret
= SHELL_execute(&seiW
, SHELL_ExecuteW
);
2063 sei
->hInstApp
= seiW
.hInstApp
;
2065 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
2066 sei
->hProcess
= seiW
.hProcess
;
2070 SHFree(wParameters
);
2077 /*************************************************************************
2078 * ShellExecuteExW [SHELL32.293]
2084 ShellExecuteExW(LPSHELLEXECUTEINFOW sei
)
2086 return SHELL_execute(sei
, SHELL_ExecuteW
);
2089 /*************************************************************************
2090 * ShellExecuteW [SHELL32.294]
2092 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
2093 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
2095 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
2096 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
2098 SHELLEXECUTEINFOW sei
;
2101 sei
.cbSize
= sizeof(sei
);
2102 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2104 sei
.lpVerb
= lpOperation
;
2105 sei
.lpFile
= lpFile
;
2106 sei
.lpParameters
= lpParameters
;
2107 sei
.lpDirectory
= lpDirectory
;
2108 sei
.nShow
= nShowCmd
;
2115 SHELL_execute(&sei
, SHELL_ExecuteW
);
2116 return sei
.hInstApp
;
2119 /*************************************************************************
2120 * WOWShellExecute [SHELL32.@]
2122 * FIXME: the callback function most likely doesn't work the same way on Windows.
2124 EXTERN_C HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
2125 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
2127 SHELLEXECUTEINFOW seiW
;
2128 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
2129 HANDLE hProcess
= 0;
2131 seiW
.lpVerb
= lpOperation
? __SHCloneStrAtoW(&wVerb
, lpOperation
) : NULL
;
2132 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
2133 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
2134 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
2136 seiW
.cbSize
= sizeof(seiW
);
2139 seiW
.nShow
= iShowCmd
;
2144 seiW
.hProcess
= hProcess
;
2146 SHELL_execute(&seiW
, (SHELL_ExecuteW32
)callback
);
2150 SHFree(wParameters
);
2152 return seiW
.hInstApp
;
2155 /*************************************************************************
2156 * OpenAs_RunDLLA [SHELL32.@]
2158 EXTERN_C
void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2160 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2163 /*************************************************************************
2164 * OpenAs_RunDLLW [SHELL32.@]
2166 EXTERN_C
void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2168 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);