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
,
1236 IID_IDataObject
, NULL
, (LPVOID
*) &dataobj
);
1239 if (pidl
!= sei
->lpIDList
)
1246 static HRESULT
shellex_run_context_menu_default(IShellExtInit
*obj
,
1247 LPSHELLEXECUTEINFOW sei
)
1249 IContextMenu
*cm
= NULL
;
1250 CMINVOKECOMMANDINFOEX ici
;
1257 TRACE("%p %p\n", obj
, sei
);
1259 r
= obj
->QueryInterface(IID_IContextMenu
, (LPVOID
*) &cm
);
1263 hmenu
= CreateMenu();
1267 /* the number of the last menu added is returned in r */
1268 r
= cm
->QueryContextMenu(hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1272 n
= GetMenuItemCount(hmenu
);
1273 for (i
= 0; i
< n
; i
++)
1275 memset(&info
, 0, sizeof(info
));
1276 info
.cbSize
= sizeof info
;
1277 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1278 info
.dwTypeData
= string
;
1279 info
.cch
= sizeof string
;
1281 GetMenuItemInfoW(hmenu
, i
, TRUE
, &info
);
1283 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1284 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1285 if ((!sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
)) ||
1286 (sei
->lpVerb
&& !lstrcmpiW(sei
->lpVerb
, string
)))
1297 memset(&ici
, 0, sizeof ici
);
1298 ici
.cbSize
= sizeof ici
;
1299 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NOASYNC
| SEE_MASK_ASYNCOK
| SEE_MASK_FLAG_NO_UI
));
1300 ici
.nShow
= sei
->nShow
;
1301 ici
.lpVerb
= MAKEINTRESOURCEA(def
);
1302 ici
.hwnd
= sei
->hwnd
;
1303 ici
.lpParametersW
= sei
->lpParameters
;
1305 r
= cm
->InvokeCommand((LPCMINVOKECOMMANDINFO
)&ici
);
1307 TRACE("invoke command returned %08x\n", r
);
1311 DestroyMenu( hmenu
);
1317 static HRESULT
shellex_load_object_and_run(HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1319 IDataObject
*dataobj
= NULL
;
1320 IObjectWithSite
*ows
= NULL
;
1321 IShellExtInit
*obj
= NULL
;
1324 TRACE("%p %s %p\n", hkey
, debugstr_guid(guid
), sei
);
1326 r
= CoInitialize(NULL
);
1330 r
= CoCreateInstance(*guid
, NULL
, CLSCTX_INPROC_SERVER
,
1331 IID_PPV_ARG(IShellExtInit
, &obj
));
1334 ERR("failed %08x\n", r
);
1338 dataobj
= shellex_get_dataobj(sei
);
1341 ERR("failed to get data object\n");
1345 r
= obj
->Initialize(NULL
, dataobj
, hkey
);
1349 r
= obj
->QueryInterface(IID_IObjectWithSite
, (LPVOID
*) &ows
);
1355 r
= shellex_run_context_menu_default(obj
, sei
);
1369 /*************************************************************************
1370 * ShellExecute_FromContextMenu [Internal]
1372 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1374 HKEY hkey
, hkeycm
= 0;
1381 TRACE("%s\n", debugstr_w(sei
->lpFile
));
1383 hkey
= ShellExecute_GetClassKey(sei
);
1385 return ERROR_FUNCTION_FAILED
;
1387 r
= RegOpenKeyW(hkey
, L
"shellex\\ContextMenuHandlers", &hkeycm
);
1388 if (r
== ERROR_SUCCESS
)
1393 r
= RegEnumKeyW(hkeycm
, i
++, szguid
, sizeof(szguid
) / sizeof(szguid
[0]));
1394 if (r
!= ERROR_SUCCESS
)
1397 hr
= CLSIDFromString(szguid
, &guid
);
1400 /* stop at the first one that succeeds in running */
1401 hr
= shellex_load_object_and_run(hkey
, &guid
, sei
);
1406 RegCloseKey(hkeycm
);
1409 if (hkey
!= sei
->hkeyClass
)
1414 static UINT_PTR
SHELL_execute_class(LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1416 WCHAR execCmd
[1024], wcmd
[1024];
1417 /* launch a document by fileclass like 'WordPad.Document.1' */
1418 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1419 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1420 ULONG cmask
= (psei
->fMask
& SEE_MASK_CLASSALL
);
1424 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1425 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1427 execCmd
, sizeof(execCmd
));
1429 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1430 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1433 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
) / sizeof(WCHAR
), execCmd
, wszApplicationName
, (LPITEMIDLIST
)psei
->lpIDList
, NULL
, &resultLen
);
1434 if (!done
&& wszApplicationName
[0])
1436 strcatW(wcmd
, L
" ");
1437 strcatW(wcmd
, wszApplicationName
);
1439 if (resultLen
> sizeof(wcmd
) / sizeof(WCHAR
))
1440 ERR("Argify buffer not large enough... truncating\n");
1441 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1444 static BOOL
SHELL_translate_idlist(LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1446 static const WCHAR wExplorer
[] = L
"explorer.exe";
1447 WCHAR buffer
[MAX_PATH
];
1448 BOOL appKnownSingular
= FALSE
;
1450 /* last chance to translate IDList: now also allow CLSID paths */
1451 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST
)sei
->lpIDList
, buffer
, sizeof(buffer
)))) {
1452 if (buffer
[0] == ':' && buffer
[1] == ':') {
1453 /* open shell folder for the specified class GUID */
1454 if (strlenW(buffer
) + 1 > parametersLen
)
1455 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1456 lstrlenW(buffer
) + 1, parametersLen
);
1457 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1458 if (strlenW(wExplorer
) > dwApplicationNameLen
)
1459 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1460 lstrlenW(wExplorer
) + 1, dwApplicationNameLen
);
1461 lstrcpynW(wszApplicationName
, wExplorer
, dwApplicationNameLen
);
1462 appKnownSingular
= TRUE
;
1464 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1466 WCHAR target
[MAX_PATH
];
1469 /* Check if we're executing a directory and if so use the
1470 handler for the Folder class */
1471 strcpyW(target
, buffer
);
1472 attribs
= GetFileAttributesW(buffer
);
1473 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1474 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1475 HCR_GetExecuteCommandW(0, L
"Folder",
1477 buffer
, sizeof(buffer
))) {
1478 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1479 buffer
, target
, (LPITEMIDLIST
)sei
->lpIDList
, NULL
, &resultLen
);
1480 if (resultLen
> dwApplicationNameLen
)
1481 ERR("Argify buffer not large enough... truncating\n");
1482 appKnownSingular
= FALSE
;
1484 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1487 return appKnownSingular
;
1490 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 WCHAR
*wszQuotedCmd
;
1496 /* Length of quotes plus length of command plus NULL terminator */
1497 len
= 2 + lstrlenW(wcmd
) + 1;
1498 if (wszParameters
[0])
1500 /* Length of space plus length of parameters */
1501 len
+= 1 + lstrlenW(wszParameters
);
1503 wszQuotedCmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1504 /* Must quote to handle case where cmd contains spaces,
1505 * else security hole if malicious user creates executable file "C:\\Program"
1507 strcpyW(wszQuotedCmd
, L
"\"");
1508 strcatW(wszQuotedCmd
, wcmd
);
1509 strcatW(wszQuotedCmd
, L
"\"");
1510 if (wszParameters
[0])
1512 strcatW(wszQuotedCmd
, L
" ");
1513 strcatW(wszQuotedCmd
, wszParameters
);
1516 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(lpstrProtocol
));
1519 retval
= execute_from_key(lpstrProtocol
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1521 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1522 HeapFree(GetProcessHeap(), 0, wszQuotedCmd
);
1526 static UINT_PTR
SHELL_execute_url(LPCWSTR lpFile
, LPCWSTR wFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1528 static const WCHAR wShell
[] = L
"\\shell\\";
1529 static const WCHAR wCommand
[] = L
"\\command";
1531 WCHAR
*lpstrProtocol
;
1536 lpstrRes
= strchrW(lpFile
, ':');
1538 iSize
= lpstrRes
- lpFile
;
1540 iSize
= strlenW(lpFile
);
1542 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1543 /* Looking for ...protocol\shell\lpOperation\command */
1544 len
= iSize
+ lstrlenW(wShell
) + lstrlenW(wCommand
) + 1;
1546 len
+= lstrlenW(psei
->lpVerb
);
1548 len
+= lstrlenW(wszOpen
);
1549 lpstrProtocol
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1550 memcpy(lpstrProtocol
, lpFile
, iSize
* sizeof(WCHAR
));
1551 lpstrProtocol
[iSize
] = '\0';
1552 strcatW(lpstrProtocol
, wShell
);
1553 strcatW(lpstrProtocol
, psei
->lpVerb
? psei
->lpVerb
: wszOpen
);
1554 strcatW(lpstrProtocol
, wCommand
);
1556 /* Remove File Protocol from lpFile */
1557 /* In the case file://path/file */
1558 if (!strncmpiW(lpFile
, wFile
, iSize
))
1561 while (*lpFile
== ':') lpFile
++;
1563 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1564 wcmd
, execfunc
, psei
, psei_out
);
1565 HeapFree(GetProcessHeap(), 0, lpstrProtocol
);
1569 void do_error_dialog(UINT_PTR retval
, HWND hwnd
, WCHAR
* filename
)
1572 DWORD_PTR msgArguments
[3] = { (DWORD_PTR
)filename
, 0, 0 };
1575 error_code
= GetLastError();
1577 if (retval
== SE_ERR_NOASSOC
)
1578 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, sizeof(msg
) / sizeof(WCHAR
));
1580 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ARGUMENT_ARRAY
,
1585 sizeof(msg
) / sizeof(WCHAR
),
1586 (va_list*)msgArguments
);
1588 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1591 /*************************************************************************
1592 * SHELL_execute [Internal]
1594 BOOL
SHELL_execute(LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1596 static const DWORD unsupportedFlags
=
1597 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1598 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1599 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1601 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1602 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
;
1603 DWORD dwApplicationNameLen
= MAX_PATH
+ 2;
1604 DWORD parametersLen
= sizeof(parametersBuffer
) / sizeof(WCHAR
);
1605 DWORD dirLen
= sizeof(dirBuffer
) / sizeof(WCHAR
);
1606 DWORD wcmdLen
= sizeof(wcmdBuffer
) / sizeof(WCHAR
);
1608 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1609 WCHAR wfileName
[MAX_PATH
];
1611 WCHAR lpstrProtocol
[256];
1613 UINT_PTR retval
= SE_ERR_NOASSOC
;
1614 BOOL appKnownSingular
= FALSE
;
1616 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1619 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1620 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1621 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1622 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1623 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1624 debugstr_w(sei_tmp
.lpClass
) : "not used");
1626 sei
->hProcess
= NULL
;
1628 /* make copies of all path/command strings */
1629 if (!sei_tmp
.lpFile
)
1631 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1632 *wszApplicationName
= '\0';
1634 else if (*sei_tmp
.lpFile
== '\"')
1636 DWORD l
= strlenW(sei_tmp
.lpFile
+ 1);
1637 if(l
>= dwApplicationNameLen
)
1638 dwApplicationNameLen
= l
+ 1;
1640 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1641 memcpy(wszApplicationName
, sei_tmp
.lpFile
+ 1, (l
+ 1)*sizeof(WCHAR
));
1643 if (wszApplicationName
[l
-1] == L
'\"')
1644 wszApplicationName
[l
-1] = L
'\0';
1645 appKnownSingular
= TRUE
;
1647 TRACE("wszApplicationName=%s\n", debugstr_w(wszApplicationName
));
1651 DWORD l
= strlenW(sei_tmp
.lpFile
) + 1;
1652 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+ 1;
1653 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
* sizeof(WCHAR
));
1654 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
* sizeof(WCHAR
));
1657 wszParameters
= parametersBuffer
;
1658 if (sei_tmp
.lpParameters
)
1660 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1661 if (len
> parametersLen
)
1663 wszParameters
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1664 parametersLen
= len
;
1666 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1669 *wszParameters
= L
'\0';
1672 if (sei_tmp
.lpDirectory
)
1674 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1677 wszDir
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1680 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1685 /* adjust string pointers to point to the new buffers */
1686 sei_tmp
.lpFile
= wszApplicationName
;
1687 sei_tmp
.lpParameters
= wszParameters
;
1688 sei_tmp
.lpDirectory
= wszDir
;
1690 if (sei_tmp
.fMask
& unsupportedFlags
)
1692 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1695 /* process the IDList */
1696 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1698 IShellExecuteHookW
* pSEH
;
1700 HRESULT hr
= SHBindToParent((LPCITEMIDLIST
)sei_tmp
.lpIDList
, IID_PPV_ARG(IShellExecuteHookW
, &pSEH
), NULL
);
1704 hr
= pSEH
->Execute(&sei_tmp
);
1710 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1711 if (wszParameters
!= parametersBuffer
)
1712 HeapFree(GetProcessHeap(), 0, wszParameters
);
1713 if (wszDir
!= dirBuffer
)
1714 HeapFree(GetProcessHeap(), 0, wszDir
);
1719 SHGetPathFromIDListW((LPCITEMIDLIST
)sei_tmp
.lpIDList
, wszApplicationName
);
1720 appKnownSingular
= TRUE
;
1721 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1724 if (ERROR_SUCCESS
== ShellExecute_FromContextMenu(&sei_tmp
))
1726 sei
->hInstApp
= (HINSTANCE
) 33;
1727 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1728 if (wszParameters
!= parametersBuffer
)
1729 HeapFree(GetProcessHeap(), 0, wszParameters
);
1730 if (wszDir
!= dirBuffer
)
1731 HeapFree(GetProcessHeap(), 0, wszDir
);
1735 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1737 retval
= SHELL_execute_class(wszApplicationName
, &sei_tmp
, sei
, execfunc
);
1738 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1745 Info
.pcszFile
= wszApplicationName
;
1746 Info
.pcszClass
= NULL
;
1747 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1749 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1750 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1752 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1753 if (wszParameters
!= parametersBuffer
)
1754 HeapFree(GetProcessHeap(), 0, wszParameters
);
1755 if (wszDir
!= dirBuffer
)
1756 HeapFree(GetProcessHeap(), 0, wszDir
);
1760 /* Has the IDList not yet been translated? */
1761 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1763 appKnownSingular
= SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1766 dwApplicationNameLen
);
1769 /* expand environment strings */
1770 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1774 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
1776 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1777 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1778 dwApplicationNameLen
= len
+ 1;
1779 wszApplicationName
= buf
;
1780 /* appKnownSingular unmodified */
1782 sei_tmp
.lpFile
= wszApplicationName
;
1785 if (*sei_tmp
.lpParameters
)
1787 len
= ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, NULL
, 0);
1792 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1793 ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buf
, len
);
1794 if (wszParameters
!= parametersBuffer
)
1795 HeapFree(GetProcessHeap(), 0, wszParameters
);
1796 wszParameters
= buf
;
1797 parametersLen
= len
;
1798 sei_tmp
.lpParameters
= wszParameters
;
1802 if (*sei_tmp
.lpDirectory
)
1804 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1809 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1810 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1811 if (wszDir
!= dirBuffer
)
1812 HeapFree(GetProcessHeap(), 0, wszDir
);
1814 sei_tmp
.lpDirectory
= wszDir
;
1818 /* Else, try to execute the filename */
1819 TRACE("execute: %s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1821 /* separate out command line arguments from executable file name */
1822 if (!*sei_tmp
.lpParameters
&& !appKnownSingular
)
1824 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1825 if (sei_tmp
.lpFile
[0] == L
'"')
1827 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1828 LPWSTR dst
= wfileName
;
1831 /* copy the unquoted executable path to 'wfileName' */
1832 while(*src
&& *src
!= L
'"')
1841 while(isspace(*src
))
1847 /* copy the parameter string to 'wszParameters' */
1848 strcpyW(wszParameters
, src
);
1850 /* terminate previous command string after the quote character */
1855 /* If the executable name is not quoted, we have to use this search loop here,
1856 that in CreateProcess() is not sufficient because it does not handle shell links. */
1857 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1860 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1861 for(s
= beg
; (space
= const_cast<LPWSTR
>(strchrW(s
, L
' '))); s
= space
+ 1)
1863 int idx
= space
- sei_tmp
.lpFile
;
1864 memcpy(buffer
, sei_tmp
.lpFile
, idx
* sizeof(WCHAR
));
1867 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1868 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
) / sizeof(xlpFile
[0]), xlpFile
, NULL
))
1870 /* separate out command from parameter string */
1871 LPCWSTR p
= space
+ 1;
1876 strcpyW(wszParameters
, p
);
1883 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1887 lstrcpynW(wfileName
, sei_tmp
.lpFile
, sizeof(wfileName
) / sizeof(WCHAR
));
1892 len
= lstrlenW(wszApplicationName
) + 3;
1893 if (sei_tmp
.lpParameters
[0])
1894 len
+= 1 + lstrlenW(wszParameters
);
1897 wcmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1900 swprintf(wcmd
, L
"\"%s\"", wszApplicationName
);
1901 if (sei_tmp
.lpParameters
[0])
1903 strcatW(wcmd
, L
" ");
1904 strcatW(wcmd
, wszParameters
);
1907 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1910 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1911 if (wszParameters
!= parametersBuffer
)
1912 HeapFree(GetProcessHeap(), 0, wszParameters
);
1913 if (wszDir
!= dirBuffer
)
1914 HeapFree(GetProcessHeap(), 0, wszDir
);
1915 if (wcmd
!= wcmdBuffer
)
1916 HeapFree(GetProcessHeap(), 0, wcmd
);
1920 /* Else, try to find the executable */
1922 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, lpstrProtocol
, &env
, (LPITEMIDLIST
)sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1923 if (retval
> 32) /* Found */
1925 retval
= SHELL_quote_and_execute(wcmd
, wszParameters
, lpstrProtocol
,
1926 wszApplicationName
, env
, &sei_tmp
,
1928 HeapFree(GetProcessHeap(), 0, env
);
1930 else if (PathIsDirectoryW(lpFile
))
1932 WCHAR wExec
[MAX_PATH
];
1933 WCHAR
* lpQuotedFile
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * (strlenW(lpFile
) + 3));
1937 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, L
"explorer",
1938 wszOpen
, wExec
, MAX_PATH
,
1939 NULL
, &env
, NULL
, NULL
);
1942 swprintf(lpQuotedFile
, L
"\"%s\"", lpFile
);
1943 retval
= SHELL_quote_and_execute(wExec
, lpQuotedFile
,
1945 wszApplicationName
, env
,
1946 &sei_tmp
, sei
, execfunc
);
1947 HeapFree(GetProcessHeap(), 0, env
);
1949 HeapFree(GetProcessHeap(), 0, lpQuotedFile
);
1952 retval
= 0; /* Out of memory */
1954 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1956 retval
= SHELL_execute_url(lpFile
, L
"file", wcmd
, &sei_tmp
, sei
, execfunc
);
1958 /* Check if file specified is in the form www.??????.*** */
1959 else if (!strncmpiW(lpFile
, L
"www", 3))
1961 /* if so, append lpFile http:// and call ShellExecute */
1962 WCHAR lpstrTmpFile
[256];
1963 strcpyW(lpstrTmpFile
, L
"http://");
1964 strcatW(lpstrTmpFile
, lpFile
);
1965 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1968 TRACE("retval %lu\n", retval
);
1970 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1977 Info
.pcszFile
= wszApplicationName
;
1978 Info
.pcszClass
= NULL
;
1979 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1981 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1982 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1985 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1986 if (wszParameters
!= parametersBuffer
)
1987 HeapFree(GetProcessHeap(), 0, wszParameters
);
1988 if (wszDir
!= dirBuffer
)
1989 HeapFree(GetProcessHeap(), 0, wszDir
);
1990 if (wcmd
!= wcmdBuffer
)
1991 HeapFree(GetProcessHeap(), 0, wcmd
);
1993 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1998 /*************************************************************************
1999 * ShellExecuteA [SHELL32.290]
2001 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
2002 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
2004 SHELLEXECUTEINFOA sei
;
2006 TRACE("%p,%s,%s,%s,%s,%d\n",
2007 hWnd
, debugstr_a(lpOperation
), debugstr_a(lpFile
),
2008 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
2010 sei
.cbSize
= sizeof(sei
);
2011 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2013 sei
.lpVerb
= lpOperation
;
2014 sei
.lpFile
= lpFile
;
2015 sei
.lpParameters
= lpParameters
;
2016 sei
.lpDirectory
= lpDirectory
;
2017 sei
.nShow
= iShowCmd
;
2024 ShellExecuteExA(&sei
);
2025 return sei
.hInstApp
;
2028 /*************************************************************************
2029 * ShellExecuteExA [SHELL32.292]
2035 ShellExecuteExA(LPSHELLEXECUTEINFOA sei
)
2037 SHELLEXECUTEINFOW seiW
;
2039 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
2043 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
2046 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
2049 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
2051 if (sei
->lpParameters
)
2052 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
2054 if (sei
->lpDirectory
)
2055 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
2057 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
2058 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
2060 seiW
.lpClass
= NULL
;
2062 ret
= SHELL_execute(&seiW
, SHELL_ExecuteW
);
2064 sei
->hInstApp
= seiW
.hInstApp
;
2066 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
2067 sei
->hProcess
= seiW
.hProcess
;
2071 SHFree(wParameters
);
2078 /*************************************************************************
2079 * ShellExecuteExW [SHELL32.293]
2085 ShellExecuteExW(LPSHELLEXECUTEINFOW sei
)
2087 return SHELL_execute(sei
, SHELL_ExecuteW
);
2090 /*************************************************************************
2091 * ShellExecuteW [SHELL32.294]
2093 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
2094 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
2096 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
2097 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
2099 SHELLEXECUTEINFOW sei
;
2102 sei
.cbSize
= sizeof(sei
);
2103 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2105 sei
.lpVerb
= lpOperation
;
2106 sei
.lpFile
= lpFile
;
2107 sei
.lpParameters
= lpParameters
;
2108 sei
.lpDirectory
= lpDirectory
;
2109 sei
.nShow
= nShowCmd
;
2116 SHELL_execute(&sei
, SHELL_ExecuteW
);
2117 return sei
.hInstApp
;
2120 /*************************************************************************
2121 * WOWShellExecute [SHELL32.@]
2123 * FIXME: the callback function most likely doesn't work the same way on Windows.
2125 EXTERN_C HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpOperation
, LPCSTR lpFile
,
2126 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
2128 SHELLEXECUTEINFOW seiW
;
2129 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
2130 HANDLE hProcess
= 0;
2132 seiW
.lpVerb
= lpOperation
? __SHCloneStrAtoW(&wVerb
, lpOperation
) : NULL
;
2133 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
2134 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
2135 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
2137 seiW
.cbSize
= sizeof(seiW
);
2140 seiW
.nShow
= iShowCmd
;
2145 seiW
.hProcess
= hProcess
;
2147 SHELL_execute(&seiW
, (SHELL_ExecuteW32
)callback
);
2151 SHFree(wParameters
);
2153 return seiW
.hInstApp
;
2156 /*************************************************************************
2157 * OpenAs_RunDLLA [SHELL32.@]
2159 EXTERN_C
void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2161 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2164 /*************************************************************************
2165 * OpenAs_RunDLLW [SHELL32.@]
2167 EXTERN_C
void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2169 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);