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!\n");
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 static HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
301 IShellFolder
* desktop
;
303 HRESULT hr
= SHGetDesktopFolder(&desktop
);
306 hr
= IShellFolder_GetDisplayNameOf(desktop
, pidl
, SHGDN_FORPARSING
, &strret
);
309 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
311 IShellFolder_Release(desktop
);
317 /*************************************************************************
318 * SHELL_ExecuteW [Internal]
321 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
322 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
324 STARTUPINFOW startup
;
325 PROCESS_INFORMATION info
;
326 UINT_PTR retval
= SE_ERR_NOASSOC
;
328 WCHAR curdir
[MAX_PATH
];
330 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
331 /* ShellExecute specifies the command from psei->lpDirectory
332 * if present. Not from the current dir as CreateProcess does */
333 if( psei
->lpDirectory
&& psei
->lpDirectory
[0] )
334 if( ( gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
335 if( !SetCurrentDirectoryW( psei
->lpDirectory
))
336 ERR("cannot set directory %s\n", debugstr_w(psei
->lpDirectory
));
337 ZeroMemory(&startup
,sizeof(STARTUPINFOW
));
338 startup
.cb
= sizeof(STARTUPINFOW
);
339 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
340 startup
.wShowWindow
= psei
->nShow
;
341 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, CREATE_UNICODE_ENVIRONMENT
, env
,
342 psei
->lpDirectory
&& *psei
->lpDirectory
? psei
->lpDirectory
: NULL
,
345 /* Give 30 seconds to the app to come up, if desired. Probably only needed
346 when starting app immediately before making a DDE connection. */
348 if (WaitForInputIdle( info
.hProcess
, 30000 ) == WAIT_FAILED
)
349 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
351 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
352 psei_out
->hProcess
= info
.hProcess
;
354 CloseHandle( info
.hProcess
);
355 CloseHandle( info
.hThread
);
357 else if ((retval
= GetLastError()) >= 32)
359 TRACE("CreateProcess returned error %ld\n", retval
);
360 retval
= ERROR_BAD_FORMAT
;
363 TRACE("returning %lu\n", retval
);
365 psei_out
->hInstApp
= (HINSTANCE
)retval
;
367 if( !SetCurrentDirectoryW( curdir
))
368 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
374 /***********************************************************************
375 * SHELL_BuildEnvW [Internal]
377 * Build the environment for the new process, adding the specified
378 * path to the PATH variable. Returned pointer must be freed by caller.
380 static void *SHELL_BuildEnvW( const WCHAR
*path
)
382 static const WCHAR wPath
[] = {'P','A','T','H','=',0};
383 WCHAR
*strings
, *new_env
;
385 int total
= strlenW(path
) + 1;
386 BOOL got_path
= FALSE
;
388 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
392 int len
= strlenW(p
) + 1;
393 if (!strncmpiW( p
, wPath
, 5 )) got_path
= TRUE
;
397 if (!got_path
) total
+= 5; /* we need to create PATH */
398 total
++; /* terminating null */
400 if (!(new_env
= HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) )))
402 FreeEnvironmentStringsW( strings
);
409 int len
= strlenW(p
) + 1;
410 memcpy( p2
, p
, len
* sizeof(WCHAR
) );
411 if (!strncmpiW( p
, wPath
, 5 ))
414 strcpyW( p2
+ len
, path
);
415 p2
+= strlenW(path
) + 1;
422 strcpyW( p2
, wPath
);
424 p2
+= strlenW(p2
) + 1;
427 FreeEnvironmentStringsW( strings
);
432 /***********************************************************************
433 * SHELL_TryAppPathW [Internal]
435 * Helper function for SHELL_FindExecutable
436 * @param lpResult - pointer to a buffer of size MAX_PATH
437 * On entry: szName is a filename (probably without path separators).
438 * On exit: if szName found in "App Path", place full path in lpResult, and return true
440 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
442 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',
443 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
444 static const WCHAR wPath
[] = {'P','a','t','h',0};
451 if (env
) *env
= NULL
;
452 strcpyW(buffer
, wszKeyAppPaths
);
453 strcatW(buffer
, szName
);
454 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
457 len
= MAX_PATH
*sizeof(WCHAR
);
458 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
464 DWORD count
= sizeof(buffer
);
465 if (!RegQueryValueExW(hkApp
, wPath
, NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
466 *env
= SHELL_BuildEnvW( buffer
);
470 if (hkApp
) RegCloseKey(hkApp
);
474 static UINT
SHELL_FindExecutableByOperation(LPCWSTR lpOperation
, LPWSTR key
, LPWSTR filetype
, LPWSTR command
, LONG commandlen
)
476 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
478 WCHAR verb
[MAX_PATH
];
480 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, filetype
, 0, 0x02000000, &hkeyClass
))
481 return SE_ERR_NOASSOC
;
482 if (!HCR_GetDefaultVerbW(hkeyClass
, lpOperation
, verb
, sizeof(verb
)))
483 return SE_ERR_NOASSOC
;
484 RegCloseKey(hkeyClass
);
486 /* Looking for ...buffer\shell\<verb>\command */
487 strcatW(filetype
, wszShell
);
488 strcatW(filetype
, verb
);
489 strcatW(filetype
, wCommand
);
491 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, command
,
492 &commandlen
) == ERROR_SUCCESS
)
494 commandlen
/= sizeof(WCHAR
);
495 if (key
) strcpyW(key
, filetype
);
499 LONG paramlen
= sizeof(param
);
500 static const WCHAR wSpace
[] = {' ',0};
502 /* FIXME: it seems all Windows version don't behave the same here.
503 * the doc states that this ddeexec information can be found after
505 * on Win98, it doesn't appear, but I think it does on Win2k
507 /* Get the parameters needed by the application
508 from the associated ddeexec key */
509 tmp
= strstrW(filetype
, wCommand
);
511 strcatW(filetype
, wDdeexec
);
512 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, param
,
513 ¶mlen
) == ERROR_SUCCESS
)
515 paramlen
/= sizeof(WCHAR
);
516 strcatW(command
, wSpace
);
517 strcatW(command
, param
);
518 commandlen
+= paramlen
;
522 command
[commandlen
] = '\0';
524 return 33; /* FIXME see SHELL_FindExecutable() */
527 return SE_ERR_NOASSOC
;
530 /*************************************************************************
531 * SHELL_FindExecutable [Internal]
533 * Utility for code sharing between FindExecutable and ShellExecute
535 * lpFile the name of a file
536 * lpOperation the operation on it (open)
538 * lpResult a buffer, big enough :-(, to store the command to do the
539 * operation on the file
540 * key a buffer, big enough, to get the key name to do actually the
541 * command (it'll be used afterwards for more information
544 UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
,
545 LPWSTR lpResult
, int resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
547 static const WCHAR wWindows
[] = {'w','i','n','d','o','w','s',0};
548 static const WCHAR wPrograms
[] = {'p','r','o','g','r','a','m','s',0};
549 static const WCHAR wExtensions
[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
550 WCHAR
*extension
= NULL
; /* pointer to file extension */
551 WCHAR filetype
[256]; /* registry name for this filetype */
552 LONG filetypelen
= sizeof(filetype
); /* length of above */
553 WCHAR command
[1024]; /* command from registry */
554 WCHAR wBuffer
[256]; /* Used to GetProfileString */
555 UINT retval
= SE_ERR_NOASSOC
;
556 WCHAR
*tok
; /* token pointer */
557 WCHAR xlpFile
[256]; /* result of SearchPath */
558 DWORD attribs
; /* file attributes */
560 TRACE("%s\n", debugstr_w(lpFile
));
563 return ERROR_INVALID_PARAMETER
;
566 lpResult
[0] = '\0'; /* Start off with an empty return string */
567 if (key
) *key
= '\0';
569 /* trap NULL parameters on entry */
572 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
573 debugstr_w(lpFile
), debugstr_w(lpResult
));
574 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
577 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
579 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
583 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
585 TRACE("SearchPathW returned non-zero\n");
587 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
590 attribs
= GetFileAttributesW(lpFile
);
591 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
593 strcpyW(filetype
, wszFolder
);
594 filetypelen
= 6; /* strlen("Folder") */
598 /* First thing we need is the file's extension */
599 extension
= strrchrW(xlpFile
, '.'); /* Assume last "." is the one; */
600 /* File->Run in progman uses */
602 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
604 if (extension
== NULL
|| extension
[1]==0)
606 WARN("Returning SE_ERR_NOASSOC\n");
607 return SE_ERR_NOASSOC
;
610 /* Three places to check: */
611 /* 1. win.ini, [windows], programs (NB no leading '.') */
612 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
613 /* 3. win.ini, [extensions], extension (NB no leading '.' */
614 /* All I know of the order is that registry is checked before */
615 /* extensions; however, it'd make sense to check the programs */
616 /* section first, so that's what happens here. */
618 /* See if it's a program - if GetProfileString fails, we skip this
619 * section. Actually, if GetProfileString fails, we've probably
620 * got a lot more to worry about than running a program... */
621 if (GetProfileStringW(wWindows
, wPrograms
, wExtensions
, wBuffer
, sizeof(wBuffer
)/sizeof(WCHAR
)) > 0)
628 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
632 while (*p
== ' ' || *p
== '\t') p
++;
635 if (strcmpiW(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
637 strcpyW(lpResult
, xlpFile
);
638 /* Need to perhaps check that the file has a path
640 TRACE("found %s\n", debugstr_w(lpResult
));
643 /* Greater than 32 to indicate success FIXME According to the
644 * docs, I should be returning a handle for the
645 * executable. Does this mean I'm supposed to open the
646 * executable file or something? More RTFM, I guess... */
653 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, filetype
,
654 &filetypelen
) == ERROR_SUCCESS
)
656 filetypelen
/= sizeof(WCHAR
);
657 if (filetypelen
== sizeof(filetype
)/sizeof(WCHAR
))
659 filetype
[filetypelen
] = '\0';
660 TRACE("File type: %s\n", debugstr_w(filetype
));
671 /* pass the operation string to SHELL_FindExecutableByOperation() */
672 filetype
[filetypelen
] = '\0';
673 retval
= SHELL_FindExecutableByOperation(lpOperation
, key
, filetype
, command
, sizeof(command
));
678 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
679 if (finishedLen
> resultLen
)
680 ERR("Argify buffer not large enough.. truncated\n");
682 /* Remove double quotation marks and command line arguments */
683 if (*lpResult
== '"')
686 while (*(p
+ 1) != '"')
695 /* Truncate on first space, like Windows:
696 * http://support.microsoft.com/?scid=kb%3Ben-us%3B140724
699 while (*p
!= ' ' && *p
!= '\0')
705 else /* Check win.ini */
707 static const WCHAR wExtensions
[] = {'e','x','t','e','n','s','i','o','n','s',0};
709 /* Toss the leading dot */
711 if (GetProfileStringW(wExtensions
, extension
, wszEmpty
, command
, sizeof(command
)/sizeof(WCHAR
)) > 0)
713 if (strlenW(command
) != 0)
715 strcpyW(lpResult
, command
);
716 tok
= strchrW(lpResult
, '^'); /* should be ^.extension? */
720 strcatW(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
721 tok
= strchrW(command
, '^'); /* see above */
722 if ((tok
!= NULL
) && (strlenW(tok
)>5))
724 strcatW(lpResult
, &tok
[5]);
727 retval
= 33; /* FIXME - see above */
732 TRACE("returning %s\n", debugstr_w(lpResult
));
736 /******************************************************************
739 * callback for the DDE connection. not really useful
741 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
742 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
743 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
745 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
746 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
750 /******************************************************************
753 * ShellExecute helper. Used to do an operation with a DDE connection
755 * Handles both the direct connection (try #1), and if it fails,
756 * launching an application and trying (#2) to connect to it
759 static unsigned dde_connect(WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
760 const WCHAR
* lpFile
, WCHAR
*env
,
761 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
762 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
764 static const WCHAR wApplication
[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
765 static const WCHAR wTopic
[] = {'\\','t','o','p','i','c',0};
766 WCHAR
* endkey
= key
+ strlenW(key
);
767 WCHAR app
[256], topic
[256], ifexec
[256], res
[256];
768 LONG applen
, topiclen
, ifexeclen
;
773 HSZ hszApp
, hszTopic
;
776 unsigned ret
= SE_ERR_NOASSOC
;
777 BOOL unicode
= !(GetVersion() & 0x80000000);
779 strcpyW(endkey
, wApplication
);
780 applen
= sizeof(app
);
781 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, app
, &applen
) != ERROR_SUCCESS
)
783 WCHAR command
[1024], fullpath
[MAX_PATH
];
784 static const WCHAR wSo
[] = { '.','s','o',0 };
785 int sizeSo
= sizeof(wSo
)/sizeof(WCHAR
);
789 /* Get application command from start string and find filename of application */
792 strcpyW(command
, start
+1);
793 if ((ptr
= strchrW(command
, '"')))
795 ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
)/sizeof(WCHAR
), fullpath
, &ptr
);
800 for (p
=(LPWSTR
)start
; (space
=strchrW(p
, ' ')); p
=space
+1)
802 int idx
= space
-start
;
803 memcpy(command
, start
, idx
*sizeof(WCHAR
));
805 if ((ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
)/sizeof(WCHAR
), fullpath
, &ptr
)))
809 ret
= SearchPathW(NULL
, start
, wszExe
, sizeof(fullpath
)/sizeof(WCHAR
), fullpath
, &ptr
);
814 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
815 return ERROR_ACCESS_DENIED
;
819 /* Remove extensions (including .so) */
820 ptr
= app
+ strlenW(app
) - (sizeSo
-1);
821 if (strlenW(app
) >= sizeSo
&&
825 ptr
= strrchrW(app
, '.');
830 strcpyW(endkey
, wTopic
);
831 topiclen
= sizeof(topic
);
832 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, topic
, &topiclen
) != ERROR_SUCCESS
)
834 static const WCHAR wSystem
[] = {'S','y','s','t','e','m',0};
835 strcpyW(topic
, wSystem
);
840 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
845 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
849 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
850 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
852 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
856 static const WCHAR wIfexec
[] = {'\\','i','f','e','x','e','c',0};
857 TRACE("Launching %s\n", debugstr_w(start
));
858 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
861 TRACE("Couldn't launch\n");
864 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
867 TRACE("Couldn't connect. ret=%d\n", ret
);
868 DdeUninitialize(ddeInst
);
869 SetLastError(ERROR_DDE_FAIL
);
870 return 30; /* whatever */
872 strcpyW(endkey
, wIfexec
);
873 ifexeclen
= sizeof(ifexec
);
874 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
880 SHELL_ArgifyW(res
, sizeof(res
)/sizeof(WCHAR
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
881 if (resultLen
> sizeof(res
)/sizeof(WCHAR
))
882 ERR("Argify buffer not large enough, truncated\n");
883 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
885 /* It's documented in the KB 330337 that IE has a bug and returns
886 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
889 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0,
890 XTYP_EXECUTE
, 30000, &tid
);
893 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
894 char *resA
= HeapAlloc(GetProcessHeap(), 0, lenA
);
895 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
896 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
897 XTYP_EXECUTE
, 10000, &tid
);
898 HeapFree(GetProcessHeap(), 0, resA
);
901 DdeFreeDataHandle(hDdeData
);
903 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
906 DdeDisconnect(hConv
);
909 DdeUninitialize(ddeInst
);
914 /*************************************************************************
915 * execute_from_key [Internal]
917 static UINT_PTR
execute_from_key(LPWSTR key
, LPCWSTR lpFile
, WCHAR
*env
, LPCWSTR szCommandline
,
918 LPCWSTR executable_name
,
919 SHELL_ExecuteW32 execfunc
,
920 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
922 static const WCHAR wCommand
[] = {'c','o','m','m','a','n','d',0};
923 static const WCHAR wDdeexec
[] = {'d','d','e','e','x','e','c',0};
924 WCHAR cmd
[256], param
[1024], ddeexec
[256];
925 LONG cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
926 UINT_PTR retval
= SE_ERR_NOASSOC
;
930 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
931 debugstr_w(szCommandline
), debugstr_w(executable_name
));
936 /* Get the application from the registry */
937 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, &cmdlen
) == ERROR_SUCCESS
)
939 TRACE("got cmd: %s\n", debugstr_w(cmd
));
941 /* Is there a replace() function anywhere? */
942 cmdlen
/= sizeof(WCHAR
);
944 SHELL_ArgifyW(param
, sizeof(param
)/sizeof(WCHAR
), cmd
, lpFile
, psei
->lpIDList
, szCommandline
, &resultLen
);
945 if (resultLen
> sizeof(param
)/sizeof(WCHAR
))
946 ERR("Argify buffer not large enough, truncating\n");
949 /* Get the parameters needed by the application
950 from the associated ddeexec key */
951 tmp
= strstrW(key
, wCommand
);
953 strcpyW(tmp
, wDdeexec
);
955 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, &ddeexeclen
) == ERROR_SUCCESS
)
957 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
958 if (!param
[0]) strcpyW(param
, executable_name
);
959 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, psei
->lpIDList
, execfunc
, psei
, psei_out
);
963 TRACE("executing: %s\n", debugstr_w(param
));
964 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
967 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
972 /*************************************************************************
973 * FindExecutableA [SHELL32.@]
975 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
978 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
979 WCHAR wResult
[MAX_PATH
];
981 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
982 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
984 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
985 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
987 SHFree( wDirectory
);
989 TRACE("returning %s\n", lpResult
);
993 /*************************************************************************
994 * FindExecutableW [SHELL32.@]
996 * This function returns the executable associated with the specified file
997 * for the default verb.
1000 * lpFile [I] The file to find the association for. This must refer to
1001 * an existing file otherwise FindExecutable fails and returns
1003 * lpResult [O] Points to a buffer into which the executable path is
1004 * copied. This parameter must not be NULL otherwise
1005 * FindExecutable() segfaults. The buffer must be of size at
1006 * least MAX_PATH characters.
1009 * A value greater than 32 on success, less than or equal to 32 otherwise.
1010 * See the SE_ERR_* constants.
1013 * On Windows XP and 2003, FindExecutable() seems to first convert the
1014 * filename into 8.3 format, thus taking into account only the first three
1015 * characters of the extension, and expects to find an association for those.
1016 * However other Windows versions behave sanely.
1018 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
1020 UINT_PTR retval
= SE_ERR_NOASSOC
;
1021 WCHAR old_dir
[1024];
1023 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
1025 lpResult
[0] = '\0'; /* Start off with an empty return string */
1027 return (HINSTANCE
)SE_ERR_FNF
;
1031 GetCurrentDirectoryW(sizeof(old_dir
)/sizeof(WCHAR
), old_dir
);
1032 SetCurrentDirectoryW(lpDirectory
);
1035 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, lpResult
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1037 TRACE("returning %s\n", debugstr_w(lpResult
));
1039 SetCurrentDirectoryW(old_dir
);
1040 return (HINSTANCE
)retval
;
1043 /* FIXME: is this already implemented somewhere else? */
1044 static HKEY
ShellExecute_GetClassKey( LPSHELLEXECUTEINFOW sei
)
1046 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1048 DWORD type
= 0, sz
= 0;
1052 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1053 return sei
->hkeyClass
;
1055 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1056 lpClass
= sei
->lpClass
;
1059 ext
= PathFindExtensionW( sei
->lpFile
);
1060 TRACE("ext = %s\n", debugstr_w( ext
) );
1064 r
= RegOpenKeyW( HKEY_CLASSES_ROOT
, ext
, &hkey
);
1065 if (r
!= ERROR_SUCCESS
)
1068 r
= RegQueryValueExW( hkey
, NULL
, 0, &type
, NULL
, &sz
);
1069 if ( r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1071 sz
+= sizeof (WCHAR
);
1072 cls
= HeapAlloc( GetProcessHeap(), 0, sz
);
1074 RegQueryValueExW( hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1077 RegCloseKey( hkey
);
1081 TRACE("class = %s\n", debugstr_w(lpClass
) );
1085 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1087 HeapFree( GetProcessHeap(), 0, cls
);
1092 static IDataObject
*shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
)
1094 LPCITEMIDLIST pidllast
= NULL
;
1095 IDataObject
*dataobj
= NULL
;
1096 IShellFolder
*shf
= NULL
;
1097 LPITEMIDLIST pidl
= NULL
;
1100 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1101 pidl
= sei
->lpIDList
;
1104 WCHAR fullpath
[MAX_PATH
];
1107 r
= GetFullPathNameW( sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1111 pidl
= ILCreateFromPathW( fullpath
);
1114 r
= SHBindToParent( pidl
, &IID_IShellFolder
, (LPVOID
*)&shf
, &pidllast
);
1118 IShellFolder_GetUIObjectOf( shf
, NULL
, 1, &pidllast
,
1119 &IID_IDataObject
, NULL
, (LPVOID
*) &dataobj
);
1122 if ( pidl
!= sei
->lpIDList
)
1125 IShellFolder_Release( shf
);
1129 static HRESULT
shellex_run_context_menu_default( IShellExtInit
*obj
,
1130 LPSHELLEXECUTEINFOW sei
)
1132 IContextMenu
*cm
= NULL
;
1133 CMINVOKECOMMANDINFOEX ici
;
1140 TRACE("%p %p\n", obj
, sei
);
1142 r
= IShellExtInit_QueryInterface( obj
, &IID_IContextMenu
, (LPVOID
*) &cm
);
1146 hmenu
= CreateMenu();
1150 /* the number of the last menu added is returned in r */
1151 r
= IContextMenu_QueryContextMenu( cm
, hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1155 n
= GetMenuItemCount( hmenu
);
1156 for ( i
= 0; i
< n
; i
++ )
1158 memset( &info
, 0, sizeof info
);
1159 info
.cbSize
= sizeof info
;
1160 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1161 info
.dwTypeData
= string
;
1162 info
.cch
= sizeof string
;
1164 GetMenuItemInfoW( hmenu
, i
, TRUE
, &info
);
1166 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1167 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1168 if ( ( !sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
) ) ||
1169 ( sei
->lpVerb
&& !lstrcmpiW( sei
->lpVerb
, string
) ) )
1180 memset( &ici
, 0, sizeof ici
);
1181 ici
.cbSize
= sizeof ici
;
1182 ici
.fMask
= CMIC_MASK_UNICODE
;
1183 ici
.nShow
= sei
->nShow
;
1184 ici
.lpVerb
= MAKEINTRESOURCEA( def
);
1185 ici
.hwnd
= sei
->hwnd
;
1186 ici
.lpParametersW
= sei
->lpParameters
;
1188 r
= IContextMenu_InvokeCommand( cm
, (LPCMINVOKECOMMANDINFO
) &ici
);
1190 TRACE("invoke command returned %08x\n", r
);
1194 DestroyMenu( hmenu
);
1196 IContextMenu_Release( cm
);
1200 static HRESULT
shellex_load_object_and_run( HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1202 IDataObject
*dataobj
= NULL
;
1203 IObjectWithSite
*ows
= NULL
;
1204 IShellExtInit
*obj
= NULL
;
1207 TRACE("%p %s %p\n", hkey
, debugstr_guid( guid
), sei
);
1209 r
= CoInitialize( NULL
);
1213 r
= CoCreateInstance( guid
, NULL
, CLSCTX_INPROC_SERVER
,
1214 &IID_IShellExtInit
, (LPVOID
*)&obj
);
1217 ERR("failed %08x\n", r
);
1221 dataobj
= shellex_get_dataobj( sei
);
1224 ERR("failed to get data object\n");
1228 r
= IShellExtInit_Initialize( obj
, NULL
, dataobj
, hkey
);
1232 r
= IShellExtInit_QueryInterface( obj
, &IID_IObjectWithSite
, (LPVOID
*) &ows
);
1236 IObjectWithSite_SetSite( ows
, NULL
);
1238 r
= shellex_run_context_menu_default( obj
, sei
);
1242 IObjectWithSite_Release( ows
);
1244 IDataObject_Release( dataobj
);
1246 IShellExtInit_Release( obj
);
1252 /*************************************************************************
1253 * ShellExecute_FromContextMenu [Internal]
1255 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1257 static const WCHAR szcm
[] = { 's','h','e','l','l','e','x','\\',
1258 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1259 HKEY hkey
, hkeycm
= 0;
1266 TRACE("%s\n", debugstr_w(sei
->lpFile
) );
1268 hkey
= ShellExecute_GetClassKey( sei
);
1270 return ERROR_FUNCTION_FAILED
;
1272 r
= RegOpenKeyW( hkey
, szcm
, &hkeycm
);
1273 if ( r
== ERROR_SUCCESS
)
1278 r
= RegEnumKeyW( hkeycm
, i
++, szguid
, 39 );
1279 if ( r
!= ERROR_SUCCESS
)
1282 hr
= CLSIDFromString( szguid
, &guid
);
1285 /* stop at the first one that succeeds in running */
1286 hr
= shellex_load_object_and_run( hkey
, &guid
, sei
);
1287 if ( SUCCEEDED( hr
) )
1291 RegCloseKey( hkeycm
);
1294 if ( hkey
!= sei
->hkeyClass
)
1295 RegCloseKey( hkey
);
1299 /*************************************************************************
1300 * SHELL_execute [Internal]
1302 BOOL
SHELL_execute( LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1304 static const WCHAR wQuote
[] = {'"',0};
1305 static const WCHAR wSpace
[] = {' ',0};
1306 static const WCHAR wWww
[] = {'w','w','w',0};
1307 static const WCHAR wFile
[] = {'f','i','l','e',0};
1308 static const WCHAR wHttp
[] = {'h','t','t','p',':','/','/',0};
1309 static const WCHAR wExplorer
[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1310 static const DWORD unsupportedFlags
=
1311 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1312 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
| SEE_MASK_FLAG_NO_UI
|
1313 SEE_MASK_UNICODE
| SEE_MASK_NO_CONSOLE
| SEE_MASK_ASYNCOK
|
1316 WCHAR
*wszApplicationName
, wszParameters
[1024], wszDir
[MAX_PATH
];
1317 DWORD dwApplicationNameLen
= MAX_PATH
+2;
1319 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1320 WCHAR wfileName
[MAX_PATH
];
1322 WCHAR lpstrProtocol
[256];
1324 UINT_PTR retval
= SE_ERR_NOASSOC
;
1326 WCHAR buffer
[MAX_PATH
];
1328 BOOL appKnownSingular
= FALSE
;
1330 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1331 memcpy(&sei_tmp
, sei
, sizeof(sei_tmp
));
1333 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1334 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1335 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1336 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1337 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1338 debugstr_w(sei_tmp
.lpClass
) : "not used");
1340 sei
->hProcess
= NULL
;
1342 /* make copies of all path/command strings */
1343 if (!sei_tmp
.lpFile
)
1345 wszApplicationName
= HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
*sizeof(WCHAR
));
1346 *wszApplicationName
= '\0';
1348 else if (*sei_tmp
.lpFile
== '\"')
1350 DWORD l
= strlenW(sei_tmp
.lpFile
+1);
1351 if(l
>= dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1352 wszApplicationName
= HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
*sizeof(WCHAR
));
1353 memcpy(wszApplicationName
, sei_tmp
.lpFile
+1, (l
+1)*sizeof(WCHAR
));
1354 if (wszApplicationName
[l
-1] == '\"')
1355 wszApplicationName
[l
-1] = '\0';
1356 appKnownSingular
= TRUE
;
1357 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName
));
1359 DWORD l
= strlenW(sei_tmp
.lpFile
)+1;
1360 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1361 wszApplicationName
= HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
*sizeof(WCHAR
));
1362 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
*sizeof(WCHAR
));
1365 if (sei_tmp
.lpParameters
)
1366 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1368 *wszParameters
= '\0';
1370 if (sei_tmp
.lpDirectory
)
1371 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1375 /* adjust string pointers to point to the new buffers */
1376 sei_tmp
.lpFile
= wszApplicationName
;
1377 sei_tmp
.lpParameters
= wszParameters
;
1378 sei_tmp
.lpDirectory
= wszDir
;
1380 if (sei_tmp
.fMask
& unsupportedFlags
)
1382 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1385 /* process the IDList */
1386 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1388 IShellExecuteHookW
* pSEH
;
1390 HRESULT hr
= SHBindToParent(sei_tmp
.lpIDList
, &IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1394 hr
= IShellExecuteHookW_Execute(pSEH
, &sei_tmp
);
1396 IShellExecuteHookW_Release(pSEH
);
1399 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1404 SHGetPathFromIDListW(sei_tmp
.lpIDList
, wszApplicationName
);
1405 appKnownSingular
= TRUE
;
1406 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1409 if ( ERROR_SUCCESS
== ShellExecute_FromContextMenu( &sei_tmp
) )
1411 sei
->hInstApp
= (HINSTANCE
) 33;
1412 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1416 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1418 /* launch a document by fileclass like 'WordPad.Document.1' */
1419 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1420 /* FIXME: szCommandline should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1421 ULONG cmask
=(sei_tmp
.fMask
& SEE_MASK_CLASSALL
);
1423 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? sei_tmp
.hkeyClass
: NULL
,
1424 (cmask
== SEE_MASK_CLASSNAME
) ? sei_tmp
.lpClass
: NULL
,
1426 wszParameters
, sizeof(wszParameters
)/sizeof(WCHAR
));
1428 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1429 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(wszParameters
), debugstr_w(wszApplicationName
));
1432 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
)/sizeof(WCHAR
), wszParameters
, wszApplicationName
, sei_tmp
.lpIDList
, NULL
, &resultLen
);
1433 if (!done
&& wszApplicationName
[0])
1435 strcatW(wcmd
, wSpace
);
1436 strcatW(wcmd
, wszApplicationName
);
1438 if (resultLen
> sizeof(wcmd
)/sizeof(WCHAR
))
1439 ERR("Argify buffer not large enough... truncating\n");
1440 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1442 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1446 /* Has the IDList not yet been translated? */
1447 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1449 /* last chance to translate IDList: now also allow CLSID paths */
1450 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW(sei_tmp
.lpIDList
, buffer
, sizeof(buffer
)))) {
1451 if (buffer
[0]==':' && buffer
[1]==':') {
1452 /* open shell folder for the specified class GUID */
1453 strcpyW(wszParameters
, buffer
);
1454 strcpyW(wszApplicationName
, wExplorer
);
1455 appKnownSingular
= TRUE
;
1457 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1459 WCHAR target
[MAX_PATH
];
1462 /* Check if we're executing a directory and if so use the
1463 handler for the Folder class */
1464 strcpyW(target
, buffer
);
1465 attribs
= GetFileAttributesW(buffer
);
1466 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1467 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1468 HCR_GetExecuteCommandW(0, wszFolder
,
1470 buffer
, sizeof(buffer
))) {
1471 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1472 buffer
, target
, sei_tmp
.lpIDList
, NULL
, &resultLen
);
1473 if (resultLen
> dwApplicationNameLen
)
1474 ERR("Argify buffer not large enough... truncating\n");
1475 appKnownSingular
= FALSE
;
1477 sei_tmp
.fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1482 /* expand environment strings */
1483 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1487 buf
= HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
1489 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+1);
1490 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1491 dwApplicationNameLen
= len
+1;
1492 wszApplicationName
= buf
;
1493 /* appKnownSingular unmodified */
1495 sei_tmp
.lpFile
= wszApplicationName
;
1498 if (*sei_tmp
.lpParameters
)
1500 len
= ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, NULL
, 0);
1505 buf
= HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
1506 ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buf
, len
);
1508 ERR("Parameters exceeds buffer size (%i > 1024)\n",len
);
1509 lstrcpynW(wszParameters
, buf
, min(1024,len
));
1510 HeapFree(GetProcessHeap(),0,buf
);
1514 if (*sei_tmp
.lpDirectory
)
1516 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1521 buf
= HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
1522 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1524 ERR("Directory exceeds buffer size (%i > 1024)\n",len
);
1525 lstrcpynW(wszDir
, buf
, min(1024,len
));
1526 HeapFree(GetProcessHeap(),0,buf
);
1530 /* Else, try to execute the filename */
1531 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1533 /* separate out command line arguments from executable file name */
1534 if (!*sei_tmp
.lpParameters
&& !appKnownSingular
) {
1535 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1536 if (sei_tmp
.lpFile
[0] == '"') {
1537 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1538 LPWSTR dst
= wfileName
;
1541 /* copy the unquoted executable path to 'wfileName' */
1542 while(*src
&& *src
!='"')
1550 while(isspace(*src
))
1555 /* copy the parameter string to 'wszParameters' */
1556 strcpyW(wszParameters
, src
);
1558 /* terminate previous command string after the quote character */
1563 /* If the executable name is not quoted, we have to use this search loop here,
1564 that in CreateProcess() is not sufficient because it does not handle shell links. */
1565 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1568 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1569 for(s
=beg
; (space
=strchrW(s
, ' ')); s
=space
+1) {
1570 int idx
= space
-sei_tmp
.lpFile
;
1571 memcpy(buffer
, sei_tmp
.lpFile
, idx
* sizeof(WCHAR
));
1574 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1575 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
), xlpFile
, NULL
))
1577 /* separate out command from parameter string */
1578 LPCWSTR p
= space
+ 1;
1583 strcpyW(wszParameters
, p
);
1590 strcpyW(wfileName
, sei_tmp
.lpFile
);
1593 strcpyW(wfileName
, sei_tmp
.lpFile
);
1597 strcpyW(wcmd
, wszApplicationName
);
1598 if (sei_tmp
.lpParameters
[0]) {
1599 strcatW(wcmd
, wSpace
);
1600 strcatW(wcmd
, wszParameters
);
1603 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1605 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1609 /* Else, try to find the executable */
1611 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, 1024, lpstrProtocol
, &env
, sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1612 if (retval
> 32) /* Found */
1614 WCHAR wszQuotedCmd
[MAX_PATH
+2];
1615 /* Must quote to handle case where cmd contains spaces,
1616 * else security hole if malicious user creates executable file "C:\\Program"
1618 strcpyW(wszQuotedCmd
, wQuote
);
1619 strcatW(wszQuotedCmd
, wcmd
);
1620 strcatW(wszQuotedCmd
, wQuote
);
1621 if (wszParameters
[0]) {
1622 strcatW(wszQuotedCmd
, wSpace
);
1623 strcatW(wszQuotedCmd
, wszParameters
);
1625 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(sei_tmp
.lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(lpstrProtocol
));
1627 retval
= execute_from_key(lpstrProtocol
, wszApplicationName
, env
, sei_tmp
.lpParameters
, wcmd
, execfunc
, &sei_tmp
, sei
);
1629 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, &sei_tmp
, sei
);
1630 HeapFree( GetProcessHeap(), 0, env
);
1632 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1634 static const WCHAR wShell
[] = {'\\','s','h','e','l','l','\\',0};
1635 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
1639 lpstrRes
= strchrW(lpFile
, ':');
1641 iSize
= lpstrRes
- lpFile
;
1643 iSize
= strlenW(lpFile
);
1645 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1646 /* Looking for ...protocol\shell\lpOperation\command */
1647 memcpy(lpstrProtocol
, lpFile
, iSize
*sizeof(WCHAR
));
1648 lpstrProtocol
[iSize
] = '\0';
1649 strcatW(lpstrProtocol
, wShell
);
1650 strcatW(lpstrProtocol
, sei_tmp
.lpVerb
? sei_tmp
.lpVerb
: wszOpen
);
1651 strcatW(lpstrProtocol
, wCommand
);
1653 /* Remove File Protocol from lpFile */
1654 /* In the case file://path/file */
1655 if (!strncmpiW(lpFile
, wFile
, iSize
))
1658 while (*lpFile
== ':') lpFile
++;
1660 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, sei_tmp
.lpParameters
, wcmd
, execfunc
, &sei_tmp
, sei
);
1662 /* Check if file specified is in the form www.??????.*** */
1663 else if (!strncmpiW(lpFile
, wWww
, 3))
1665 /* if so, append lpFile http:// and call ShellExecute */
1666 WCHAR lpstrTmpFile
[256];
1667 strcpyW(lpstrTmpFile
, wHttp
);
1668 strcatW(lpstrTmpFile
, lpFile
);
1669 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1672 TRACE("retval %lu\n", retval
);
1674 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1676 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1680 /*************************************************************************
1681 * ShellExecuteA [SHELL32.290]
1683 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
1684 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
)
1686 SHELLEXECUTEINFOA sei
;
1688 TRACE("%p,%s,%s,%s,%s,%d\n",
1689 hWnd
, debugstr_a(lpOperation
), debugstr_a(lpFile
),
1690 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
1692 sei
.cbSize
= sizeof(sei
);
1695 sei
.lpVerb
= lpOperation
;
1696 sei
.lpFile
= lpFile
;
1697 sei
.lpParameters
= lpParameters
;
1698 sei
.lpDirectory
= lpDirectory
;
1699 sei
.nShow
= iShowCmd
;
1706 ShellExecuteExA (&sei
);
1707 return sei
.hInstApp
;
1710 /*************************************************************************
1711 * ShellExecuteExA [SHELL32.292]
1714 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1716 SHELLEXECUTEINFOW seiW
;
1718 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1722 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1725 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1728 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1730 if (sei
->lpParameters
)
1731 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1733 if (sei
->lpDirectory
)
1734 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1736 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
1737 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1739 seiW
.lpClass
= NULL
;
1741 ret
= SHELL_execute( &seiW
, SHELL_ExecuteW
);
1743 sei
->hInstApp
= seiW
.hInstApp
;
1745 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
1746 sei
->hProcess
= seiW
.hProcess
;
1750 SHFree(wParameters
);
1757 /*************************************************************************
1758 * ShellExecuteExW [SHELL32.293]
1761 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1763 return SHELL_execute( sei
, SHELL_ExecuteW
);
1766 /*************************************************************************
1767 * ShellExecuteW [SHELL32.294]
1769 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1770 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1772 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1773 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1775 SHELLEXECUTEINFOW sei
;
1778 sei
.cbSize
= sizeof(sei
);
1781 sei
.lpVerb
= lpOperation
;
1782 sei
.lpFile
= lpFile
;
1783 sei
.lpParameters
= lpParameters
;
1784 sei
.lpDirectory
= lpDirectory
;
1785 sei
.nShow
= nShowCmd
;
1792 SHELL_execute( &sei
, SHELL_ExecuteW
);
1793 return sei
.hInstApp
;
1796 /*************************************************************************
1797 * OpenAs_RunDLLA [SHELL32.@]
1799 void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
1801 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
1804 /*************************************************************************
1805 * OpenAs_RunDLLW [SHELL32.@]
1807 void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
1809 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);