2 * Shell Library Functions
4 * Copyright 1998 Marcus Meissner
5 * Copyright 2002 Eric Pouech
6 * Copyright 2018 Katayama Hirofumi MZ
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <undocshell.h>
26 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
28 static const WCHAR wszOpen
[] = L
"open";
29 static const WCHAR wszExe
[] = L
".exe";
30 static const WCHAR wszCom
[] = L
".com";
32 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
34 typedef UINT_PTR (*SHELL_ExecuteW32
)(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
35 const SHELLEXECUTEINFOW
*sei
, LPSHELLEXECUTEINFOW sei_out
);
37 static void ParseNoTildeEffect(PWSTR
&res
, LPCWSTR
&args
, DWORD
&len
, DWORD
&used
, int argNum
)
39 bool firstCharQuote
= false;
40 bool quotes_opened
= false;
41 bool backslash_encountered
= false;
43 for (int curArg
= 0; curArg
<= argNum
&& *args
; ++curArg
)
45 firstCharQuote
= false;
49 firstCharQuote
= true;
57 // if we found a backslash then flip the variable
58 backslash_encountered
= !backslash_encountered
;
60 else if (*args
== '"')
64 if (*(args
+ 1) != '"')
66 quotes_opened
= false;
80 backslash_encountered
= false;
84 backslash_encountered
= false;
85 if (*args
== ' ' && !firstCharQuote
)
104 static void ParseTildeEffect(PWSTR
&res
, LPCWSTR
&args
, DWORD
&len
, DWORD
&used
, int argNum
)
106 bool quotes_opened
= false;
107 bool backslash_encountered
= false;
109 for (int curArg
= 0; curArg
<= argNum
&& *args
; ++curArg
)
115 // if we found a backslash then flip the variable
116 backslash_encountered
= !backslash_encountered
;
118 else if (*args
== '"')
122 if (*(args
+ 1) != '"')
124 quotes_opened
= false;
133 quotes_opened
= true;
136 backslash_encountered
= false;
140 backslash_encountered
= false;
141 if (*args
== ' ' && !quotes_opened
&& curArg
!= argNum
)
145 if (curArg
== argNum
)
157 /***********************************************************************
158 * SHELL_ArgifyW [Internal]
160 * this function is supposed to expand the escape sequences found in the registry
161 * some diving reported that the following were used:
162 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
167 * %I address of a global item ID (explorer switch /idlist)
168 * %L seems to be %1 as long filename followed by the 8+3 variation
170 * %* all following parameters (see batfile)
172 * The way we parse the command line arguments was determined through extensive
173 * testing and can be summed up by the following rules"
176 * - if first letter is " break on first non literal " and include any white spaces
177 * - if first letter is NOT " break on first " or white space
178 * - if " is opened any pair of consecutive " results in ONE literal "
181 * - use rules from here http://www.autohotkey.net/~deleyd/parameters/parameters.htm
184 static BOOL
SHELL_ArgifyW(WCHAR
* out
, DWORD len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
, DWORD
* out_len
, const WCHAR
* lpDir
)
188 BOOL found_p1
= FALSE
;
192 bool tildeEffect
= false;
194 TRACE("Before parsing: %p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
195 debugstr_w(lpFile
), pidl
, args
);
256 ParseTildeEffect(res
, args
, len
, used
, *fmt
- '2');
261 ParseNoTildeEffect(res
, args
, len
, used
, *fmt
- '2');
268 if (!done
|| (*fmt
== '1'))
270 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
271 if (SearchPathW(lpDir
, lpFile
, wszExe
, sizeof(xlpFile
) / sizeof(WCHAR
), xlpFile
, NULL
))
287 * IE uses this a lot for activating things such as windows media
288 * player. This is not verified to be fully correct but it appears
295 used
+= wcslen(lpFile
);
299 res
+= wcslen(lpFile
);
310 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
311 * allowing a buffer of 100 should more than exceed all needs */
314 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
315 pv
= SHLockShared(hmem
, 0);
316 chars
= swprintf(buf
, L
":%p", pv
);
318 if (chars
>= sizeof(buf
) / sizeof(WCHAR
))
319 ERR("pidl format buffer too small!\n");
335 * Check if this is an env-variable here...
338 /* Make sure that we have at least one more %.*/
339 if (strchrW(fmt
, '%'))
341 WCHAR tmpBuffer
[1024];
342 PWSTR tmpB
= tmpBuffer
;
343 WCHAR tmpEnvBuff
[MAX_PATH
];
350 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer
));
352 envRet
= GetEnvironmentVariableW(tmpBuffer
, tmpEnvBuff
, MAX_PATH
);
353 if (envRet
== 0 || envRet
> MAX_PATH
)
355 used
+= wcslen(tmpBuffer
);
358 wcscpy( res
, tmpBuffer
);
359 res
+= wcslen(tmpBuffer
);
364 used
+= wcslen(tmpEnvBuff
);
367 wcscpy( res
, tmpEnvBuff
);
368 res
+= wcslen(tmpEnvBuff
);
375 /* Don't skip past terminator (catch a single '%' at the end) */
392 if (res
- out
< static_cast<int>(len
))
397 TRACE("used %i of %i space\n", used
, len
);
401 TRACE("After parsing: %p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
402 debugstr_w(lpFile
), pidl
, args
);
407 static HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
410 CComPtr
<IShellFolder
> desktop
;
412 HRESULT hr
= SHGetDesktopFolder(&desktop
);
416 hr
= desktop
->GetDisplayNameOf(pidl
, SHGDN_FORPARSING
, &strret
);
419 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
425 /*************************************************************************
426 * SHELL_ExecuteW [Internal]
429 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
430 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
432 STARTUPINFOW startup
;
433 PROCESS_INFORMATION info
;
434 UINT_PTR retval
= SE_ERR_NOASSOC
;
436 WCHAR curdir
[MAX_PATH
];
437 DWORD dwCreationFlags
;
438 const WCHAR
*lpDirectory
= NULL
;
440 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
442 /* make sure we don't fail the CreateProcess if the calling app passes in
443 * a bad working directory */
444 if (psei
->lpDirectory
&& psei
->lpDirectory
[0])
446 DWORD attr
= GetFileAttributesW(psei
->lpDirectory
);
447 if (attr
!= INVALID_FILE_ATTRIBUTES
&& attr
& FILE_ATTRIBUTE_DIRECTORY
)
448 lpDirectory
= psei
->lpDirectory
;
451 /* ShellExecute specifies the command from psei->lpDirectory
452 * if present. Not from the current dir as CreateProcess does */
454 if ((gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
455 if (!SetCurrentDirectoryW( lpDirectory
))
456 ERR("cannot set directory %s\n", debugstr_w(lpDirectory
));
458 ZeroMemory(&startup
, sizeof(STARTUPINFOW
));
459 startup
.cb
= sizeof(STARTUPINFOW
);
460 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
461 startup
.wShowWindow
= psei
->nShow
;
462 dwCreationFlags
= CREATE_UNICODE_ENVIRONMENT
;
463 if (!(psei
->fMask
& SEE_MASK_NO_CONSOLE
))
464 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
465 startup
.lpTitle
= (LPWSTR
)(psei
->fMask
& (SEE_MASK_HASLINKNAME
| SEE_MASK_HASTITLE
) ? psei
->lpClass
: NULL
);
467 if (psei
->fMask
& SEE_MASK_HASLINKNAME
)
468 startup
.dwFlags
|= STARTF_TITLEISLINKNAME
;
470 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, dwCreationFlags
, env
,
471 lpDirectory
, &startup
, &info
))
473 /* Give 30 seconds to the app to come up, if desired. Probably only needed
474 when starting app immediately before making a DDE connection. */
476 if (WaitForInputIdle(info
.hProcess
, 30000) == WAIT_FAILED
)
477 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
480 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
481 psei_out
->hProcess
= info
.hProcess
;
483 CloseHandle( info
.hProcess
);
484 CloseHandle( info
.hThread
);
486 else if ((retval
= GetLastError()) >= 32)
488 WARN("CreateProcess returned error %ld\n", retval
);
489 retval
= ERROR_BAD_FORMAT
;
492 TRACE("returning %lu\n", retval
);
494 psei_out
->hInstApp
= (HINSTANCE
)retval
;
497 if (!SetCurrentDirectoryW(curdir
))
498 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
504 /***********************************************************************
505 * SHELL_BuildEnvW [Internal]
507 * Build the environment for the new process, adding the specified
508 * path to the PATH variable. Returned pointer must be freed by caller.
510 static LPWSTR
SHELL_BuildEnvW( const WCHAR
*path
)
512 static const WCHAR wPath
[] = L
"PATH=";
513 WCHAR
*strings
, *new_env
;
515 int total
= wcslen(path
) + 1;
516 BOOL got_path
= FALSE
;
518 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
522 int len
= wcslen(p
) + 1;
523 if (!_wcsnicmp( p
, wPath
, 5 )) got_path
= TRUE
;
527 if (!got_path
) total
+= 5; /* we need to create PATH */
528 total
++; /* terminating null */
530 if (!(new_env
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, total
* sizeof(WCHAR
))))
532 FreeEnvironmentStringsW(strings
);
539 int len
= wcslen(p
) + 1;
540 memcpy(p2
, p
, len
* sizeof(WCHAR
));
541 if (!_wcsnicmp( p
, wPath
, 5 ))
544 wcscpy( p2
+ len
, path
);
545 p2
+= wcslen(path
) + 1;
554 p2
+= wcslen(p2
) + 1;
557 FreeEnvironmentStringsW(strings
);
562 /***********************************************************************
563 * SHELL_TryAppPathW [Internal]
565 * Helper function for SHELL_FindExecutable
566 * @param lpResult - pointer to a buffer of size MAX_PATH
567 * On entry: szName is a filename (probably without path separators).
568 * On exit: if szName found in "App Path", place full path in lpResult, and return true
570 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
578 if (env
) *env
= NULL
;
579 wcscpy(buffer
, L
"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
580 wcscat(buffer
, szName
);
581 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
584 // Add ".exe" extension, if extension does not exists
585 if (PathAddExtensionW(buffer
, wszExe
))
587 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
592 len
= MAX_PATH
* sizeof(WCHAR
);
593 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
599 DWORD count
= sizeof(buffer
);
600 if (!RegQueryValueExW(hkApp
, L
"Path", NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
601 *env
= SHELL_BuildEnvW(buffer
);
605 if (hkApp
) RegCloseKey(hkApp
);
609 /*************************************************************************
610 * SHELL_FindExecutableByVerb [Internal]
612 * called from SHELL_FindExecutable or SHELL_execute_class
614 * classname a buffer, big enough, to get the key name to do actually the
615 * command "WordPad.Document.1\\shell\\open\\command"
616 * passed as "WordPad.Document.1"
618 * lpVerb the operation on it (open)
619 * commandlen the size of command buffer (in bytes)
621 * command a buffer, to store the command to do the
622 * operation on the file
623 * key a buffer, big enough, to get the key name to do actually the
624 * command "WordPad.Document.1\\shell\\open\\command"
627 static UINT
SHELL_FindExecutableByVerb(LPCWSTR lpVerb
, LPWSTR key
, LPWSTR classname
, LPWSTR command
, LONG commandlen
)
629 static const WCHAR wCommand
[] = L
"\\command";
631 WCHAR verb
[MAX_PATH
];
633 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, classname
, 0, 0x02000000, &hkeyClass
))
634 return SE_ERR_NOASSOC
;
635 if (!HCR_GetDefaultVerbW(hkeyClass
, lpVerb
, verb
, sizeof(verb
) / sizeof(verb
[0])))
636 return SE_ERR_NOASSOC
;
637 RegCloseKey(hkeyClass
);
639 /* Looking for ...buffer\shell\<verb>\command */
640 wcscat(classname
, L
"\\shell\\");
641 wcscat(classname
, verb
);
642 wcscat(classname
, wCommand
);
644 if (RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, command
,
645 &commandlen
) == ERROR_SUCCESS
)
647 commandlen
/= sizeof(WCHAR
);
648 if (key
) wcscpy(key
, classname
);
652 LONG paramlen
= sizeof(param
);
653 static const WCHAR wSpace
[] = {' ', 0};
655 /* FIXME: it seems all Windows version don't behave the same here.
656 * the doc states that this ddeexec information can be found after
658 * on Win98, it doesn't appear, but I think it does on Win2k
660 /* Get the parameters needed by the application
661 from the associated ddeexec key */
662 tmp
= strstrW(classname
, wCommand
);
664 wcscat(classname
, wDdeexec
);
665 if (RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, param
,
666 ¶mlen
) == ERROR_SUCCESS
)
668 paramlen
/= sizeof(WCHAR
);
669 wcscat(command
, wSpace
);
670 wcscat(command
, param
);
671 commandlen
+= paramlen
;
675 command
[commandlen
] = '\0';
677 return 33; /* FIXME see SHELL_FindExecutable() */
680 return SE_ERR_NOASSOC
;
683 /*************************************************************************
684 * SHELL_FindExecutable [Internal]
686 * Utility for code sharing between FindExecutable and ShellExecute
688 * lpFile the name of a file
689 * lpVerb the operation on it (open)
691 * lpResult a buffer, big enough :-(, to store the command to do the
692 * operation on the file
693 * key a buffer, big enough, to get the key name to do actually the
694 * command (it'll be used afterwards for more information
697 static UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpVerb
,
698 LPWSTR lpResult
, DWORD resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
700 WCHAR
*extension
= NULL
; /* pointer to file extension */
701 WCHAR classname
[256]; /* registry name for this file type */
702 LONG classnamelen
= sizeof(classname
); /* length of above */
703 WCHAR command
[1024]; /* command from registry */
704 WCHAR wBuffer
[256]; /* Used to GetProfileString */
705 UINT retval
= SE_ERR_NOASSOC
;
706 WCHAR
*tok
; /* token pointer */
707 WCHAR xlpFile
[256]; /* result of SearchPath */
708 DWORD attribs
; /* file attributes */
710 TRACE("%s\n", debugstr_w(lpFile
));
713 return ERROR_INVALID_PARAMETER
;
716 lpResult
[0] = '\0'; /* Start off with an empty return string */
717 if (key
) *key
= '\0';
719 /* trap NULL parameters on entry */
722 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
723 debugstr_w(lpFile
), debugstr_w(lpResult
));
724 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
727 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
729 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
733 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
) / sizeof(WCHAR
), xlpFile
, NULL
))
735 TRACE("SearchPathW returned non-zero\n");
737 /* The file was found in the application-supplied default directory (or the system search path) */
739 else if (lpPath
&& SearchPathW(NULL
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
741 TRACE("SearchPathW returned non-zero\n");
743 /* The file was found in one of the directories in the system-wide search path */
746 attribs
= GetFileAttributesW(lpFile
);
747 if (attribs
!= INVALID_FILE_ATTRIBUTES
&& (attribs
& FILE_ATTRIBUTE_DIRECTORY
))
749 wcscpy(classname
, L
"Folder");
753 /* Did we get something? Anything? */
756 TRACE("Returning SE_ERR_FNF\n");
759 /* First thing we need is the file's extension */
760 extension
= wcsrchr(xlpFile
, '.'); /* Assume last "." is the one; */
761 /* File->Run in progman uses */
763 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
765 if (extension
== NULL
|| extension
[1] == 0)
767 WARN("Returning SE_ERR_NOASSOC\n");
768 return SE_ERR_NOASSOC
;
771 /* Three places to check: */
772 /* 1. win.ini, [windows], programs (NB no leading '.') */
773 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
774 /* 3. win.ini, [extensions], extension (NB no leading '.' */
775 /* All I know of the order is that registry is checked before */
776 /* extensions; however, it'd make sense to check the programs */
777 /* section first, so that's what happens here. */
779 /* See if it's a program - if GetProfileString fails, we skip this
780 * section. Actually, if GetProfileString fails, we've probably
781 * got a lot more to worry about than running a program... */
782 if (GetProfileStringW(L
"windows", L
"programs", L
"exe pif bat cmd com", wBuffer
, sizeof(wBuffer
) / sizeof(WCHAR
)) > 0)
789 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
793 while (*p
== ' ' || *p
== '\t') p
++;
796 if (wcsicmp(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
798 wcscpy(lpResult
, xlpFile
);
799 /* Need to perhaps check that the file has a path
801 TRACE("found %s\n", debugstr_w(lpResult
));
803 /* Greater than 32 to indicate success */
810 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, classname
,
811 &classnamelen
) == ERROR_SUCCESS
)
813 classnamelen
/= sizeof(WCHAR
);
814 if (classnamelen
== sizeof(classname
) / sizeof(WCHAR
))
817 classname
[classnamelen
] = '\0';
818 TRACE("File type: %s\n", debugstr_w(classname
));
828 /* pass the verb string to SHELL_FindExecutableByVerb() */
829 retval
= SHELL_FindExecutableByVerb(lpVerb
, key
, classname
, command
, sizeof(command
));
834 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
, lpPath
);
835 if (finishedLen
> resultLen
)
836 ERR("Argify buffer not large enough.. truncated\n");
837 /* Remove double quotation marks and command line arguments */
838 if (*lpResult
== '"')
841 while (*(p
+ 1) != '"')
850 /* Truncate on first space */
852 while (*p
!= ' ' && *p
!= '\0')
858 else /* Check win.ini */
860 /* Toss the leading dot */
862 if (GetProfileStringW(L
"extensions", extension
, L
"", command
, sizeof(command
) / sizeof(WCHAR
)) > 0)
864 if (wcslen(command
) != 0)
866 wcscpy(lpResult
, command
);
867 tok
= wcschr(lpResult
, '^'); /* should be ^.extension? */
871 wcscat(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
872 tok
= wcschr(command
, '^'); /* see above */
873 if ((tok
!= NULL
) && (wcslen(tok
) > 5))
875 wcscat(lpResult
, &tok
[5]);
878 retval
= 33; /* FIXME - see above */
883 TRACE("returning %s\n", debugstr_w(lpResult
));
887 /******************************************************************
890 * callback for the DDE connection. not really useful
892 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
893 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
894 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
896 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
897 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
901 /******************************************************************
904 * ShellExecute helper. Used to do an operation with a DDE connection
906 * Handles both the direct connection (try #1), and if it fails,
907 * launching an application and trying (#2) to connect to it
910 static unsigned dde_connect(const WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
911 const WCHAR
* lpFile
, WCHAR
*env
,
912 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
913 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
916 WCHAR
* endkey
= regkey
+ wcslen(key
);
917 WCHAR app
[256], topic
[256], ifexec
[256], static_res
[256];
918 WCHAR
* dynamic_res
=NULL
;
920 LONG applen
, topiclen
, ifexeclen
;
924 DWORD resultLen
, endkeyLen
;
925 HSZ hszApp
, hszTopic
;
928 unsigned ret
= SE_ERR_NOASSOC
;
929 BOOL unicode
= !(GetVersion() & 0x80000000);
931 if (strlenW(key
) + 1 > sizeof(regkey
) / sizeof(regkey
[0]))
933 FIXME("input parameter %s larger than buffer\n", debugstr_w(key
));
937 static const WCHAR wApplication
[] = L
"\\application";
938 endkeyLen
= sizeof(regkey
) / sizeof(regkey
[0]) - (endkey
- regkey
);
939 if (strlenW(wApplication
) + 1 > endkeyLen
)
941 FIXME("endkey %s overruns buffer\n", debugstr_w(wApplication
));
944 wcscpy(endkey
, wApplication
);
945 applen
= sizeof(app
);
946 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, app
, &applen
) != ERROR_SUCCESS
)
948 WCHAR command
[1024], fullpath
[MAX_PATH
];
949 static const WCHAR wSo
[] = L
".so";
950 DWORD sizeSo
= sizeof(wSo
) / sizeof(WCHAR
);
954 /* Get application command from start string and find filename of application */
957 if (strlenW(start
+ 1) + 1 > sizeof(command
) / sizeof(command
[0]))
959 FIXME("size of input parameter %s larger than buffer\n",
960 debugstr_w(start
+ 1));
963 wcscpy(command
, start
+ 1);
964 if ((ptr
= wcschr(command
, '"')))
966 ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
);
972 for (p
= start
; (space
= const_cast<LPWSTR
>(strchrW(p
, ' '))); p
= space
+ 1)
974 int idx
= space
- start
;
975 memcpy(command
, start
, idx
* sizeof(WCHAR
));
977 if ((ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
)))
981 ret
= SearchPathW(NULL
, start
, wszExe
, sizeof(fullpath
) / sizeof(WCHAR
), fullpath
, &ptr
);
986 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
987 return ERROR_ACCESS_DENIED
;
989 if (strlenW(ptr
) + 1 > sizeof(app
) / sizeof(app
[0]))
991 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr
));
996 /* Remove extensions (including .so) */
997 ptr
= app
+ wcslen(app
) - (sizeSo
- 1);
998 if (wcslen(app
) >= sizeSo
&&
1002 ptr
= const_cast<LPWSTR
>(strrchrW(app
, '.'));
1007 static const WCHAR wTopic
[] = L
"\\topic";
1008 if (strlenW(wTopic
) + 1 > endkeyLen
)
1010 FIXME("endkey %s overruns buffer\n", debugstr_w(wTopic
));
1013 wcscpy(endkey
, wTopic
);
1014 topiclen
= sizeof(topic
);
1015 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, topic
, &topiclen
) != ERROR_SUCCESS
)
1017 wcscpy(topic
, L
"System");
1022 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
1027 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
1031 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
1032 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
1034 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
1038 TRACE("Launching %s\n", debugstr_w(start
));
1039 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
1042 TRACE("Couldn't launch\n");
1045 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
1048 TRACE("Couldn't connect. ret=%d\n", ret
);
1049 DdeUninitialize(ddeInst
);
1050 SetLastError(ERROR_DDE_FAIL
);
1051 return 30; /* whatever */
1053 static const WCHAR wIfexec
[] = L
"\\ifexec";
1054 if (strlenW(wIfexec
) + 1 > endkeyLen
)
1056 FIXME("endkey %s overruns buffer\n", debugstr_w(wIfexec
));
1059 strcpyW(endkey
, wIfexec
);
1060 ifexeclen
= sizeof(ifexec
);
1061 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
1067 SHELL_ArgifyW(static_res
, sizeof(static_res
)/sizeof(WCHAR
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
, NULL
);
1068 if (resultLen
> sizeof(static_res
)/sizeof(WCHAR
))
1070 res
= dynamic_res
= static_cast<WCHAR
*>(HeapAlloc(GetProcessHeap(), 0, resultLen
* sizeof(WCHAR
)));
1071 SHELL_ArgifyW(dynamic_res
, resultLen
, exec
, lpFile
, pidl
, szCommandline
, NULL
, NULL
);
1075 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
1077 /* It's documented in the KB 330337 that IE has a bug and returns
1078 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
1081 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0, XTYP_EXECUTE
, 30000, &tid
);
1084 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
1085 char *resA
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, lenA
);
1086 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
1087 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
1088 XTYP_EXECUTE
, 10000, &tid
);
1089 HeapFree(GetProcessHeap(), 0, resA
);
1092 DdeFreeDataHandle(hDdeData
);
1094 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
1097 HeapFree(GetProcessHeap(), 0, dynamic_res
);
1099 DdeDisconnect(hConv
);
1102 DdeUninitialize(ddeInst
);
1107 /*************************************************************************
1108 * execute_from_key [Internal]
1110 static UINT_PTR
execute_from_key(LPCWSTR key
, LPCWSTR lpFile
, WCHAR
*env
,
1111 LPCWSTR szCommandline
, LPCWSTR executable_name
,
1112 SHELL_ExecuteW32 execfunc
,
1113 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
1115 WCHAR cmd
[256], param
[1024], ddeexec
[256];
1116 DWORD cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
1117 UINT_PTR retval
= SE_ERR_NOASSOC
;
1121 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
1122 debugstr_w(szCommandline
), debugstr_w(executable_name
));
1127 /* Get the application from the registry */
1128 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, (LONG
*)&cmdlen
) == ERROR_SUCCESS
)
1130 TRACE("got cmd: %s\n", debugstr_w(cmd
));
1132 /* Is there a replace() function anywhere? */
1133 cmdlen
/= sizeof(WCHAR
);
1134 if (cmdlen
>= sizeof(cmd
) / sizeof(WCHAR
))
1135 cmdlen
= sizeof(cmd
) / sizeof(WCHAR
) - 1;
1137 SHELL_ArgifyW(param
, sizeof(param
) / sizeof(WCHAR
), cmd
, lpFile
, (LPITEMIDLIST
)psei
->lpIDList
, szCommandline
, &resultLen
,
1138 (psei
->lpDirectory
&& *psei
->lpDirectory
) ? psei
->lpDirectory
: NULL
);
1139 if (resultLen
> sizeof(param
) / sizeof(WCHAR
))
1140 ERR("Argify buffer not large enough, truncating\n");
1143 /* Get the parameters needed by the application
1144 from the associated ddeexec key */
1145 tmp
= const_cast<LPWSTR
>(strstrW(key
, L
"command"));
1147 wcscpy(tmp
, L
"ddeexec");
1149 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, (LONG
*)&ddeexeclen
) == ERROR_SUCCESS
)
1151 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
1152 if (!param
[0]) strcpyW(param
, executable_name
);
1153 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, (LPITEMIDLIST
)psei
->lpIDList
, execfunc
, psei
, psei_out
);
1157 TRACE("executing: %s\n", debugstr_w(param
));
1158 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
1161 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
1166 /*************************************************************************
1167 * FindExecutableA [SHELL32.@]
1169 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
1172 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
1173 WCHAR wResult
[MAX_PATH
];
1175 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
1176 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
1178 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
1179 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
1183 TRACE("returning %s\n", lpResult
);
1187 /*************************************************************************
1188 * FindExecutableW [SHELL32.@]
1190 * This function returns the executable associated with the specified file
1191 * for the default verb.
1194 * lpFile [I] The file to find the association for. This must refer to
1195 * an existing file otherwise FindExecutable fails and returns
1197 * lpResult [O] Points to a buffer into which the executable path is
1198 * copied. This parameter must not be NULL otherwise
1199 * FindExecutable() segfaults. The buffer must be of size at
1200 * least MAX_PATH characters.
1203 * A value greater than 32 on success, less than or equal to 32 otherwise.
1204 * See the SE_ERR_* constants.
1207 * On Windows XP and 2003, FindExecutable() seems to first convert the
1208 * filename into 8.3 format, thus taking into account only the first three
1209 * characters of the extension, and expects to find an association for those.
1210 * However other Windows versions behave sanely.
1212 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
1214 UINT_PTR retval
= SE_ERR_NOASSOC
;
1215 WCHAR old_dir
[1024];
1216 WCHAR res
[MAX_PATH
];
1218 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
1220 lpResult
[0] = '\0'; /* Start off with an empty return string */
1222 return (HINSTANCE
)SE_ERR_FNF
;
1226 GetCurrentDirectoryW(sizeof(old_dir
) / sizeof(WCHAR
), old_dir
);
1227 SetCurrentDirectoryW(lpDirectory
);
1230 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, res
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1232 strcpyW(lpResult
, res
);
1234 TRACE("returning %s\n", debugstr_w(lpResult
));
1236 SetCurrentDirectoryW(old_dir
);
1237 return (HINSTANCE
)retval
;
1240 /* FIXME: is this already implemented somewhere else? */
1241 static HKEY
ShellExecute_GetClassKey(const SHELLEXECUTEINFOW
*sei
)
1243 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1245 DWORD type
= 0, sz
= 0;
1249 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1250 return sei
->hkeyClass
;
1252 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1253 lpClass
= sei
->lpClass
;
1256 ext
= PathFindExtensionW(sei
->lpFile
);
1257 TRACE("ext = %s\n", debugstr_w(ext
));
1261 r
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
1262 if (r
!= ERROR_SUCCESS
)
1265 r
= RegQueryValueExW(hkey
, NULL
, 0, &type
, NULL
, &sz
);
1266 if (r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1268 sz
+= sizeof (WCHAR
);
1269 cls
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sz
);
1271 RegQueryValueExW(hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1274 RegCloseKey( hkey
);
1278 TRACE("class = %s\n", debugstr_w(lpClass
));
1282 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1284 HeapFree(GetProcessHeap(), 0, cls
);
1289 static IDataObject
*shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
)
1291 LPCITEMIDLIST pidllast
= NULL
;
1292 CComPtr
<IDataObject
> dataobj
;
1293 CComPtr
<IShellFolder
> shf
;
1294 LPITEMIDLIST pidl
= NULL
;
1297 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1298 pidl
= (LPITEMIDLIST
)sei
->lpIDList
;
1301 WCHAR fullpath
[MAX_PATH
];
1305 ret
= GetFullPathNameW(sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1309 pidl
= ILCreateFromPathW(fullpath
);
1312 r
= SHBindToParent(pidl
, IID_PPV_ARG(IShellFolder
, &shf
), &pidllast
);
1316 shf
->GetUIObjectOf(NULL
, 1, &pidllast
, IID_NULL_PPV_ARG(IDataObject
, &dataobj
));
1319 if (pidl
!= sei
->lpIDList
)
1321 return dataobj
.Detach();
1324 static HRESULT
shellex_run_context_menu_default(IShellExtInit
*obj
,
1325 LPSHELLEXECUTEINFOW sei
)
1327 CComPtr
<IContextMenu
> cm
= NULL
;
1328 CMINVOKECOMMANDINFOEX ici
;
1335 TRACE("%p %p\n", obj
, sei
);
1337 r
= obj
->QueryInterface(IID_PPV_ARG(IContextMenu
, &cm
));
1341 hmenu
= CreateMenu();
1345 /* the number of the last menu added is returned in r */
1346 r
= cm
->QueryContextMenu(hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1350 n
= GetMenuItemCount(hmenu
);
1351 for (i
= 0; i
< n
; i
++)
1353 memset(&info
, 0, sizeof(info
));
1354 info
.cbSize
= sizeof info
;
1355 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1356 info
.dwTypeData
= string
;
1357 info
.cch
= sizeof string
;
1359 GetMenuItemInfoW(hmenu
, i
, TRUE
, &info
);
1361 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1362 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1363 if ((!sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
)) ||
1364 (sei
->lpVerb
&& !lstrcmpiW(sei
->lpVerb
, string
)))
1375 memset(&ici
, 0, sizeof ici
);
1376 ici
.cbSize
= sizeof ici
;
1377 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NO_CONSOLE
| SEE_MASK_NOASYNC
| SEE_MASK_ASYNCOK
| SEE_MASK_FLAG_NO_UI
));
1378 ici
.nShow
= sei
->nShow
;
1379 ici
.lpVerb
= MAKEINTRESOURCEA(def
);
1380 ici
.hwnd
= sei
->hwnd
;
1381 ici
.lpParametersW
= sei
->lpParameters
;
1383 r
= cm
->InvokeCommand((LPCMINVOKECOMMANDINFO
)&ici
);
1385 TRACE("invoke command returned %08x\n", r
);
1389 DestroyMenu( hmenu
);
1393 static HRESULT
shellex_load_object_and_run(HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1395 // Can not use CComPtr here because of CoUninitialize at the end, before the destructors would run.
1396 IDataObject
*dataobj
= NULL
;
1397 IObjectWithSite
*ows
= NULL
;
1398 IShellExtInit
*obj
= NULL
;
1401 TRACE("%p %s %p\n", hkey
, debugstr_guid(guid
), sei
);
1403 r
= CoInitialize(NULL
);
1407 r
= CoCreateInstance(*guid
, NULL
, CLSCTX_INPROC_SERVER
,
1408 IID_PPV_ARG(IShellExtInit
, &obj
));
1411 ERR("failed %08x\n", r
);
1415 dataobj
= shellex_get_dataobj(sei
);
1418 ERR("failed to get data object\n");
1423 r
= obj
->Initialize(NULL
, dataobj
, hkey
);
1427 r
= obj
->QueryInterface(IID_PPV_ARG(IObjectWithSite
, &ows
));
1433 r
= shellex_run_context_menu_default(obj
, sei
);
1447 /*************************************************************************
1448 * ShellExecute_FromContextMenu [Internal]
1450 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1452 HKEY hkey
, hkeycm
= 0;
1459 TRACE("%s\n", debugstr_w(sei
->lpFile
));
1461 hkey
= ShellExecute_GetClassKey(sei
);
1463 return ERROR_FUNCTION_FAILED
;
1465 r
= RegOpenKeyW(hkey
, L
"shellex\\ContextMenuHandlers", &hkeycm
);
1466 if (r
== ERROR_SUCCESS
)
1471 r
= RegEnumKeyW(hkeycm
, i
++, szguid
, sizeof(szguid
) / sizeof(szguid
[0]));
1472 if (r
!= ERROR_SUCCESS
)
1475 hr
= CLSIDFromString(szguid
, &guid
);
1478 /* stop at the first one that succeeds in running */
1479 hr
= shellex_load_object_and_run(hkey
, &guid
, sei
);
1484 RegCloseKey(hkeycm
);
1487 if (hkey
!= sei
->hkeyClass
)
1492 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
);
1494 static UINT_PTR
SHELL_execute_class(LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1496 WCHAR execCmd
[1024], classname
[1024];
1497 /* launch a document by fileclass like 'WordPad.Document.1' */
1498 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1499 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1500 ULONG cmask
= (psei
->fMask
& SEE_MASK_CLASSALL
);
1505 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1506 if (cmask
!= SEE_MASK_CLASSNAME
)
1509 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1510 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1512 execCmd
, sizeof(execCmd
));
1514 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1515 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1518 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
) / sizeof(WCHAR
), execCmd
, wszApplicationName
, (LPITEMIDLIST
)psei
->lpIDList
, NULL
, &resultLen
,
1519 (psei
->lpDirectory
&& *psei
->lpDirectory
) ? psei
->lpDirectory
: NULL
);
1520 if (!done
&& wszApplicationName
[0])
1522 strcatW(wcmd
, L
" ");
1523 if (*wszApplicationName
!= '"')
1525 strcatW(wcmd
, L
"\"");
1526 strcatW(wcmd
, wszApplicationName
);
1527 strcatW(wcmd
, L
"\"");
1530 strcatW(wcmd
, wszApplicationName
);
1532 if (resultLen
> sizeof(wcmd
) / sizeof(WCHAR
))
1533 ERR("Argify buffer not large enough... truncating\n");
1534 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1537 strcpyW(classname
, psei
->lpClass
);
1538 rslt
= SHELL_FindExecutableByVerb(psei
->lpVerb
, NULL
, classname
, execCmd
, sizeof(execCmd
));
1540 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt
, debugstr_w(classname
), debugstr_w(execCmd
));
1543 rslt
= SHELL_quote_and_execute( execCmd
, L
"", classname
,
1544 wszApplicationName
, NULL
, psei
,
1545 psei_out
, execfunc
);
1550 static BOOL
SHELL_translate_idlist(LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1552 static const WCHAR wExplorer
[] = L
"explorer.exe";
1553 WCHAR buffer
[MAX_PATH
];
1554 BOOL appKnownSingular
= FALSE
;
1556 /* last chance to translate IDList: now also allow CLSID paths */
1557 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST
)sei
->lpIDList
, buffer
, sizeof(buffer
)/sizeof(WCHAR
)))) {
1558 if (buffer
[0] == ':' && buffer
[1] == ':') {
1559 /* open shell folder for the specified class GUID */
1560 if (strlenW(buffer
) + 1 > parametersLen
)
1561 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1562 lstrlenW(buffer
) + 1, parametersLen
);
1563 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1564 if (strlenW(wExplorer
) > dwApplicationNameLen
)
1565 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1566 lstrlenW(wExplorer
) + 1, dwApplicationNameLen
);
1567 lstrcpynW(wszApplicationName
, wExplorer
, dwApplicationNameLen
);
1568 appKnownSingular
= TRUE
;
1570 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1572 WCHAR target
[MAX_PATH
];
1575 /* Check if we're executing a directory and if so use the
1576 handler for the Folder class */
1577 strcpyW(target
, buffer
);
1578 attribs
= GetFileAttributesW(buffer
);
1579 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1580 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1581 HCR_GetExecuteCommandW(0, L
"Folder",
1583 buffer
, sizeof(buffer
))) {
1584 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1585 buffer
, target
, (LPITEMIDLIST
)sei
->lpIDList
, NULL
, &resultLen
,
1586 (sei
->lpDirectory
&& *sei
->lpDirectory
) ? sei
->lpDirectory
: NULL
);
1587 if (resultLen
> dwApplicationNameLen
)
1588 ERR("Argify buffer not large enough... truncating\n");
1589 appKnownSingular
= FALSE
;
1591 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1594 return appKnownSingular
;
1597 static UINT_PTR
SHELL_quote_and_execute(LPCWSTR wcmd
, LPCWSTR wszParameters
, LPCWSTR wszKeyname
, LPCWSTR wszApplicationName
, LPWSTR env
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1601 WCHAR
*wszQuotedCmd
;
1603 /* Length of quotes plus length of command plus NULL terminator */
1604 len
= 2 + lstrlenW(wcmd
) + 1;
1605 if (wszParameters
[0])
1607 /* Length of space plus length of parameters */
1608 len
+= 1 + lstrlenW(wszParameters
);
1610 wszQuotedCmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1611 /* Must quote to handle case where cmd contains spaces,
1612 * else security hole if malicious user creates executable file "C:\\Program"
1614 strcpyW(wszQuotedCmd
, L
"\"");
1615 strcatW(wszQuotedCmd
, wcmd
);
1616 strcatW(wszQuotedCmd
, L
"\"");
1617 if (wszParameters
[0])
1619 strcatW(wszQuotedCmd
, L
" ");
1620 strcatW(wszQuotedCmd
, wszParameters
);
1623 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(wszKeyname
));
1626 retval
= execute_from_key(wszKeyname
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1628 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1629 HeapFree(GetProcessHeap(), 0, wszQuotedCmd
);
1633 static UINT_PTR
SHELL_execute_url(LPCWSTR lpFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1635 static const WCHAR wShell
[] = L
"\\shell\\";
1636 static const WCHAR wCommand
[] = L
"\\command";
1638 WCHAR
*lpstrProtocol
;
1643 lpstrRes
= strchrW(lpFile
, ':');
1645 iSize
= lpstrRes
- lpFile
;
1647 iSize
= strlenW(lpFile
);
1649 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1650 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1651 len
= iSize
+ lstrlenW(wShell
) + lstrlenW(wCommand
) + 1;
1652 if (psei
->lpVerb
&& *psei
->lpVerb
)
1653 len
+= lstrlenW(psei
->lpVerb
);
1655 len
+= lstrlenW(wszOpen
);
1656 lpstrProtocol
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1657 memcpy(lpstrProtocol
, lpFile
, iSize
* sizeof(WCHAR
));
1658 lpstrProtocol
[iSize
] = '\0';
1659 strcatW(lpstrProtocol
, wShell
);
1660 strcatW(lpstrProtocol
, psei
->lpVerb
&& *psei
->lpVerb
? psei
->lpVerb
: wszOpen
);
1661 strcatW(lpstrProtocol
, wCommand
);
1663 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1664 wcmd
, execfunc
, psei
, psei_out
);
1665 HeapFree(GetProcessHeap(), 0, lpstrProtocol
);
1669 static void do_error_dialog(UINT_PTR retval
, HWND hwnd
, WCHAR
* filename
)
1672 DWORD_PTR msgArguments
[3] = { (DWORD_PTR
)filename
, 0, 0 };
1675 error_code
= GetLastError();
1676 if (retval
== SE_ERR_NOASSOC
)
1677 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, sizeof(msg
) / sizeof(WCHAR
));
1679 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ARGUMENT_ARRAY
,
1684 sizeof(msg
) / sizeof(WCHAR
),
1685 (va_list*)msgArguments
);
1687 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1690 static WCHAR
*expand_environment( const WCHAR
*str
)
1695 len
= ExpandEnvironmentStringsW(str
, NULL
, 0);
1696 if (!len
) return NULL
;
1698 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1699 if (!buf
) return NULL
;
1701 len
= ExpandEnvironmentStringsW(str
, buf
, len
);
1704 HeapFree(GetProcessHeap(), 0, buf
);
1710 /*************************************************************************
1711 * SHELL_execute [Internal]
1713 static BOOL
SHELL_execute(LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1715 static const DWORD unsupportedFlags
=
1716 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1717 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1718 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1720 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1721 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
;
1722 DWORD dwApplicationNameLen
= MAX_PATH
+ 2;
1723 DWORD parametersLen
= sizeof(parametersBuffer
) / sizeof(WCHAR
);
1724 DWORD dirLen
= sizeof(dirBuffer
) / sizeof(WCHAR
);
1725 DWORD wcmdLen
= sizeof(wcmdBuffer
) / sizeof(WCHAR
);
1727 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1728 WCHAR wfileName
[MAX_PATH
];
1730 WCHAR wszKeyname
[256];
1732 UINT_PTR retval
= SE_ERR_NOASSOC
;
1733 BOOL appKnownSingular
= FALSE
;
1735 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1738 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1739 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1740 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1741 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1742 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1743 debugstr_w(sei_tmp
.lpClass
) : "not used");
1745 sei
->hProcess
= NULL
;
1747 /* make copies of all path/command strings */
1748 if (!sei_tmp
.lpFile
)
1750 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1751 *wszApplicationName
= '\0';
1753 else if (*sei_tmp
.lpFile
== '\"' && sei_tmp
.lpFile
[(len
= strlenW(sei_tmp
.lpFile
))-1] == '\"')
1755 if(len
-1 >= dwApplicationNameLen
)
1756 dwApplicationNameLen
= len
;
1758 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1759 memcpy(wszApplicationName
, sei_tmp
.lpFile
+ 1, len
* sizeof(WCHAR
));
1762 wszApplicationName
[len
-2] = '\0';
1763 appKnownSingular
= TRUE
;
1765 TRACE("wszApplicationName=%s\n", debugstr_w(wszApplicationName
));
1769 DWORD l
= strlenW(sei_tmp
.lpFile
) + 1;
1770 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+ 1;
1771 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1772 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
* sizeof(WCHAR
));
1775 wszParameters
= parametersBuffer
;
1776 if (sei_tmp
.lpParameters
)
1778 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1779 if (len
> parametersLen
)
1781 wszParameters
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1782 parametersLen
= len
;
1784 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1787 *wszParameters
= L
'\0';
1790 if (sei_tmp
.lpDirectory
)
1792 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1795 wszDir
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1798 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1803 /* adjust string pointers to point to the new buffers */
1804 sei_tmp
.lpFile
= wszApplicationName
;
1805 sei_tmp
.lpParameters
= wszParameters
;
1806 sei_tmp
.lpDirectory
= wszDir
;
1808 if (sei_tmp
.fMask
& unsupportedFlags
)
1810 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1813 /* process the IDList */
1814 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1816 CComPtr
<IShellExecuteHookW
> pSEH
;
1818 HRESULT hr
= SHBindToParent((LPCITEMIDLIST
)sei_tmp
.lpIDList
, IID_PPV_ARG(IShellExecuteHookW
, &pSEH
), NULL
);
1822 hr
= pSEH
->Execute(&sei_tmp
);
1826 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1827 if (wszParameters
!= parametersBuffer
)
1828 HeapFree(GetProcessHeap(), 0, wszParameters
);
1829 if (wszDir
!= dirBuffer
)
1830 HeapFree(GetProcessHeap(), 0, wszDir
);
1835 SHGetPathFromIDListW((LPCITEMIDLIST
)sei_tmp
.lpIDList
, wszApplicationName
);
1836 appKnownSingular
= TRUE
;
1837 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1840 if (sei_tmp
.fMask
& SEE_MASK_DOENVSUBST
)
1844 tmp
= expand_environment(sei_tmp
.lpFile
);
1849 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1850 sei_tmp
.lpFile
= wszApplicationName
= tmp
;
1852 tmp
= expand_environment(sei_tmp
.lpDirectory
);
1857 if (wszDir
!= dirBuffer
) HeapFree(GetProcessHeap(), 0, wszDir
);
1858 sei_tmp
.lpDirectory
= wszDir
= tmp
;
1861 if (ERROR_SUCCESS
== ShellExecute_FromContextMenu(&sei_tmp
))
1863 sei
->hInstApp
= (HINSTANCE
) 33;
1864 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1865 if (wszParameters
!= parametersBuffer
)
1866 HeapFree(GetProcessHeap(), 0, wszParameters
);
1867 if (wszDir
!= dirBuffer
)
1868 HeapFree(GetProcessHeap(), 0, wszDir
);
1872 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1874 retval
= SHELL_execute_class(wszApplicationName
, &sei_tmp
, sei
, execfunc
);
1875 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1882 Info
.pcszFile
= wszApplicationName
;
1883 Info
.pcszClass
= NULL
;
1884 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1886 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1887 DBG_UNREFERENCED_LOCAL_VARIABLE(Info
);
1888 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1890 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1891 if (wszParameters
!= parametersBuffer
)
1892 HeapFree(GetProcessHeap(), 0, wszParameters
);
1893 if (wszDir
!= dirBuffer
)
1894 HeapFree(GetProcessHeap(), 0, wszDir
);
1898 /* Has the IDList not yet been translated? */
1899 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1901 appKnownSingular
= SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1904 dwApplicationNameLen
);
1907 /* convert file URLs */
1908 if (UrlIsFileUrlW(sei_tmp
.lpFile
))
1914 buf
= static_cast<LPWSTR
>(HeapAlloc(GetProcessHeap(), 0, size
* sizeof(WCHAR
)));
1915 if (!buf
|| FAILED(PathCreateFromUrlW(sei_tmp
.lpFile
, buf
, &size
, 0)))
1917 HeapFree(GetProcessHeap(), 0, buf
);
1921 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1922 wszApplicationName
= buf
;
1923 sei_tmp
.lpFile
= wszApplicationName
;
1925 else /* or expand environment strings (not both!) */
1927 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1931 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1933 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1934 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1935 wszApplicationName
= buf
;
1936 /* appKnownSingular unmodified */
1938 sei_tmp
.lpFile
= wszApplicationName
;
1942 if (*sei_tmp
.lpDirectory
)
1944 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1949 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1950 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1951 if (wszDir
!= dirBuffer
)
1952 HeapFree(GetProcessHeap(), 0, wszDir
);
1954 sei_tmp
.lpDirectory
= wszDir
;
1958 /* Else, try to execute the filename */
1959 TRACE("execute: %s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1961 /* separate out command line arguments from executable file name */
1962 if (!*sei_tmp
.lpParameters
&& !appKnownSingular
)
1964 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1965 if (sei_tmp
.lpFile
[0] == L
'"')
1967 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1968 LPWSTR dst
= wfileName
;
1971 /* copy the unquoted executable path to 'wfileName' */
1972 while(*src
&& *src
!= L
'"')
1981 while(isspaceW(*src
))
1987 /* copy the parameter string to 'wszParameters' */
1988 strcpyW(wszParameters
, src
);
1990 /* terminate previous command string after the quote character */
1996 /* If the executable name is not quoted, we have to use this search loop here,
1997 that in CreateProcess() is not sufficient because it does not handle shell links. */
1998 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
2001 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
2002 for(s
= beg
; (space
= const_cast<LPWSTR
>(strchrW(s
, L
' '))); s
= space
+ 1)
2004 int idx
= space
- sei_tmp
.lpFile
;
2005 memcpy(buffer
, sei_tmp
.lpFile
, idx
* sizeof(WCHAR
));
2008 /*FIXME This finds directory paths if the targeted file name contains spaces. */
2009 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
) / sizeof(xlpFile
[0]), xlpFile
, NULL
))
2011 /* separate out command from parameter string */
2012 LPCWSTR p
= space
+ 1;
2017 strcpyW(wszParameters
, p
);
2024 lpFile
= sei_tmp
.lpFile
;
2028 lpFile
= sei_tmp
.lpFile
;
2031 len
= lstrlenW(wszApplicationName
) + 3;
2032 if (sei_tmp
.lpParameters
[0])
2033 len
+= 1 + lstrlenW(wszParameters
);
2036 wcmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
2039 swprintf(wcmd
, L
"\"%s\"", wszApplicationName
);
2040 if (sei_tmp
.lpParameters
[0])
2042 strcatW(wcmd
, L
" ");
2043 strcatW(wcmd
, wszParameters
);
2046 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
2049 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
2050 if (wszParameters
!= parametersBuffer
)
2051 HeapFree(GetProcessHeap(), 0, wszParameters
);
2052 if (wszDir
!= dirBuffer
)
2053 HeapFree(GetProcessHeap(), 0, wszDir
);
2054 if (wcmd
!= wcmdBuffer
)
2055 HeapFree(GetProcessHeap(), 0, wcmd
);
2059 /* Else, try to find the executable */
2061 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, wszKeyname
, &env
, (LPITEMIDLIST
)sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
2062 if (retval
> 32) /* Found */
2064 retval
= SHELL_quote_and_execute(wcmd
, wszParameters
, wszKeyname
,
2065 wszApplicationName
, env
, &sei_tmp
,
2067 HeapFree(GetProcessHeap(), 0, env
);
2069 else if (PathIsDirectoryW(lpFile
))
2071 WCHAR wExec
[MAX_PATH
];
2072 WCHAR
* lpQuotedFile
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (strlenW(lpFile
) + 3));
2076 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, L
"explorer",
2077 wszOpen
, wExec
, MAX_PATH
,
2078 NULL
, &env
, NULL
, NULL
);
2081 swprintf(lpQuotedFile
, L
"\"%s\"", lpFile
);
2082 retval
= SHELL_quote_and_execute(wExec
, lpQuotedFile
,
2084 wszApplicationName
, env
,
2085 &sei_tmp
, sei
, execfunc
);
2086 HeapFree(GetProcessHeap(), 0, env
);
2088 HeapFree(GetProcessHeap(), 0, lpQuotedFile
);
2091 retval
= 0; /* Out of memory */
2093 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
2095 retval
= SHELL_execute_url(lpFile
, wcmd
, &sei_tmp
, sei
, execfunc
);
2097 /* Check if file specified is in the form www.??????.*** */
2098 else if (!strncmpiW(lpFile
, L
"www", 3))
2100 /* if so, prefix lpFile with http:// and call ShellExecute */
2101 WCHAR lpstrTmpFile
[256];
2102 strcpyW(lpstrTmpFile
, L
"http://");
2103 strcatW(lpstrTmpFile
, lpFile
);
2104 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
2107 TRACE("retval %lu\n", retval
);
2109 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
2116 Info
.pcszFile
= wszApplicationName
;
2117 Info
.pcszClass
= NULL
;
2118 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
2120 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
2121 DBG_UNREFERENCED_LOCAL_VARIABLE(Info
);
2122 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
2125 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
2126 if (wszParameters
!= parametersBuffer
)
2127 HeapFree(GetProcessHeap(), 0, wszParameters
);
2128 if (wszDir
!= dirBuffer
)
2129 HeapFree(GetProcessHeap(), 0, wszDir
);
2130 if (wcmd
!= wcmdBuffer
)
2131 HeapFree(GetProcessHeap(), 0, wcmd
);
2133 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
2138 /*************************************************************************
2139 * ShellExecuteA [SHELL32.290]
2141 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpVerb
, LPCSTR lpFile
,
2142 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
2144 SHELLEXECUTEINFOA sei
;
2146 TRACE("%p,%s,%s,%s,%s,%d\n",
2147 hWnd
, debugstr_a(lpVerb
), debugstr_a(lpFile
),
2148 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
2150 sei
.cbSize
= sizeof(sei
);
2151 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2153 sei
.lpVerb
= lpVerb
;
2154 sei
.lpFile
= lpFile
;
2155 sei
.lpParameters
= lpParameters
;
2156 sei
.lpDirectory
= lpDirectory
;
2157 sei
.nShow
= iShowCmd
;
2164 ShellExecuteExA(&sei
);
2165 return sei
.hInstApp
;
2168 /*************************************************************************
2169 * ShellExecuteExA [SHELL32.292]
2175 ShellExecuteExA(LPSHELLEXECUTEINFOA sei
)
2177 SHELLEXECUTEINFOW seiW
;
2179 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
2183 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
2186 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
2189 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
2191 if (sei
->lpParameters
)
2192 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
2194 if (sei
->lpDirectory
)
2195 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
2197 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
2198 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
2200 seiW
.lpClass
= NULL
;
2202 ret
= SHELL_execute(&seiW
, SHELL_ExecuteW
);
2204 sei
->hInstApp
= seiW
.hInstApp
;
2206 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
2207 sei
->hProcess
= seiW
.hProcess
;
2211 SHFree(wParameters
);
2218 /*************************************************************************
2219 * ShellExecuteExW [SHELL32.293]
2225 ShellExecuteExW(LPSHELLEXECUTEINFOW sei
)
2227 return SHELL_execute(sei
, SHELL_ExecuteW
);
2230 /*************************************************************************
2231 * ShellExecuteW [SHELL32.294]
2233 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
2234 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
2236 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpVerb
, LPCWSTR lpFile
,
2237 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
2239 SHELLEXECUTEINFOW sei
;
2242 sei
.cbSize
= sizeof(sei
);
2243 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2245 sei
.lpVerb
= lpVerb
;
2246 sei
.lpFile
= lpFile
;
2247 sei
.lpParameters
= lpParameters
;
2248 sei
.lpDirectory
= lpDirectory
;
2249 sei
.nShow
= nShowCmd
;
2256 SHELL_execute(&sei
, SHELL_ExecuteW
);
2257 return sei
.hInstApp
;
2260 /*************************************************************************
2261 * WOWShellExecute [SHELL32.@]
2263 * FIXME: the callback function most likely doesn't work the same way on Windows.
2265 EXTERN_C HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpVerb
, LPCSTR lpFile
,
2266 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
2268 SHELLEXECUTEINFOW seiW
;
2269 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
2270 HANDLE hProcess
= 0;
2272 seiW
.lpVerb
= lpVerb
? __SHCloneStrAtoW(&wVerb
, lpVerb
) : NULL
;
2273 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
2274 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
2275 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
2277 seiW
.cbSize
= sizeof(seiW
);
2280 seiW
.nShow
= iShowCmd
;
2285 seiW
.hProcess
= hProcess
;
2287 SHELL_execute(&seiW
, (SHELL_ExecuteW32
)callback
);
2291 SHFree(wParameters
);
2293 return seiW
.hInstApp
;
2296 /*************************************************************************
2297 * OpenAs_RunDLLW [SHELL32.@]
2299 EXTERN_C
void WINAPI
2300 OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2303 TRACE("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);
2305 ZeroMemory(&info
, sizeof(info
));
2306 info
.pcszFile
= cmdline
;
2307 info
.pcszClass
= NULL
;
2308 info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_REGISTER_EXT
| OAIF_EXEC
;
2310 SHOpenWithDialog(hwnd
, &info
);
2313 /*************************************************************************
2314 * OpenAs_RunDLLA [SHELL32.@]
2316 EXTERN_C
void WINAPI
2317 OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2319 LPWSTR pszCmdLineW
= NULL
;
2320 TRACE("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2323 __SHCloneStrAtoW(&pszCmdLineW
, cmdline
);
2324 OpenAs_RunDLLW(hwnd
, hinst
, pszCmdLineW
, cmdshow
);
2325 SHFree(pszCmdLineW
);
2328 /*************************************************************************/
2331 SplitParams(LPCWSTR psz
, LPWSTR pszArg0
, size_t cchArg0
)
2337 // 1st argument is quoted. the string in quotes is quoted 1st argument.
2338 // [pch] --> [pszArg0+ich]
2339 for (pch
= psz
+ 1; *pch
&& ich
+ 1 < cchArg0
; ++ich
, ++pch
)
2341 if (*pch
== L
'"' && pch
[1] == L
'"')
2343 // doubled double quotations found!
2344 pszArg0
[ich
] = L
'"';
2346 else if (*pch
== L
'"')
2348 // single double quotation found!
2355 pszArg0
[ich
] = *pch
;
2361 // 1st argument is unquoted. non-space sequence is 1st argument.
2362 // [pch] --> [pszArg0+ich]
2363 for (pch
= psz
; *pch
&& !iswspace(*pch
) && ich
+ 1 < cchArg0
; ++ich
, ++pch
)
2365 pszArg0
[ich
] = *pch
;
2371 while (iswspace(*pch
))
2377 HRESULT WINAPI
ShellExecCmdLine(
2379 LPCWSTR pwszCommand
,
2380 LPCWSTR pwszStartDir
,
2385 SHELLEXECUTEINFOW info
;
2386 DWORD dwSize
, dwError
, dwType
, dwFlags
= SEE_MASK_DOENVSUBST
| SEE_MASK_NOASYNC
;
2387 LPCWSTR pszVerb
= NULL
;
2388 WCHAR szFile
[MAX_PATH
], szFile2
[MAX_PATH
];
2391 LPWSTR lpCommand
= NULL
;
2393 if (pwszCommand
== NULL
)
2394 RaiseException(EXCEPTION_ACCESS_VIOLATION
, EXCEPTION_NONCONTINUABLE
,
2395 1, (ULONG_PTR
*)pwszCommand
);
2397 __SHCloneStrW(&lpCommand
, pwszCommand
);
2398 StrTrimW(lpCommand
, L
" \t");
2400 if (dwSeclFlags
& SECL_NO_UI
)
2401 dwFlags
|= SEE_MASK_FLAG_NO_UI
;
2402 if (dwSeclFlags
& SECL_LOG_USAGE
)
2403 dwFlags
|= SEE_MASK_FLAG_LOG_USAGE
;
2404 if (dwSeclFlags
& SECL_USE_IDLIST
)
2405 dwFlags
|= SEE_MASK_INVOKEIDLIST
;
2407 if (dwSeclFlags
& SECL_RUNAS
)
2410 hr
= AssocQueryStringW(0, ASSOCSTR_COMMAND
, lpCommand
, L
"RunAs", NULL
, &dwSize
);
2411 if (SUCCEEDED(hr
) && dwSize
!= 0)
2417 if (UrlIsFileUrlW(lpCommand
))
2419 StringCchCopyW(szFile
, _countof(szFile
), lpCommand
);
2424 pchParams
= SplitParams(lpCommand
, szFile
, _countof(szFile
));
2425 if (SearchPathW(NULL
, szFile
, NULL
, _countof(szFile2
), szFile2
, NULL
) ||
2426 SearchPathW(NULL
, szFile
, wszExe
, _countof(szFile2
), szFile2
, NULL
) ||
2427 SearchPathW(NULL
, szFile
, wszCom
, _countof(szFile2
), szFile2
, NULL
) ||
2428 SearchPathW(pwszStartDir
, szFile
, NULL
, _countof(szFile2
), szFile2
, NULL
) ||
2429 SearchPathW(pwszStartDir
, szFile
, wszExe
, _countof(szFile2
), szFile2
, NULL
) ||
2430 SearchPathW(pwszStartDir
, szFile
, wszCom
, _countof(szFile2
), szFile2
, NULL
))
2432 StringCchCopyW(szFile
, _countof(szFile
), szFile2
);
2434 else if (SearchPathW(NULL
, lpCommand
, NULL
, _countof(szFile2
), szFile2
, NULL
) ||
2435 SearchPathW(NULL
, lpCommand
, wszExe
, _countof(szFile2
), szFile2
, NULL
) ||
2436 SearchPathW(NULL
, lpCommand
, wszCom
, _countof(szFile2
), szFile2
, NULL
) ||
2437 SearchPathW(pwszStartDir
, lpCommand
, NULL
, _countof(szFile2
), szFile2
, NULL
) ||
2438 SearchPathW(pwszStartDir
, lpCommand
, wszExe
, _countof(szFile2
), szFile2
, NULL
) ||
2439 SearchPathW(pwszStartDir
, lpCommand
, wszCom
, _countof(szFile2
), szFile2
, NULL
))
2441 StringCchCopyW(szFile
, _countof(szFile
), szFile2
);
2445 if (!(dwSeclFlags
& SECL_ALLOW_NONEXE
))
2447 if (!GetBinaryTypeW(szFile
, &dwType
))
2451 if (!(dwSeclFlags
& SECL_NO_UI
))
2453 WCHAR szText
[128 + MAX_PATH
], szFormat
[128];
2454 LoadStringW(shell32_hInstance
, IDS_FILE_NOT_FOUND
, szFormat
, _countof(szFormat
));
2455 StringCchPrintfW(szText
, _countof(szText
), szFormat
, szFile
);
2456 MessageBoxW(hwnd
, szText
, NULL
, MB_ICONERROR
);
2458 return CO_E_APPNOTFOUND
;
2463 if (GetFileAttributesW(szFile
) == INVALID_FILE_ATTRIBUTES
)
2467 if (!(dwSeclFlags
& SECL_NO_UI
))
2469 WCHAR szText
[128 + MAX_PATH
], szFormat
[128];
2470 LoadStringW(shell32_hInstance
, IDS_FILE_NOT_FOUND
, szFormat
, _countof(szFormat
));
2471 StringCchPrintfW(szText
, _countof(szText
), szFormat
, szFile
);
2472 MessageBoxW(hwnd
, szText
, NULL
, MB_ICONERROR
);
2474 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND
);
2479 ZeroMemory(&info
, sizeof(info
));
2480 info
.cbSize
= sizeof(info
);
2481 info
.fMask
= dwFlags
;
2483 info
.lpVerb
= pszVerb
;
2484 info
.lpFile
= szFile
;
2485 info
.lpParameters
= (pchParams
&& *pchParams
) ? pchParams
: NULL
;
2486 info
.lpDirectory
= pwszStartDir
;
2488 if (ShellExecuteExW(&info
))
2491 CoTaskMemFree(info
.lpIDList
);
2498 dwError
= GetLastError();
2502 return HRESULT_FROM_WIN32(dwError
);