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)
739 if (strlenW(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
= sizeof(wSo
)/sizeof(WCHAR
);
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 IDataObject
*shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
)
1170 LPCITEMIDLIST pidllast
= NULL
;
1171 IDataObject
*dataobj
= NULL
;
1172 IShellFolder
*shf
= NULL
;
1173 LPITEMIDLIST pidl
= NULL
;
1176 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1177 pidl
= sei
->lpIDList
;
1180 WCHAR fullpath
[MAX_PATH
];
1184 ret
= GetFullPathNameW( sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1188 pidl
= ILCreateFromPathW( fullpath
);
1191 r
= SHBindToParent( pidl
, &IID_IShellFolder
, (LPVOID
*)&shf
, &pidllast
);
1195 IShellFolder_GetUIObjectOf( shf
, NULL
, 1, &pidllast
,
1196 &IID_IDataObject
, NULL
, (LPVOID
*) &dataobj
);
1199 if ( pidl
!= sei
->lpIDList
)
1202 IShellFolder_Release( shf
);
1206 static HRESULT
shellex_run_context_menu_default( IShellExtInit
*obj
,
1207 LPSHELLEXECUTEINFOW sei
)
1209 IContextMenu
*cm
= NULL
;
1210 CMINVOKECOMMANDINFOEX ici
;
1217 TRACE("%p %p\n", obj
, sei
);
1219 r
= IShellExtInit_QueryInterface( obj
, &IID_IContextMenu
, (LPVOID
*) &cm
);
1223 hmenu
= CreateMenu();
1227 /* the number of the last menu added is returned in r */
1228 r
= IContextMenu_QueryContextMenu( cm
, hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1232 n
= GetMenuItemCount( hmenu
);
1233 for ( i
= 0; i
< n
; i
++ )
1235 memset( &info
, 0, sizeof info
);
1236 info
.cbSize
= sizeof info
;
1237 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1238 info
.dwTypeData
= string
;
1239 info
.cch
= sizeof string
;
1241 GetMenuItemInfoW( hmenu
, i
, TRUE
, &info
);
1243 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1244 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1245 if ( ( !sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
) ) ||
1246 ( sei
->lpVerb
&& !lstrcmpiW( sei
->lpVerb
, string
) ) )
1257 memset( &ici
, 0, sizeof ici
);
1258 ici
.cbSize
= sizeof ici
;
1259 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NO_CONSOLE
|SEE_MASK_NOASYNC
|SEE_MASK_ASYNCOK
|SEE_MASK_FLAG_NO_UI
));
1260 ici
.nShow
= sei
->nShow
;
1261 ici
.lpVerb
= MAKEINTRESOURCEA( def
);
1262 ici
.hwnd
= sei
->hwnd
;
1263 ici
.lpParametersW
= sei
->lpParameters
;
1265 r
= IContextMenu_InvokeCommand( cm
, (LPCMINVOKECOMMANDINFO
) &ici
);
1267 TRACE("invoke command returned %08x\n", r
);
1271 DestroyMenu( hmenu
);
1273 IContextMenu_Release( cm
);
1277 static HRESULT
shellex_load_object_and_run( HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1279 IDataObject
*dataobj
= NULL
;
1280 IObjectWithSite
*ows
= NULL
;
1281 IShellExtInit
*obj
= NULL
;
1284 TRACE("%p %s %p\n", hkey
, debugstr_guid( guid
), sei
);
1286 r
= CoInitialize( NULL
);
1290 r
= CoCreateInstance( guid
, NULL
, CLSCTX_INPROC_SERVER
,
1291 &IID_IShellExtInit
, (LPVOID
*)&obj
);
1294 ERR("failed %08x\n", r
);
1298 dataobj
= shellex_get_dataobj( sei
);
1301 ERR("failed to get data object\n");
1305 r
= IShellExtInit_Initialize( obj
, NULL
, dataobj
, hkey
);
1309 r
= IShellExtInit_QueryInterface( obj
, &IID_IObjectWithSite
, (LPVOID
*) &ows
);
1313 IObjectWithSite_SetSite( ows
, NULL
);
1315 r
= shellex_run_context_menu_default( obj
, sei
);
1319 IObjectWithSite_Release( ows
);
1321 IDataObject_Release( dataobj
);
1323 IShellExtInit_Release( obj
);
1329 /*************************************************************************
1330 * ShellExecute_FromContextMenu [Internal]
1332 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1334 static const WCHAR szcm
[] = { 's','h','e','l','l','e','x','\\',
1335 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1336 HKEY hkey
, hkeycm
= 0;
1343 TRACE("%s\n", debugstr_w(sei
->lpFile
) );
1345 hkey
= ShellExecute_GetClassKey( sei
);
1347 return ERROR_FUNCTION_FAILED
;
1349 r
= RegOpenKeyW( hkey
, szcm
, &hkeycm
);
1350 if ( r
== ERROR_SUCCESS
)
1355 r
= RegEnumKeyW( hkeycm
, i
++, szguid
, ARRAY_SIZE(szguid
));
1356 if ( r
!= ERROR_SUCCESS
)
1359 hr
= CLSIDFromString( szguid
, &guid
);
1362 /* stop at the first one that succeeds in running */
1363 hr
= shellex_load_object_and_run( hkey
, &guid
, sei
);
1364 if ( SUCCEEDED( hr
) )
1368 RegCloseKey( hkeycm
);
1371 if ( hkey
!= sei
->hkeyClass
)
1372 RegCloseKey( hkey
);
1376 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
);
1378 static UINT_PTR
SHELL_execute_class( LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1380 static const WCHAR wQuote
[] = {'"',0};
1381 static const WCHAR wSpace
[] = {' ',0};
1382 WCHAR execCmd
[1024], classname
[1024];
1383 /* launch a document by fileclass like 'WordPad.Document.1' */
1384 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1385 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1386 ULONG cmask
=(psei
->fMask
& SEE_MASK_CLASSALL
);
1391 /* FIXME: remove following block when SHELL_quote_and_execute supports hkeyClass parameter */
1392 if (cmask
!= SEE_MASK_CLASSNAME
)
1395 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1396 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1398 execCmd
, sizeof(execCmd
));
1400 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1401 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1404 done
= SHELL_ArgifyW(wcmd
, ARRAY_SIZE(wcmd
), execCmd
, wszApplicationName
, psei
->lpIDList
, NULL
, &resultLen
);
1405 if (!done
&& wszApplicationName
[0])
1407 strcatW(wcmd
, wSpace
);
1408 if (*wszApplicationName
!= '"')
1410 strcatW(wcmd
, wQuote
);
1411 strcatW(wcmd
, wszApplicationName
);
1412 strcatW(wcmd
, wQuote
);
1415 strcatW(wcmd
, wszApplicationName
);
1417 if (resultLen
> ARRAY_SIZE(wcmd
))
1418 ERR("Argify buffer not large enough... truncating\n");
1419 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1422 strcpyW(classname
, psei
->lpClass
);
1423 rslt
= SHELL_FindExecutableByVerb(psei
->lpVerb
, NULL
, classname
, execCmd
, sizeof(execCmd
));
1425 TRACE("SHELL_FindExecutableByVerb returned %u (%s, %s)\n", (unsigned int)rslt
, debugstr_w(classname
), debugstr_w(execCmd
));
1428 rslt
= SHELL_quote_and_execute( execCmd
, wszEmpty
, classname
,
1429 wszApplicationName
, NULL
, psei
,
1430 psei_out
, execfunc
);
1434 static void SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1436 static const WCHAR wExplorer
[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1437 WCHAR buffer
[MAX_PATH
];
1439 /* last chance to translate IDList: now also allow CLSID paths */
1440 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei
->lpIDList
, buffer
, ARRAY_SIZE(buffer
)))) {
1441 if (buffer
[0]==':' && buffer
[1]==':') {
1442 /* open shell folder for the specified class GUID */
1443 if (strlenW(buffer
) + 1 > parametersLen
)
1444 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1445 lstrlenW(buffer
) + 1, parametersLen
);
1446 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1447 if (strlenW(wExplorer
) > dwApplicationNameLen
)
1448 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1449 lstrlenW(wExplorer
) + 1, dwApplicationNameLen
);
1450 lstrcpynW(wszApplicationName
, wExplorer
, dwApplicationNameLen
);
1452 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1454 WCHAR target
[MAX_PATH
];
1457 /* Check if we're executing a directory and if so use the
1458 handler for the Folder class */
1459 strcpyW(target
, buffer
);
1460 attribs
= GetFileAttributesW(buffer
);
1461 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1462 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1463 HCR_GetExecuteCommandW(0, wszFolder
,
1465 buffer
, sizeof(buffer
))) {
1466 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1467 buffer
, target
, sei
->lpIDList
, NULL
, &resultLen
);
1468 if (resultLen
> dwApplicationNameLen
)
1469 ERR("Argify buffer not large enough... truncating\n");
1471 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1476 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
)
1478 static const WCHAR wQuote
[] = {'"',0};
1479 static const WCHAR wSpace
[] = {' ',0};
1482 WCHAR
*wszQuotedCmd
;
1484 /* Length of quotes plus length of command plus NULL terminator */
1485 len
= 2 + lstrlenW(wcmd
) + 1;
1486 if (wszParameters
[0])
1488 /* Length of space plus length of parameters */
1489 len
+= 1 + lstrlenW(wszParameters
);
1491 wszQuotedCmd
= heap_alloc(len
* sizeof(WCHAR
));
1492 /* Must quote to handle case where cmd contains spaces,
1493 * else security hole if malicious user creates executable file "C:\\Program"
1495 strcpyW(wszQuotedCmd
, wQuote
);
1496 strcatW(wszQuotedCmd
, wcmd
);
1497 strcatW(wszQuotedCmd
, wQuote
);
1498 if (wszParameters
[0]) {
1499 strcatW(wszQuotedCmd
, wSpace
);
1500 strcatW(wszQuotedCmd
, wszParameters
);
1502 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(wszKeyname
));
1504 retval
= execute_from_key(wszKeyname
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1506 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1507 heap_free(wszQuotedCmd
);
1511 static UINT_PTR
SHELL_execute_url( LPCWSTR lpFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1513 static const WCHAR wShell
[] = {'\\','s','h','e','l','l','\\',0};
1514 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
1516 WCHAR
*lpstrProtocol
;
1521 lpstrRes
= strchrW(lpFile
, ':');
1523 iSize
= lpstrRes
- lpFile
;
1525 iSize
= strlenW(lpFile
);
1527 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1528 /* Looking for ...<protocol>\shell\<lpVerb>\command */
1529 len
= iSize
+ lstrlenW(wShell
) + lstrlenW(wCommand
) + 1;
1530 if (psei
->lpVerb
&& *psei
->lpVerb
)
1531 len
+= lstrlenW(psei
->lpVerb
);
1533 len
+= lstrlenW(wszOpen
);
1534 lpstrProtocol
= heap_alloc(len
* sizeof(WCHAR
));
1535 memcpy(lpstrProtocol
, lpFile
, iSize
*sizeof(WCHAR
));
1536 lpstrProtocol
[iSize
] = '\0';
1537 strcatW(lpstrProtocol
, wShell
);
1538 strcatW(lpstrProtocol
, psei
->lpVerb
&& *psei
->lpVerb
? psei
->lpVerb
: wszOpen
);
1539 strcatW(lpstrProtocol
, wCommand
);
1541 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1542 wcmd
, execfunc
, psei
, psei_out
);
1543 heap_free(lpstrProtocol
);
1547 static void do_error_dialog( UINT_PTR retval
, HWND hwnd
)
1550 int error_code
=GetLastError();
1552 if (retval
== SE_ERR_NOASSOC
)
1553 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, ARRAY_SIZE(msg
));
1555 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
, NULL
, error_code
, 0, msg
, ARRAY_SIZE(msg
), NULL
);
1557 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1560 /*************************************************************************
1561 * SHELL_execute [Internal]
1563 static BOOL
SHELL_execute( LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1565 static const WCHAR wWww
[] = {'w','w','w',0};
1566 static const WCHAR wHttp
[] = {'h','t','t','p',':','/','/',0};
1567 static const DWORD unsupportedFlags
=
1568 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1569 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1570 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1572 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1573 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
;
1574 DWORD dwApplicationNameLen
= MAX_PATH
+2;
1575 DWORD parametersLen
= ARRAY_SIZE(parametersBuffer
);
1576 DWORD wcmdLen
= ARRAY_SIZE(wcmdBuffer
);
1578 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1580 WCHAR wszKeyname
[256];
1582 UINT_PTR retval
= SE_ERR_NOASSOC
;
1584 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1587 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1588 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1589 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1590 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1591 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1592 debugstr_w(sei_tmp
.lpClass
) : "not used");
1594 sei
->hProcess
= NULL
;
1596 /* make copies of all path/command strings */
1597 if (!sei_tmp
.lpFile
)
1599 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1600 *wszApplicationName
= '\0';
1602 else if (*sei_tmp
.lpFile
== '\"' && sei_tmp
.lpFile
[(len
= strlenW(sei_tmp
.lpFile
))-1] == '\"')
1604 if(len
-1 >= dwApplicationNameLen
) dwApplicationNameLen
= len
;
1605 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1606 memcpy(wszApplicationName
, sei_tmp
.lpFile
+1, len
*sizeof(WCHAR
));
1608 wszApplicationName
[len
-2] = '\0';
1609 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName
));
1611 DWORD l
= strlenW(sei_tmp
.lpFile
)+1;
1612 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1613 wszApplicationName
= heap_alloc(dwApplicationNameLen
*sizeof(WCHAR
));
1614 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
*sizeof(WCHAR
));
1617 wszParameters
= parametersBuffer
;
1618 if (sei_tmp
.lpParameters
)
1620 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1621 if (len
> parametersLen
)
1623 wszParameters
= heap_alloc(len
* sizeof(WCHAR
));
1624 parametersLen
= len
;
1626 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1629 *wszParameters
= '\0';
1632 if (sei_tmp
.lpDirectory
)
1634 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1635 if (len
> ARRAY_SIZE(dirBuffer
))
1636 wszDir
= heap_alloc(len
* sizeof(WCHAR
));
1637 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1642 /* adjust string pointers to point to the new buffers */
1643 sei_tmp
.lpFile
= wszApplicationName
;
1644 sei_tmp
.lpParameters
= wszParameters
;
1645 sei_tmp
.lpDirectory
= wszDir
;
1647 if (sei_tmp
.fMask
& unsupportedFlags
)
1649 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1652 /* process the IDList */
1653 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1655 IShellExecuteHookW
* pSEH
;
1657 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1661 hr
= IShellExecuteHookW_Execute(pSEH
, &sei_tmp
);
1663 IShellExecuteHookW_Release(pSEH
);
1666 heap_free(wszApplicationName
);
1667 if (wszParameters
!= parametersBuffer
)
1668 heap_free(wszParameters
);
1669 if (wszDir
!= dirBuffer
)
1675 SHGetPathFromIDListW(sei_tmp
.lpIDList
, wszApplicationName
);
1676 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1679 if ( ERROR_SUCCESS
== ShellExecute_FromContextMenu( &sei_tmp
) )
1681 sei
->hInstApp
= (HINSTANCE
) 33;
1682 heap_free(wszApplicationName
);
1683 if (wszParameters
!= parametersBuffer
)
1684 heap_free(wszParameters
);
1685 if (wszDir
!= dirBuffer
)
1690 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1692 retval
= SHELL_execute_class( wszApplicationName
, &sei_tmp
, sei
,
1694 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1695 do_error_dialog(retval
, sei_tmp
.hwnd
);
1696 heap_free(wszApplicationName
);
1697 if (wszParameters
!= parametersBuffer
)
1698 heap_free(wszParameters
);
1699 if (wszDir
!= dirBuffer
)
1704 /* Has the IDList not yet been translated? */
1705 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1707 SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1710 dwApplicationNameLen
);
1713 /* convert file URLs */
1714 if (UrlIsFileUrlW(sei_tmp
.lpFile
))
1720 buf
= heap_alloc(size
* sizeof(WCHAR
));
1721 if (!buf
|| FAILED(PathCreateFromUrlW(sei_tmp
.lpFile
, buf
, &size
, 0))) {
1726 heap_free(wszApplicationName
);
1727 wszApplicationName
= buf
;
1728 sei_tmp
.lpFile
= wszApplicationName
;
1730 else /* or expand environment strings (not both!) */
1732 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1736 buf
= heap_alloc((len
+ 1) * sizeof(WCHAR
));
1738 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+ 1);
1739 heap_free(wszApplicationName
);
1740 wszApplicationName
= buf
;
1742 sei_tmp
.lpFile
= wszApplicationName
;
1746 if (*sei_tmp
.lpDirectory
)
1748 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1753 buf
= heap_alloc(len
* sizeof(WCHAR
));
1754 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1755 if (wszDir
!= dirBuffer
)
1758 sei_tmp
.lpDirectory
= wszDir
;
1762 /* Else, try to execute the filename */
1763 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1764 lpFile
= sei_tmp
.lpFile
;
1766 len
= lstrlenW(wszApplicationName
) + 3;
1767 if (sei_tmp
.lpParameters
[0])
1768 len
+= 1 + lstrlenW(wszParameters
);
1771 wcmd
= heap_alloc(len
* sizeof(WCHAR
));
1775 len
= lstrlenW(wszApplicationName
);
1776 memcpy(wcmd
+1, wszApplicationName
, len
* sizeof(WCHAR
));
1780 if (sei_tmp
.lpParameters
[0]) {
1782 strcpyW(wcmd
+len
, wszParameters
);
1785 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1787 heap_free(wszApplicationName
);
1788 if (wszParameters
!= parametersBuffer
)
1789 heap_free(wszParameters
);
1790 if (wszDir
!= dirBuffer
)
1792 if (wcmd
!= wcmdBuffer
)
1797 /* Else, try to find the executable */
1799 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, wszKeyname
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1800 if (retval
> 32) /* Found */
1802 retval
= SHELL_quote_and_execute( wcmd
, wszParameters
, wszKeyname
,
1803 wszApplicationName
, env
, &sei_tmp
,
1807 else if (PathIsDirectoryW(lpFile
))
1809 static const WCHAR wExplorer
[] = {'e','x','p','l','o','r','e','r',0};
1810 static const WCHAR wQuote
[] = {'"',0};
1811 WCHAR wExec
[MAX_PATH
];
1812 WCHAR
* lpQuotedFile
= heap_alloc( sizeof(WCHAR
) * (strlenW(lpFile
) + 3) );
1816 retval
= SHELL_FindExecutable( sei_tmp
.lpDirectory
, wExplorer
,
1817 wszOpen
, wExec
, MAX_PATH
,
1818 NULL
, &env
, NULL
, NULL
);
1821 strcpyW(lpQuotedFile
, wQuote
);
1822 strcatW(lpQuotedFile
, lpFile
);
1823 strcatW(lpQuotedFile
, wQuote
);
1824 retval
= SHELL_quote_and_execute( wExec
, lpQuotedFile
,
1826 wszApplicationName
, env
,
1827 &sei_tmp
, sei
, execfunc
);
1830 heap_free( lpQuotedFile
);
1833 retval
= 0; /* Out of memory */
1835 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1837 retval
= SHELL_execute_url( lpFile
, wcmd
, &sei_tmp
, sei
, execfunc
);
1839 /* Check if file specified is in the form www.??????.*** */
1840 else if (!strncmpiW(lpFile
, wWww
, 3))
1842 /* if so, prefix lpFile with http:// and call ShellExecute */
1843 WCHAR lpstrTmpFile
[256];
1844 strcpyW(lpstrTmpFile
, wHttp
);
1845 strcatW(lpstrTmpFile
, lpFile
);
1846 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1849 TRACE("retval %lu\n", retval
);
1851 heap_free(wszApplicationName
);
1852 if (wszParameters
!= parametersBuffer
)
1853 heap_free(wszParameters
);
1854 if (wszDir
!= dirBuffer
)
1856 if (wcmd
!= wcmdBuffer
)
1859 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1861 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1862 do_error_dialog(retval
, sei_tmp
.hwnd
);
1866 /*************************************************************************
1867 * ShellExecuteA [SHELL32.290]
1869 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpVerb
, LPCSTR lpFile
,
1870 LPCSTR lpParameters
, LPCSTR lpDirectory
, INT iShowCmd
)
1872 SHELLEXECUTEINFOA sei
;
1874 TRACE("%p,%s,%s,%s,%s,%d\n",
1875 hWnd
, debugstr_a(lpVerb
), debugstr_a(lpFile
),
1876 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
1878 sei
.cbSize
= sizeof(sei
);
1879 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1881 sei
.lpVerb
= lpVerb
;
1882 sei
.lpFile
= lpFile
;
1883 sei
.lpParameters
= lpParameters
;
1884 sei
.lpDirectory
= lpDirectory
;
1885 sei
.nShow
= iShowCmd
;
1892 ShellExecuteExA (&sei
);
1893 return sei
.hInstApp
;
1896 /*************************************************************************
1897 * ShellExecuteExA [SHELL32.292]
1900 BOOL WINAPI DECLSPEC_HOTPATCH
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1902 SHELLEXECUTEINFOW seiW
;
1904 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1908 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1911 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1914 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1916 if (sei
->lpParameters
)
1917 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1919 if (sei
->lpDirectory
)
1920 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1922 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
1923 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1925 seiW
.lpClass
= NULL
;
1927 ret
= SHELL_execute( &seiW
, SHELL_ExecuteW
);
1929 sei
->hInstApp
= seiW
.hInstApp
;
1931 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
1932 sei
->hProcess
= seiW
.hProcess
;
1936 SHFree(wParameters
);
1943 /*************************************************************************
1944 * ShellExecuteExW [SHELL32.293]
1947 BOOL WINAPI DECLSPEC_HOTPATCH
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1949 return SHELL_execute( sei
, SHELL_ExecuteW
);
1952 /*************************************************************************
1953 * ShellExecuteW [SHELL32.294]
1955 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpVerb,
1956 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1958 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpVerb
, LPCWSTR lpFile
,
1959 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1961 SHELLEXECUTEINFOW sei
;
1964 sei
.cbSize
= sizeof(sei
);
1965 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1967 sei
.lpVerb
= lpVerb
;
1968 sei
.lpFile
= lpFile
;
1969 sei
.lpParameters
= lpParameters
;
1970 sei
.lpDirectory
= lpDirectory
;
1971 sei
.nShow
= nShowCmd
;
1978 SHELL_execute( &sei
, SHELL_ExecuteW
);
1979 return sei
.hInstApp
;
1982 /*************************************************************************
1983 * WOWShellExecute [SHELL32.@]
1985 * FIXME: the callback function most likely doesn't work the same way on Windows.
1987 HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpVerb
,LPCSTR lpFile
,
1988 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
1990 SHELLEXECUTEINFOW seiW
;
1991 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
1992 HANDLE hProcess
= 0;
1994 seiW
.lpVerb
= lpVerb
? __SHCloneStrAtoW(&wVerb
, lpVerb
) : NULL
;
1995 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
1996 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
1997 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
1999 seiW
.cbSize
= sizeof(seiW
);
2002 seiW
.nShow
= iShowCmd
;
2007 seiW
.hProcess
= hProcess
;
2009 SHELL_execute( &seiW
, callback
);
2013 SHFree(wParameters
);
2015 return seiW
.hInstApp
;
2018 /*************************************************************************
2019 * ShellExec_RunDLLW [SHELL32.@]
2021 void WINAPI
ShellExec_RunDLLW(HWND hwnd
, HINSTANCE instance
, WCHAR
*cmdline
, int cmdshow
)
2023 BOOL in_single_quotes
= FALSE
, in_double_quotes
= FALSE
;
2026 TRACE("%p, %p, %s, %d\n", hwnd
, instance
, debugstr_w(cmdline
), cmdshow
);
2028 /* Replace the first whitespace character in the command line string with a
2029 null terminator to separate the program name from the program arguments */
2030 for (args
= cmdline
; *args
; args
++)
2035 args
++; /* skip the next character */
2038 if (!in_double_quotes
)
2039 in_single_quotes
= !in_single_quotes
;
2042 if (!in_single_quotes
)
2043 in_double_quotes
= !in_double_quotes
;
2047 if (!in_single_quotes
&& !in_double_quotes
)
2057 ShellExecuteW(hwnd
, NULL
, cmdline
, args
, NULL
, cmdshow
);
2060 /*************************************************************************
2061 * ShellExec_RunDLLA [SHELL32.@]
2063 void WINAPI
ShellExec_RunDLLA(HWND hwnd
, HINSTANCE instance
, CHAR
*cmdline
, int cmdshow
)
2066 ShellExec_RunDLLW(hwnd
, instance
, __SHCloneStrAtoW(&cmdlineW
, cmdline
), cmdshow
);
2070 /*************************************************************************
2071 * OpenAs_RunDLLA [SHELL32.@]
2073 void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2075 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2078 /*************************************************************************
2079 * OpenAs_RunDLLW [SHELL32.@]
2081 void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2083 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);
2086 /*************************************************************************
2087 * RegenerateUserEnvironment [SHELL32.@]
2089 BOOL WINAPI
RegenerateUserEnvironment(WCHAR
*wunknown
, BOOL bunknown
)
2091 FIXME("stub: %p, %d\n", wunknown
, bunknown
);