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 "wine/winbase16.h"
46 #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)
63 /***********************************************************************
64 * SHELL_ArgifyW [Internal]
66 * this function is supposed to expand the escape sequences found in the registry
67 * some diving reported that the following were used:
68 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
73 * %I address of a global item ID (explorer switch /idlist)
74 * %L seems to be %1 as long filename followed by the 8+3 variation
76 * %* all following parameters (see batfile)
79 static BOOL
SHELL_ArgifyW(WCHAR
* out
, int len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
, DWORD
* out_len
)
83 BOOL found_p1
= FALSE
;
88 TRACE("%p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
89 debugstr_w(lpFile
), pidl
, args
);
135 while(*args
&& !isspace(*args
))
144 while(isspace(*args
))
149 /* else fall through */
151 if (!done
|| (*fmt
== '1'))
153 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
154 if (SearchPathW(NULL
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
159 /* Add double quotation marks unless we already have them
160 (e.g.: "file://%1" %* for exefile) or unless the arg is already
161 enclosed in double quotation marks */
162 if ((res
== out
|| *(fmt
+ 1) != '"') && *cmd
!= '"')
167 used
+= strlenW(cmd
);
179 used
+= strlenW(cmd
);
191 * IE uses this a lot for activating things such as windows media
192 * player. This is not verified to be fully correct but it appears
198 used
+= strlenW(lpFile
);
201 strcpyW(res
, lpFile
);
202 res
+= strlenW(lpFile
);
212 /* %p should not exceed 8, maybe 16 when looking foward to 64bit.
213 * allowing a buffer of 100 should more than exceed all needs */
216 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
217 pv
= SHLockShared(hmem
, 0);
218 chars
= sprintfW(buf
, wszILPtr
, pv
);
219 if (chars
>= sizeof(buf
)/sizeof(WCHAR
))
220 ERR("pidl format buffer too small!");
234 * Check if this is an env-variable here...
237 /* Make sure that we have at least one more %.*/
238 if (strchrW(fmt
, '%'))
240 WCHAR tmpBuffer
[1024];
241 PWSTR tmpB
= tmpBuffer
;
242 WCHAR tmpEnvBuff
[MAX_PATH
];
249 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer
));
251 envRet
= GetEnvironmentVariableW(tmpBuffer
, tmpEnvBuff
, MAX_PATH
);
252 if (envRet
== 0 || envRet
> MAX_PATH
)
254 used
+= strlenW(tmpBuffer
);
257 strcpyW( res
, tmpBuffer
);
258 res
+= strlenW(tmpBuffer
);
263 used
+= strlenW(tmpEnvBuff
);
266 strcpyW( res
, tmpEnvBuff
);
267 res
+= strlenW(tmpEnvBuff
);
274 /* Don't skip past terminator (catch a single '%' at the end) */
291 TRACE("used %i of %i space\n",used
,len
);
298 HRESULT
SHELL_GetPathFromIDListForExecuteA(LPCITEMIDLIST pidl
, LPSTR pszPath
, UINT uOutSize
)
301 IShellFolder
* desktop
;
303 HRESULT hr
= SHGetDesktopFolder(&desktop
);
306 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
309 StrRetToStrNA(pszPath
, uOutSize
, &strret
, pidl
);
311 IShellFolder_Release(desktop
);
317 HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
320 IShellFolder
* desktop
;
322 HRESULT hr
= SHGetDesktopFolder(&desktop
);
325 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
328 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
330 IShellFolder_Release(desktop
);
336 /*************************************************************************
337 * SHELL_ExecuteW [Internal]
340 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
341 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
343 STARTUPINFOW startup
;
344 PROCESS_INFORMATION info
;
345 UINT_PTR retval
= 31;
347 WCHAR curdir
[MAX_PATH
];
349 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
350 /* ShellExecute specifies the command from psei->lpDirectory
351 * if present. Not from the current dir as CreateProcess does */
352 if( psei
->lpDirectory
&& psei
->lpDirectory
[0] )
353 if( ( gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
354 if( !SetCurrentDirectoryW( psei
->lpDirectory
))
355 ERR("cannot set directory %s\n", debugstr_w(psei
->lpDirectory
));
356 ZeroMemory(&startup
,sizeof(STARTUPINFOW
));
357 startup
.cb
= sizeof(STARTUPINFOW
);
358 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
359 startup
.wShowWindow
= psei
->nShow
;
360 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, CREATE_UNICODE_ENVIRONMENT
, env
,
361 psei
->lpDirectory
&& *psei
->lpDirectory
? psei
->lpDirectory
: NULL
,
364 /* Give 30 seconds to the app to come up, if desired. Probably only needed
365 when starting app immediately before making a DDE connection. */
367 if (WaitForInputIdle( info
.hProcess
, 30000 ) == WAIT_FAILED
)
368 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
370 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
371 psei_out
->hProcess
= info
.hProcess
;
373 CloseHandle( info
.hProcess
);
374 CloseHandle( info
.hThread
);
376 else if ((retval
= GetLastError()) >= 32)
378 TRACE("CreateProcess returned error %d\n", retval
);
379 retval
= ERROR_BAD_FORMAT
;
382 TRACE("returning %u\n", retval
);
384 psei_out
->hInstApp
= (HINSTANCE
)retval
;
386 if( !SetCurrentDirectoryW( curdir
))
387 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
393 /***********************************************************************
394 * SHELL_BuildEnvW [Internal]
396 * Build the environment for the new process, adding the specified
397 * path to the PATH variable. Returned pointer must be freed by caller.
399 static void *SHELL_BuildEnvW( const WCHAR
*path
)
401 static const WCHAR wPath
[] = {'P','A','T','H','=',0};
402 WCHAR
*strings
, *new_env
;
404 int total
= strlenW(path
) + 1;
405 BOOL got_path
= FALSE
;
407 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
411 int len
= strlenW(p
) + 1;
412 if (!strncmpiW( p
, wPath
, 5 )) got_path
= TRUE
;
416 if (!got_path
) total
+= 5; /* we need to create PATH */
417 total
++; /* terminating null */
419 if (!(new_env
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) )))
421 FreeEnvironmentStringsW( strings
);
428 int len
= strlenW(p
) + 1;
429 memcpy( p2
, p
, len
* sizeof(WCHAR
) );
430 if (!strncmpiW( p
, wPath
, 5 ))
433 strcpyW( p2
+ len
, path
);
434 p2
+= strlenW(path
) + 1;
441 strcpyW( p2
, wPath
);
443 p2
+= strlenW(p2
) + 1;
446 FreeEnvironmentStringsW( strings
);
451 /***********************************************************************
452 * SHELL_TryAppPathW [Internal]
454 * Helper function for SHELL_FindExecutable
455 * @param lpResult - pointer to a buffer of size MAX_PATH
456 * On entry: szName is a filename (probably without path separators).
457 * On exit: if szName found in "App Path", place full path in lpResult, and return true
459 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
461 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',
462 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
463 static const WCHAR wPath
[] = {'P','a','t','h',0};
470 if (env
) *env
= NULL
;
471 strcpyW(buffer
, wszKeyAppPaths
);
472 strcatW(buffer
, szName
);
473 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
476 len
= MAX_PATH
*sizeof(WCHAR
);
477 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
483 DWORD count
= sizeof(buffer
);
484 if (!RegQueryValueExW(hkApp
, wPath
, NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
485 *env
= SHELL_BuildEnvW( buffer
);
489 if (hkApp
) RegCloseKey(hkApp
);
493 static UINT
SHELL_FindExecutableByOperation(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
, LPWSTR key
, LPWSTR filetype
, LPWSTR command
, LONG commandlen
)
495 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
497 WCHAR verb
[MAX_PATH
];
499 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, filetype
, 0, 0x02000000, &hkeyClass
))
500 return 31; /* default - 'No association was found' */
501 if (!HCR_GetDefaultVerbW(hkeyClass
, lpOperation
, verb
, sizeof(verb
)))
502 return 31; /* default - 'No association was found' */
503 RegCloseKey(hkeyClass
);
505 /* Looking for ...buffer\shell\<verb>\command */
506 strcatW(filetype
, wszShell
);
507 strcatW(filetype
, verb
);
508 strcatW(filetype
, wCommand
);
510 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, command
,
511 &commandlen
) == ERROR_SUCCESS
)
513 commandlen
/= sizeof(WCHAR
);
514 if (key
) strcpyW(key
, filetype
);
518 LONG paramlen
= sizeof(param
);
519 static const WCHAR wSpace
[] = {' ',0};
521 /* FIXME: it seems all Windows version don't behave the same here.
522 * the doc states that this ddeexec information can be found after
524 * on Win98, it doesn't appear, but I think it does on Win2k
526 /* Get the parameters needed by the application
527 from the associated ddeexec key */
528 tmp
= strstrW(filetype
, wCommand
);
530 strcatW(filetype
, wDdeexec
);
531 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, param
,
532 ¶mlen
) == ERROR_SUCCESS
)
534 paramlen
/= sizeof(WCHAR
);
535 strcatW(command
, wSpace
);
536 strcatW(command
, param
);
537 commandlen
+= paramlen
;
541 command
[commandlen
] = '\0';
543 return 33; /* FIXME see SHELL_FindExecutable() */
546 return 31; /* default - 'No association was found' */
549 /*************************************************************************
550 * SHELL_FindExecutable [Internal]
552 * Utility for code sharing between FindExecutable and ShellExecute
554 * lpFile the name of a file
555 * lpOperation the operation on it (open)
557 * lpResult a buffer, big enough :-(, to store the command to do the
558 * operation on the file
559 * key a buffer, big enough, to get the key name to do actually the
560 * command (it'll be used afterwards for more information
563 UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
,
564 LPWSTR lpResult
, int resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
566 static const WCHAR wWindows
[] = {'w','i','n','d','o','w','s',0};
567 static const WCHAR wPrograms
[] = {'p','r','o','g','r','a','m','s',0};
568 static const WCHAR wExtensions
[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
569 WCHAR
*extension
= NULL
; /* pointer to file extension */
570 WCHAR filetype
[256]; /* registry name for this filetype */
571 LONG filetypelen
= sizeof(filetype
); /* length of above */
572 WCHAR command
[1024]; /* command from registry */
573 WCHAR wBuffer
[256]; /* Used to GetProfileString */
574 UINT retval
= 31; /* default - 'No association was found' */
575 WCHAR
*tok
; /* token pointer */
576 WCHAR xlpFile
[256]; /* result of SearchPath */
577 DWORD attribs
; /* file attributes */
579 TRACE("%s\n", (lpFile
!= NULL
) ? debugstr_w(lpFile
) : "-");
582 lpResult
[0] = '\0'; /* Start off with an empty return string */
583 if (key
) *key
= '\0';
585 /* trap NULL parameters on entry */
586 if ((lpFile
== NULL
) || (lpResult
== NULL
))
588 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
589 debugstr_w(lpFile
), debugstr_w(lpResult
));
590 return 2; /* File not found. Close enough, I guess. */
593 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
595 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
599 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
601 TRACE("SearchPathW returned non-zero\n");
603 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
606 attribs
= GetFileAttributesW(lpFile
);
607 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
609 strcpyW(filetype
, wszFolder
);
610 filetypelen
= 6; /* strlen("Folder") */
614 /* First thing we need is the file's extension */
615 extension
= strrchrW(xlpFile
, '.'); /* Assume last "." is the one; */
616 /* File->Run in progman uses */
618 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
620 if (extension
== NULL
|| extension
[1]==0)
622 WARN("Returning 31 - No association\n");
623 return 31; /* no association */
626 /* Three places to check: */
627 /* 1. win.ini, [windows], programs (NB no leading '.') */
628 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
629 /* 3. win.ini, [extensions], extension (NB no leading '.' */
630 /* All I know of the order is that registry is checked before */
631 /* extensions; however, it'd make sense to check the programs */
632 /* section first, so that's what happens here. */
634 /* See if it's a program - if GetProfileString fails, we skip this
635 * section. Actually, if GetProfileString fails, we've probably
636 * got a lot more to worry about than running a program... */
637 if (GetProfileStringW(wWindows
, wPrograms
, wExtensions
, wBuffer
, sizeof(wBuffer
)/sizeof(WCHAR
)) > 0)
644 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
648 while (*p
== ' ' || *p
== '\t') p
++;
651 if (strcmpiW(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
653 strcpyW(lpResult
, xlpFile
);
654 /* Need to perhaps check that the file has a path
656 TRACE("found %s\n", debugstr_w(lpResult
));
659 /* Greater than 32 to indicate success FIXME According to the
660 * docs, I should be returning a handle for the
661 * executable. Does this mean I'm supposed to open the
662 * executable file or something? More RTFM, I guess... */
669 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, filetype
,
670 &filetypelen
) == ERROR_SUCCESS
)
672 filetypelen
/= sizeof(WCHAR
);
673 filetype
[filetypelen
] = '\0';
674 TRACE("File type: %s\n", debugstr_w(filetype
));
685 /* pass the operation string to SHELL_FindExecutableByOperation() */
686 filetype
[filetypelen
] = '\0';
687 retval
= SHELL_FindExecutableByOperation(lpPath
, lpFile
, lpOperation
, key
, filetype
, command
, sizeof(command
));
692 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
693 if (finishedLen
> resultLen
)
694 ERR("Argify buffer not large enough.. truncated\n");
696 /* Remove double quotation marks and command line arguments */
697 if (*lpResult
== '"')
700 while (*(p
+ 1) != '"')
709 else /* Check win.ini */
711 static const WCHAR wExtensions
[] = {'e','x','t','e','n','s','i','o','n','s',0};
713 /* Toss the leading dot */
715 if (GetProfileStringW(wExtensions
, extension
, wszEmpty
, command
, sizeof(command
)/sizeof(WCHAR
)) > 0)
717 if (strlenW(command
) != 0)
719 strcpyW(lpResult
, command
);
720 tok
= strchrW(lpResult
, '^'); /* should be ^.extension? */
724 strcatW(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
725 tok
= strchrW(command
, '^'); /* see above */
726 if ((tok
!= NULL
) && (strlenW(tok
)>5))
728 strcatW(lpResult
, &tok
[5]);
731 retval
= 33; /* FIXME - see above */
736 TRACE("returning %s\n", debugstr_w(lpResult
));
740 /******************************************************************
743 * callback for the DDE connection. not really useful
745 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
746 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
747 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
749 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
750 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
754 /******************************************************************
757 * ShellExecute helper. Used to do an operation with a DDE connection
759 * Handles both the direct connection (try #1), and if it fails,
760 * launching an application and trying (#2) to connect to it
763 static unsigned dde_connect(WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
764 const WCHAR
* lpFile
, WCHAR
*env
,
765 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
766 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
768 static const WCHAR wApplication
[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
769 static const WCHAR wTopic
[] = {'\\','t','o','p','i','c',0};
770 WCHAR
* endkey
= key
+ strlenW(key
);
771 WCHAR app
[256], topic
[256], ifexec
[256], res
[256];
772 LONG applen
, topiclen
, ifexeclen
;
777 HSZ hszApp
, hszTopic
;
781 BOOL unicode
= !(GetVersion() & 0x80000000);
783 strcpyW(endkey
, wApplication
);
784 applen
= sizeof(app
);
785 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, app
, &applen
) != ERROR_SUCCESS
)
787 FIXME("default app name NIY %s\n", debugstr_w(key
));
791 strcpyW(endkey
, wTopic
);
792 topiclen
= sizeof(topic
);
793 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, topic
, &topiclen
) != ERROR_SUCCESS
)
795 static const WCHAR wSystem
[] = {'S','y','s','t','e','m',0};
796 strcpyW(topic
, wSystem
);
801 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
806 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
810 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
811 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
813 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
817 static const WCHAR wIfexec
[] = {'\\','i','f','e','x','e','c',0};
818 TRACE("Launching '%s'\n", debugstr_w(start
));
819 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
822 TRACE("Couldn't launch\n");
825 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
828 TRACE("Couldn't connect. ret=%d\n", ret
);
829 DdeUninitialize(ddeInst
);
830 SetLastError(ERROR_DDE_FAIL
);
831 return 30; /* whatever */
833 strcpyW(endkey
, wIfexec
);
834 ifexeclen
= sizeof(ifexec
);
835 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
841 SHELL_ArgifyW(res
, sizeof(res
)/sizeof(WCHAR
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
842 if (resultLen
> sizeof(res
)/sizeof(WCHAR
))
843 ERR("Argify buffer not large enough, truncated\n");
844 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
846 /* It's documented in the KB 330337 that IE has a bug and returns
847 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
850 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0,
851 XTYP_EXECUTE
, 30000, &tid
);
854 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
855 char *resA
= HeapAlloc(GetProcessHeap(), 0, lenA
);
856 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
857 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
858 XTYP_EXECUTE
, 10000, &tid
);
859 HeapFree(GetProcessHeap(), 0, resA
);
862 DdeFreeDataHandle(hDdeData
);
864 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
867 DdeDisconnect(hConv
);
870 DdeUninitialize(ddeInst
);
875 /*************************************************************************
876 * execute_from_key [Internal]
878 static UINT_PTR
execute_from_key(LPWSTR key
, LPCWSTR lpFile
, WCHAR
*env
, LPCWSTR szCommandline
,
879 LPCWSTR executable_name
,
880 SHELL_ExecuteW32 execfunc
,
881 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
884 LONG cmdlen
= sizeof(cmd
);
885 UINT_PTR retval
= 31;
887 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
888 debugstr_w(szCommandline
), debugstr_w(executable_name
));
892 /* Get the application from the registry */
893 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
898 TRACE("got cmd: %s\n", debugstr_w(cmd
));
902 /* Is there a replace() function anywhere? */
903 cmdlen
/= sizeof(WCHAR
);
905 if (!SHELL_ArgifyW(param
, sizeof(param
)/sizeof(WCHAR
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
, &resultLen
))
907 /* looks like there is no %1 param in the cmd, add one */
908 static const WCHAR oneW
[] = { ' ','\"','%','1','\"',0 };
910 SHELL_ArgifyW(param
, sizeof(param
)/sizeof(WCHAR
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
, &resultLen
);
912 if (resultLen
> sizeof(param
)/sizeof(WCHAR
))
913 ERR("Argify buffer not large enough, truncating\n");
915 TRACE("executing: %s\n", debugstr_w(param
));
916 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
920 static const WCHAR wCommand
[] = {'c','o','m','m','a','n','d',0};
921 static const WCHAR wDdeexec
[] = {'d','d','e','e','x','e','c',0};
924 LONG paramlen
= sizeof(param
);
928 /* Get the parameters needed by the application
929 from the associated ddeexec key */
930 tmp
= strstrW(key
, wCommand
);
932 strcpyW(tmp
, wDdeexec
);
934 TRACE("trying ddeexec cmd: %s\n", debugstr_w(key
));
936 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, param
, ¶mlen
) == ERROR_SUCCESS
)
938 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(param
));
939 retval
= dde_connect(key
, executable_name
, param
, lpFile
, env
, szCommandline
, psei
->lpIDList
, execfunc
, psei
, psei_out
);
942 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
948 /*************************************************************************
949 * FindExecutableA [SHELL32.@]
951 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
954 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
955 WCHAR wResult
[MAX_PATH
];
957 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
958 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
960 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
961 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
963 SHFree( wDirectory
);
965 TRACE("returning %s\n", lpResult
);
969 /*************************************************************************
970 * FindExecutableW [SHELL32.@]
972 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
974 UINT_PTR retval
= 31; /* default - 'No association was found' */
977 TRACE("File %s, Dir %s\n",
978 (lpFile
!= NULL
? debugstr_w(lpFile
) : "-"), (lpDirectory
!= NULL
? debugstr_w(lpDirectory
) : "-"));
980 lpResult
[0] = '\0'; /* Start off with an empty return string */
982 /* trap NULL parameters on entry */
983 if ((lpFile
== NULL
) || (lpResult
== NULL
))
985 /* FIXME - should throw a warning, perhaps! */
986 return (HINSTANCE
)2; /* File not found. Close enough, I guess. */
991 GetCurrentDirectoryW(sizeof(old_dir
)/sizeof(WCHAR
), old_dir
);
992 SetCurrentDirectoryW(lpDirectory
);
995 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, lpResult
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
997 TRACE("returning %s\n", debugstr_w(lpResult
));
999 SetCurrentDirectoryW(old_dir
);
1000 return (HINSTANCE
)retval
;
1003 /* FIXME: is this already implemented somewhere else? */
1004 static HKEY
ShellExecute_GetClassKey( LPSHELLEXECUTEINFOW sei
)
1006 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1008 DWORD type
= 0, sz
= 0;
1012 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1013 return sei
->hkeyClass
;
1015 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1016 lpClass
= sei
->lpClass
;
1019 ext
= PathFindExtensionW( sei
->lpFile
);
1020 TRACE("ext = %s\n", debugstr_w( ext
) );
1024 r
= RegOpenKeyW( HKEY_CLASSES_ROOT
, ext
, &hkey
);
1025 if (r
!= ERROR_SUCCESS
)
1028 r
= RegQueryValueExW( hkey
, NULL
, 0, &type
, NULL
, &sz
);
1029 if ( r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1031 sz
+= sizeof (WCHAR
);
1032 cls
= HeapAlloc( GetProcessHeap(), 0, sz
);
1034 RegQueryValueExW( hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1037 RegCloseKey( hkey
);
1041 TRACE("class = %s\n", debugstr_w(lpClass
) );
1045 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1047 HeapFree( GetProcessHeap(), 0, cls
);
1052 static IDataObject
*shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
)
1054 LPCITEMIDLIST pidllast
= NULL
;
1055 IDataObject
*dataobj
= NULL
;
1056 IShellFolder
*shf
= NULL
;
1057 LPITEMIDLIST pidl
= NULL
;
1060 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1061 pidl
= sei
->lpIDList
;
1064 WCHAR fullpath
[MAX_PATH
];
1067 r
= GetFullPathNameW( sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1071 pidl
= ILCreateFromPathW( fullpath
);
1074 r
= SHBindToParent( pidl
, &IID_IShellFolder
, (LPVOID
*)&shf
, &pidllast
);
1078 IShellFolder_GetUIObjectOf( shf
, NULL
, 1, &pidllast
,
1079 &IID_IDataObject
, NULL
, (LPVOID
*) &dataobj
);
1082 if ( pidl
!= sei
->lpIDList
)
1085 IShellFolder_Release( shf
);
1089 static HRESULT
shellex_run_context_menu_default( IShellExtInit
*obj
,
1090 LPSHELLEXECUTEINFOW sei
)
1092 IContextMenu
*cm
= NULL
;
1093 CMINVOKECOMMANDINFOEX ici
;
1100 TRACE("%p %p\n", obj
, sei
);
1102 r
= IShellExtInit_QueryInterface( obj
, &IID_IContextMenu
, (LPVOID
*) &cm
);
1106 hmenu
= CreateMenu();
1110 /* the number of the last menu added is returned in r */
1111 r
= IContextMenu_QueryContextMenu( cm
, hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1115 n
= GetMenuItemCount( hmenu
);
1116 for ( i
= 0; i
< n
; i
++ )
1118 memset( &info
, 0, sizeof info
);
1119 info
.cbSize
= sizeof info
;
1120 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1121 info
.dwTypeData
= string
;
1122 info
.cch
= sizeof string
;
1124 GetMenuItemInfoW( hmenu
, i
, TRUE
, &info
);
1126 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1127 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1128 if ( ( !sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
) ) ||
1129 ( sei
->lpVerb
&& !lstrcmpiW( sei
->lpVerb
, string
) ) )
1140 memset( &ici
, 0, sizeof ici
);
1141 ici
.cbSize
= sizeof ici
;
1142 ici
.fMask
= CMIC_MASK_UNICODE
;
1143 ici
.nShow
= sei
->nShow
;
1144 ici
.lpVerb
= MAKEINTRESOURCEA( def
);
1145 ici
.hwnd
= sei
->hwnd
;
1146 ici
.lpParametersW
= sei
->lpParameters
;
1148 r
= IContextMenu_InvokeCommand( cm
, (LPCMINVOKECOMMANDINFO
) &ici
);
1150 TRACE("invoke command returned %08x\n", r
);
1154 DestroyMenu( hmenu
);
1156 IContextMenu_Release( cm
);
1160 static HRESULT
shellex_load_object_and_run( HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1162 IDataObject
*dataobj
= NULL
;
1163 IObjectWithSite
*ows
= NULL
;
1164 IShellExtInit
*obj
= NULL
;
1167 TRACE("%p %s %p\n", hkey
, debugstr_guid( guid
), sei
);
1169 r
= CoInitialize( NULL
);
1173 r
= CoCreateInstance( guid
, NULL
, CLSCTX_INPROC_SERVER
,
1174 &IID_IShellExtInit
, (LPVOID
*)&obj
);
1177 ERR("failed %08x\n", r
);
1181 dataobj
= shellex_get_dataobj( sei
);
1184 ERR("failed to get data object\n");
1188 r
= IShellExtInit_Initialize( obj
, NULL
, dataobj
, hkey
);
1192 r
= IShellExtInit_QueryInterface( obj
, &IID_IObjectWithSite
, (LPVOID
*) &ows
);
1196 IObjectWithSite_SetSite( ows
, NULL
);
1198 r
= shellex_run_context_menu_default( obj
, sei
);
1202 IObjectWithSite_Release( ows
);
1204 IDataObject_Release( dataobj
);
1206 IShellExtInit_Release( obj
);
1212 /*************************************************************************
1213 * ShellExecute_FromContextMenu [Internal]
1215 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1217 static const WCHAR szcm
[] = { 's','h','e','l','l','e','x','\\',
1218 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1219 HKEY hkey
, hkeycm
= 0;
1226 TRACE("%s\n", debugstr_w(sei
->lpFile
) );
1228 hkey
= ShellExecute_GetClassKey( sei
);
1230 return ERROR_FUNCTION_FAILED
;
1232 r
= RegOpenKeyW( hkey
, szcm
, &hkeycm
);
1233 if ( r
== ERROR_SUCCESS
)
1238 r
= RegEnumKeyW( hkeycm
, i
++, szguid
, 39 );
1239 if ( r
!= ERROR_SUCCESS
)
1242 hr
= CLSIDFromString( szguid
, &guid
);
1245 /* stop at the first one that succeeds in running */
1246 hr
= shellex_load_object_and_run( hkey
, &guid
, sei
);
1247 if ( SUCCEEDED( hr
) )
1251 RegCloseKey( hkeycm
);
1254 if ( hkey
!= sei
->hkeyClass
)
1255 RegCloseKey( hkey
);
1259 /*************************************************************************
1260 * SHELL_execute [Internal]
1262 BOOL
SHELL_execute( LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1264 static const WCHAR wQuote
[] = {'"',0};
1265 static const WCHAR wSpace
[] = {' ',0};
1266 static const WCHAR wWww
[] = {'w','w','w',0};
1267 static const WCHAR wFile
[] = {'f','i','l','e',0};
1268 static const WCHAR wHttp
[] = {'h','t','t','p',':','/','/',0};
1269 static const WCHAR wExplorer
[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1270 static const DWORD unsupportedFlags
=
1271 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1272 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
| SEE_MASK_FLAG_NO_UI
|
1273 SEE_MASK_UNICODE
| SEE_MASK_NO_CONSOLE
| SEE_MASK_ASYNCOK
|
1276 WCHAR
*wszApplicationName
, wszParameters
[1024], wszDir
[MAX_PATH
];
1277 DWORD dwApplicationNameLen
= MAX_PATH
+2;
1279 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1280 WCHAR wfileName
[MAX_PATH
];
1282 WCHAR lpstrProtocol
[256];
1284 UINT_PTR retval
= 31;
1286 WCHAR buffer
[MAX_PATH
];
1289 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1290 memcpy(&sei_tmp
, sei
, sizeof(sei_tmp
));
1292 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1293 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1294 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1295 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1296 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1297 debugstr_w(sei_tmp
.lpClass
) : "not used");
1299 sei
->hProcess
= NULL
;
1301 /* make copies of all path/command strings */
1302 if (!sei_tmp
.lpFile
)
1304 wszApplicationName
= HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
*sizeof(WCHAR
));
1305 *wszApplicationName
= '\0';
1307 else if (*sei_tmp
.lpFile
== '\"')
1309 DWORD l
= strlenW(sei_tmp
.lpFile
+1);
1310 if(l
>= dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1311 wszApplicationName
= HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
*sizeof(WCHAR
));
1312 memcpy(wszApplicationName
, sei_tmp
.lpFile
+1, (l
+1)*sizeof(WCHAR
));
1313 if (wszApplicationName
[l
-1] == '\"')
1314 wszApplicationName
[l
-1] = '\0';
1315 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName
));
1317 DWORD l
= strlenW(sei_tmp
.lpFile
)+1;
1318 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1319 wszApplicationName
= HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
*sizeof(WCHAR
));
1320 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
*sizeof(WCHAR
));
1323 if (sei_tmp
.lpParameters
)
1324 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1326 *wszParameters
= '\0';
1328 if (sei_tmp
.lpDirectory
)
1329 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1333 /* adjust string pointers to point to the new buffers */
1334 sei_tmp
.lpFile
= wszApplicationName
;
1335 sei_tmp
.lpParameters
= wszParameters
;
1336 sei_tmp
.lpDirectory
= wszDir
;
1338 if (sei_tmp
.fMask
& unsupportedFlags
)
1340 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1343 /* process the IDList */
1344 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1346 IShellExecuteHookW
* pSEH
;
1348 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1352 hr
= IShellExecuteHookW_Execute(pSEH
, &sei_tmp
);
1354 IShellExecuteHookW_Release(pSEH
);
1357 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1362 SHGetPathFromIDListW(sei_tmp
.lpIDList
, wszApplicationName
);
1363 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1366 if ( ERROR_SUCCESS
== ShellExecute_FromContextMenu( &sei_tmp
) )
1368 sei
->hInstApp
= (HINSTANCE
) 33;
1369 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1373 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1375 /* launch a document by fileclass like 'WordPad.Document.1' */
1376 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1377 /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1378 ULONG cmask
=(sei_tmp
.fMask
& SEE_MASK_CLASSALL
);
1380 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? sei_tmp
.hkeyClass
: NULL
,
1381 (cmask
== SEE_MASK_CLASSNAME
) ? sei_tmp
.lpClass
: NULL
,
1383 wszParameters
, sizeof(wszParameters
)/sizeof(WCHAR
));
1385 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1386 TRACE("SEE_MASK_CLASSNAME->'%s', doc->'%s'\n", debugstr_w(wszParameters
), debugstr_w(wszApplicationName
));
1389 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
)/sizeof(WCHAR
), wszParameters
, wszApplicationName
, sei_tmp
.lpIDList
, NULL
, &resultLen
);
1390 if (!done
&& wszApplicationName
[0])
1392 strcatW(wcmd
, wSpace
);
1393 strcatW(wcmd
, wszApplicationName
);
1395 if (resultLen
> sizeof(wcmd
)/sizeof(WCHAR
))
1396 ERR("Argify buffer not large enough... truncating\n");
1397 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1399 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1403 /* Has the IDList not yet been translated? */
1404 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1406 /* last chance to translate IDList: now also allow CLSID paths */
1407 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp
.lpIDList
, buffer
, sizeof(buffer
)))) {
1408 if (buffer
[0]==':' && buffer
[1]==':') {
1409 /* open shell folder for the specified class GUID */
1410 strcpyW(wszParameters
, buffer
);
1411 strcpyW(wszApplicationName
, wExplorer
);
1413 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1415 WCHAR target
[MAX_PATH
];
1418 /* Check if we're executing a directory and if so use the
1419 handler for the Folder class */
1420 strcpyW(target
, buffer
);
1421 attribs
= GetFileAttributesW(buffer
);
1422 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1423 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1424 HCR_GetExecuteCommandW(0, wszFolder
,
1426 buffer
, sizeof(buffer
))) {
1427 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1428 buffer
, target
, sei_tmp
.lpIDList
, NULL
, &resultLen
);
1429 if (resultLen
> dwApplicationNameLen
)
1430 ERR("Argify buffer not large enough... truncating\n");
1432 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1437 /* expand environment strings */
1438 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1442 buf
= HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
1444 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+1);
1445 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1446 dwApplicationNameLen
= len
+1;
1447 wszApplicationName
= buf
;
1449 sei_tmp
.lpFile
= wszApplicationName
;
1452 if (*sei_tmp
.lpParameters
)
1454 len
= ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, NULL
, 0);
1459 buf
= HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
1460 ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buf
, len
);
1462 ERR("Parameters exceeds buffer size (%i > 1024)\n",len
);
1463 lstrcpynW(wszParameters
, buf
, min(1024,len
));
1464 HeapFree(GetProcessHeap(),0,buf
);
1468 if (*sei_tmp
.lpDirectory
)
1470 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1475 buf
= HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
1476 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1478 ERR("Directory exceeds buffer size (%i > 1024)\n",len
);
1479 lstrcpynW(wszDir
, buf
, min(1024,len
));
1480 HeapFree(GetProcessHeap(),0,buf
);
1484 /* Else, try to execute the filename */
1485 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1487 /* separate out command line arguments from executable file name */
1488 if (!*sei_tmp
.lpParameters
) {
1489 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1490 if (sei_tmp
.lpFile
[0] == '"') {
1491 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1492 LPWSTR dst
= wfileName
;
1495 /* copy the unquoted executable path to 'wfileName' */
1496 while(*src
&& *src
!='"')
1504 while(isspace(*src
))
1509 /* copy the parameter string to 'wszParameters' */
1510 strcpyW(wszParameters
, src
);
1512 /* terminate previous command string after the quote character */
1517 /* If the executable name is not quoted, we have to use this search loop here,
1518 that in CreateProcess() is not sufficient because it does not handle shell links. */
1519 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1522 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1523 for(s
=beg
; (space
=strchrW(s
, ' ')); s
=space
+1) {
1524 int idx
= space
-sei_tmp
.lpFile
;
1525 memcpy(buffer
, sei_tmp
.lpFile
, idx
* sizeof(WCHAR
));
1528 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1529 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
), xlpFile
, NULL
))
1531 /* separate out command from parameter string */
1532 LPCWSTR p
= space
+ 1;
1537 strcpyW(wszParameters
, p
);
1544 strcpyW(wfileName
, sei_tmp
.lpFile
);
1547 strcpyW(wfileName
, sei_tmp
.lpFile
);
1551 strcpyW(wcmd
, wszApplicationName
);
1552 if (sei_tmp
.lpParameters
[0]) {
1553 strcatW(wcmd
, wSpace
);
1554 strcatW(wcmd
, wszParameters
);
1557 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1559 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1563 /* Else, try to find the executable */
1565 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, 1024, lpstrProtocol
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1566 if (retval
> 32) /* Found */
1568 WCHAR wszQuotedCmd
[MAX_PATH
+2];
1569 /* Must quote to handle case where cmd contains spaces,
1570 * else security hole if malicious user creates executable file "C:\\Program"
1572 strcpyW(wszQuotedCmd
, wQuote
);
1573 strcatW(wszQuotedCmd
, wcmd
);
1574 strcatW(wszQuotedCmd
, wQuote
);
1575 if (wszParameters
[0]) {
1576 strcatW(wszQuotedCmd
, wSpace
);
1577 strcatW(wszQuotedCmd
, wszParameters
);
1579 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(sei_tmp
.lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(lpstrProtocol
));
1581 retval
= execute_from_key(lpstrProtocol
, wszApplicationName
, env
, sei_tmp
.lpParameters
, wcmd
, execfunc
, &sei_tmp
, sei
);
1583 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, &sei_tmp
, sei
);
1584 HeapFree( GetProcessHeap(), 0, env
);
1586 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1588 static const WCHAR wShell
[] = {'\\','s','h','e','l','l','\\',0};
1589 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
1593 lpstrRes
= strchrW(lpFile
, ':');
1595 iSize
= lpstrRes
- lpFile
;
1597 iSize
= strlenW(lpFile
);
1599 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1600 /* Looking for ...protocol\shell\lpOperation\command */
1601 memcpy(lpstrProtocol
, lpFile
, iSize
*sizeof(WCHAR
));
1602 lpstrProtocol
[iSize
] = '\0';
1603 strcatW(lpstrProtocol
, wShell
);
1604 strcatW(lpstrProtocol
, sei_tmp
.lpVerb
? sei_tmp
.lpVerb
: wszOpen
);
1605 strcatW(lpstrProtocol
, wCommand
);
1607 /* Remove File Protocol from lpFile */
1608 /* In the case file://path/file */
1609 if (!strncmpiW(lpFile
, wFile
, iSize
))
1612 while (*lpFile
== ':') lpFile
++;
1614 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, sei_tmp
.lpParameters
, wcmd
, execfunc
, &sei_tmp
, sei
);
1616 /* Check if file specified is in the form www.??????.*** */
1617 else if (!strncmpiW(lpFile
, wWww
, 3))
1619 /* if so, append lpFile http:// and call ShellExecute */
1620 WCHAR lpstrTmpFile
[256];
1621 strcpyW(lpstrTmpFile
, wHttp
);
1622 strcatW(lpstrTmpFile
, lpFile
);
1623 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1626 TRACE("retval %u\n", retval
);
1628 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1630 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1634 /*************************************************************************
1635 * ShellExecuteA [SHELL32.290]
1637 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
1638 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
)
1640 SHELLEXECUTEINFOA sei
;
1642 TRACE("%p,%s,%s,%s,%s,%d\n",
1643 hWnd
, debugstr_a(lpOperation
), debugstr_a(lpFile
),
1644 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
1646 sei
.cbSize
= sizeof(sei
);
1649 sei
.lpVerb
= lpOperation
;
1650 sei
.lpFile
= lpFile
;
1651 sei
.lpParameters
= lpParameters
;
1652 sei
.lpDirectory
= lpDirectory
;
1653 sei
.nShow
= iShowCmd
;
1660 ShellExecuteExA (&sei
);
1661 return sei
.hInstApp
;
1664 /*************************************************************************
1665 * ShellExecuteExA [SHELL32.292]
1668 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1670 SHELLEXECUTEINFOW seiW
;
1672 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1676 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1679 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1682 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1684 if (sei
->lpParameters
)
1685 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1687 if (sei
->lpDirectory
)
1688 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1690 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
1691 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1693 seiW
.lpClass
= NULL
;
1695 ret
= SHELL_execute( &seiW
, SHELL_ExecuteW
);
1697 sei
->hInstApp
= seiW
.hInstApp
;
1699 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
1700 sei
->hProcess
= seiW
.hProcess
;
1704 SHFree(wParameters
);
1711 /*************************************************************************
1712 * ShellExecuteExW [SHELL32.293]
1715 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1717 return SHELL_execute( sei
, SHELL_ExecuteW
);
1720 /*************************************************************************
1721 * ShellExecuteW [SHELL32.294]
1723 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1724 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1726 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1727 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1729 SHELLEXECUTEINFOW sei
;
1732 sei
.cbSize
= sizeof(sei
);
1735 sei
.lpVerb
= lpOperation
;
1736 sei
.lpFile
= lpFile
;
1737 sei
.lpParameters
= lpParameters
;
1738 sei
.lpDirectory
= lpDirectory
;
1739 sei
.nShow
= nShowCmd
;
1746 SHELL_execute( &sei
, SHELL_ExecuteW
);
1747 return sei
.hInstApp
;