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
39 #include "shell32_main.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
47 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
49 typedef UINT_PTR (*SHELL_ExecuteW32
)(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
50 const SHELLEXECUTEINFOW
*sei
, LPSHELLEXECUTEINFOW sei_out
);
52 static inline BOOL
isSpace(WCHAR c
)
54 return c
== ' ' || c
== '\f' || c
== '\n' || c
== '\r' || c
== '\t' || c
== '\v';
57 /***********************************************************************
58 * SHELL_ArgifyW [Internal]
60 * this function is supposed to expand the escape sequences found in the registry
61 * some diving reported that the following were used:
62 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
67 * %I address of a global item ID (explorer switch /idlist)
68 * %L seems to be %1 as long filename followed by the 8+3 variation
70 * %* all following parameters (see batfile)
73 static BOOL
SHELL_ArgifyW(WCHAR
* out
, int len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
, DWORD
* out_len
)
77 BOOL found_p1
= FALSE
;
82 TRACE("%p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
83 debugstr_w(lpFile
), pidl
, args
);
125 while(*args
&& !isSpace(*args
))
134 while(isSpace(*args
))
139 /* else fall through */
141 if (!done
|| (*fmt
== '1'))
143 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
144 if (SearchPathW(NULL
, lpFile
, L
".exe", ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
149 used
+= lstrlenW(cmd
);
153 res
+= lstrlenW(cmd
);
160 * IE uses this a lot for activating things such as windows media
161 * player. This is not verified to be fully correct but it appears
167 used
+= lstrlenW(lpFile
);
170 lstrcpyW(res
, lpFile
);
171 res
+= lstrlenW(lpFile
);
181 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
182 * allowing a buffer of 100 should more than exceed all needs */
185 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
186 pv
= SHLockShared(hmem
, 0);
187 chars
= swprintf(buf
, ARRAY_SIZE(buf
), L
":%p", pv
);
188 if (chars
>= ARRAY_SIZE(buf
))
189 ERR("pidl format buffer too small!\n");
203 * Check if this is an env-variable here...
206 /* Make sure that we have at least one more %.*/
207 if (wcschr(fmt
, '%'))
209 WCHAR tmpBuffer
[1024];
210 PWSTR tmpB
= tmpBuffer
;
211 WCHAR tmpEnvBuff
[MAX_PATH
];
218 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer
));
220 envRet
= GetEnvironmentVariableW(tmpBuffer
, tmpEnvBuff
, MAX_PATH
);
221 if (envRet
== 0 || envRet
> MAX_PATH
)
223 used
+= lstrlenW(tmpBuffer
);
226 lstrcpyW( res
, tmpBuffer
);
227 res
+= lstrlenW(tmpBuffer
);
232 used
+= lstrlenW(tmpEnvBuff
);
235 lstrcpyW( res
, tmpEnvBuff
);
236 res
+= lstrlenW(tmpEnvBuff
);
243 /* Don't skip past terminator (catch a single '%' at the end) */
265 TRACE("used %li of %i space\n",used
,len
);
272 static HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
275 IShellFolder
* desktop
;
277 HRESULT hr
= SHGetDesktopFolder(&desktop
);
280 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
283 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
285 IShellFolder_Release(desktop
);
291 /*************************************************************************
292 * SHELL_ExecuteW [Internal]
295 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
296 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
298 STARTUPINFOW startup
;
299 PROCESS_INFORMATION info
;
300 UINT_PTR retval
= SE_ERR_NOASSOC
;
302 WCHAR curdir
[MAX_PATH
];
303 DWORD dwCreationFlags
;
305 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
307 /* make sure we don't fail the CreateProcess if the calling app passes in
308 * a bad working directory */
309 if (psei
->lpDirectory
&& psei
->lpDirectory
[0])
311 /* ShellExecute specifies the command from psei->lpDirectory
312 * if present. Not from the current dir as CreateProcess does */
313 if( ( gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
314 if( !SetCurrentDirectoryW( psei
->lpDirectory
))
315 ERR("cannot set directory %s\n", debugstr_w(psei
->lpDirectory
));
318 ZeroMemory(&startup
,sizeof(STARTUPINFOW
));
319 startup
.cb
= sizeof(STARTUPINFOW
);
320 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
321 startup
.wShowWindow
= psei
->nShow
;
322 dwCreationFlags
= CREATE_UNICODE_ENVIRONMENT
;
323 if (!(psei
->fMask
& SEE_MASK_NO_CONSOLE
))
324 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
325 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, dwCreationFlags
, env
,
326 NULL
, &startup
, &info
))
328 /* Give 30 seconds to the app to come up, if desired. Probably only needed
329 when starting app immediately before making a DDE connection. */
331 if (WaitForInputIdle( info
.hProcess
, 30000 ) == WAIT_FAILED
)
332 WARN("WaitForInputIdle failed: Error %ld\n", GetLastError() );
334 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
335 psei_out
->hProcess
= info
.hProcess
;
337 CloseHandle( info
.hProcess
);
338 CloseHandle( info
.hThread
);
340 else if ((retval
= GetLastError()) >= 32)
342 TRACE("CreateProcess returned error %Id\n", retval
);
343 retval
= ERROR_BAD_FORMAT
;
346 TRACE("returning %Iu\n", retval
);
348 psei_out
->hInstApp
= (HINSTANCE
)retval
;
350 if( !SetCurrentDirectoryW( curdir
))
351 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
357 /***********************************************************************
358 * SHELL_BuildEnvW [Internal]
360 * Build the environment for the new process, adding the specified
361 * path to the PATH variable. Returned pointer must be freed by caller.
363 static void *SHELL_BuildEnvW( const WCHAR
*path
)
365 WCHAR
*strings
, *new_env
;
367 int total
= lstrlenW(path
) + 1;
368 BOOL got_path
= FALSE
;
370 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
374 int len
= lstrlenW(p
) + 1;
375 if (!wcsnicmp( p
, L
"PATH=", 5 )) got_path
= TRUE
;
379 if (!got_path
) total
+= 5; /* we need to create PATH */
380 total
++; /* terminating null */
382 if (!(new_env
= heap_alloc( total
* sizeof(WCHAR
) )))
384 FreeEnvironmentStringsW( strings
);
391 int len
= lstrlenW(p
) + 1;
392 memcpy( p2
, p
, len
* sizeof(WCHAR
) );
393 if (!wcsnicmp( p
, L
"PATH=", 5 ))
396 lstrcpyW( p2
+ len
, path
);
397 p2
+= lstrlenW(path
) + 1;
404 lstrcpyW( p2
, L
"PATH=" );
405 lstrcatW( p2
, path
);
406 p2
+= lstrlenW(p2
) + 1;
409 FreeEnvironmentStringsW( strings
);
414 /***********************************************************************
415 * SHELL_TryAppPathW [Internal]
417 * Helper function for SHELL_FindExecutable
418 * @param lpResult - pointer to a buffer of size MAX_PATH
419 * On entry: szName is a filename (probably without path separators).
420 * On exit: if szName found in "App Path", place full path in lpResult, and return true
422 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
430 if (env
) *env
= NULL
;
431 lstrcpyW(buffer
, L
"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
432 lstrcatW(buffer
, szName
);
433 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
436 len
= MAX_PATH
*sizeof(WCHAR
);
437 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
443 DWORD count
= sizeof(buffer
);
444 if (!RegQueryValueExW(hkApp
, L
"Path", NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
445 *env
= SHELL_BuildEnvW( buffer
);
449 if (hkApp
) RegCloseKey(hkApp
);
453 /*************************************************************************
454 * SHELL_FindExecutableByVerb [Internal]
456 * called from SHELL_FindExecutable or SHELL_execute_class
458 * classname a buffer, big enough, to get the key name to do actually the
459 * command "WordPad.Document.1\\shell\\open\\command"
460 * passed as "WordPad.Document.1"
462 * lpVerb the operation on it (open)
463 * commandlen the size of command buffer (in bytes)
465 * command a buffer, to store the command to do the
466 * operation on the file
467 * key a buffer, big enough, to get the key name to do actually the
468 * command "WordPad.Document.1\\shell\\open\\command"
471 static UINT
SHELL_FindExecutableByVerb(LPCWSTR lpVerb
, LPWSTR key
, LPWSTR classname
, LPWSTR command
, LONG commandlen
)
474 WCHAR verb
[MAX_PATH
];
476 if (*classname
== '.')
478 /* Extension, default key value holds class name. Extension may also have
479 * empty class name and shell\verb\command subkey. */
480 WCHAR
class[MAX_PATH
];
484 if (!RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, class, &len
) && *class)
485 wcscpy(classname
, class);
488 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, classname
, 0, 0x02000000, &hkeyClass
))
489 return SE_ERR_NOASSOC
;
490 if (!HCR_GetDefaultVerbW(hkeyClass
, lpVerb
, verb
, ARRAY_SIZE(verb
)))
491 return SE_ERR_NOASSOC
;
492 RegCloseKey(hkeyClass
);
494 /* Looking for ...buffer\shell\<verb>\command */
495 lstrcatW(classname
, L
"\\shell\\");
496 lstrcatW(classname
, verb
);
497 lstrcatW(classname
, L
"\\command");
499 if (RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, command
,
500 &commandlen
) == ERROR_SUCCESS
)
502 commandlen
/= sizeof(WCHAR
);
503 if (key
) lstrcpyW(key
, classname
);
507 LONG paramlen
= sizeof(param
);
509 /* FIXME: it seems all Windows version don't behave the same here.
510 * the doc states that this ddeexec information can be found after
512 * on Win98, it doesn't appear, but I think it does on Win2k
514 /* Get the parameters needed by the application
515 from the associated ddeexec key */
516 tmp
= wcsstr(classname
, L
"\\command");
518 lstrcatW(classname
, wDdeexec
);
519 if (RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, param
,
520 ¶mlen
) == ERROR_SUCCESS
)
522 paramlen
/= sizeof(WCHAR
);
523 lstrcatW(command
, L
" ");
524 lstrcatW(command
, param
);
525 commandlen
+= paramlen
;
529 command
[commandlen
] = '\0';
531 return 33; /* FIXME see SHELL_FindExecutable() */
534 return SE_ERR_NOASSOC
;
537 /*************************************************************************
538 * SHELL_FindExecutable [Internal]
540 * Utility for code sharing between FindExecutable and ShellExecute
542 * lpFile the name of a file
543 * lpVerb the operation on it (open)
545 * lpResult a buffer, big enough :-(, to store the command to do the
546 * operation on the file
547 * key a buffer, big enough, to get the key name to do actually the
548 * command (it'll be used afterwards for more information
551 static UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpVerb
,
552 LPWSTR lpResult
, int resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
554 WCHAR
*extension
= NULL
; /* pointer to file extension */
555 WCHAR classname
[256]; /* registry name for this file type */
556 LONG classnamelen
= sizeof(classname
); /* length of above */
557 WCHAR command
[1024]; /* command from registry */
558 WCHAR wBuffer
[256]; /* Used to GetProfileString */
559 UINT retval
= SE_ERR_NOASSOC
;
560 WCHAR
*tok
; /* token pointer */
561 WCHAR xlpFile
[256]; /* result of SearchPath */
562 DWORD attribs
; /* file attributes */
564 TRACE("%s\n", debugstr_w(lpFile
));
567 return ERROR_INVALID_PARAMETER
;
570 lpResult
[0] = '\0'; /* Start off with an empty return string */
571 if (key
) *key
= '\0';
573 /* trap NULL parameters on entry */
576 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
577 debugstr_w(lpFile
), debugstr_w(lpResult
));
578 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
581 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
583 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
587 if (SearchPathW(lpPath
, lpFile
, L
".exe", ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
589 TRACE("SearchPathW returned non-zero\n");
591 /* The file was found in the application-supplied default directory (or the system search path) */
593 else if (lpPath
&& SearchPathW(NULL
, lpFile
, L
".exe", ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
595 TRACE("SearchPathW returned non-zero\n");
597 /* The file was found in one of the directories in the system-wide search path */
600 attribs
= GetFileAttributesW(lpFile
);
601 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
603 lstrcpyW(classname
, L
"Folder");
607 /* Did we get something? Anything? */
610 TRACE("Returning SE_ERR_FNF\n");
613 /* First thing we need is the file's extension */
614 extension
= wcsrchr(xlpFile
, '.'); /* Assume last "." is the one; */
615 /* File->Run in progman uses */
617 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
619 if (extension
== NULL
|| extension
[1]==0)
621 WARN("Returning SE_ERR_NOASSOC\n");
622 return SE_ERR_NOASSOC
;
625 /* Three places to check: */
626 /* 1. win.ini, [windows], programs (NB no leading '.') */
627 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
628 /* 3. win.ini, [extensions], extension (NB no leading '.' */
629 /* All I know of the order is that registry is checked before */
630 /* extensions; however, it'd make sense to check the programs */
631 /* section first, so that's what happens here. */
633 /* See if it's a program - if GetProfileString fails, we skip this
634 * section. Actually, if GetProfileString fails, we've probably
635 * got a lot more to worry about than running a program... */
636 if (GetProfileStringW(L
"windows", L
"programs", L
"exe pif bat cmd com", wBuffer
, ARRAY_SIZE(wBuffer
)) > 0)
643 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
647 while (*p
== ' ' || *p
== '\t') p
++;
650 if (wcsicmp(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
652 lstrcpyW(lpResult
, xlpFile
);
653 /* Need to perhaps check that the file has a path
655 TRACE("found %s\n", debugstr_w(lpResult
));
657 /* Greater than 32 to indicate success */
664 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, classname
,
665 &classnamelen
) == ERROR_SUCCESS
)
667 classnamelen
/= sizeof(WCHAR
);
668 if (classnamelen
== ARRAY_SIZE(classname
))
670 classname
[classnamelen
] = '\0';
671 TRACE("File type: %s\n", debugstr_w(classname
));
681 /* pass the verb string to SHELL_FindExecutableByVerb() */
682 retval
= SHELL_FindExecutableByVerb(lpVerb
, key
, classname
, command
, sizeof(command
));
687 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
688 if (finishedLen
> resultLen
)
689 ERR("Argify buffer not large enough.. truncated\n");
691 /* Remove double quotation marks and command line arguments */
692 if (*lpResult
== '"')
695 while (*(p
+ 1) != '"')
704 /* Truncate on first space */
706 while (*p
!= ' ' && *p
!= '\0')
712 else /* Check win.ini */
714 /* Toss the leading dot */
716 if (GetProfileStringW(L
"extensions", extension
, L
"", command
, ARRAY_SIZE(command
)) > 0)
720 lstrcpyW(lpResult
, command
);
721 tok
= wcschr(lpResult
, '^'); /* should be ^.extension? */
725 lstrcatW(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
726 tok
= wcschr(command
, '^'); /* see above */
727 if ((tok
!= NULL
) && (lstrlenW(tok
)>5))
729 lstrcatW(lpResult
, &tok
[5]);
732 retval
= 33; /* FIXME - see above */
737 TRACE("returning %s\n", debugstr_w(lpResult
));
741 /******************************************************************
744 * callback for the DDE connection. not really useful
746 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
747 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
748 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
750 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08Ix, %08Ix\n",
751 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
755 /******************************************************************
758 * ShellExecute helper. Used to do an operation with a DDE connection
760 * Handles both the direct connection (try #1), and if it fails,
761 * launching an application and trying (#2) to connect to it
764 static unsigned dde_connect(const WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
765 const WCHAR
* lpFile
, WCHAR
*env
,
766 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
767 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
770 WCHAR
* endkey
= regkey
+ lstrlenW(key
);
771 WCHAR app
[256], topic
[256], ifexec
[256], static_res
[256];
772 WCHAR
* dynamic_res
=NULL
;
774 LONG applen
, topiclen
, ifexeclen
;
778 DWORD resultLen
, endkeyLen
;
779 HSZ hszApp
, hszTopic
;
782 unsigned ret
= SE_ERR_NOASSOC
;
783 BOOL unicode
= !(GetVersion() & 0x80000000);
785 if (lstrlenW(key
) + 1 > ARRAY_SIZE(regkey
))
787 FIXME("input parameter %s larger than buffer\n", debugstr_w(key
));
790 lstrcpyW(regkey
, key
);
791 endkeyLen
= ARRAY_SIZE(regkey
) - (endkey
- regkey
);
792 if (lstrlenW(L
"\\application") + 1 > endkeyLen
)
794 FIXME("endkey overruns buffer\n");
797 lstrcpyW(endkey
, L
"\\application");
798 applen
= sizeof(app
);
799 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, app
, &applen
) != ERROR_SUCCESS
)
801 WCHAR command
[1024], fullpath
[MAX_PATH
];
805 /* Get application command from start string and find filename of application */
808 if (lstrlenW(start
+ 1) + 1 > ARRAY_SIZE(command
))
810 FIXME("size of input parameter %s larger than buffer\n",
811 debugstr_w(start
+ 1));
814 lstrcpyW(command
, start
+1);
815 if ((ptr
= wcschr(command
, '"')))
817 ret
= SearchPathW(NULL
, command
, L
".exe", ARRAY_SIZE(fullpath
), fullpath
, &ptr
);
823 for (p
=start
; (space
=wcschr(p
, ' ')); p
=space
+1)
825 int idx
= space
-start
;
826 memcpy(command
, start
, idx
*sizeof(WCHAR
));
828 if ((ret
= SearchPathW(NULL
, command
, L
".exe", ARRAY_SIZE(fullpath
), fullpath
, &ptr
)))
832 ret
= SearchPathW(NULL
, start
, L
".exe", ARRAY_SIZE(fullpath
), fullpath
, &ptr
);
837 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
838 return ERROR_ACCESS_DENIED
;
840 if (lstrlenW(ptr
) + 1 > ARRAY_SIZE(app
))
842 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr
));
847 /* Remove extensions (including .so) */
848 ptr
= app
+ lstrlenW(app
) - 3;
849 if (ptr
> app
&& !wcscmp(ptr
, L
".so"))
852 ptr
= wcsrchr(app
, '.');
857 if (lstrlenW(L
"\\topic") + 1 > endkeyLen
)
859 FIXME("endkey overruns buffer\n");
862 lstrcpyW(endkey
, L
"\\topic");
863 topiclen
= sizeof(topic
);
864 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, topic
, &topiclen
) != ERROR_SUCCESS
)
865 lstrcpyW(topic
, L
"System");
869 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
874 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
878 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
879 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
881 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
885 TRACE("Launching %s\n", debugstr_w(start
));
886 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
889 TRACE("Couldn't launch\n");
892 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
895 TRACE("Couldn't connect. ret=%d\n", ret
);
896 DdeUninitialize(ddeInst
);
897 SetLastError(ERROR_DDE_FAIL
);
898 return 30; /* whatever */
900 if (lstrlenW(L
"\\ifexec") + 1 > endkeyLen
)
902 FIXME("endkey overruns buffer\n");
905 lstrcpyW(endkey
, L
"\\ifexec");
906 ifexeclen
= sizeof(ifexec
);
907 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
913 SHELL_ArgifyW(static_res
, ARRAY_SIZE(static_res
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
914 if (resultLen
> ARRAY_SIZE(static_res
))
916 res
= dynamic_res
= heap_alloc(resultLen
* sizeof(WCHAR
));
917 SHELL_ArgifyW(dynamic_res
, resultLen
, exec
, lpFile
, pidl
, szCommandline
, NULL
);
921 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
923 /* It's documented in the KB 330337 that IE has a bug and returns
924 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
927 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (lstrlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0,
928 XTYP_EXECUTE
, 30000, &tid
);
931 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
932 char *resA
= heap_alloc(lenA
);
933 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
934 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
935 XTYP_EXECUTE
, 10000, &tid
);
939 DdeFreeDataHandle(hDdeData
);
941 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
944 heap_free(dynamic_res
);
946 DdeDisconnect(hConv
);
949 DdeUninitialize(ddeInst
);
954 /*************************************************************************
955 * execute_from_key [Internal]
957 static UINT_PTR
execute_from_key(LPCWSTR key
, LPCWSTR lpFile
, WCHAR
*env
, LPCWSTR szCommandline
,
958 LPCWSTR executable_name
,
959 SHELL_ExecuteW32 execfunc
,
960 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
962 WCHAR cmd
[256], param
[1024], ddeexec
[256];
963 LONG cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
964 UINT_PTR retval
= SE_ERR_NOASSOC
;
968 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
969 debugstr_w(szCommandline
), debugstr_w(executable_name
));
974 /* Get the application from the registry */
975 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
977 TRACE("got cmd: %s\n", debugstr_w(cmd
));
979 /* Is there a replace() function anywhere? */
980 cmdlen
/= sizeof(WCHAR
);
981 if (cmdlen
>= ARRAY_SIZE(cmd
))
982 cmdlen
= ARRAY_SIZE(cmd
) - 1;
984 SHELL_ArgifyW(param
, ARRAY_SIZE(param
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
, &resultLen
);
985 if (resultLen
> ARRAY_SIZE(param
))
986 ERR("Argify buffer not large enough, truncating\n");
989 /* Get the parameters needed by the application
990 from the associated ddeexec key */
991 tmp
= wcsstr(key
, L
"command");
993 lstrcpyW(tmp
, L
"ddeexec");
995 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, &ddeexeclen
) == ERROR_SUCCESS
)
997 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
998 if (!param
[0]) lstrcpyW(param
, executable_name
);
999 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, psei
->lpIDList
, execfunc
, psei
, psei_out
);
1003 TRACE("executing: %s\n", debugstr_w(param
));
1004 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
1007 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
1012 /*************************************************************************
1013 * FindExecutableA [SHELL32.@]
1015 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
1018 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
1019 WCHAR wResult
[MAX_PATH
];
1021 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
1022 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
1024 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
1025 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
1027 SHFree( wDirectory
);
1029 TRACE("returning %s\n", lpResult
);
1033 /*************************************************************************
1034 * FindExecutableW [SHELL32.@]
1036 * This function returns the executable associated with the specified file
1037 * for the default verb.
1040 * lpFile [I] The file to find the association for. This must refer to
1041 * an existing file otherwise FindExecutable fails and returns
1043 * lpResult [O] Points to a buffer into which the executable path is
1044 * copied. This parameter must not be NULL otherwise
1045 * FindExecutable() segfaults. The buffer must be of size at
1046 * least MAX_PATH characters.
1049 * A value greater than 32 on success, less than or equal to 32 otherwise.
1050 * See the SE_ERR_* constants.
1053 * On Windows XP and 2003, FindExecutable() seems to first convert the
1054 * filename into 8.3 format, thus taking into account only the first three
1055 * characters of the extension, and expects to find an association for those.
1056 * However other Windows versions behave sanely.
1058 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
1060 UINT_PTR retval
= SE_ERR_NOASSOC
;
1061 WCHAR old_dir
[1024];
1062 WCHAR res
[MAX_PATH
];
1064 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
1066 lpResult
[0] = '\0'; /* Start off with an empty return string */
1068 return (HINSTANCE
)SE_ERR_FNF
;
1072 GetCurrentDirectoryW(ARRAY_SIZE(old_dir
), old_dir
);
1073 SetCurrentDirectoryW(lpDirectory
);
1076 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, L
"open", res
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1079 lstrcpyW(lpResult
, res
);
1081 TRACE("returning %s\n", debugstr_w(lpResult
));
1083 SetCurrentDirectoryW(old_dir
);
1084 return (HINSTANCE
)retval
;
1087 /* FIXME: is this already implemented somewhere else? */
1088 static HKEY
ShellExecute_GetClassKey( const SHELLEXECUTEINFOW
*sei
)
1090 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1092 DWORD type
= 0, sz
= 0;
1096 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1097 return sei
->hkeyClass
;
1099 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1100 lpClass
= sei
->lpClass
;
1103 ext
= PathFindExtensionW( sei
->lpFile
);
1104 TRACE("ext = %s\n", debugstr_w( ext
) );
1108 r
= RegOpenKeyW( HKEY_CLASSES_ROOT
, ext
, &hkey
);
1109 if (r
!= ERROR_SUCCESS
)
1112 r
= RegQueryValueExW( hkey
, NULL
, 0, &type
, NULL
, &sz
);
1113 if ( r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1115 sz
+= sizeof (WCHAR
);
1116 cls
= heap_alloc( sz
);
1118 RegQueryValueExW( hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1121 RegCloseKey( hkey
);
1125 TRACE("class = %s\n", debugstr_w(lpClass
) );
1129 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1136 static HRESULT
shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
, IDataObject
**dataobj
)
1138 LPCITEMIDLIST pidllast
= NULL
;
1139 IShellFolder
*shf
= NULL
;
1140 LPITEMIDLIST pidl
= NULL
;
1141 HRESULT r
= SE_ERR_DLLNOTFOUND
;
1143 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1144 pidl
= sei
->lpIDList
;
1147 WCHAR fullpath
[MAX_PATH
];
1151 ret
= GetFullPathNameW( sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1155 pidl
= ILCreateFromPathW( fullpath
);
1158 r
= SHBindToParent( pidl
, &IID_IShellFolder
, (LPVOID
*)&shf
, &pidllast
);
1162 r
= IShellFolder_GetUIObjectOf( shf
, NULL
, 1, &pidllast
,
1163 &IID_IDataObject
, NULL
, (void**)dataobj
);
1166 if ( pidl
!= sei
->lpIDList
)
1169 IShellFolder_Release( shf
);
1173 static HRESULT
shellex_run_context_menu_default( IShellExtInit
*obj
,
1174 LPSHELLEXECUTEINFOW sei
)
1176 IContextMenu
*cm
= NULL
;
1177 CMINVOKECOMMANDINFOEX ici
;
1184 TRACE("%p %p\n", obj
, sei
);
1186 r
= IShellExtInit_QueryInterface( obj
, &IID_IContextMenu
, (LPVOID
*) &cm
);
1190 hmenu
= CreateMenu();
1194 /* the number of the last menu added is returned in r */
1195 r
= IContextMenu_QueryContextMenu( cm
, hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1199 n
= GetMenuItemCount( hmenu
);
1200 for ( i
= 0; i
< n
; i
++ )
1202 memset( &info
, 0, sizeof info
);
1203 info
.cbSize
= sizeof info
;
1204 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1205 info
.dwTypeData
= string
;
1206 info
.cch
= ARRAY_SIZE(string
);
1208 GetMenuItemInfoW( hmenu
, i
, TRUE
, &info
);
1210 TRACE("menu %d %s %08x %08Ix %08x %08x\n", i
, debugstr_w(string
),
1211 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1212 if ( ( !sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
) ) ||
1213 ( sei
->lpVerb
&& !lstrcmpiW( sei
->lpVerb
, string
) ) )
1224 memset( &ici
, 0, sizeof ici
);
1225 ici
.cbSize
= sizeof ici
;
1226 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NO_CONSOLE
|SEE_MASK_NOASYNC
|SEE_MASK_ASYNCOK
|SEE_MASK_FLAG_NO_UI
));
1227 ici
.nShow
= sei
->nShow
;
1228 ici
.lpVerb
= MAKEINTRESOURCEA( def
);
1229 ici
.hwnd
= sei
->hwnd
;
1230 ici
.lpParametersW
= sei
->lpParameters
;
1232 r
= IContextMenu_InvokeCommand( cm
, (LPCMINVOKECOMMANDINFO
) &ici
);
1234 TRACE("invoke command returned %08lx\n", r
);
1238 DestroyMenu( hmenu
);
1240 IContextMenu_Release( cm
);
1244 static HRESULT
shellex_load_object_and_run( HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1246 IDataObject
*dataobj
= NULL
;
1247 IObjectWithSite
*ows
= NULL
;
1248 IShellExtInit
*obj
= NULL
;
1251 TRACE("%p %s %p\n", hkey
, debugstr_guid( guid
), sei
);
1253 r
= CoInitialize( NULL
);
1257 r
= CoCreateInstance( guid
, NULL
, CLSCTX_INPROC_SERVER
,
1258 &IID_IShellExtInit
, (LPVOID
*)&obj
);
1261 ERR("failed %08lx\n", r
);
1265 r
= shellex_get_dataobj( sei
, &dataobj
);
1268 ERR("failed to get data object\n");
1272 r
= IShellExtInit_Initialize( obj
, NULL
, dataobj
, hkey
);
1276 r
= IShellExtInit_QueryInterface( obj
, &IID_IObjectWithSite
, (LPVOID
*) &ows
);
1280 IObjectWithSite_SetSite( ows
, NULL
);
1282 r
= shellex_run_context_menu_default( obj
, sei
);
1286 IObjectWithSite_Release( ows
);
1288 IDataObject_Release( dataobj
);
1290 IShellExtInit_Release( obj
);
1296 /*************************************************************************
1297 * ShellExecute_FromContextMenu [Internal]
1299 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1301 HKEY hkey
, hkeycm
= 0;
1308 TRACE("%s\n", debugstr_w(sei
->lpFile
) );
1310 hkey
= ShellExecute_GetClassKey( sei
);
1312 return ERROR_FUNCTION_FAILED
;
1314 r
= RegOpenKeyW( hkey
, L
"shellex\\ContextMenuHandlers", &hkeycm
);
1315 if ( r
== ERROR_SUCCESS
)
1320 r
= RegEnumKeyW( hkeycm
, i
++, szguid
, ARRAY_SIZE(szguid
));
1321 if ( r
!= ERROR_SUCCESS
)
1324 hr
= CLSIDFromString( szguid
, &guid
);
1327 /* stop at the first one that succeeds in running */
1328 hr
= shellex_load_object_and_run( hkey
, &guid
, sei
);
1329 if ( SUCCEEDED( hr
) )
1333 RegCloseKey( hkeycm
);
1336 if ( hkey
!= sei
->hkeyClass
)
1337 RegCloseKey( hkey
);
1341 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
);
1343 static UINT_PTR
SHELL_execute_class( LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1345 WCHAR execCmd
[1024], classname
[1024];
1346 /* launch a document by fileclass like 'WordPad.Document.1' */
1347 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1348 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1349 ULONG cmask
=(psei
->fMask
& SEE_MASK_CLASSALL
);
1354 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1355 if (cmask
!= SEE_MASK_CLASSNAME
)
1358 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1359 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1361 execCmd
, sizeof(execCmd
));
1363 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1364 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1367 done
= SHELL_ArgifyW(wcmd
, ARRAY_SIZE(wcmd
), execCmd
, wszApplicationName
, psei
->lpIDList
, NULL
, &resultLen
);
1368 if (!done
&& wszApplicationName
[0])
1370 lstrcatW(wcmd
, L
" ");
1371 if (*wszApplicationName
!= '"')
1373 lstrcatW(wcmd
, L
"\"");
1374 lstrcatW(wcmd
, wszApplicationName
);
1375 lstrcatW(wcmd
, L
"\"");
1378 lstrcatW(wcmd
, wszApplicationName
);
1380 if (resultLen
> ARRAY_SIZE(wcmd
))
1381 ERR("Argify buffer not large enough... truncating\n");
1382 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1385 lstrcpyW(classname
, psei
->lpClass
);
1386 rslt
= SHELL_FindExecutableByVerb(psei
->lpVerb
, NULL
, classname
, execCmd
, sizeof(execCmd
));
1388 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt
, debugstr_w(classname
), debugstr_w(execCmd
));
1391 rslt
= SHELL_quote_and_execute( execCmd
, L
"", classname
,
1392 wszApplicationName
, NULL
, psei
,
1393 psei_out
, execfunc
);
1397 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1399 WCHAR buffer
[MAX_PATH
];
1401 /* last chance to translate IDList: now also allow CLSID paths */
1402 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei
->lpIDList
, buffer
, ARRAY_SIZE(buffer
)))) {
1403 if (buffer
[0]==':' && buffer
[1]==':') {
1404 /* open shell folder for the specified class GUID */
1405 if (lstrlenW(buffer
) + 1 > parametersLen
)
1406 ERR("parameters len exceeds buffer size (%i > %li), truncating\n",
1407 lstrlenW(buffer
) + 1, parametersLen
);
1408 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1409 if (lstrlenW(L
"explorer.exe") > dwApplicationNameLen
)
1410 ERR("application len exceeds buffer size (%li), truncating\n",
1411 dwApplicationNameLen
);
1412 lstrcpynW(wszApplicationName
, L
"explorer.exe", dwApplicationNameLen
);
1414 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1416 WCHAR target
[MAX_PATH
];
1419 /* Check if we're executing a directory and if so use the
1420 handler for the Folder class */
1421 lstrcpyW(target
, buffer
);
1422 attribs
= GetFileAttributesW(buffer
);
1423 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1424 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1425 HCR_GetExecuteCommandW(0, L
"Folder",
1427 buffer
, sizeof(buffer
))) {
1428 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1429 buffer
, target
, sei
->lpIDList
, NULL
, &resultLen
);
1430 if (resultLen
> dwApplicationNameLen
)
1431 ERR("Argify buffer not large enough... truncating\n");
1433 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1438 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
)
1442 WCHAR
*wszQuotedCmd
;
1444 /* Length of quotes plus length of command plus NULL terminator */
1445 len
= 2 + lstrlenW(wcmd
) + 1;
1446 if (wszParameters
[0])
1448 /* Length of space plus length of parameters */
1449 len
+= 1 + lstrlenW(wszParameters
);
1451 wszQuotedCmd
= heap_alloc(len
* sizeof(WCHAR
));
1452 /* Must quote to handle case where cmd contains spaces,
1453 * else security hole if malicious user creates executable file "C:\\Program"
1455 lstrcpyW(wszQuotedCmd
, L
"\"");
1456 lstrcatW(wszQuotedCmd
, wcmd
);
1457 lstrcatW(wszQuotedCmd
, L
"\"");
1458 if (wszParameters
[0]) {
1459 lstrcatW(wszQuotedCmd
, L
" ");
1460 lstrcatW(wszQuotedCmd
, wszParameters
);
1462 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(wszKeyname
));
1464 retval
= execute_from_key(wszKeyname
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1466 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1467 heap_free(wszQuotedCmd
);
1471 static UINT_PTR
SHELL_execute_url( LPCWSTR lpFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1474 WCHAR
*lpstrProtocol
;
1479 lpstrRes
= wcschr(lpFile
, ':');
1481 iSize
= lpstrRes
- lpFile
;
1483 iSize
= lstrlenW(lpFile
);
1485 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1486 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1487 len
= iSize
+ lstrlenW(L
"\\shell\\") + lstrlenW(L
"\\command") + 1;
1488 if (psei
->lpVerb
&& *psei
->lpVerb
)
1489 len
+= lstrlenW(psei
->lpVerb
);
1491 len
+= lstrlenW(L
"open");
1492 lpstrProtocol
= heap_alloc(len
* sizeof(WCHAR
));
1493 memcpy(lpstrProtocol
, lpFile
, iSize
*sizeof(WCHAR
));
1494 lpstrProtocol
[iSize
] = '\0';
1495 lstrcatW(lpstrProtocol
, L
"\\shell\\");
1496 lstrcatW(lpstrProtocol
, psei
->lpVerb
&& *psei
->lpVerb
? psei
->lpVerb
: L
"open");
1497 lstrcatW(lpstrProtocol
, L
"\\command");
1499 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1500 wcmd
, execfunc
, psei
, psei_out
);
1501 heap_free(lpstrProtocol
);
1505 static void do_error_dialog( UINT_PTR retval
, HWND hwnd
)
1508 int error_code
=GetLastError();
1510 if (retval
== SE_ERR_NOASSOC
)
1511 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, ARRAY_SIZE(msg
));
1513 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error_code
, 0, msg
, ARRAY_SIZE(msg
), NULL
);
1515 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1518 static WCHAR
*expand_environment( const WCHAR
*str
)
1523 len
= ExpandEnvironmentStringsW(str
, NULL
, 0);
1524 if (!len
) return NULL
;
1526 buf
= heap_alloc(len
* sizeof(WCHAR
));
1527 if (!buf
) return NULL
;
1529 len
= ExpandEnvironmentStringsW(str
, buf
, len
);
1538 /*************************************************************************
1539 * SHELL_execute [Internal]
1541 static BOOL
SHELL_execute( LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1543 static const DWORD unsupportedFlags
=
1544 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1545 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1546 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1548 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1549 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
= NULL
;
1550 DWORD dwApplicationNameLen
= MAX_PATH
+2;
1551 DWORD parametersLen
= ARRAY_SIZE(parametersBuffer
);
1552 DWORD wcmdLen
= ARRAY_SIZE(wcmdBuffer
);
1554 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1556 WCHAR wszKeyname
[256];
1558 UINT_PTR retval
= SE_ERR_NOASSOC
;
1560 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1563 TRACE("mask=0x%08lx hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1564 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1565 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1566 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1567 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1568 debugstr_w(sei_tmp
.lpClass
) : "not used");
1570 sei
->hProcess
= NULL
;
1572 /* make copies of all path/command strings */
1573 if (!sei_tmp
.lpFile
)
1575 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1576 *wszApplicationName
= '\0';
1578 else if (*sei_tmp
.lpFile
== '\"' && sei_tmp
.lpFile
[(len
= lstrlenW(sei_tmp
.lpFile
))-1] == '\"')
1580 if(len
-1 >= dwApplicationNameLen
) dwApplicationNameLen
= len
;
1581 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1582 memcpy(wszApplicationName
, sei_tmp
.lpFile
+1, len
*sizeof(WCHAR
));
1584 wszApplicationName
[len
-2] = '\0';
1585 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName
));
1587 DWORD l
= lstrlenW(sei_tmp
.lpFile
)+1;
1588 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1589 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1590 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
*sizeof(WCHAR
));
1593 wszParameters
= parametersBuffer
;
1594 if (sei_tmp
.lpParameters
)
1596 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1597 if (len
> parametersLen
)
1599 wszParameters
= heap_alloc(len
* sizeof(WCHAR
));
1600 parametersLen
= len
;
1602 lstrcpyW(wszParameters
, sei_tmp
.lpParameters
);
1605 *wszParameters
= '\0';
1608 if (sei_tmp
.lpDirectory
)
1610 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1611 if (len
> ARRAY_SIZE(dirBuffer
))
1612 wszDir
= heap_alloc(len
* sizeof(WCHAR
));
1613 lstrcpyW(wszDir
, sei_tmp
.lpDirectory
);
1618 /* adjust string pointers to point to the new buffers */
1619 sei_tmp
.lpFile
= wszApplicationName
;
1620 sei_tmp
.lpParameters
= wszParameters
;
1621 sei_tmp
.lpDirectory
= wszDir
;
1623 if (sei_tmp
.fMask
& unsupportedFlags
)
1625 FIXME("flags ignored: 0x%08lx\n", sei_tmp
.fMask
& unsupportedFlags
);
1628 /* process the IDList */
1629 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1631 IShellExecuteHookW
* pSEH
;
1633 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1637 hr
= IShellExecuteHookW_Execute(pSEH
, &sei_tmp
);
1639 IShellExecuteHookW_Release(pSEH
);
1642 heap_free(wszApplicationName
);
1643 if (wszParameters
!= parametersBuffer
)
1644 heap_free(wszParameters
);
1645 if (wszDir
!= dirBuffer
)
1651 SHGetPathFromIDListW(sei_tmp
.lpIDList
, wszApplicationName
);
1652 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1655 if (sei_tmp
.fMask
& SEE_MASK_DOENVSUBST
)
1659 tmp
= expand_environment(sei_tmp
.lpFile
);
1662 retval
= SE_ERR_OOM
;
1665 heap_free(wszApplicationName
);
1666 sei_tmp
.lpFile
= wszApplicationName
= tmp
;
1668 tmp
= expand_environment(sei_tmp
.lpDirectory
);
1671 retval
= SE_ERR_OOM
;
1674 if (wszDir
!= dirBuffer
)
1676 sei_tmp
.lpDirectory
= wszDir
= tmp
;
1679 if ( ERROR_SUCCESS
== ShellExecute_FromContextMenu( &sei_tmp
) )
1681 sei
->hInstApp
= (HINSTANCE
) 33;
1682 heap_free(wszApplicationName
);
1683 if (wszParameters
!= parametersBuffer
)
1684 heap_free(wszParameters
);
1685 if (wszDir
!= dirBuffer
)
1690 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1692 retval
= SHELL_execute_class( wszApplicationName
, &sei_tmp
, sei
,
1694 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1695 do_error_dialog(retval
, sei_tmp
.hwnd
);
1696 heap_free(wszApplicationName
);
1697 if (wszParameters
!= parametersBuffer
)
1698 heap_free(wszParameters
);
1699 if (wszDir
!= dirBuffer
)
1704 /* Has the IDList not yet been translated? */
1705 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1707 SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1710 dwApplicationNameLen
);
1713 /* convert file URLs */
1714 if (UrlIsFileUrlW(sei_tmp
.lpFile
))
1720 buf
= heap_alloc(size
* sizeof(WCHAR
));
1721 if (!buf
|| FAILED(PathCreateFromUrlW(sei_tmp
.lpFile
, buf
, &size
, 0))) {
1723 retval
= SE_ERR_OOM
;
1727 heap_free(wszApplicationName
);
1728 wszApplicationName
= buf
;
1729 sei_tmp
.lpFile
= wszApplicationName
;
1731 else /* or expand environment strings (not both!) */
1733 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1737 buf
= heap_alloc((len
+ 1) * sizeof(WCHAR
));
1739 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1740 heap_free(wszApplicationName
);
1741 wszApplicationName
= buf
;
1743 sei_tmp
.lpFile
= wszApplicationName
;
1747 if (*sei_tmp
.lpDirectory
)
1749 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1754 buf
= heap_alloc(len
* sizeof(WCHAR
));
1755 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1756 if (wszDir
!= dirBuffer
)
1759 sei_tmp
.lpDirectory
= wszDir
;
1763 /* Else, try to execute the filename */
1764 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1765 lpFile
= sei_tmp
.lpFile
;
1767 lstrcpyW(wcmd
, wszApplicationName
);
1768 if (sei_tmp
.lpDirectory
)
1770 LPCWSTR searchPath
[] = {
1771 sei_tmp
.lpDirectory
,
1774 PathFindOnPathW(wcmd
, searchPath
);
1776 retval
= SHELL_quote_and_execute( wcmd
, wszParameters
, L
"",
1777 wszApplicationName
, NULL
, &sei_tmp
,
1780 heap_free(wszApplicationName
);
1781 if (wszParameters
!= parametersBuffer
)
1782 heap_free(wszParameters
);
1783 if (wszDir
!= dirBuffer
)
1785 if (wcmd
!= wcmdBuffer
)
1790 /* Else, try to find the executable */
1792 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, wszKeyname
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1793 if (retval
> 32) /* Found */
1795 retval
= SHELL_quote_and_execute( wcmd
, wszParameters
, wszKeyname
,
1796 wszApplicationName
, env
, &sei_tmp
,
1800 else if (PathIsDirectoryW(lpFile
))
1802 WCHAR wExec
[MAX_PATH
];
1803 WCHAR
* lpQuotedFile
= heap_alloc( sizeof(WCHAR
) * (lstrlenW(lpFile
) + 3) );
1807 retval
= SHELL_FindExecutable( sei_tmp
.lpDirectory
, L
"explorer",
1808 L
"open", wExec
, MAX_PATH
,
1809 NULL
, &env
, NULL
, NULL
);
1812 lstrcpyW(lpQuotedFile
, L
"\"");
1813 lstrcatW(lpQuotedFile
, lpFile
);
1814 lstrcatW(lpQuotedFile
, L
"\"");
1815 retval
= SHELL_quote_and_execute( wExec
, lpQuotedFile
,
1817 wszApplicationName
, env
,
1818 &sei_tmp
, sei
, execfunc
);
1821 heap_free( lpQuotedFile
);
1824 retval
= 0; /* Out of memory */
1826 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1828 retval
= SHELL_execute_url( lpFile
, wcmd
, &sei_tmp
, sei
, execfunc
);
1830 /* Check if file specified is in the form www.??????.*** */
1831 else if (!wcsnicmp(lpFile
, L
"www", 3))
1833 /* if so, prefix lpFile with http:// and call ShellExecute */
1834 WCHAR lpstrTmpFile
[256];
1835 lstrcpyW(lpstrTmpFile
, L
"http://");
1836 lstrcatW(lpstrTmpFile
, lpFile
);
1837 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1841 TRACE("retval %Iu\n", retval
);
1843 heap_free(wszApplicationName
);
1844 if (wszParameters
!= parametersBuffer
)
1845 heap_free(wszParameters
);
1846 if (wszDir
!= dirBuffer
)
1848 if (wcmd
!= wcmdBuffer
)
1851 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1853 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1854 do_error_dialog(retval
, sei_tmp
.hwnd
);
1858 /*************************************************************************
1859 * ShellExecuteA [SHELL32.290]
1861 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpVerb
, LPCSTR lpFile
,
1862 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1864 SHELLEXECUTEINFOA sei
;
1866 TRACE("%p,%s,%s,%s,%s,%d\n",
1867 hWnd
, debugstr_a(lpVerb
), debugstr_a(lpFile
),
1868 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
1870 sei
.cbSize
= sizeof(sei
);
1871 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1873 sei
.lpVerb
= lpVerb
;
1874 sei
.lpFile
= lpFile
;
1875 sei
.lpParameters
= lpParameters
;
1876 sei
.lpDirectory
= lpDirectory
;
1877 sei
.nShow
= iShowCmd
;
1884 ShellExecuteExA (&sei
);
1885 return sei
.hInstApp
;
1888 /*************************************************************************
1889 * ShellExecuteExA [SHELL32.292]
1892 BOOL WINAPI DECLSPEC_HOTPATCH
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1894 SHELLEXECUTEINFOW seiW
;
1896 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1900 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1903 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1906 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1908 if (sei
->lpParameters
)
1909 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1911 if (sei
->lpDirectory
)
1912 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1914 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
1915 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1917 seiW
.lpClass
= NULL
;
1919 ret
= SHELL_execute( &seiW
, SHELL_ExecuteW
);
1921 sei
->hInstApp
= seiW
.hInstApp
;
1923 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
1924 sei
->hProcess
= seiW
.hProcess
;
1928 SHFree(wParameters
);
1935 /*************************************************************************
1936 * ShellExecuteExW [SHELL32.293]
1939 BOOL WINAPI DECLSPEC_HOTPATCH
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1941 return SHELL_execute( sei
, SHELL_ExecuteW
);
1944 /*************************************************************************
1945 * ShellExecuteW [SHELL32.294]
1947 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1948 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1950 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpVerb
, LPCWSTR lpFile
,
1951 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1953 SHELLEXECUTEINFOW sei
;
1956 sei
.cbSize
= sizeof(sei
);
1957 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1959 sei
.lpVerb
= lpVerb
;
1960 sei
.lpFile
= lpFile
;
1961 sei
.lpParameters
= lpParameters
;
1962 sei
.lpDirectory
= lpDirectory
;
1963 sei
.nShow
= nShowCmd
;
1970 SHELL_execute( &sei
, SHELL_ExecuteW
);
1971 return sei
.hInstApp
;
1974 /*************************************************************************
1975 * WOWShellExecute [SHELL32.@]
1977 * FIXME: the callback function most likely doesn't work the same way on Windows.
1979 HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpVerb
,LPCSTR lpFile
,
1980 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
1982 SHELLEXECUTEINFOW seiW
;
1983 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
1984 HANDLE hProcess
= 0;
1986 seiW
.lpVerb
= lpVerb
? __SHCloneStrAtoW(&wVerb
, lpVerb
) : NULL
;
1987 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
1988 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
1989 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
1991 seiW
.cbSize
= sizeof(seiW
);
1994 seiW
.nShow
= iShowCmd
;
1999 seiW
.hProcess
= hProcess
;
2001 SHELL_execute( &seiW
, callback
);
2005 SHFree(wParameters
);
2007 return seiW
.hInstApp
;
2010 /*************************************************************************
2011 * ShellExec_RunDLLW [SHELL32.@]
2013 void WINAPI
ShellExec_RunDLLW(HWND hwnd
, HINSTANCE instance
, WCHAR
*cmdline
, int cmdshow
)
2015 BOOL in_single_quotes
= FALSE
, in_double_quotes
= FALSE
;
2018 TRACE("%p, %p, %s, %d\n", hwnd
, instance
, debugstr_w(cmdline
), cmdshow
);
2020 /* Replace the first whitespace character in the command line string with a
2021 null terminator to separate the program name from the program arguments */
2022 for (args
= cmdline
; *args
; args
++)
2027 args
++; /* skip the next character */
2030 if (!in_double_quotes
)
2031 in_single_quotes
= !in_single_quotes
;
2034 if (!in_single_quotes
)
2035 in_double_quotes
= !in_double_quotes
;
2039 if (!in_single_quotes
&& !in_double_quotes
)
2049 ShellExecuteW(hwnd
, NULL
, cmdline
, args
, NULL
, cmdshow
);
2052 /*************************************************************************
2053 * ShellExec_RunDLLA [SHELL32.@]
2055 void WINAPI
ShellExec_RunDLLA(HWND hwnd
, HINSTANCE instance
, CHAR
*cmdline
, int cmdshow
)
2058 ShellExec_RunDLLW(hwnd
, instance
, __SHCloneStrAtoW(&cmdlineW
, cmdline
), cmdshow
);
2062 /*************************************************************************
2063 * OpenAs_RunDLLA [SHELL32.@]
2065 void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2067 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2070 /*************************************************************************
2071 * OpenAs_RunDLLW [SHELL32.@]
2073 void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2075 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);
2078 /*************************************************************************
2079 * RegenerateUserEnvironment [SHELL32.@]
2081 BOOL WINAPI
RegenerateUserEnvironment(WCHAR
*wunknown
, BOOL bunknown
)
2083 FIXME("stub: %p, %d\n", wunknown
, bunknown
);