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
23 #include "wine/port.h"
45 #include "shell32_main.h"
49 #include "wine/debug.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
53 static const WCHAR wszOpen
[] = {'o','p','e','n',0};
54 static const WCHAR wszExe
[] = {'.','e','x','e',0};
55 static const WCHAR wszILPtr
[] = {':','%','p',0};
56 static const WCHAR wszShell
[] = {'\\','s','h','e','l','l','\\',0};
57 static const WCHAR wszFolder
[] = {'F','o','l','d','e','r',0};
58 static const WCHAR wszEmpty
[] = {0};
60 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
62 typedef UINT_PTR (*SHELL_ExecuteW32
)(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
63 const SHELLEXECUTEINFOW
*sei
, LPSHELLEXECUTEINFOW sei_out
);
65 static inline BOOL
isSpace(WCHAR c
)
67 return c
== ' ' || c
== '\f' || c
== '\n' || c
== '\r' || c
== '\t' || c
== '\v';
70 /***********************************************************************
71 * SHELL_ArgifyW [Internal]
73 * this function is supposed to expand the escape sequences found in the registry
74 * some diving reported that the following were used:
75 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
80 * %I address of a global item ID (explorer switch /idlist)
81 * %L seems to be %1 as long filename followed by the 8+3 variation
83 * %* all following parameters (see batfile)
86 static BOOL
SHELL_ArgifyW(WCHAR
* out
, int len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
, DWORD
* out_len
)
90 BOOL found_p1
= FALSE
;
95 TRACE("%p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
96 debugstr_w(lpFile
), pidl
, args
);
142 while(*args
&& !isSpace(*args
))
151 while(isSpace(*args
))
156 /* else fall through */
158 if (!done
|| (*fmt
== '1'))
160 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
161 if (SearchPathW(NULL
, lpFile
, wszExe
, ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
166 used
+= strlenW(cmd
);
177 * IE uses this a lot for activating things such as windows media
178 * player. This is not verified to be fully correct but it appears
184 used
+= strlenW(lpFile
);
187 strcpyW(res
, lpFile
);
188 res
+= strlenW(lpFile
);
198 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
199 * allowing a buffer of 100 should more than exceed all needs */
202 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
203 pv
= SHLockShared(hmem
, 0);
204 chars
= sprintfW(buf
, wszILPtr
, pv
);
205 if (chars
>= ARRAY_SIZE(buf
))
206 ERR("pidl format buffer too small!\n");
220 * Check if this is an env-variable here...
223 /* Make sure that we have at least one more %.*/
224 if (strchrW(fmt
, '%'))
226 WCHAR tmpBuffer
[1024];
227 PWSTR tmpB
= tmpBuffer
;
228 WCHAR tmpEnvBuff
[MAX_PATH
];
235 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer
));
237 envRet
= GetEnvironmentVariableW(tmpBuffer
, tmpEnvBuff
, MAX_PATH
);
238 if (envRet
== 0 || envRet
> MAX_PATH
)
240 used
+= strlenW(tmpBuffer
);
243 strcpyW( res
, tmpBuffer
);
244 res
+= strlenW(tmpBuffer
);
249 used
+= strlenW(tmpEnvBuff
);
252 strcpyW( res
, tmpEnvBuff
);
253 res
+= strlenW(tmpEnvBuff
);
260 /* Don't skip past terminator (catch a single '%' at the end) */
282 TRACE("used %i of %i space\n",used
,len
);
289 static HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
292 IShellFolder
* desktop
;
294 HRESULT hr
= SHGetDesktopFolder(&desktop
);
297 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
300 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
302 IShellFolder_Release(desktop
);
308 /*************************************************************************
309 * SHELL_ExecuteW [Internal]
312 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
313 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
315 STARTUPINFOW startup
;
316 PROCESS_INFORMATION info
;
317 UINT_PTR retval
= SE_ERR_NOASSOC
;
319 WCHAR curdir
[MAX_PATH
];
320 DWORD dwCreationFlags
;
321 const WCHAR
*lpDirectory
= NULL
;
323 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
325 /* make sure we don't fail the CreateProcess if the calling app passes in
326 * a bad working directory */
327 if (psei
->lpDirectory
&& psei
->lpDirectory
[0])
329 DWORD attr
= GetFileAttributesW(psei
->lpDirectory
);
330 if (attr
!= INVALID_FILE_ATTRIBUTES
&& attr
& FILE_ATTRIBUTE_DIRECTORY
)
331 lpDirectory
= psei
->lpDirectory
;
334 /* ShellExecute specifies the command from psei->lpDirectory
335 * if present. Not from the current dir as CreateProcess does */
337 if( ( gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
338 if( !SetCurrentDirectoryW( lpDirectory
))
339 ERR("cannot set directory %s\n", debugstr_w(lpDirectory
));
340 ZeroMemory(&startup
,sizeof(STARTUPINFOW
));
341 startup
.cb
= sizeof(STARTUPINFOW
);
342 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
343 startup
.wShowWindow
= psei
->nShow
;
344 dwCreationFlags
= CREATE_UNICODE_ENVIRONMENT
;
345 if (!(psei
->fMask
& SEE_MASK_NO_CONSOLE
))
346 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
347 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, dwCreationFlags
, env
,
348 lpDirectory
, &startup
, &info
))
350 /* Give 30 seconds to the app to come up, if desired. Probably only needed
351 when starting app immediately before making a DDE connection. */
353 if (WaitForInputIdle( info
.hProcess
, 30000 ) == WAIT_FAILED
)
354 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
356 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
357 psei_out
->hProcess
= info
.hProcess
;
359 CloseHandle( info
.hProcess
);
360 CloseHandle( info
.hThread
);
362 else if ((retval
= GetLastError()) >= 32)
364 TRACE("CreateProcess returned error %ld\n", retval
);
365 retval
= ERROR_BAD_FORMAT
;
368 TRACE("returning %lu\n", retval
);
370 psei_out
->hInstApp
= (HINSTANCE
)retval
;
372 if( !SetCurrentDirectoryW( curdir
))
373 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
379 /***********************************************************************
380 * SHELL_BuildEnvW [Internal]
382 * Build the environment for the new process, adding the specified
383 * path to the PATH variable. Returned pointer must be freed by caller.
385 static void *SHELL_BuildEnvW( const WCHAR
*path
)
387 static const WCHAR wPath
[] = {'P','A','T','H','=',0};
388 WCHAR
*strings
, *new_env
;
390 int total
= strlenW(path
) + 1;
391 BOOL got_path
= FALSE
;
393 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
397 int len
= strlenW(p
) + 1;
398 if (!strncmpiW( p
, wPath
, 5 )) got_path
= TRUE
;
402 if (!got_path
) total
+= 5; /* we need to create PATH */
403 total
++; /* terminating null */
405 if (!(new_env
= heap_alloc( total
* sizeof(WCHAR
) )))
407 FreeEnvironmentStringsW( strings
);
414 int len
= strlenW(p
) + 1;
415 memcpy( p2
, p
, len
* sizeof(WCHAR
) );
416 if (!strncmpiW( p
, wPath
, 5 ))
419 strcpyW( p2
+ len
, path
);
420 p2
+= strlenW(path
) + 1;
427 strcpyW( p2
, wPath
);
429 p2
+= strlenW(p2
) + 1;
432 FreeEnvironmentStringsW( strings
);
437 /***********************************************************************
438 * SHELL_TryAppPathW [Internal]
440 * Helper function for SHELL_FindExecutable
441 * @param lpResult - pointer to a buffer of size MAX_PATH
442 * On entry: szName is a filename (probably without path separators).
443 * On exit: if szName found in "App Path", place full path in lpResult, and return true
445 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
447 static const WCHAR wszKeyAppPaths
[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',
448 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
449 static const WCHAR wPath
[] = {'P','a','t','h',0};
456 if (env
) *env
= NULL
;
457 strcpyW(buffer
, wszKeyAppPaths
);
458 strcatW(buffer
, szName
);
459 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
462 len
= MAX_PATH
*sizeof(WCHAR
);
463 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
469 DWORD count
= sizeof(buffer
);
470 if (!RegQueryValueExW(hkApp
, wPath
, NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
471 *env
= SHELL_BuildEnvW( buffer
);
475 if (hkApp
) RegCloseKey(hkApp
);
479 /*************************************************************************
480 * SHELL_FindExecutableByVerb [Internal]
482 * called from SHELL_FindExecutable or SHELL_execute_class
484 * classname a buffer, big enough, to get the key name to do actually the
485 * command "WordPad.Document.1\\shell\\open\\command"
486 * passed as "WordPad.Document.1"
488 * lpVerb the operation on it (open)
489 * commandlen the size of command buffer (in bytes)
491 * command a buffer, to store the command to do the
492 * operation on the file
493 * key a buffer, big enough, to get the key name to do actually the
494 * command "WordPad.Document.1\\shell\\open\\command"
497 static UINT
SHELL_FindExecutableByVerb(LPCWSTR lpVerb
, LPWSTR key
, LPWSTR classname
, LPWSTR command
, LONG commandlen
)
499 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
501 WCHAR verb
[MAX_PATH
];
503 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, classname
, 0, 0x02000000, &hkeyClass
))
504 return SE_ERR_NOASSOC
;
505 if (!HCR_GetDefaultVerbW(hkeyClass
, lpVerb
, verb
, ARRAY_SIZE(verb
)))
506 return SE_ERR_NOASSOC
;
507 RegCloseKey(hkeyClass
);
509 /* Looking for ...buffer\shell\<verb>\command */
510 strcatW(classname
, wszShell
);
511 strcatW(classname
, verb
);
512 strcatW(classname
, wCommand
);
514 if (RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, command
,
515 &commandlen
) == ERROR_SUCCESS
)
517 commandlen
/= sizeof(WCHAR
);
518 if (key
) strcpyW(key
, classname
);
522 LONG paramlen
= sizeof(param
);
523 static const WCHAR wSpace
[] = {' ',0};
525 /* FIXME: it seems all Windows version don't behave the same here.
526 * the doc states that this ddeexec information can be found after
528 * on Win98, it doesn't appear, but I think it does on Win2k
530 /* Get the parameters needed by the application
531 from the associated ddeexec key */
532 tmp
= strstrW(classname
, wCommand
);
534 strcatW(classname
, wDdeexec
);
535 if (RegQueryValueW(HKEY_CLASSES_ROOT
, classname
, param
,
536 ¶mlen
) == ERROR_SUCCESS
)
538 paramlen
/= sizeof(WCHAR
);
539 strcatW(command
, wSpace
);
540 strcatW(command
, param
);
541 commandlen
+= paramlen
;
545 command
[commandlen
] = '\0';
547 return 33; /* FIXME see SHELL_FindExecutable() */
550 return SE_ERR_NOASSOC
;
553 /*************************************************************************
554 * SHELL_FindExecutable [Internal]
556 * Utility for code sharing between FindExecutable and ShellExecute
558 * lpFile the name of a file
559 * lpVerb the operation on it (open)
561 * lpResult a buffer, big enough :-(, to store the command to do the
562 * operation on the file
563 * key a buffer, big enough, to get the key name to do actually the
564 * command (it'll be used afterwards for more information
567 static UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpVerb
,
568 LPWSTR lpResult
, int resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
570 static const WCHAR wWindows
[] = {'w','i','n','d','o','w','s',0};
571 static const WCHAR wPrograms
[] = {'p','r','o','g','r','a','m','s',0};
572 static const WCHAR wExtensions
[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
573 WCHAR
*extension
= NULL
; /* pointer to file extension */
574 WCHAR classname
[256]; /* registry name for this file type */
575 LONG classnamelen
= sizeof(classname
); /* length of above */
576 WCHAR command
[1024]; /* command from registry */
577 WCHAR wBuffer
[256]; /* Used to GetProfileString */
578 UINT retval
= SE_ERR_NOASSOC
;
579 WCHAR
*tok
; /* token pointer */
580 WCHAR xlpFile
[256]; /* result of SearchPath */
581 DWORD attribs
; /* file attributes */
583 TRACE("%s\n", debugstr_w(lpFile
));
586 return ERROR_INVALID_PARAMETER
;
589 lpResult
[0] = '\0'; /* Start off with an empty return string */
590 if (key
) *key
= '\0';
592 /* trap NULL parameters on entry */
595 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
596 debugstr_w(lpFile
), debugstr_w(lpResult
));
597 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
600 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
602 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
606 if (SearchPathW(lpPath
, lpFile
, wszExe
, ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
608 TRACE("SearchPathW returned non-zero\n");
610 /* The file was found in the application-supplied default directory (or the system search path) */
612 else if (lpPath
&& SearchPathW(NULL
, lpFile
, wszExe
, ARRAY_SIZE(xlpFile
), xlpFile
, NULL
))
614 TRACE("SearchPathW returned non-zero\n");
616 /* The file was found in one of the directories in the system-wide search path */
619 attribs
= GetFileAttributesW(lpFile
);
620 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
622 strcpyW(classname
, wszFolder
);
626 /* Did we get something? Anything? */
629 TRACE("Returning SE_ERR_FNF\n");
632 /* First thing we need is the file's extension */
633 extension
= strrchrW(xlpFile
, '.'); /* Assume last "." is the one; */
634 /* File->Run in progman uses */
636 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
638 if (extension
== NULL
|| extension
[1]==0)
640 WARN("Returning SE_ERR_NOASSOC\n");
641 return SE_ERR_NOASSOC
;
644 /* Three places to check: */
645 /* 1. win.ini, [windows], programs (NB no leading '.') */
646 /* 2. Registry, HKEY_CLASS_ROOT\<classname>\shell\open\command */
647 /* 3. win.ini, [extensions], extension (NB no leading '.' */
648 /* All I know of the order is that registry is checked before */
649 /* extensions; however, it'd make sense to check the programs */
650 /* section first, so that's what happens here. */
652 /* See if it's a program - if GetProfileString fails, we skip this
653 * section. Actually, if GetProfileString fails, we've probably
654 * got a lot more to worry about than running a program... */
655 if (GetProfileStringW(wWindows
, wPrograms
, wExtensions
, wBuffer
, ARRAY_SIZE(wBuffer
)) > 0)
662 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
666 while (*p
== ' ' || *p
== '\t') p
++;
669 if (strcmpiW(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
671 strcpyW(lpResult
, xlpFile
);
672 /* Need to perhaps check that the file has a path
674 TRACE("found %s\n", debugstr_w(lpResult
));
676 /* Greater than 32 to indicate success */
683 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, classname
,
684 &classnamelen
) == ERROR_SUCCESS
)
686 classnamelen
/= sizeof(WCHAR
);
687 if (classnamelen
== ARRAY_SIZE(classname
))
689 classname
[classnamelen
] = '\0';
690 TRACE("File type: %s\n", debugstr_w(classname
));
700 /* pass the verb string to SHELL_FindExecutableByVerb() */
701 retval
= SHELL_FindExecutableByVerb(lpVerb
, key
, classname
, command
, sizeof(command
));
706 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
707 if (finishedLen
> resultLen
)
708 ERR("Argify buffer not large enough.. truncated\n");
710 /* Remove double quotation marks and command line arguments */
711 if (*lpResult
== '"')
714 while (*(p
+ 1) != '"')
723 /* Truncate on first space */
725 while (*p
!= ' ' && *p
!= '\0')
731 else /* Check win.ini */
733 static const WCHAR wExtensions
[] = {'e','x','t','e','n','s','i','o','n','s',0};
735 /* Toss the leading dot */
737 if (GetProfileStringW(wExtensions
, extension
, wszEmpty
, command
, ARRAY_SIZE(command
)) > 0)
741 strcpyW(lpResult
, command
);
742 tok
= strchrW(lpResult
, '^'); /* should be ^.extension? */
746 strcatW(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
747 tok
= strchrW(command
, '^'); /* see above */
748 if ((tok
!= NULL
) && (strlenW(tok
)>5))
750 strcatW(lpResult
, &tok
[5]);
753 retval
= 33; /* FIXME - see above */
758 TRACE("returning %s\n", debugstr_w(lpResult
));
762 /******************************************************************
765 * callback for the DDE connection. not really useful
767 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
768 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
769 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
771 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
772 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
776 /******************************************************************
779 * ShellExecute helper. Used to do an operation with a DDE connection
781 * Handles both the direct connection (try #1), and if it fails,
782 * launching an application and trying (#2) to connect to it
785 static unsigned dde_connect(const WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
786 const WCHAR
* lpFile
, WCHAR
*env
,
787 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
788 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
790 static const WCHAR wApplication
[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
791 static const WCHAR wTopic
[] = {'\\','t','o','p','i','c',0};
793 WCHAR
* endkey
= regkey
+ strlenW(key
);
794 WCHAR app
[256], topic
[256], ifexec
[256], static_res
[256];
795 WCHAR
* dynamic_res
=NULL
;
797 LONG applen
, topiclen
, ifexeclen
;
801 DWORD resultLen
, endkeyLen
;
802 HSZ hszApp
, hszTopic
;
805 unsigned ret
= SE_ERR_NOASSOC
;
806 BOOL unicode
= !(GetVersion() & 0x80000000);
808 if (strlenW(key
) + 1 > ARRAY_SIZE(regkey
))
810 FIXME("input parameter %s larger than buffer\n", debugstr_w(key
));
813 strcpyW(regkey
, key
);
814 endkeyLen
= ARRAY_SIZE(regkey
) - (endkey
- regkey
);
815 if (strlenW(wApplication
) + 1 > endkeyLen
)
817 FIXME("endkey %s overruns buffer\n", debugstr_w(wApplication
));
820 strcpyW(endkey
, wApplication
);
821 applen
= sizeof(app
);
822 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, app
, &applen
) != ERROR_SUCCESS
)
824 WCHAR command
[1024], fullpath
[MAX_PATH
];
825 static const WCHAR wSo
[] = { '.','s','o',0 };
826 int sizeSo
= ARRAY_SIZE(wSo
);
830 /* Get application command from start string and find filename of application */
833 if (strlenW(start
+ 1) + 1 > ARRAY_SIZE(command
))
835 FIXME("size of input parameter %s larger than buffer\n",
836 debugstr_w(start
+ 1));
839 strcpyW(command
, start
+1);
840 if ((ptr
= strchrW(command
, '"')))
842 ret
= SearchPathW(NULL
, command
, wszExe
, ARRAY_SIZE(fullpath
), fullpath
, &ptr
);
848 for (p
=start
; (space
=strchrW(p
, ' ')); p
=space
+1)
850 int idx
= space
-start
;
851 memcpy(command
, start
, idx
*sizeof(WCHAR
));
853 if ((ret
= SearchPathW(NULL
, command
, wszExe
, ARRAY_SIZE(fullpath
), fullpath
, &ptr
)))
857 ret
= SearchPathW(NULL
, start
, wszExe
, ARRAY_SIZE(fullpath
), fullpath
, &ptr
);
862 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
863 return ERROR_ACCESS_DENIED
;
865 if (strlenW(ptr
) + 1 > ARRAY_SIZE(app
))
867 FIXME("size of found path %s larger than buffer\n", debugstr_w(ptr
));
872 /* Remove extensions (including .so) */
873 ptr
= app
+ strlenW(app
) - (sizeSo
-1);
874 if (strlenW(app
) >= sizeSo
&&
878 ptr
= strrchrW(app
, '.');
883 if (strlenW(wTopic
) + 1 > endkeyLen
)
885 FIXME("endkey %s overruns buffer\n", debugstr_w(wTopic
));
888 strcpyW(endkey
, wTopic
);
889 topiclen
= sizeof(topic
);
890 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, topic
, &topiclen
) != ERROR_SUCCESS
)
892 static const WCHAR wSystem
[] = {'S','y','s','t','e','m',0};
893 strcpyW(topic
, wSystem
);
898 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
903 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
907 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
908 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
910 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
914 static const WCHAR wIfexec
[] = {'\\','i','f','e','x','e','c',0};
915 TRACE("Launching %s\n", debugstr_w(start
));
916 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
919 TRACE("Couldn't launch\n");
922 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
925 TRACE("Couldn't connect. ret=%d\n", ret
);
926 DdeUninitialize(ddeInst
);
927 SetLastError(ERROR_DDE_FAIL
);
928 return 30; /* whatever */
930 if (strlenW(wIfexec
) + 1 > endkeyLen
)
932 FIXME("endkey %s overruns buffer\n", debugstr_w(wIfexec
));
935 strcpyW(endkey
, wIfexec
);
936 ifexeclen
= sizeof(ifexec
);
937 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
943 SHELL_ArgifyW(static_res
, ARRAY_SIZE(static_res
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
944 if (resultLen
> ARRAY_SIZE(static_res
))
946 res
= dynamic_res
= heap_alloc(resultLen
* sizeof(WCHAR
));
947 SHELL_ArgifyW(dynamic_res
, resultLen
, exec
, lpFile
, pidl
, szCommandline
, NULL
);
951 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
953 /* It's documented in the KB 330337 that IE has a bug and returns
954 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
957 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0,
958 XTYP_EXECUTE
, 30000, &tid
);
961 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
962 char *resA
= heap_alloc(lenA
);
963 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
964 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
965 XTYP_EXECUTE
, 10000, &tid
);
969 DdeFreeDataHandle(hDdeData
);
971 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
974 heap_free(dynamic_res
);
976 DdeDisconnect(hConv
);
979 DdeUninitialize(ddeInst
);
984 /*************************************************************************
985 * execute_from_key [Internal]
987 static UINT_PTR
execute_from_key(LPCWSTR key
, LPCWSTR lpFile
, WCHAR
*env
, LPCWSTR szCommandline
,
988 LPCWSTR executable_name
,
989 SHELL_ExecuteW32 execfunc
,
990 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
992 static const WCHAR wCommand
[] = {'c','o','m','m','a','n','d',0};
993 static const WCHAR wDdeexec
[] = {'d','d','e','e','x','e','c',0};
994 WCHAR cmd
[256], param
[1024], ddeexec
[256];
995 LONG cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
996 UINT_PTR retval
= SE_ERR_NOASSOC
;
1000 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
1001 debugstr_w(szCommandline
), debugstr_w(executable_name
));
1006 /* Get the application from the registry */
1007 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
1009 TRACE("got cmd: %s\n", debugstr_w(cmd
));
1011 /* Is there a replace() function anywhere? */
1012 cmdlen
/= sizeof(WCHAR
);
1013 if (cmdlen
>= ARRAY_SIZE(cmd
))
1014 cmdlen
= ARRAY_SIZE(cmd
) - 1;
1016 SHELL_ArgifyW(param
, ARRAY_SIZE(param
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
, &resultLen
);
1017 if (resultLen
> ARRAY_SIZE(param
))
1018 ERR("Argify buffer not large enough, truncating\n");
1021 /* Get the parameters needed by the application
1022 from the associated ddeexec key */
1023 tmp
= strstrW(key
, wCommand
);
1025 strcpyW(tmp
, wDdeexec
);
1027 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, &ddeexeclen
) == ERROR_SUCCESS
)
1029 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
1030 if (!param
[0]) strcpyW(param
, executable_name
);
1031 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, psei
->lpIDList
, execfunc
, psei
, psei_out
);
1035 TRACE("executing: %s\n", debugstr_w(param
));
1036 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
1039 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
1044 /*************************************************************************
1045 * FindExecutableA [SHELL32.@]
1047 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
1050 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
1051 WCHAR wResult
[MAX_PATH
];
1053 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
1054 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
1056 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
1057 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
1059 SHFree( wDirectory
);
1061 TRACE("returning %s\n", lpResult
);
1065 /*************************************************************************
1066 * FindExecutableW [SHELL32.@]
1068 * This function returns the executable associated with the specified file
1069 * for the default verb.
1072 * lpFile [I] The file to find the association for. This must refer to
1073 * an existing file otherwise FindExecutable fails and returns
1075 * lpResult [O] Points to a buffer into which the executable path is
1076 * copied. This parameter must not be NULL otherwise
1077 * FindExecutable() segfaults. The buffer must be of size at
1078 * least MAX_PATH characters.
1081 * A value greater than 32 on success, less than or equal to 32 otherwise.
1082 * See the SE_ERR_* constants.
1085 * On Windows XP and 2003, FindExecutable() seems to first convert the
1086 * filename into 8.3 format, thus taking into account only the first three
1087 * characters of the extension, and expects to find an association for those.
1088 * However other Windows versions behave sanely.
1090 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
1092 UINT_PTR retval
= SE_ERR_NOASSOC
;
1093 WCHAR old_dir
[1024];
1094 WCHAR res
[MAX_PATH
];
1096 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
1098 lpResult
[0] = '\0'; /* Start off with an empty return string */
1100 return (HINSTANCE
)SE_ERR_FNF
;
1104 GetCurrentDirectoryW(ARRAY_SIZE(old_dir
), old_dir
);
1105 SetCurrentDirectoryW(lpDirectory
);
1108 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, res
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1111 strcpyW(lpResult
, res
);
1113 TRACE("returning %s\n", debugstr_w(lpResult
));
1115 SetCurrentDirectoryW(old_dir
);
1116 return (HINSTANCE
)retval
;
1119 /* FIXME: is this already implemented somewhere else? */
1120 static HKEY
ShellExecute_GetClassKey( const SHELLEXECUTEINFOW
*sei
)
1122 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1124 DWORD type
= 0, sz
= 0;
1128 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1129 return sei
->hkeyClass
;
1131 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1132 lpClass
= sei
->lpClass
;
1135 ext
= PathFindExtensionW( sei
->lpFile
);
1136 TRACE("ext = %s\n", debugstr_w( ext
) );
1140 r
= RegOpenKeyW( HKEY_CLASSES_ROOT
, ext
, &hkey
);
1141 if (r
!= ERROR_SUCCESS
)
1144 r
= RegQueryValueExW( hkey
, NULL
, 0, &type
, NULL
, &sz
);
1145 if ( r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1147 sz
+= sizeof (WCHAR
);
1148 cls
= heap_alloc( sz
);
1150 RegQueryValueExW( hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1153 RegCloseKey( hkey
);
1157 TRACE("class = %s\n", debugstr_w(lpClass
) );
1161 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1168 static HRESULT
shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
, IDataObject
**dataobj
)
1170 LPCITEMIDLIST pidllast
= NULL
;
1171 IShellFolder
*shf
= NULL
;
1172 LPITEMIDLIST pidl
= NULL
;
1173 HRESULT r
= SE_ERR_DLLNOTFOUND
;
1175 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1176 pidl
= sei
->lpIDList
;
1179 WCHAR fullpath
[MAX_PATH
];
1183 ret
= GetFullPathNameW( sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1187 pidl
= ILCreateFromPathW( fullpath
);
1190 r
= SHBindToParent( pidl
, &IID_IShellFolder
, (LPVOID
*)&shf
, &pidllast
);
1194 r
= IShellFolder_GetUIObjectOf( shf
, NULL
, 1, &pidllast
,
1195 &IID_IDataObject
, NULL
, (void**)dataobj
);
1198 if ( pidl
!= sei
->lpIDList
)
1201 IShellFolder_Release( shf
);
1205 static HRESULT
shellex_run_context_menu_default( IShellExtInit
*obj
,
1206 LPSHELLEXECUTEINFOW sei
)
1208 IContextMenu
*cm
= NULL
;
1209 CMINVOKECOMMANDINFOEX ici
;
1216 TRACE("%p %p\n", obj
, sei
);
1218 r
= IShellExtInit_QueryInterface( obj
, &IID_IContextMenu
, (LPVOID
*) &cm
);
1222 hmenu
= CreateMenu();
1226 /* the number of the last menu added is returned in r */
1227 r
= IContextMenu_QueryContextMenu( cm
, hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1231 n
= GetMenuItemCount( hmenu
);
1232 for ( i
= 0; i
< n
; i
++ )
1234 memset( &info
, 0, sizeof info
);
1235 info
.cbSize
= sizeof info
;
1236 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1237 info
.dwTypeData
= string
;
1238 info
.cch
= ARRAY_SIZE(string
);
1240 GetMenuItemInfoW( hmenu
, i
, TRUE
, &info
);
1242 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1243 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1244 if ( ( !sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
) ) ||
1245 ( sei
->lpVerb
&& !lstrcmpiW( sei
->lpVerb
, string
) ) )
1256 memset( &ici
, 0, sizeof ici
);
1257 ici
.cbSize
= sizeof ici
;
1258 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NO_CONSOLE
|SEE_MASK_NOASYNC
|SEE_MASK_ASYNCOK
|SEE_MASK_FLAG_NO_UI
));
1259 ici
.nShow
= sei
->nShow
;
1260 ici
.lpVerb
= MAKEINTRESOURCEA( def
);
1261 ici
.hwnd
= sei
->hwnd
;
1262 ici
.lpParametersW
= sei
->lpParameters
;
1264 r
= IContextMenu_InvokeCommand( cm
, (LPCMINVOKECOMMANDINFO
) &ici
);
1266 TRACE("invoke command returned %08x\n", r
);
1270 DestroyMenu( hmenu
);
1272 IContextMenu_Release( cm
);
1276 static HRESULT
shellex_load_object_and_run( HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1278 IDataObject
*dataobj
= NULL
;
1279 IObjectWithSite
*ows
= NULL
;
1280 IShellExtInit
*obj
= NULL
;
1283 TRACE("%p %s %p\n", hkey
, debugstr_guid( guid
), sei
);
1285 r
= CoInitialize( NULL
);
1289 r
= CoCreateInstance( guid
, NULL
, CLSCTX_INPROC_SERVER
,
1290 &IID_IShellExtInit
, (LPVOID
*)&obj
);
1293 ERR("failed %08x\n", r
);
1297 r
= shellex_get_dataobj( sei
, &dataobj
);
1300 ERR("failed to get data object\n");
1304 r
= IShellExtInit_Initialize( obj
, NULL
, dataobj
, hkey
);
1308 r
= IShellExtInit_QueryInterface( obj
, &IID_IObjectWithSite
, (LPVOID
*) &ows
);
1312 IObjectWithSite_SetSite( ows
, NULL
);
1314 r
= shellex_run_context_menu_default( obj
, sei
);
1318 IObjectWithSite_Release( ows
);
1320 IDataObject_Release( dataobj
);
1322 IShellExtInit_Release( obj
);
1328 /*************************************************************************
1329 * ShellExecute_FromContextMenu [Internal]
1331 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1333 static const WCHAR szcm
[] = { 's','h','e','l','l','e','x','\\',
1334 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1335 HKEY hkey
, hkeycm
= 0;
1342 TRACE("%s\n", debugstr_w(sei
->lpFile
) );
1344 hkey
= ShellExecute_GetClassKey( sei
);
1346 return ERROR_FUNCTION_FAILED
;
1348 r
= RegOpenKeyW( hkey
, szcm
, &hkeycm
);
1349 if ( r
== ERROR_SUCCESS
)
1354 r
= RegEnumKeyW( hkeycm
, i
++, szguid
, ARRAY_SIZE(szguid
));
1355 if ( r
!= ERROR_SUCCESS
)
1358 hr
= CLSIDFromString( szguid
, &guid
);
1361 /* stop at the first one that succeeds in running */
1362 hr
= shellex_load_object_and_run( hkey
, &guid
, sei
);
1363 if ( SUCCEEDED( hr
) )
1367 RegCloseKey( hkeycm
);
1370 if ( hkey
!= sei
->hkeyClass
)
1371 RegCloseKey( hkey
);
1375 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
);
1377 static UINT_PTR
SHELL_execute_class( LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1379 static const WCHAR wQuote
[] = {'"',0};
1380 static const WCHAR wSpace
[] = {' ',0};
1381 WCHAR execCmd
[1024], classname
[1024];
1382 /* launch a document by fileclass like 'WordPad.Document.1' */
1383 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1384 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1385 ULONG cmask
=(psei
->fMask
& SEE_MASK_CLASSALL
);
1390 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1391 if (cmask
!= SEE_MASK_CLASSNAME
)
1394 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1395 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1397 execCmd
, sizeof(execCmd
));
1399 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1400 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1403 done
= SHELL_ArgifyW(wcmd
, ARRAY_SIZE(wcmd
), execCmd
, wszApplicationName
, psei
->lpIDList
, NULL
, &resultLen
);
1404 if (!done
&& wszApplicationName
[0])
1406 strcatW(wcmd
, wSpace
);
1407 if (*wszApplicationName
!= '"')
1409 strcatW(wcmd
, wQuote
);
1410 strcatW(wcmd
, wszApplicationName
);
1411 strcatW(wcmd
, wQuote
);
1414 strcatW(wcmd
, wszApplicationName
);
1416 if (resultLen
> ARRAY_SIZE(wcmd
))
1417 ERR("Argify buffer not large enough... truncating\n");
1418 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1421 strcpyW(classname
, psei
->lpClass
);
1422 rslt
= SHELL_FindExecutableByVerb(psei
->lpVerb
, NULL
, classname
, execCmd
, sizeof(execCmd
));
1424 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt
, debugstr_w(classname
), debugstr_w(execCmd
));
1427 rslt
= SHELL_quote_and_execute( execCmd
, wszEmpty
, classname
,
1428 wszApplicationName
, NULL
, psei
,
1429 psei_out
, execfunc
);
1433 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1435 static const WCHAR wExplorer
[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1436 WCHAR buffer
[MAX_PATH
];
1438 /* last chance to translate IDList: now also allow CLSID paths */
1439 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei
->lpIDList
, buffer
, ARRAY_SIZE(buffer
)))) {
1440 if (buffer
[0]==':' && buffer
[1]==':') {
1441 /* open shell folder for the specified class GUID */
1442 if (strlenW(buffer
) + 1 > parametersLen
)
1443 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1444 lstrlenW(buffer
) + 1, parametersLen
);
1445 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1446 if (strlenW(wExplorer
) > dwApplicationNameLen
)
1447 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1448 lstrlenW(wExplorer
) + 1, dwApplicationNameLen
);
1449 lstrcpynW(wszApplicationName
, wExplorer
, dwApplicationNameLen
);
1451 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1453 WCHAR target
[MAX_PATH
];
1456 /* Check if we're executing a directory and if so use the
1457 handler for the Folder class */
1458 strcpyW(target
, buffer
);
1459 attribs
= GetFileAttributesW(buffer
);
1460 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1461 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1462 HCR_GetExecuteCommandW(0, wszFolder
,
1464 buffer
, sizeof(buffer
))) {
1465 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1466 buffer
, target
, sei
->lpIDList
, NULL
, &resultLen
);
1467 if (resultLen
> dwApplicationNameLen
)
1468 ERR("Argify buffer not large enough... truncating\n");
1470 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1475 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
)
1477 static const WCHAR wQuote
[] = {'"',0};
1478 static const WCHAR wSpace
[] = {' ',0};
1481 WCHAR
*wszQuotedCmd
;
1483 /* Length of quotes plus length of command plus NULL terminator */
1484 len
= 2 + lstrlenW(wcmd
) + 1;
1485 if (wszParameters
[0])
1487 /* Length of space plus length of parameters */
1488 len
+= 1 + lstrlenW(wszParameters
);
1490 wszQuotedCmd
= heap_alloc(len
* sizeof(WCHAR
));
1491 /* Must quote to handle case where cmd contains spaces,
1492 * else security hole if malicious user creates executable file "C:\\Program"
1494 strcpyW(wszQuotedCmd
, wQuote
);
1495 strcatW(wszQuotedCmd
, wcmd
);
1496 strcatW(wszQuotedCmd
, wQuote
);
1497 if (wszParameters
[0]) {
1498 strcatW(wszQuotedCmd
, wSpace
);
1499 strcatW(wszQuotedCmd
, wszParameters
);
1501 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(wszKeyname
));
1503 retval
= execute_from_key(wszKeyname
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1505 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1506 heap_free(wszQuotedCmd
);
1510 static UINT_PTR
SHELL_execute_url( LPCWSTR lpFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1512 static const WCHAR wShell
[] = {'\\','s','h','e','l','l','\\',0};
1513 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
1515 WCHAR
*lpstrProtocol
;
1520 lpstrRes
= strchrW(lpFile
, ':');
1522 iSize
= lpstrRes
- lpFile
;
1524 iSize
= strlenW(lpFile
);
1526 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1527 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1528 len
= iSize
+ lstrlenW(wShell
) + lstrlenW(wCommand
) + 1;
1529 if (psei
->lpVerb
&& *psei
->lpVerb
)
1530 len
+= lstrlenW(psei
->lpVerb
);
1532 len
+= lstrlenW(wszOpen
);
1533 lpstrProtocol
= heap_alloc(len
* sizeof(WCHAR
));
1534 memcpy(lpstrProtocol
, lpFile
, iSize
*sizeof(WCHAR
));
1535 lpstrProtocol
[iSize
] = '\0';
1536 strcatW(lpstrProtocol
, wShell
);
1537 strcatW(lpstrProtocol
, psei
->lpVerb
&& *psei
->lpVerb
? psei
->lpVerb
: wszOpen
);
1538 strcatW(lpstrProtocol
, wCommand
);
1540 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1541 wcmd
, execfunc
, psei
, psei_out
);
1542 heap_free(lpstrProtocol
);
1546 static void do_error_dialog( UINT_PTR retval
, HWND hwnd
)
1549 int error_code
=GetLastError();
1551 if (retval
== SE_ERR_NOASSOC
)
1552 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, ARRAY_SIZE(msg
));
1554 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error_code
, 0, msg
, ARRAY_SIZE(msg
), NULL
);
1556 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1559 static WCHAR
*expand_environment( const WCHAR
*str
)
1564 len
= ExpandEnvironmentStringsW(str
, NULL
, 0);
1565 if (!len
) return NULL
;
1567 buf
= heap_alloc(len
* sizeof(WCHAR
));
1568 if (!buf
) return NULL
;
1570 len
= ExpandEnvironmentStringsW(str
, buf
, len
);
1579 /*************************************************************************
1580 * SHELL_execute [Internal]
1582 static BOOL
SHELL_execute( LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1584 static const WCHAR wWww
[] = {'w','w','w',0};
1585 static const WCHAR wHttp
[] = {'h','t','t','p',':','/','/',0};
1586 static const DWORD unsupportedFlags
=
1587 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1588 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1589 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1591 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1592 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
= NULL
;
1593 DWORD dwApplicationNameLen
= MAX_PATH
+2;
1594 DWORD parametersLen
= ARRAY_SIZE(parametersBuffer
);
1595 DWORD wcmdLen
= ARRAY_SIZE(wcmdBuffer
);
1597 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1599 WCHAR wszKeyname
[256];
1601 UINT_PTR retval
= SE_ERR_NOASSOC
;
1603 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1606 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1607 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1608 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1609 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1610 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1611 debugstr_w(sei_tmp
.lpClass
) : "not used");
1613 sei
->hProcess
= NULL
;
1615 /* make copies of all path/command strings */
1616 if (!sei_tmp
.lpFile
)
1618 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1619 *wszApplicationName
= '\0';
1621 else if (*sei_tmp
.lpFile
== '\"' && sei_tmp
.lpFile
[(len
= strlenW(sei_tmp
.lpFile
))-1] == '\"')
1623 if(len
-1 >= dwApplicationNameLen
) dwApplicationNameLen
= len
;
1624 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1625 memcpy(wszApplicationName
, sei_tmp
.lpFile
+1, len
*sizeof(WCHAR
));
1627 wszApplicationName
[len
-2] = '\0';
1628 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName
));
1630 DWORD l
= strlenW(sei_tmp
.lpFile
)+1;
1631 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1632 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1633 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
*sizeof(WCHAR
));
1636 wszParameters
= parametersBuffer
;
1637 if (sei_tmp
.lpParameters
)
1639 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1640 if (len
> parametersLen
)
1642 wszParameters
= heap_alloc(len
* sizeof(WCHAR
));
1643 parametersLen
= len
;
1645 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1648 *wszParameters
= '\0';
1651 if (sei_tmp
.lpDirectory
)
1653 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1654 if (len
> ARRAY_SIZE(dirBuffer
))
1655 wszDir
= heap_alloc(len
* sizeof(WCHAR
));
1656 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1661 /* adjust string pointers to point to the new buffers */
1662 sei_tmp
.lpFile
= wszApplicationName
;
1663 sei_tmp
.lpParameters
= wszParameters
;
1664 sei_tmp
.lpDirectory
= wszDir
;
1666 if (sei_tmp
.fMask
& unsupportedFlags
)
1668 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1671 /* process the IDList */
1672 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1674 IShellExecuteHookW
* pSEH
;
1676 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1680 hr
= IShellExecuteHookW_Execute(pSEH
, &sei_tmp
);
1682 IShellExecuteHookW_Release(pSEH
);
1685 heap_free(wszApplicationName
);
1686 if (wszParameters
!= parametersBuffer
)
1687 heap_free(wszParameters
);
1688 if (wszDir
!= dirBuffer
)
1694 SHGetPathFromIDListW(sei_tmp
.lpIDList
, wszApplicationName
);
1695 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1698 if (sei_tmp
.fMask
& SEE_MASK_DOENVSUBST
)
1702 tmp
= expand_environment(sei_tmp
.lpFile
);
1705 retval
= SE_ERR_OOM
;
1708 heap_free(wszApplicationName
);
1709 sei_tmp
.lpFile
= wszApplicationName
= tmp
;
1711 tmp
= expand_environment(sei_tmp
.lpDirectory
);
1714 retval
= SE_ERR_OOM
;
1717 if (wszDir
!= dirBuffer
)
1719 sei_tmp
.lpDirectory
= wszDir
= tmp
;
1722 if ( ERROR_SUCCESS
== ShellExecute_FromContextMenu( &sei_tmp
) )
1724 sei
->hInstApp
= (HINSTANCE
) 33;
1725 heap_free(wszApplicationName
);
1726 if (wszParameters
!= parametersBuffer
)
1727 heap_free(wszParameters
);
1728 if (wszDir
!= dirBuffer
)
1733 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1735 retval
= SHELL_execute_class( wszApplicationName
, &sei_tmp
, sei
,
1737 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1738 do_error_dialog(retval
, sei_tmp
.hwnd
);
1739 heap_free(wszApplicationName
);
1740 if (wszParameters
!= parametersBuffer
)
1741 heap_free(wszParameters
);
1742 if (wszDir
!= dirBuffer
)
1747 /* Has the IDList not yet been translated? */
1748 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1750 SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1753 dwApplicationNameLen
);
1756 /* convert file URLs */
1757 if (UrlIsFileUrlW(sei_tmp
.lpFile
))
1763 buf
= heap_alloc(size
* sizeof(WCHAR
));
1764 if (!buf
|| FAILED(PathCreateFromUrlW(sei_tmp
.lpFile
, buf
, &size
, 0))) {
1769 heap_free(wszApplicationName
);
1770 wszApplicationName
= buf
;
1771 sei_tmp
.lpFile
= wszApplicationName
;
1773 else /* or expand environment strings (not both!) */
1775 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1779 buf
= heap_alloc((len
+ 1) * sizeof(WCHAR
));
1781 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1782 heap_free(wszApplicationName
);
1783 wszApplicationName
= buf
;
1785 sei_tmp
.lpFile
= wszApplicationName
;
1789 if (*sei_tmp
.lpDirectory
)
1791 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1796 buf
= heap_alloc(len
* sizeof(WCHAR
));
1797 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1798 if (wszDir
!= dirBuffer
)
1801 sei_tmp
.lpDirectory
= wszDir
;
1805 /* Else, try to execute the filename */
1806 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1807 lpFile
= sei_tmp
.lpFile
;
1809 strcpyW(wcmd
, wszApplicationName
);
1810 if (sei_tmp
.lpDirectory
)
1812 LPCWSTR searchPath
[] = {
1813 sei_tmp
.lpDirectory
,
1816 PathFindOnPathW(wcmd
, searchPath
);
1818 retval
= SHELL_quote_and_execute( wcmd
, wszParameters
, wszEmpty
,
1819 wszApplicationName
, NULL
, &sei_tmp
,
1822 heap_free(wszApplicationName
);
1823 if (wszParameters
!= parametersBuffer
)
1824 heap_free(wszParameters
);
1825 if (wszDir
!= dirBuffer
)
1827 if (wcmd
!= wcmdBuffer
)
1832 /* Else, try to find the executable */
1834 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, wszKeyname
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1835 if (retval
> 32) /* Found */
1837 retval
= SHELL_quote_and_execute( wcmd
, wszParameters
, wszKeyname
,
1838 wszApplicationName
, env
, &sei_tmp
,
1842 else if (PathIsDirectoryW(lpFile
))
1844 static const WCHAR wExplorer
[] = {'e','x','p','l','o','r','e','r',0};
1845 static const WCHAR wQuote
[] = {'"',0};
1846 WCHAR wExec
[MAX_PATH
];
1847 WCHAR
* lpQuotedFile
= heap_alloc( sizeof(WCHAR
) * (strlenW(lpFile
) + 3) );
1851 retval
= SHELL_FindExecutable( sei_tmp
.lpDirectory
, wExplorer
,
1852 wszOpen
, wExec
, MAX_PATH
,
1853 NULL
, &env
, NULL
, NULL
);
1856 strcpyW(lpQuotedFile
, wQuote
);
1857 strcatW(lpQuotedFile
, lpFile
);
1858 strcatW(lpQuotedFile
, wQuote
);
1859 retval
= SHELL_quote_and_execute( wExec
, lpQuotedFile
,
1861 wszApplicationName
, env
,
1862 &sei_tmp
, sei
, execfunc
);
1865 heap_free( lpQuotedFile
);
1868 retval
= 0; /* Out of memory */
1870 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1872 retval
= SHELL_execute_url( lpFile
, wcmd
, &sei_tmp
, sei
, execfunc
);
1874 /* Check if file specified is in the form www.??????.*** */
1875 else if (!strncmpiW(lpFile
, wWww
, 3))
1877 /* if so, prefix lpFile with http:// and call ShellExecute */
1878 WCHAR lpstrTmpFile
[256];
1879 strcpyW(lpstrTmpFile
, wHttp
);
1880 strcatW(lpstrTmpFile
, lpFile
);
1881 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1885 TRACE("retval %lu\n", retval
);
1887 heap_free(wszApplicationName
);
1888 if (wszParameters
!= parametersBuffer
)
1889 heap_free(wszParameters
);
1890 if (wszDir
!= dirBuffer
)
1892 if (wcmd
!= wcmdBuffer
)
1895 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1897 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1898 do_error_dialog(retval
, sei_tmp
.hwnd
);
1902 /*************************************************************************
1903 * ShellExecuteA [SHELL32.290]
1905 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpVerb
, LPCSTR lpFile
,
1906 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1908 SHELLEXECUTEINFOA sei
;
1910 TRACE("%p,%s,%s,%s,%s,%d\n",
1911 hWnd
, debugstr_a(lpVerb
), debugstr_a(lpFile
),
1912 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
1914 sei
.cbSize
= sizeof(sei
);
1915 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1917 sei
.lpVerb
= lpVerb
;
1918 sei
.lpFile
= lpFile
;
1919 sei
.lpParameters
= lpParameters
;
1920 sei
.lpDirectory
= lpDirectory
;
1921 sei
.nShow
= iShowCmd
;
1928 ShellExecuteExA (&sei
);
1929 return sei
.hInstApp
;
1932 /*************************************************************************
1933 * ShellExecuteExA [SHELL32.292]
1936 BOOL WINAPI DECLSPEC_HOTPATCH
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1938 SHELLEXECUTEINFOW seiW
;
1940 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1944 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1947 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1950 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1952 if (sei
->lpParameters
)
1953 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1955 if (sei
->lpDirectory
)
1956 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1958 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
1959 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1961 seiW
.lpClass
= NULL
;
1963 ret
= SHELL_execute( &seiW
, SHELL_ExecuteW
);
1965 sei
->hInstApp
= seiW
.hInstApp
;
1967 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
1968 sei
->hProcess
= seiW
.hProcess
;
1972 SHFree(wParameters
);
1979 /*************************************************************************
1980 * ShellExecuteExW [SHELL32.293]
1983 BOOL WINAPI DECLSPEC_HOTPATCH
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1985 return SHELL_execute( sei
, SHELL_ExecuteW
);
1988 /*************************************************************************
1989 * ShellExecuteW [SHELL32.294]
1991 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1992 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1994 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpVerb
, LPCWSTR lpFile
,
1995 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1997 SHELLEXECUTEINFOW sei
;
2000 sei
.cbSize
= sizeof(sei
);
2001 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
2003 sei
.lpVerb
= lpVerb
;
2004 sei
.lpFile
= lpFile
;
2005 sei
.lpParameters
= lpParameters
;
2006 sei
.lpDirectory
= lpDirectory
;
2007 sei
.nShow
= nShowCmd
;
2014 SHELL_execute( &sei
, SHELL_ExecuteW
);
2015 return sei
.hInstApp
;
2018 /*************************************************************************
2019 * WOWShellExecute [SHELL32.@]
2021 * FIXME: the callback function most likely doesn't work the same way on Windows.
2023 HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpVerb
,LPCSTR lpFile
,
2024 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
2026 SHELLEXECUTEINFOW seiW
;
2027 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
2028 HANDLE hProcess
= 0;
2030 seiW
.lpVerb
= lpVerb
? __SHCloneStrAtoW(&wVerb
, lpVerb
) : NULL
;
2031 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
2032 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
2033 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
2035 seiW
.cbSize
= sizeof(seiW
);
2038 seiW
.nShow
= iShowCmd
;
2043 seiW
.hProcess
= hProcess
;
2045 SHELL_execute( &seiW
, callback
);
2049 SHFree(wParameters
);
2051 return seiW
.hInstApp
;
2054 /*************************************************************************
2055 * ShellExec_RunDLLW [SHELL32.@]
2057 void WINAPI
ShellExec_RunDLLW(HWND hwnd
, HINSTANCE instance
, WCHAR
*cmdline
, int cmdshow
)
2059 BOOL in_single_quotes
= FALSE
, in_double_quotes
= FALSE
;
2062 TRACE("%p, %p, %s, %d\n", hwnd
, instance
, debugstr_w(cmdline
), cmdshow
);
2064 /* Replace the first whitespace character in the command line string with a
2065 null terminator to separate the program name from the program arguments */
2066 for (args
= cmdline
; *args
; args
++)
2071 args
++; /* skip the next character */
2074 if (!in_double_quotes
)
2075 in_single_quotes
= !in_single_quotes
;
2078 if (!in_single_quotes
)
2079 in_double_quotes
= !in_double_quotes
;
2083 if (!in_single_quotes
&& !in_double_quotes
)
2093 ShellExecuteW(hwnd
, NULL
, cmdline
, args
, NULL
, cmdshow
);
2096 /*************************************************************************
2097 * ShellExec_RunDLLA [SHELL32.@]
2099 void WINAPI
ShellExec_RunDLLA(HWND hwnd
, HINSTANCE instance
, CHAR
*cmdline
, int cmdshow
)
2102 ShellExec_RunDLLW(hwnd
, instance
, __SHCloneStrAtoW(&cmdlineW
, cmdline
), cmdshow
);
2106 /*************************************************************************
2107 * OpenAs_RunDLLA [SHELL32.@]
2109 void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2111 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2114 /*************************************************************************
2115 * OpenAs_RunDLLW [SHELL32.@]
2117 void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2119 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);
2122 /*************************************************************************
2123 * RegenerateUserEnvironment [SHELL32.@]
2125 BOOL WINAPI
RegenerateUserEnvironment(WCHAR
*wunknown
, BOOL bunknown
)
2127 FIXME("stub: %p, %d\n", wunknown
, bunknown
);