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
);
129 while(*args
&& !isSpace(*args
))
138 while(isSpace(*args
))
143 /* else fall through */
145 if (!done
|| (*fmt
== '1'))
147 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
148 if (SearchPathW(NULL
, lpFile
, L
".exe", ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
153 used
+= lstrlenW(cmd
);
157 res
+= lstrlenW(cmd
);
164 * IE uses this a lot for activating things such as windows media
165 * player. This is not verified to be fully correct but it appears
171 used
+= lstrlenW(lpFile
);
174 lstrcpyW(res
, lpFile
);
175 res
+= lstrlenW(lpFile
);
185 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
186 * allowing a buffer of 100 should more than exceed all needs */
189 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
190 pv
= SHLockShared(hmem
, 0);
191 chars
= swprintf(buf
, ARRAY_SIZE(buf
), L
":%p", pv
);
192 if (chars
>= ARRAY_SIZE(buf
))
193 ERR("pidl format buffer too small!\n");
207 * Check if this is an env-variable here...
210 /* Make sure that we have at least one more %.*/
211 if (wcschr(fmt
, '%'))
213 WCHAR tmpBuffer
[1024];
214 PWSTR tmpB
= tmpBuffer
;
215 WCHAR tmpEnvBuff
[MAX_PATH
];
222 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer
));
224 envRet
= GetEnvironmentVariableW(tmpBuffer
, tmpEnvBuff
, MAX_PATH
);
225 if (envRet
== 0 || envRet
> MAX_PATH
)
227 used
+= lstrlenW(tmpBuffer
);
230 lstrcpyW( res
, tmpBuffer
);
231 res
+= lstrlenW(tmpBuffer
);
236 used
+= lstrlenW(tmpEnvBuff
);
239 lstrcpyW( res
, tmpEnvBuff
);
240 res
+= lstrlenW(tmpEnvBuff
);
247 /* Don't skip past terminator (catch a single '%' at the end) */
269 TRACE("used %i of %i space\n",used
,len
);
276 static HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
279 IShellFolder
* desktop
;
281 HRESULT hr
= SHGetDesktopFolder(&desktop
);
284 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
287 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
289 IShellFolder_Release(desktop
);
295 /*************************************************************************
296 * SHELL_ExecuteW [Internal]
299 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
300 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
302 STARTUPINFOW startup
;
303 PROCESS_INFORMATION info
;
304 UINT_PTR retval
= SE_ERR_NOASSOC
;
306 WCHAR curdir
[MAX_PATH
];
307 DWORD dwCreationFlags
;
309 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
311 /* make sure we don't fail the CreateProcess if the calling app passes in
312 * a bad working directory */
313 if (psei
->lpDirectory
&& psei
->lpDirectory
[0])
315 /* ShellExecute specifies the command from psei->lpDirectory
316 * if present. Not from the current dir as CreateProcess does */
317 if( ( gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
318 if( !SetCurrentDirectoryW( psei
->lpDirectory
))
319 ERR("cannot set directory %s\n", debugstr_w(psei
->lpDirectory
));
322 ZeroMemory(&startup
,sizeof(STARTUPINFOW
));
323 startup
.cb
= sizeof(STARTUPINFOW
);
324 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
325 startup
.wShowWindow
= psei
->nShow
;
326 dwCreationFlags
= CREATE_UNICODE_ENVIRONMENT
;
327 if (!(psei
->fMask
& SEE_MASK_NO_CONSOLE
))
328 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
329 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, dwCreationFlags
, env
,
330 NULL
, &startup
, &info
))
332 /* Give 30 seconds to the app to come up, if desired. Probably only needed
333 when starting app immediately before making a DDE connection. */
335 if (WaitForInputIdle( info
.hProcess
, 30000 ) == WAIT_FAILED
)
336 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
338 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
339 psei_out
->hProcess
= info
.hProcess
;
341 CloseHandle( info
.hProcess
);
342 CloseHandle( info
.hThread
);
344 else if ((retval
= GetLastError()) >= 32)
346 TRACE("CreateProcess returned error %ld\n", retval
);
347 retval
= ERROR_BAD_FORMAT
;
350 TRACE("returning %lu\n", retval
);
352 psei_out
->hInstApp
= (HINSTANCE
)retval
;
354 if( !SetCurrentDirectoryW( curdir
))
355 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
361 /***********************************************************************
362 * SHELL_BuildEnvW [Internal]
364 * Build the environment for the new process, adding the specified
365 * path to the PATH variable. Returned pointer must be freed by caller.
367 static void *SHELL_BuildEnvW( const WCHAR
*path
)
369 WCHAR
*strings
, *new_env
;
371 int total
= lstrlenW(path
) + 1;
372 BOOL got_path
= FALSE
;
374 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
378 int len
= lstrlenW(p
) + 1;
379 if (!wcsnicmp( p
, L
"PATH=", 5 )) got_path
= TRUE
;
383 if (!got_path
) total
+= 5; /* we need to create PATH */
384 total
++; /* terminating null */
386 if (!(new_env
= heap_alloc( total
* sizeof(WCHAR
) )))
388 FreeEnvironmentStringsW( strings
);
395 int len
= lstrlenW(p
) + 1;
396 memcpy( p2
, p
, len
* sizeof(WCHAR
) );
397 if (!wcsnicmp( p
, L
"PATH=", 5 ))
400 lstrcpyW( p2
+ len
, path
);
401 p2
+= lstrlenW(path
) + 1;
408 lstrcpyW( p2
, L
"PATH=" );
409 lstrcatW( p2
, path
);
410 p2
+= lstrlenW(p2
) + 1;
413 FreeEnvironmentStringsW( strings
);
418 /***********************************************************************
419 * SHELL_TryAppPathW [Internal]
421 * Helper function for SHELL_FindExecutable
422 * @param lpResult - pointer to a buffer of size MAX_PATH
423 * On entry: szName is a filename (probably without path separators).
424 * On exit: if szName found in "App Path", place full path in lpResult, and return true
426 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
434 if (env
) *env
= NULL
;
435 lstrcpyW(buffer
, L
"Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\");
436 lstrcatW(buffer
, szName
);
437 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
440 len
= MAX_PATH
*sizeof(WCHAR
);
441 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
447 DWORD count
= sizeof(buffer
);
448 if (!RegQueryValueExW(hkApp
, L
"Path", NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
449 *env
= SHELL_BuildEnvW( buffer
);
453 if (hkApp
) RegCloseKey(hkApp
);
457 /*************************************************************************
458 * SHELL_FindExecutableByVerb [Internal]
460 * called from SHELL_FindExecutable or SHELL_execute_class
462 * classname a buffer, big enough, to get the key name to do actually the
463 * command "WordPad.Document.1\\shell\\open\\command"
464 * passed as "WordPad.Document.1"
466 * lpVerb the operation on it (open)
467 * commandlen the size of command buffer (in bytes)
469 * command a buffer, to store the command to do the
470 * operation on the file
471 * key a buffer, big enough, to get the key name to do actually the
472 * command "WordPad.Document.1\\shell\\open\\command"
475 static UINT
SHELL_FindExecutableByVerb(LPCWSTR lpVerb
, LPWSTR key
, LPWSTR classname
, LPWSTR command
, LONG commandlen
)
478 WCHAR verb
[MAX_PATH
];
480 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, classname
, 0, 0x02000000, &hkeyClass
))
481 return SE_ERR_NOASSOC
;
482 if (!HCR_GetDefaultVerbW(hkeyClass
, lpVerb
, verb
, ARRAY_SIZE(verb
)))
483 return SE_ERR_NOASSOC
;
484 RegCloseKey(hkeyClass
);
486 /* Looking for ...buffer\shell\<verb>\command */
487 lstrcatW(classname
, L
"\\shell\\");
488 lstrcatW(classname
, verb
);
489 lstrcatW(classname
, L
"\\command");
491 if (RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, command
,
492 &commandlen
) == ERROR_SUCCESS
)
494 commandlen
/= sizeof(WCHAR
);
495 if (key
) lstrcpyW(key
, classname
);
499 LONG paramlen
= sizeof(param
);
501 /* FIXME: it seems all Windows version don't behave the same here.
502 * the doc states that this ddeexec information can be found after
504 * on Win98, it doesn't appear, but I think it does on Win2k
506 /* Get the parameters needed by the application
507 from the associated ddeexec key */
508 tmp
= wcsstr(classname
, L
"\\command");
510 lstrcatW(classname
, wDdeexec
);
511 if (RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, param
,
512 ¶mlen
) == ERROR_SUCCESS
)
514 paramlen
/= sizeof(WCHAR
);
515 lstrcatW(command
, L
" ");
516 lstrcatW(command
, param
);
517 commandlen
+= paramlen
;
521 command
[commandlen
] = '\0';
523 return 33; /* FIXME see SHELL_FindExecutable() */
526 return SE_ERR_NOASSOC
;
529 /*************************************************************************
530 * SHELL_FindExecutable [Internal]
532 * Utility for code sharing between FindExecutable and ShellExecute
534 * lpFile the name of a file
535 * lpVerb the operation on it (open)
537 * lpResult a buffer, big enough :-(, to store the command to do the
538 * operation on the file
539 * key a buffer, big enough, to get the key name to do actually the
540 * command (it'll be used afterwards for more information
543 static UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpVerb
,
544 LPWSTR lpResult
, int resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
546 WCHAR
*extension
= NULL
; /* pointer to file extension */
547 WCHAR classname
[256]; /* registry name for this file type */
548 LONG classnamelen
= sizeof(classname
); /* length of above */
549 WCHAR command
[1024]; /* command from registry */
550 WCHAR wBuffer
[256]; /* Used to GetProfileString */
551 UINT retval
= SE_ERR_NOASSOC
;
552 WCHAR
*tok
; /* token pointer */
553 WCHAR xlpFile
[256]; /* result of SearchPath */
554 DWORD attribs
; /* file attributes */
556 TRACE("%s\n", debugstr_w(lpFile
));
559 return ERROR_INVALID_PARAMETER
;
562 lpResult
[0] = '\0'; /* Start off with an empty return string */
563 if (key
) *key
= '\0';
565 /* trap NULL parameters on entry */
568 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
569 debugstr_w(lpFile
), debugstr_w(lpResult
));
570 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
573 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
575 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
579 if (SearchPathW(lpPath
, lpFile
, L
".exe", ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
581 TRACE("SearchPathW returned non-zero\n");
583 /* The file was found in the application-supplied default directory (or the system search path) */
585 else if (lpPath
&& SearchPathW(NULL
, lpFile
, L
".exe", ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
587 TRACE("SearchPathW returned non-zero\n");
589 /* The file was found in one of the directories in the system-wide search path */
592 attribs
= GetFileAttributesW(lpFile
);
593 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
595 lstrcpyW(classname
, L
"Folder");
599 /* Did we get something? Anything? */
602 TRACE("Returning SE_ERR_FNF\n");
605 /* First thing we need is the file's extension */
606 extension
= wcsrchr(xlpFile
, '.'); /* Assume last "." is the one; */
607 /* File->Run in progman uses */
609 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
611 if (extension
== NULL
|| extension
[1]==0)
613 WARN("Returning SE_ERR_NOASSOC\n");
614 return SE_ERR_NOASSOC
;
617 /* Three places to check: */
618 /* 1. win.ini, [windows], programs (NB no leading '.') */
619 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
620 /* 3. win.ini, [extensions], extension (NB no leading '.' */
621 /* All I know of the order is that registry is checked before */
622 /* extensions; however, it'd make sense to check the programs */
623 /* section first, so that's what happens here. */
625 /* See if it's a program - if GetProfileString fails, we skip this
626 * section. Actually, if GetProfileString fails, we've probably
627 * got a lot more to worry about than running a program... */
628 if (GetProfileStringW(L
"windows", L
"programs", L
"exe pif bat cmd com", wBuffer
, ARRAY_SIZE(wBuffer
)) > 0)
635 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
639 while (*p
== ' ' || *p
== '\t') p
++;
642 if (wcsicmp(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
644 lstrcpyW(lpResult
, xlpFile
);
645 /* Need to perhaps check that the file has a path
647 TRACE("found %s\n", debugstr_w(lpResult
));
649 /* Greater than 32 to indicate success */
656 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, classname
,
657 &classnamelen
) == ERROR_SUCCESS
)
659 classnamelen
/= sizeof(WCHAR
);
660 if (classnamelen
== ARRAY_SIZE(classname
))
662 classname
[classnamelen
] = '\0';
663 TRACE("File type: %s\n", debugstr_w(classname
));
673 /* pass the verb string to SHELL_FindExecutableByVerb() */
674 retval
= SHELL_FindExecutableByVerb(lpVerb
, key
, classname
, command
, sizeof(command
));
679 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
680 if (finishedLen
> resultLen
)
681 ERR("Argify buffer not large enough.. truncated\n");
683 /* Remove double quotation marks and command line arguments */
684 if (*lpResult
== '"')
687 while (*(p
+ 1) != '"')
696 /* Truncate on first space */
698 while (*p
!= ' ' && *p
!= '\0')
704 else /* Check win.ini */
706 /* Toss the leading dot */
708 if (GetProfileStringW(L
"extensions", extension
, L
"", command
, ARRAY_SIZE(command
)) > 0)
712 lstrcpyW(lpResult
, command
);
713 tok
= wcschr(lpResult
, '^'); /* should be ^.extension? */
717 lstrcatW(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
718 tok
= wcschr(command
, '^'); /* see above */
719 if ((tok
!= NULL
) && (lstrlenW(tok
)>5))
721 lstrcatW(lpResult
, &tok
[5]);
724 retval
= 33; /* FIXME - see above */
729 TRACE("returning %s\n", debugstr_w(lpResult
));
733 /******************************************************************
736 * callback for the DDE connection. not really useful
738 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
739 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
740 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
742 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
743 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
747 /******************************************************************
750 * ShellExecute helper. Used to do an operation with a DDE connection
752 * Handles both the direct connection (try #1), and if it fails,
753 * launching an application and trying (#2) to connect to it
756 static unsigned dde_connect(const WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
757 const WCHAR
* lpFile
, WCHAR
*env
,
758 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
759 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
762 WCHAR
* endkey
= regkey
+ lstrlenW(key
);
763 WCHAR app
[256], topic
[256], ifexec
[256], static_res
[256];
764 WCHAR
* dynamic_res
=NULL
;
766 LONG applen
, topiclen
, ifexeclen
;
770 DWORD resultLen
, endkeyLen
;
771 HSZ hszApp
, hszTopic
;
774 unsigned ret
= SE_ERR_NOASSOC
;
775 BOOL unicode
= !(GetVersion() & 0x80000000);
777 if (lstrlenW(key
) + 1 > ARRAY_SIZE(regkey
))
779 FIXME("input parameter %s larger than buffer\n", debugstr_w(key
));
782 lstrcpyW(regkey
, key
);
783 endkeyLen
= ARRAY_SIZE(regkey
) - (endkey
- regkey
);
784 if (lstrlenW(L
"\\application") + 1 > endkeyLen
)
786 FIXME("endkey overruns buffer\n");
789 lstrcpyW(endkey
, L
"\\application");
790 applen
= sizeof(app
);
791 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, app
, &applen
) != ERROR_SUCCESS
)
793 WCHAR command
[1024], fullpath
[MAX_PATH
];
797 /* Get application command from start string and find filename of application */
800 if (lstrlenW(start
+ 1) + 1 > ARRAY_SIZE(command
))
802 FIXME("size of input parameter %s larger than buffer\n",
803 debugstr_w(start
+ 1));
806 lstrcpyW(command
, start
+1);
807 if ((ptr
= wcschr(command
, '"')))
809 ret
= SearchPathW(NULL
, command
, L
".exe", ARRAY_SIZE(fullpath
), fullpath
, &ptr
);
815 for (p
=start
; (space
=wcschr(p
, ' ')); p
=space
+1)
817 int idx
= space
-start
;
818 memcpy(command
, start
, idx
*sizeof(WCHAR
));
820 if ((ret
= SearchPathW(NULL
, command
, L
".exe", ARRAY_SIZE(fullpath
), fullpath
, &ptr
)))
824 ret
= SearchPathW(NULL
, start
, L
".exe", ARRAY_SIZE(fullpath
), fullpath
, &ptr
);
829 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
830 return ERROR_ACCESS_DENIED
;
832 if (lstrlenW(ptr
) + 1 > ARRAY_SIZE(app
))
834 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr
));
839 /* Remove extensions (including .so) */
840 ptr
= app
+ lstrlenW(app
) - 3;
841 if (ptr
> app
&& !wcscmp(ptr
, L
".so"))
844 ptr
= wcsrchr(app
, '.');
849 if (lstrlenW(L
"\\topic") + 1 > endkeyLen
)
851 FIXME("endkey overruns buffer\n");
854 lstrcpyW(endkey
, L
"\\topic");
855 topiclen
= sizeof(topic
);
856 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, topic
, &topiclen
) != ERROR_SUCCESS
)
857 lstrcpyW(topic
, L
"System");
861 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
866 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
870 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
871 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
873 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
877 TRACE("Launching %s\n", debugstr_w(start
));
878 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
881 TRACE("Couldn't launch\n");
884 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
887 TRACE("Couldn't connect. ret=%d\n", ret
);
888 DdeUninitialize(ddeInst
);
889 SetLastError(ERROR_DDE_FAIL
);
890 return 30; /* whatever */
892 if (lstrlenW(L
"\\ifexec") + 1 > endkeyLen
)
894 FIXME("endkey overruns buffer\n");
897 lstrcpyW(endkey
, L
"\\ifexec");
898 ifexeclen
= sizeof(ifexec
);
899 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
905 SHELL_ArgifyW(static_res
, ARRAY_SIZE(static_res
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
906 if (resultLen
> ARRAY_SIZE(static_res
))
908 res
= dynamic_res
= heap_alloc(resultLen
* sizeof(WCHAR
));
909 SHELL_ArgifyW(dynamic_res
, resultLen
, exec
, lpFile
, pidl
, szCommandline
, NULL
);
913 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
915 /* It's documented in the KB 330337 that IE has a bug and returns
916 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
919 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (lstrlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0,
920 XTYP_EXECUTE
, 30000, &tid
);
923 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
924 char *resA
= heap_alloc(lenA
);
925 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
926 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
927 XTYP_EXECUTE
, 10000, &tid
);
931 DdeFreeDataHandle(hDdeData
);
933 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
936 heap_free(dynamic_res
);
938 DdeDisconnect(hConv
);
941 DdeUninitialize(ddeInst
);
946 /*************************************************************************
947 * execute_from_key [Internal]
949 static UINT_PTR
execute_from_key(LPCWSTR key
, LPCWSTR lpFile
, WCHAR
*env
, LPCWSTR szCommandline
,
950 LPCWSTR executable_name
,
951 SHELL_ExecuteW32 execfunc
,
952 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
954 WCHAR cmd
[256], param
[1024], ddeexec
[256];
955 LONG cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
956 UINT_PTR retval
= SE_ERR_NOASSOC
;
960 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
961 debugstr_w(szCommandline
), debugstr_w(executable_name
));
966 /* Get the application from the registry */
967 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
969 TRACE("got cmd: %s\n", debugstr_w(cmd
));
971 /* Is there a replace() function anywhere? */
972 cmdlen
/= sizeof(WCHAR
);
973 if (cmdlen
>= ARRAY_SIZE(cmd
))
974 cmdlen
= ARRAY_SIZE(cmd
) - 1;
976 SHELL_ArgifyW(param
, ARRAY_SIZE(param
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
, &resultLen
);
977 if (resultLen
> ARRAY_SIZE(param
))
978 ERR("Argify buffer not large enough, truncating\n");
981 /* Get the parameters needed by the application
982 from the associated ddeexec key */
983 tmp
= wcsstr(key
, L
"command");
985 lstrcpyW(tmp
, L
"ddeexec");
987 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, &ddeexeclen
) == ERROR_SUCCESS
)
989 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
990 if (!param
[0]) lstrcpyW(param
, executable_name
);
991 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, psei
->lpIDList
, execfunc
, psei
, psei_out
);
995 TRACE("executing: %s\n", debugstr_w(param
));
996 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
999 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
1004 /*************************************************************************
1005 * FindExecutableA [SHELL32.@]
1007 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
1010 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
1011 WCHAR wResult
[MAX_PATH
];
1013 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
1014 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
1016 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
1017 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
1019 SHFree( wDirectory
);
1021 TRACE("returning %s\n", lpResult
);
1025 /*************************************************************************
1026 * FindExecutableW [SHELL32.@]
1028 * This function returns the executable associated with the specified file
1029 * for the default verb.
1032 * lpFile [I] The file to find the association for. This must refer to
1033 * an existing file otherwise FindExecutable fails and returns
1035 * lpResult [O] Points to a buffer into which the executable path is
1036 * copied. This parameter must not be NULL otherwise
1037 * FindExecutable() segfaults. The buffer must be of size at
1038 * least MAX_PATH characters.
1041 * A value greater than 32 on success, less than or equal to 32 otherwise.
1042 * See the SE_ERR_* constants.
1045 * On Windows XP and 2003, FindExecutable() seems to first convert the
1046 * filename into 8.3 format, thus taking into account only the first three
1047 * characters of the extension, and expects to find an association for those.
1048 * However other Windows versions behave sanely.
1050 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
1052 UINT_PTR retval
= SE_ERR_NOASSOC
;
1053 WCHAR old_dir
[1024];
1054 WCHAR res
[MAX_PATH
];
1056 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
1058 lpResult
[0] = '\0'; /* Start off with an empty return string */
1060 return (HINSTANCE
)SE_ERR_FNF
;
1064 GetCurrentDirectoryW(ARRAY_SIZE(old_dir
), old_dir
);
1065 SetCurrentDirectoryW(lpDirectory
);
1068 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, L
"open", res
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1071 lstrcpyW(lpResult
, res
);
1073 TRACE("returning %s\n", debugstr_w(lpResult
));
1075 SetCurrentDirectoryW(old_dir
);
1076 return (HINSTANCE
)retval
;
1079 /* FIXME: is this already implemented somewhere else? */
1080 static HKEY
ShellExecute_GetClassKey( const SHELLEXECUTEINFOW
*sei
)
1082 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1084 DWORD type
= 0, sz
= 0;
1088 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1089 return sei
->hkeyClass
;
1091 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1092 lpClass
= sei
->lpClass
;
1095 ext
= PathFindExtensionW( sei
->lpFile
);
1096 TRACE("ext = %s\n", debugstr_w( ext
) );
1100 r
= RegOpenKeyW( HKEY_CLASSES_ROOT
, ext
, &hkey
);
1101 if (r
!= ERROR_SUCCESS
)
1104 r
= RegQueryValueExW( hkey
, NULL
, 0, &type
, NULL
, &sz
);
1105 if ( r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1107 sz
+= sizeof (WCHAR
);
1108 cls
= heap_alloc( sz
);
1110 RegQueryValueExW( hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1113 RegCloseKey( hkey
);
1117 TRACE("class = %s\n", debugstr_w(lpClass
) );
1121 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1128 static HRESULT
shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
, IDataObject
**dataobj
)
1130 LPCITEMIDLIST pidllast
= NULL
;
1131 IShellFolder
*shf
= NULL
;
1132 LPITEMIDLIST pidl
= NULL
;
1133 HRESULT r
= SE_ERR_DLLNOTFOUND
;
1135 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1136 pidl
= sei
->lpIDList
;
1139 WCHAR fullpath
[MAX_PATH
];
1143 ret
= GetFullPathNameW( sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1147 pidl
= ILCreateFromPathW( fullpath
);
1150 r
= SHBindToParent( pidl
, &IID_IShellFolder
, (LPVOID
*)&shf
, &pidllast
);
1154 r
= IShellFolder_GetUIObjectOf( shf
, NULL
, 1, &pidllast
,
1155 &IID_IDataObject
, NULL
, (void**)dataobj
);
1158 if ( pidl
!= sei
->lpIDList
)
1161 IShellFolder_Release( shf
);
1165 static HRESULT
shellex_run_context_menu_default( IShellExtInit
*obj
,
1166 LPSHELLEXECUTEINFOW sei
)
1168 IContextMenu
*cm
= NULL
;
1169 CMINVOKECOMMANDINFOEX ici
;
1176 TRACE("%p %p\n", obj
, sei
);
1178 r
= IShellExtInit_QueryInterface( obj
, &IID_IContextMenu
, (LPVOID
*) &cm
);
1182 hmenu
= CreateMenu();
1186 /* the number of the last menu added is returned in r */
1187 r
= IContextMenu_QueryContextMenu( cm
, hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1191 n
= GetMenuItemCount( hmenu
);
1192 for ( i
= 0; i
< n
; i
++ )
1194 memset( &info
, 0, sizeof info
);
1195 info
.cbSize
= sizeof info
;
1196 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1197 info
.dwTypeData
= string
;
1198 info
.cch
= ARRAY_SIZE(string
);
1200 GetMenuItemInfoW( hmenu
, i
, TRUE
, &info
);
1202 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1203 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1204 if ( ( !sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
) ) ||
1205 ( sei
->lpVerb
&& !lstrcmpiW( sei
->lpVerb
, string
) ) )
1216 memset( &ici
, 0, sizeof ici
);
1217 ici
.cbSize
= sizeof ici
;
1218 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NO_CONSOLE
|SEE_MASK_NOASYNC
|SEE_MASK_ASYNCOK
|SEE_MASK_FLAG_NO_UI
));
1219 ici
.nShow
= sei
->nShow
;
1220 ici
.lpVerb
= MAKEINTRESOURCEA( def
);
1221 ici
.hwnd
= sei
->hwnd
;
1222 ici
.lpParametersW
= sei
->lpParameters
;
1224 r
= IContextMenu_InvokeCommand( cm
, (LPCMINVOKECOMMANDINFO
) &ici
);
1226 TRACE("invoke command returned %08x\n", r
);
1230 DestroyMenu( hmenu
);
1232 IContextMenu_Release( cm
);
1236 static HRESULT
shellex_load_object_and_run( HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1238 IDataObject
*dataobj
= NULL
;
1239 IObjectWithSite
*ows
= NULL
;
1240 IShellExtInit
*obj
= NULL
;
1243 TRACE("%p %s %p\n", hkey
, debugstr_guid( guid
), sei
);
1245 r
= CoInitialize( NULL
);
1249 r
= CoCreateInstance( guid
, NULL
, CLSCTX_INPROC_SERVER
,
1250 &IID_IShellExtInit
, (LPVOID
*)&obj
);
1253 ERR("failed %08x\n", r
);
1257 r
= shellex_get_dataobj( sei
, &dataobj
);
1260 ERR("failed to get data object\n");
1264 r
= IShellExtInit_Initialize( obj
, NULL
, dataobj
, hkey
);
1268 r
= IShellExtInit_QueryInterface( obj
, &IID_IObjectWithSite
, (LPVOID
*) &ows
);
1272 IObjectWithSite_SetSite( ows
, NULL
);
1274 r
= shellex_run_context_menu_default( obj
, sei
);
1278 IObjectWithSite_Release( ows
);
1280 IDataObject_Release( dataobj
);
1282 IShellExtInit_Release( obj
);
1288 /*************************************************************************
1289 * ShellExecute_FromContextMenu [Internal]
1291 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1293 HKEY hkey
, hkeycm
= 0;
1300 TRACE("%s\n", debugstr_w(sei
->lpFile
) );
1302 hkey
= ShellExecute_GetClassKey( sei
);
1304 return ERROR_FUNCTION_FAILED
;
1306 r
= RegOpenKeyW( hkey
, L
"shellex\\ContextMenuHandlers", &hkeycm
);
1307 if ( r
== ERROR_SUCCESS
)
1312 r
= RegEnumKeyW( hkeycm
, i
++, szguid
, ARRAY_SIZE(szguid
));
1313 if ( r
!= ERROR_SUCCESS
)
1316 hr
= CLSIDFromString( szguid
, &guid
);
1319 /* stop at the first one that succeeds in running */
1320 hr
= shellex_load_object_and_run( hkey
, &guid
, sei
);
1321 if ( SUCCEEDED( hr
) )
1325 RegCloseKey( hkeycm
);
1328 if ( hkey
!= sei
->hkeyClass
)
1329 RegCloseKey( hkey
);
1333 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
);
1335 static UINT_PTR
SHELL_execute_class( LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1337 WCHAR execCmd
[1024], classname
[1024];
1338 /* launch a document by fileclass like 'WordPad.Document.1' */
1339 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1340 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1341 ULONG cmask
=(psei
->fMask
& SEE_MASK_CLASSALL
);
1346 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1347 if (cmask
!= SEE_MASK_CLASSNAME
)
1350 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1351 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1353 execCmd
, sizeof(execCmd
));
1355 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1356 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1359 done
= SHELL_ArgifyW(wcmd
, ARRAY_SIZE(wcmd
), execCmd
, wszApplicationName
, psei
->lpIDList
, NULL
, &resultLen
);
1360 if (!done
&& wszApplicationName
[0])
1362 lstrcatW(wcmd
, L
" ");
1363 if (*wszApplicationName
!= '"')
1365 lstrcatW(wcmd
, L
"\"");
1366 lstrcatW(wcmd
, wszApplicationName
);
1367 lstrcatW(wcmd
, L
"\"");
1370 lstrcatW(wcmd
, wszApplicationName
);
1372 if (resultLen
> ARRAY_SIZE(wcmd
))
1373 ERR("Argify buffer not large enough... truncating\n");
1374 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1377 lstrcpyW(classname
, psei
->lpClass
);
1378 rslt
= SHELL_FindExecutableByVerb(psei
->lpVerb
, NULL
, classname
, execCmd
, sizeof(execCmd
));
1380 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt
, debugstr_w(classname
), debugstr_w(execCmd
));
1383 rslt
= SHELL_quote_and_execute( execCmd
, L
"", classname
,
1384 wszApplicationName
, NULL
, psei
,
1385 psei_out
, execfunc
);
1389 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1391 WCHAR buffer
[MAX_PATH
];
1393 /* last chance to translate IDList: now also allow CLSID paths */
1394 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei
->lpIDList
, buffer
, ARRAY_SIZE(buffer
)))) {
1395 if (buffer
[0]==':' && buffer
[1]==':') {
1396 /* open shell folder for the specified class GUID */
1397 if (lstrlenW(buffer
) + 1 > parametersLen
)
1398 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1399 lstrlenW(buffer
) + 1, parametersLen
);
1400 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1401 if (lstrlenW(L
"explorer.exe") > dwApplicationNameLen
)
1402 ERR("application len exceeds buffer size (%i), truncating\n",
1403 dwApplicationNameLen
);
1404 lstrcpynW(wszApplicationName
, L
"explorer.exe", dwApplicationNameLen
);
1406 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1408 WCHAR target
[MAX_PATH
];
1411 /* Check if we're executing a directory and if so use the
1412 handler for the Folder class */
1413 lstrcpyW(target
, buffer
);
1414 attribs
= GetFileAttributesW(buffer
);
1415 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1416 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1417 HCR_GetExecuteCommandW(0, L
"Folder",
1419 buffer
, sizeof(buffer
))) {
1420 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1421 buffer
, target
, sei
->lpIDList
, NULL
, &resultLen
);
1422 if (resultLen
> dwApplicationNameLen
)
1423 ERR("Argify buffer not large enough... truncating\n");
1425 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1430 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
)
1434 WCHAR
*wszQuotedCmd
;
1436 /* Length of quotes plus length of command plus NULL terminator */
1437 len
= 2 + lstrlenW(wcmd
) + 1;
1438 if (wszParameters
[0])
1440 /* Length of space plus length of parameters */
1441 len
+= 1 + lstrlenW(wszParameters
);
1443 wszQuotedCmd
= heap_alloc(len
* sizeof(WCHAR
));
1444 /* Must quote to handle case where cmd contains spaces,
1445 * else security hole if malicious user creates executable file "C:\\Program"
1447 lstrcpyW(wszQuotedCmd
, L
"\"");
1448 lstrcatW(wszQuotedCmd
, wcmd
);
1449 lstrcatW(wszQuotedCmd
, L
"\"");
1450 if (wszParameters
[0]) {
1451 lstrcatW(wszQuotedCmd
, L
" ");
1452 lstrcatW(wszQuotedCmd
, wszParameters
);
1454 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(wszKeyname
));
1456 retval
= execute_from_key(wszKeyname
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1458 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1459 heap_free(wszQuotedCmd
);
1463 static UINT_PTR
SHELL_execute_url( LPCWSTR lpFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1466 WCHAR
*lpstrProtocol
;
1471 lpstrRes
= wcschr(lpFile
, ':');
1473 iSize
= lpstrRes
- lpFile
;
1475 iSize
= lstrlenW(lpFile
);
1477 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1478 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1479 len
= iSize
+ lstrlenW(L
"\\shell\\") + lstrlenW(L
"\\command") + 1;
1480 if (psei
->lpVerb
&& *psei
->lpVerb
)
1481 len
+= lstrlenW(psei
->lpVerb
);
1483 len
+= lstrlenW(L
"open");
1484 lpstrProtocol
= heap_alloc(len
* sizeof(WCHAR
));
1485 memcpy(lpstrProtocol
, lpFile
, iSize
*sizeof(WCHAR
));
1486 lpstrProtocol
[iSize
] = '\0';
1487 lstrcatW(lpstrProtocol
, L
"\\shell\\");
1488 lstrcatW(lpstrProtocol
, psei
->lpVerb
&& *psei
->lpVerb
? psei
->lpVerb
: L
"open");
1489 lstrcatW(lpstrProtocol
, L
"\\command");
1491 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1492 wcmd
, execfunc
, psei
, psei_out
);
1493 heap_free(lpstrProtocol
);
1497 static void do_error_dialog( UINT_PTR retval
, HWND hwnd
)
1500 int error_code
=GetLastError();
1502 if (retval
== SE_ERR_NOASSOC
)
1503 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, ARRAY_SIZE(msg
));
1505 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error_code
, 0, msg
, ARRAY_SIZE(msg
), NULL
);
1507 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1510 static WCHAR
*expand_environment( const WCHAR
*str
)
1515 len
= ExpandEnvironmentStringsW(str
, NULL
, 0);
1516 if (!len
) return NULL
;
1518 buf
= heap_alloc(len
* sizeof(WCHAR
));
1519 if (!buf
) return NULL
;
1521 len
= ExpandEnvironmentStringsW(str
, buf
, len
);
1530 /*************************************************************************
1531 * SHELL_execute [Internal]
1533 static BOOL
SHELL_execute( LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1535 static const DWORD unsupportedFlags
=
1536 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1537 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1538 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1540 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1541 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
= NULL
;
1542 DWORD dwApplicationNameLen
= MAX_PATH
+2;
1543 DWORD parametersLen
= ARRAY_SIZE(parametersBuffer
);
1544 DWORD wcmdLen
= ARRAY_SIZE(wcmdBuffer
);
1546 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1548 WCHAR wszKeyname
[256];
1550 UINT_PTR retval
= SE_ERR_NOASSOC
;
1552 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1555 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1556 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1557 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1558 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1559 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1560 debugstr_w(sei_tmp
.lpClass
) : "not used");
1562 sei
->hProcess
= NULL
;
1564 /* make copies of all path/command strings */
1565 if (!sei_tmp
.lpFile
)
1567 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1568 *wszApplicationName
= '\0';
1570 else if (*sei_tmp
.lpFile
== '\"' && sei_tmp
.lpFile
[(len
= lstrlenW(sei_tmp
.lpFile
))-1] == '\"')
1572 if(len
-1 >= dwApplicationNameLen
) dwApplicationNameLen
= len
;
1573 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1574 memcpy(wszApplicationName
, sei_tmp
.lpFile
+1, len
*sizeof(WCHAR
));
1576 wszApplicationName
[len
-2] = '\0';
1577 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName
));
1579 DWORD l
= lstrlenW(sei_tmp
.lpFile
)+1;
1580 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1581 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1582 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
*sizeof(WCHAR
));
1585 wszParameters
= parametersBuffer
;
1586 if (sei_tmp
.lpParameters
)
1588 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1589 if (len
> parametersLen
)
1591 wszParameters
= heap_alloc(len
* sizeof(WCHAR
));
1592 parametersLen
= len
;
1594 lstrcpyW(wszParameters
, sei_tmp
.lpParameters
);
1597 *wszParameters
= '\0';
1600 if (sei_tmp
.lpDirectory
)
1602 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1603 if (len
> ARRAY_SIZE(dirBuffer
))
1604 wszDir
= heap_alloc(len
* sizeof(WCHAR
));
1605 lstrcpyW(wszDir
, sei_tmp
.lpDirectory
);
1610 /* adjust string pointers to point to the new buffers */
1611 sei_tmp
.lpFile
= wszApplicationName
;
1612 sei_tmp
.lpParameters
= wszParameters
;
1613 sei_tmp
.lpDirectory
= wszDir
;
1615 if (sei_tmp
.fMask
& unsupportedFlags
)
1617 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1620 /* process the IDList */
1621 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1623 IShellExecuteHookW
* pSEH
;
1625 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1629 hr
= IShellExecuteHookW_Execute(pSEH
, &sei_tmp
);
1631 IShellExecuteHookW_Release(pSEH
);
1634 heap_free(wszApplicationName
);
1635 if (wszParameters
!= parametersBuffer
)
1636 heap_free(wszParameters
);
1637 if (wszDir
!= dirBuffer
)
1643 SHGetPathFromIDListW(sei_tmp
.lpIDList
, wszApplicationName
);
1644 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1647 if (sei_tmp
.fMask
& SEE_MASK_DOENVSUBST
)
1651 tmp
= expand_environment(sei_tmp
.lpFile
);
1654 retval
= SE_ERR_OOM
;
1657 heap_free(wszApplicationName
);
1658 sei_tmp
.lpFile
= wszApplicationName
= tmp
;
1660 tmp
= expand_environment(sei_tmp
.lpDirectory
);
1663 retval
= SE_ERR_OOM
;
1666 if (wszDir
!= dirBuffer
)
1668 sei_tmp
.lpDirectory
= wszDir
= tmp
;
1671 if ( ERROR_SUCCESS
== ShellExecute_FromContextMenu( &sei_tmp
) )
1673 sei
->hInstApp
= (HINSTANCE
) 33;
1674 heap_free(wszApplicationName
);
1675 if (wszParameters
!= parametersBuffer
)
1676 heap_free(wszParameters
);
1677 if (wszDir
!= dirBuffer
)
1682 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1684 retval
= SHELL_execute_class( wszApplicationName
, &sei_tmp
, sei
,
1686 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1687 do_error_dialog(retval
, sei_tmp
.hwnd
);
1688 heap_free(wszApplicationName
);
1689 if (wszParameters
!= parametersBuffer
)
1690 heap_free(wszParameters
);
1691 if (wszDir
!= dirBuffer
)
1696 /* Has the IDList not yet been translated? */
1697 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1699 SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1702 dwApplicationNameLen
);
1705 /* convert file URLs */
1706 if (UrlIsFileUrlW(sei_tmp
.lpFile
))
1712 buf
= heap_alloc(size
* sizeof(WCHAR
));
1713 if (!buf
|| FAILED(PathCreateFromUrlW(sei_tmp
.lpFile
, buf
, &size
, 0))) {
1715 retval
= SE_ERR_OOM
;
1719 heap_free(wszApplicationName
);
1720 wszApplicationName
= buf
;
1721 sei_tmp
.lpFile
= wszApplicationName
;
1723 else /* or expand environment strings (not both!) */
1725 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1729 buf
= heap_alloc((len
+ 1) * sizeof(WCHAR
));
1731 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1732 heap_free(wszApplicationName
);
1733 wszApplicationName
= buf
;
1735 sei_tmp
.lpFile
= wszApplicationName
;
1739 if (*sei_tmp
.lpDirectory
)
1741 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1746 buf
= heap_alloc(len
* sizeof(WCHAR
));
1747 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1748 if (wszDir
!= dirBuffer
)
1751 sei_tmp
.lpDirectory
= wszDir
;
1755 /* Else, try to execute the filename */
1756 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1757 lpFile
= sei_tmp
.lpFile
;
1759 lstrcpyW(wcmd
, wszApplicationName
);
1760 if (sei_tmp
.lpDirectory
)
1762 LPCWSTR searchPath
[] = {
1763 sei_tmp
.lpDirectory
,
1766 PathFindOnPathW(wcmd
, searchPath
);
1768 retval
= SHELL_quote_and_execute( wcmd
, wszParameters
, L
"",
1769 wszApplicationName
, NULL
, &sei_tmp
,
1772 heap_free(wszApplicationName
);
1773 if (wszParameters
!= parametersBuffer
)
1774 heap_free(wszParameters
);
1775 if (wszDir
!= dirBuffer
)
1777 if (wcmd
!= wcmdBuffer
)
1782 /* Else, try to find the executable */
1784 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, wszKeyname
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1785 if (retval
> 32) /* Found */
1787 retval
= SHELL_quote_and_execute( wcmd
, wszParameters
, wszKeyname
,
1788 wszApplicationName
, env
, &sei_tmp
,
1792 else if (PathIsDirectoryW(lpFile
))
1794 WCHAR wExec
[MAX_PATH
];
1795 WCHAR
* lpQuotedFile
= heap_alloc( sizeof(WCHAR
) * (lstrlenW(lpFile
) + 3) );
1799 retval
= SHELL_FindExecutable( sei_tmp
.lpDirectory
, L
"explorer",
1800 L
"open", wExec
, MAX_PATH
,
1801 NULL
, &env
, NULL
, NULL
);
1804 lstrcpyW(lpQuotedFile
, L
"\"");
1805 lstrcatW(lpQuotedFile
, lpFile
);
1806 lstrcatW(lpQuotedFile
, L
"\"");
1807 retval
= SHELL_quote_and_execute( wExec
, lpQuotedFile
,
1809 wszApplicationName
, env
,
1810 &sei_tmp
, sei
, execfunc
);
1813 heap_free( lpQuotedFile
);
1816 retval
= 0; /* Out of memory */
1818 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1820 retval
= SHELL_execute_url( lpFile
, wcmd
, &sei_tmp
, sei
, execfunc
);
1822 /* Check if file specified is in the form www.??????.*** */
1823 else if (!wcsnicmp(lpFile
, L
"www", 3))
1825 /* if so, prefix lpFile with http:// and call ShellExecute */
1826 WCHAR lpstrTmpFile
[256];
1827 lstrcpyW(lpstrTmpFile
, L
"http://");
1828 lstrcatW(lpstrTmpFile
, lpFile
);
1829 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1833 TRACE("retval %lu\n", retval
);
1835 heap_free(wszApplicationName
);
1836 if (wszParameters
!= parametersBuffer
)
1837 heap_free(wszParameters
);
1838 if (wszDir
!= dirBuffer
)
1840 if (wcmd
!= wcmdBuffer
)
1843 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1845 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1846 do_error_dialog(retval
, sei_tmp
.hwnd
);
1850 /*************************************************************************
1851 * ShellExecuteA [SHELL32.290]
1853 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpVerb
, LPCSTR lpFile
,
1854 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1856 SHELLEXECUTEINFOA sei
;
1858 TRACE("%p,%s,%s,%s,%s,%d\n",
1859 hWnd
, debugstr_a(lpVerb
), debugstr_a(lpFile
),
1860 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
1862 sei
.cbSize
= sizeof(sei
);
1863 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1865 sei
.lpVerb
= lpVerb
;
1866 sei
.lpFile
= lpFile
;
1867 sei
.lpParameters
= lpParameters
;
1868 sei
.lpDirectory
= lpDirectory
;
1869 sei
.nShow
= iShowCmd
;
1876 ShellExecuteExA (&sei
);
1877 return sei
.hInstApp
;
1880 /*************************************************************************
1881 * ShellExecuteExA [SHELL32.292]
1884 BOOL WINAPI DECLSPEC_HOTPATCH
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1886 SHELLEXECUTEINFOW seiW
;
1888 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1892 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1895 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1898 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1900 if (sei
->lpParameters
)
1901 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1903 if (sei
->lpDirectory
)
1904 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1906 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
1907 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1909 seiW
.lpClass
= NULL
;
1911 ret
= SHELL_execute( &seiW
, SHELL_ExecuteW
);
1913 sei
->hInstApp
= seiW
.hInstApp
;
1915 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
1916 sei
->hProcess
= seiW
.hProcess
;
1920 SHFree(wParameters
);
1927 /*************************************************************************
1928 * ShellExecuteExW [SHELL32.293]
1931 BOOL WINAPI DECLSPEC_HOTPATCH
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1933 return SHELL_execute( sei
, SHELL_ExecuteW
);
1936 /*************************************************************************
1937 * ShellExecuteW [SHELL32.294]
1939 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1940 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1942 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpVerb
, LPCWSTR lpFile
,
1943 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1945 SHELLEXECUTEINFOW sei
;
1948 sei
.cbSize
= sizeof(sei
);
1949 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1951 sei
.lpVerb
= lpVerb
;
1952 sei
.lpFile
= lpFile
;
1953 sei
.lpParameters
= lpParameters
;
1954 sei
.lpDirectory
= lpDirectory
;
1955 sei
.nShow
= nShowCmd
;
1962 SHELL_execute( &sei
, SHELL_ExecuteW
);
1963 return sei
.hInstApp
;
1966 /*************************************************************************
1967 * WOWShellExecute [SHELL32.@]
1969 * FIXME: the callback function most likely doesn't work the same way on Windows.
1971 HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpVerb
,LPCSTR lpFile
,
1972 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
1974 SHELLEXECUTEINFOW seiW
;
1975 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
1976 HANDLE hProcess
= 0;
1978 seiW
.lpVerb
= lpVerb
? __SHCloneStrAtoW(&wVerb
, lpVerb
) : NULL
;
1979 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
1980 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
1981 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
1983 seiW
.cbSize
= sizeof(seiW
);
1986 seiW
.nShow
= iShowCmd
;
1991 seiW
.hProcess
= hProcess
;
1993 SHELL_execute( &seiW
, callback
);
1997 SHFree(wParameters
);
1999 return seiW
.hInstApp
;
2002 /*************************************************************************
2003 * ShellExec_RunDLLW [SHELL32.@]
2005 void WINAPI
ShellExec_RunDLLW(HWND hwnd
, HINSTANCE instance
, WCHAR
*cmdline
, int cmdshow
)
2007 BOOL in_single_quotes
= FALSE
, in_double_quotes
= FALSE
;
2010 TRACE("%p, %p, %s, %d\n", hwnd
, instance
, debugstr_w(cmdline
), cmdshow
);
2012 /* Replace the first whitespace character in the command line string with a
2013 null terminator to separate the program name from the program arguments */
2014 for (args
= cmdline
; *args
; args
++)
2019 args
++; /* skip the next character */
2022 if (!in_double_quotes
)
2023 in_single_quotes
= !in_single_quotes
;
2026 if (!in_single_quotes
)
2027 in_double_quotes
= !in_double_quotes
;
2031 if (!in_single_quotes
&& !in_double_quotes
)
2041 ShellExecuteW(hwnd
, NULL
, cmdline
, args
, NULL
, cmdshow
);
2044 /*************************************************************************
2045 * ShellExec_RunDLLA [SHELL32.@]
2047 void WINAPI
ShellExec_RunDLLA(HWND hwnd
, HINSTANCE instance
, CHAR
*cmdline
, int cmdshow
)
2050 ShellExec_RunDLLW(hwnd
, instance
, __SHCloneStrAtoW(&cmdlineW
, cmdline
), cmdshow
);
2054 /*************************************************************************
2055 * OpenAs_RunDLLA [SHELL32.@]
2057 void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2059 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2062 /*************************************************************************
2063 * OpenAs_RunDLLW [SHELL32.@]
2065 void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2067 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);
2070 /*************************************************************************
2071 * RegenerateUserEnvironment [SHELL32.@]
2073 BOOL WINAPI
RegenerateUserEnvironment(WCHAR
*wunknown
, BOOL bunknown
)
2075 FIXME("stub: %p, %d\n", wunknown
, bunknown
);