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
24 WINE_DEFAULT_DEBUG_CHANNEL(exec
);
26 static const WCHAR wszOpen
[] = {'o','p','e','n',0};
27 static const WCHAR wszExe
[] = {'.','e','x','e',0};
28 static const WCHAR wszILPtr
[] = {':','%','p',0};
29 static const WCHAR wszShell
[] = {'\\','s','h','e','l','l','\\',0};
30 static const WCHAR wszFolder
[] = {'F','o','l','d','e','r',0};
31 static const WCHAR wszEmpty
[] = {0};
33 #define SEE_MASK_CLASSALL (SEE_MASK_CLASSNAME | SEE_MASK_CLASSKEY)
36 /***********************************************************************
37 * SHELL_ArgifyW [Internal]
39 * this function is supposed to expand the escape sequences found in the registry
40 * some diving reported that the following were used:
41 * + %1, %2... seem to report to parameter of index N in ShellExecute pmts
46 * %I address of a global item ID (explorer switch /idlist)
47 * %L seems to be %1 as long filename followed by the 8+3 variation
49 * %* all following parameters (see batfile)
52 static BOOL
SHELL_ArgifyW(WCHAR
* out
, DWORD len
, const WCHAR
* fmt
, const WCHAR
* lpFile
, LPITEMIDLIST pidl
, LPCWSTR args
, DWORD
* out_len
)
56 BOOL found_p1
= FALSE
;
61 TRACE("%p, %d, %s, %s, %p, %p\n", out
, len
, debugstr_w(fmt
),
62 debugstr_w(lpFile
), pidl
, args
);
109 while(*args
&& !isspace(*args
))
118 while(isspace(*args
))
128 if (!done
|| (*fmt
== '1'))
130 /*FIXME Is the call to SearchPathW() really needed? We already have separated out the parameter string in args. */
131 if (SearchPathW(NULL
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
147 * IE uses this a lot for activating things such as windows media
148 * player. This is not verified to be fully correct but it appears
154 used
+= wcslen(lpFile
);
158 res
+= wcslen(lpFile
);
168 /* %p should not exceed 8, maybe 16 when looking forward to 64bit.
169 * allowing a buffer of 100 should more than exceed all needs */
172 HGLOBAL hmem
= SHAllocShared(pidl
, ILGetSize(pidl
), 0);
173 pv
= SHLockShared(hmem
, 0);
174 chars
= swprintf(buf
, wszILPtr
, pv
);
175 if (chars
>= sizeof(buf
)/sizeof(WCHAR
))
176 ERR("pidl format buffer too small!\n");
190 * Check if this is an env-variable here...
193 /* Make sure that we have at least one more %.*/
194 if (strchrW(fmt
, '%'))
196 WCHAR tmpBuffer
[1024];
197 PWSTR tmpB
= tmpBuffer
;
198 WCHAR tmpEnvBuff
[MAX_PATH
];
205 TRACE("Checking %s to be an env-var\n", debugstr_w(tmpBuffer
));
207 envRet
= GetEnvironmentVariableW(tmpBuffer
, tmpEnvBuff
, MAX_PATH
);
208 if (envRet
== 0 || envRet
> MAX_PATH
)
210 used
+= wcslen(tmpBuffer
);
213 wcscpy( res
, tmpBuffer
);
214 res
+= wcslen(tmpBuffer
);
219 used
+= wcslen(tmpEnvBuff
);
222 wcscpy( res
, tmpEnvBuff
);
223 res
+= wcslen(tmpEnvBuff
);
230 /* Don't skip past terminator (catch a single '%' at the end) */
247 TRACE("used %i of %i space\n",used
,len
);
254 static HRESULT
SHELL_GetPathFromIDListForExecuteW(LPCITEMIDLIST pidl
, LPWSTR pszPath
, UINT uOutSize
)
257 IShellFolder
* desktop
;
259 HRESULT hr
= SHGetDesktopFolder(&desktop
);
262 hr
= desktop
->GetDisplayNameOf(pidl
, SHGDN_FORPARSING
, &strret
);
265 StrRetToStrNW(pszPath
, uOutSize
, &strret
, pidl
);
273 /*************************************************************************
274 * SHELL_ExecuteW [Internal]
277 static UINT_PTR
SHELL_ExecuteW(const WCHAR
*lpCmd
, WCHAR
*env
, BOOL shWait
,
278 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
280 STARTUPINFOW startup
;
281 PROCESS_INFORMATION info
;
282 UINT_PTR retval
= SE_ERR_NOASSOC
;
284 WCHAR curdir
[MAX_PATH
];
285 DWORD dwCreationFlags
;
286 const WCHAR
*lpDirectory
= NULL
;
288 TRACE("Execute %s from directory %s\n", debugstr_w(lpCmd
), debugstr_w(psei
->lpDirectory
));
290 /* make sure we don't fail the CreateProcess if the calling app passes in
291 * a bad working directory */
292 if (psei
->lpDirectory
&& psei
->lpDirectory
[0])
294 DWORD attr
= GetFileAttributesW(psei
->lpDirectory
);
295 if (attr
!= INVALID_FILE_ATTRIBUTES
&& attr
& FILE_ATTRIBUTE_DIRECTORY
)
296 lpDirectory
= psei
->lpDirectory
;
299 /* ShellExecute specifies the command from psei->lpDirectory
300 * if present. Not from the current dir as CreateProcess does */
302 if( ( gcdret
= GetCurrentDirectoryW( MAX_PATH
, curdir
)))
303 if( !SetCurrentDirectoryW( lpDirectory
))
304 ERR("cannot set directory %s\n", debugstr_w(lpDirectory
));
305 ZeroMemory(&startup
,sizeof(STARTUPINFOW
));
306 startup
.cb
= sizeof(STARTUPINFOW
);
307 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
308 startup
.wShowWindow
= psei
->nShow
;
309 dwCreationFlags
= CREATE_UNICODE_ENVIRONMENT
;
310 if (psei
->fMask
& SEE_MASK_NO_CONSOLE
)
311 dwCreationFlags
|= CREATE_NEW_CONSOLE
;
312 if (CreateProcessW(NULL
, (LPWSTR
)lpCmd
, NULL
, NULL
, FALSE
, dwCreationFlags
, env
,
313 lpDirectory
, &startup
, &info
))
315 /* Give 30 seconds to the app to come up, if desired. Probably only needed
316 when starting app immediately before making a DDE connection. */
318 if (WaitForInputIdle( info
.hProcess
, 30000 ) == WAIT_FAILED
)
319 WARN("WaitForInputIdle failed: Error %d\n", GetLastError() );
321 if (psei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
322 psei_out
->hProcess
= info
.hProcess
;
324 CloseHandle( info
.hProcess
);
325 CloseHandle( info
.hThread
);
327 else if ((retval
= GetLastError()) >= 32)
329 TRACE("CreateProcess returned error %ld\n", retval
);
330 retval
= ERROR_BAD_FORMAT
;
333 TRACE("returning %lu\n", retval
);
335 psei_out
->hInstApp
= (HINSTANCE
)retval
;
337 if( !SetCurrentDirectoryW( curdir
))
338 ERR("cannot return to directory %s\n", debugstr_w(curdir
));
344 /***********************************************************************
345 * SHELL_BuildEnvW [Internal]
347 * Build the environment for the new process, adding the specified
348 * path to the PATH variable. Returned pointer must be freed by caller.
350 static LPWSTR
SHELL_BuildEnvW( const WCHAR
*path
)
352 static const WCHAR wPath
[] = {'P','A','T','H','=',0};
353 WCHAR
*strings
, *new_env
;
355 int total
= wcslen(path
) + 1;
356 BOOL got_path
= FALSE
;
358 if (!(strings
= GetEnvironmentStringsW())) return NULL
;
362 int len
= wcslen(p
) + 1;
363 if (!_wcsnicmp( p
, wPath
, 5 )) got_path
= TRUE
;
367 if (!got_path
) total
+= 5; /* we need to create PATH */
368 total
++; /* terminating null */
370 if (!(new_env
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0, total
* sizeof(WCHAR
) )))
372 FreeEnvironmentStringsW( strings
);
379 int len
= wcslen(p
) + 1;
380 memcpy( p2
, p
, len
* sizeof(WCHAR
) );
381 if (!_wcsnicmp( p
, wPath
, 5 ))
384 wcscpy( p2
+ len
, path
);
385 p2
+= wcslen(path
) + 1;
394 p2
+= wcslen(p2
) + 1;
397 FreeEnvironmentStringsW( strings
);
402 /***********************************************************************
403 * SHELL_TryAppPathW [Internal]
405 * Helper function for SHELL_FindExecutable
406 * @param lpResult - pointer to a buffer of size MAX_PATH
407 * On entry: szName is a filename (probably without path separators).
408 * On exit: if szName found in "App Path", place full path in lpResult, and return true
410 static BOOL
SHELL_TryAppPathW( LPCWSTR szName
, LPWSTR lpResult
, WCHAR
**env
)
412 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',
413 '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','A','p','p',' ','P','a','t','h','s','\\',0};
414 static const WCHAR wPath
[] = {'P','a','t','h',0};
421 if (env
) *env
= NULL
;
422 wcscpy(buffer
, wszKeyAppPaths
);
423 wcscat(buffer
, szName
);
424 res
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, buffer
, 0, KEY_READ
, &hkApp
);
427 len
= MAX_PATH
*sizeof(WCHAR
);
428 res
= RegQueryValueW(hkApp
, NULL
, lpResult
, &len
);
434 DWORD count
= sizeof(buffer
);
435 if (!RegQueryValueExW(hkApp
, wPath
, NULL
, NULL
, (LPBYTE
)buffer
, &count
) && buffer
[0])
436 *env
= SHELL_BuildEnvW( buffer
);
440 if (hkApp
) RegCloseKey(hkApp
);
444 static UINT
SHELL_FindExecutableByOperation(LPCWSTR lpOperation
, LPWSTR key
, LPWSTR filetype
, LPWSTR command
, LONG commandlen
)
446 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
448 WCHAR verb
[MAX_PATH
];
450 if (RegOpenKeyExW(HKEY_CLASSES_ROOT
, filetype
, 0, 0x02000000, &hkeyClass
))
451 return SE_ERR_NOASSOC
;
452 if (!HCR_GetDefaultVerbW(hkeyClass
, lpOperation
, verb
, sizeof(verb
)/sizeof(verb
[0])))
453 return SE_ERR_NOASSOC
;
454 RegCloseKey(hkeyClass
);
456 /* Looking for ...buffer\shell\<verb>\command */
457 wcscat(filetype
, wszShell
);
458 wcscat(filetype
, verb
);
459 wcscat(filetype
, wCommand
);
461 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, command
,
462 &commandlen
) == ERROR_SUCCESS
)
464 commandlen
/= sizeof(WCHAR
);
465 if (key
) wcscpy(key
, filetype
);
469 LONG paramlen
= sizeof(param
);
470 static const WCHAR wSpace
[] = {' ',0};
472 /* FIXME: it seems all Windows version don't behave the same here.
473 * the doc states that this ddeexec information can be found after
475 * on Win98, it doesn't appear, but I think it does on Win2k
477 /* Get the parameters needed by the application
478 from the associated ddeexec key */
479 tmp
= strstrW(filetype
, wCommand
);
481 wcscat(filetype
, wDdeexec
);
482 if (RegQueryValueW(HKEY_CLASSES_ROOT
, filetype
, param
,
483 ¶mlen
) == ERROR_SUCCESS
)
485 paramlen
/= sizeof(WCHAR
);
486 wcscat(command
, wSpace
);
487 wcscat(command
, param
);
488 commandlen
+= paramlen
;
492 command
[commandlen
] = '\0';
494 return 33; /* FIXME see SHELL_FindExecutable() */
497 return SE_ERR_NOASSOC
;
500 /*************************************************************************
501 * SHELL_FindExecutable [Internal]
503 * Utility for code sharing between FindExecutable and ShellExecute
505 * lpFile the name of a file
506 * lpOperation the operation on it (open)
508 * lpResult a buffer, big enough :-(, to store the command to do the
509 * operation on the file
510 * key a buffer, big enough, to get the key name to do actually the
511 * command (it'll be used afterwards for more information
514 static UINT
SHELL_FindExecutable(LPCWSTR lpPath
, LPCWSTR lpFile
, LPCWSTR lpOperation
,
515 LPWSTR lpResult
, DWORD resultLen
, LPWSTR key
, WCHAR
**env
, LPITEMIDLIST pidl
, LPCWSTR args
)
517 static const WCHAR wWindows
[] = {'w','i','n','d','o','w','s',0};
518 static const WCHAR wPrograms
[] = {'p','r','o','g','r','a','m','s',0};
519 static const WCHAR wExtensions
[] = {'e','x','e',' ','p','i','f',' ','b','a','t',' ','c','m','d',' ','c','o','m',0};
520 WCHAR
*extension
= NULL
; /* pointer to file extension */
521 WCHAR filetype
[256]; /* registry name for this filetype */
522 LONG filetypelen
= sizeof(filetype
); /* length of above */
523 WCHAR command
[1024]; /* command from registry */
524 WCHAR wBuffer
[256]; /* Used to GetProfileString */
525 UINT retval
= SE_ERR_NOASSOC
;
526 WCHAR
*tok
; /* token pointer */
527 WCHAR xlpFile
[256]; /* result of SearchPath */
528 DWORD attribs
; /* file attributes */
530 TRACE("%s\n", debugstr_w(lpFile
));
533 return ERROR_INVALID_PARAMETER
;
536 lpResult
[0] = '\0'; /* Start off with an empty return string */
537 if (key
) *key
= '\0';
539 /* trap NULL parameters on entry */
542 WARN("(lpFile=%s,lpResult=%s): NULL parameter\n",
543 debugstr_w(lpFile
), debugstr_w(lpResult
));
544 return ERROR_FILE_NOT_FOUND
; /* File not found. Close enough, I guess. */
547 if (SHELL_TryAppPathW( lpFile
, lpResult
, env
))
549 TRACE("found %s via App Paths\n", debugstr_w(lpResult
));
553 if (SearchPathW(lpPath
, lpFile
, wszExe
, sizeof(xlpFile
)/sizeof(WCHAR
), xlpFile
, NULL
))
555 TRACE("SearchPathW returned non-zero\n");
557 /* Hey, isn't this value ignored? Why make this call? Shouldn't we return here? --dank*/
560 attribs
= GetFileAttributesW(lpFile
);
561 if (attribs
!=INVALID_FILE_ATTRIBUTES
&& (attribs
&FILE_ATTRIBUTE_DIRECTORY
))
563 wcscpy(filetype
, wszFolder
);
564 filetypelen
= 6; /* strlen("Folder") */
568 /* Did we get something? Anything? */
571 TRACE("Returning SE_ERR_FNF\n");
574 /* First thing we need is the file's extension */
575 extension
= wcsrchr(xlpFile
, '.'); /* Assume last "." is the one; */
576 /* File->Run in progman uses */
578 TRACE("xlpFile=%s,extension=%s\n", debugstr_w(xlpFile
), debugstr_w(extension
));
580 if (extension
== NULL
|| extension
[1]==0)
582 WARN("Returning SE_ERR_NOASSOC\n");
583 return SE_ERR_NOASSOC
;
586 /* Three places to check: */
587 /* 1. win.ini, [windows], programs (NB no leading '.') */
588 /* 2. Registry, HKEY_CLASS_ROOT\<filetype>\shell\open\command */
589 /* 3. win.ini, [extensions], extension (NB no leading '.' */
590 /* All I know of the order is that registry is checked before */
591 /* extensions; however, it'd make sense to check the programs */
592 /* section first, so that's what happens here. */
594 /* See if it's a program - if GetProfileString fails, we skip this
595 * section. Actually, if GetProfileString fails, we've probably
596 * got a lot more to worry about than running a program... */
597 if (GetProfileStringW(wWindows
, wPrograms
, wExtensions
, wBuffer
, sizeof(wBuffer
)/sizeof(WCHAR
)) > 0)
604 while (*p
&& *p
!= ' ' && *p
!= '\t') p
++;
608 while (*p
== ' ' || *p
== '\t') p
++;
611 if (wcsicmp(tok
, &extension
[1]) == 0) /* have to skip the leading "." */
613 wcscpy(lpResult
, xlpFile
);
614 /* Need to perhaps check that the file has a path
616 TRACE("found %s\n", debugstr_w(lpResult
));
618 /* Greater than 32 to indicate success */
625 if (RegQueryValueW(HKEY_CLASSES_ROOT
, extension
, filetype
,
626 &filetypelen
) == ERROR_SUCCESS
)
628 filetypelen
/= sizeof(WCHAR
);
629 if (filetypelen
== sizeof(filetype
)/sizeof(WCHAR
))
631 filetype
[filetypelen
] = '\0';
632 TRACE("File type: %s\n", debugstr_w(filetype
));
643 /* pass the operation string to SHELL_FindExecutableByOperation() */
644 filetype
[filetypelen
] = '\0';
645 retval
= SHELL_FindExecutableByOperation(lpOperation
, key
, filetype
, command
, sizeof(command
));
650 SHELL_ArgifyW(lpResult
, resultLen
, command
, xlpFile
, pidl
, args
, &finishedLen
);
651 if (finishedLen
> resultLen
)
652 ERR("Argify buffer not large enough.. truncated\n");
654 /* Remove double quotation marks and command line arguments */
655 if (*lpResult
== '"')
658 while (*(p
+ 1) != '"')
667 /* Truncate on first space */
669 while (*p
!= ' ' && *p
!= '\0')
675 else /* Check win.ini */
677 static const WCHAR wExtensions
[] = {'e','x','t','e','n','s','i','o','n','s',0};
679 /* Toss the leading dot */
681 if (GetProfileStringW(wExtensions
, extension
, wszEmpty
, command
, sizeof(command
)/sizeof(WCHAR
)) > 0)
683 if (wcslen(command
) != 0)
685 wcscpy(lpResult
, command
);
686 tok
= wcschr(lpResult
, '^'); /* should be ^.extension? */
690 wcscat(lpResult
, xlpFile
); /* what if no dir in xlpFile? */
691 tok
= wcschr(command
, '^'); /* see above */
692 if ((tok
!= NULL
) && (wcslen(tok
)>5))
694 wcscat(lpResult
, &tok
[5]);
697 retval
= 33; /* FIXME - see above */
702 TRACE("returning %s\n", debugstr_w(lpResult
));
706 /******************************************************************
709 * callback for the DDE connection. not really useful
711 static HDDEDATA CALLBACK
dde_cb(UINT uType
, UINT uFmt
, HCONV hConv
,
712 HSZ hsz1
, HSZ hsz2
, HDDEDATA hData
,
713 ULONG_PTR dwData1
, ULONG_PTR dwData2
)
715 TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
716 uType
, uFmt
, hConv
, hsz1
, hsz2
, hData
, dwData1
, dwData2
);
720 /******************************************************************
723 * ShellExecute helper. Used to do an operation with a DDE connection
725 * Handles both the direct connection (try #1), and if it fails,
726 * launching an application and trying (#2) to connect to it
729 static unsigned dde_connect(const WCHAR
* key
, const WCHAR
* start
, WCHAR
* ddeexec
,
730 const WCHAR
* lpFile
, WCHAR
*env
,
731 LPCWSTR szCommandline
, LPITEMIDLIST pidl
, SHELL_ExecuteW32 execfunc
,
732 const SHELLEXECUTEINFOW
*psei
, LPSHELLEXECUTEINFOW psei_out
)
734 static const WCHAR wApplication
[] = {'\\','a','p','p','l','i','c','a','t','i','o','n',0};
735 static const WCHAR wTopic
[] = {'\\','t','o','p','i','c',0};
737 WCHAR
* endkey
= regkey
+ wcslen(key
);
738 WCHAR app
[256], topic
[256], ifexec
[256], res
[256];
739 LONG applen
, topiclen
, ifexeclen
;
744 HSZ hszApp
, hszTopic
;
747 unsigned ret
= SE_ERR_NOASSOC
;
748 BOOL unicode
= !(GetVersion() & 0x80000000);
751 wcscpy(endkey
, wApplication
);
752 applen
= sizeof(app
);
753 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, app
, &applen
) != ERROR_SUCCESS
)
755 WCHAR command
[1024], fullpath
[MAX_PATH
];
756 static const WCHAR wSo
[] = { '.','s','o',0 };
757 DWORD sizeSo
= sizeof(wSo
)/sizeof(WCHAR
);
761 /* Get application command from start string and find filename of application */
764 wcscpy(command
, start
+1);
765 if ((ptr
= wcschr(command
, '"')))
767 ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
)/sizeof(WCHAR
), fullpath
, &ptr
);
772 for (p
=(LPWSTR
)start
; (space
= const_cast<LPWSTR
>(strchrW(p
, ' '))); p
=space
+1)
774 int idx
= space
-start
;
775 memcpy(command
, start
, idx
*sizeof(WCHAR
));
777 if ((ret
= SearchPathW(NULL
, command
, wszExe
, sizeof(fullpath
)/sizeof(WCHAR
), fullpath
, &ptr
)))
781 ret
= SearchPathW(NULL
, start
, wszExe
, sizeof(fullpath
)/sizeof(WCHAR
), fullpath
, &ptr
);
786 ERR("Unable to find application path for command %s\n", debugstr_w(start
));
787 return ERROR_ACCESS_DENIED
;
791 /* Remove extensions (including .so) */
792 ptr
= app
+ wcslen(app
) - (sizeSo
-1);
793 if (wcslen(app
) >= sizeSo
&&
797 ptr
= const_cast<LPWSTR
>(strrchrW(app
, '.'));
802 wcscpy(endkey
, wTopic
);
803 topiclen
= sizeof(topic
);
804 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, topic
, &topiclen
) != ERROR_SUCCESS
)
806 static const WCHAR wSystem
[] = {'S','y','s','t','e','m',0};
807 wcscpy(topic
, wSystem
);
812 if (DdeInitializeW(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
817 if (DdeInitializeA(&ddeInst
, dde_cb
, APPCMD_CLIENTONLY
, 0L) != DMLERR_NO_ERROR
)
821 hszApp
= DdeCreateStringHandleW(ddeInst
, app
, CP_WINUNICODE
);
822 hszTopic
= DdeCreateStringHandleW(ddeInst
, topic
, CP_WINUNICODE
);
824 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
828 static const WCHAR wIfexec
[] = {'\\','i','f','e','x','e','c',0};
829 TRACE("Launching %s\n", debugstr_w(start
));
830 ret
= execfunc(start
, env
, TRUE
, psei
, psei_out
);
833 TRACE("Couldn't launch\n");
836 hConv
= DdeConnect(ddeInst
, hszApp
, hszTopic
, NULL
);
839 TRACE("Couldn't connect. ret=%d\n", ret
);
840 DdeUninitialize(ddeInst
);
841 SetLastError(ERROR_DDE_FAIL
);
842 return 30; /* whatever */
844 strcpyW(endkey
, wIfexec
);
845 ifexeclen
= sizeof(ifexec
);
846 if (RegQueryValueW(HKEY_CLASSES_ROOT
, regkey
, ifexec
, &ifexeclen
) == ERROR_SUCCESS
)
852 SHELL_ArgifyW(res
, sizeof(res
)/sizeof(WCHAR
), exec
, lpFile
, pidl
, szCommandline
, &resultLen
);
853 if (resultLen
> sizeof(res
)/sizeof(WCHAR
))
854 ERR("Argify buffer not large enough, truncated\n");
855 TRACE("%s %s => %s\n", debugstr_w(exec
), debugstr_w(lpFile
), debugstr_w(res
));
857 /* It's documented in the KB 330337 that IE has a bug and returns
858 * error DMLERR_NOTPROCESSED on XTYP_EXECUTE request.
861 hDdeData
= DdeClientTransaction((LPBYTE
)res
, (strlenW(res
) + 1) * sizeof(WCHAR
), hConv
, 0L, 0,
862 XTYP_EXECUTE
, 30000, &tid
);
865 DWORD lenA
= WideCharToMultiByte(CP_ACP
, 0, res
, -1, NULL
, 0, NULL
, NULL
);
866 char *resA
= (LPSTR
)HeapAlloc(GetProcessHeap(), 0, lenA
);
867 WideCharToMultiByte(CP_ACP
, 0, res
, -1, resA
, lenA
, NULL
, NULL
);
868 hDdeData
= DdeClientTransaction( (LPBYTE
)resA
, lenA
, hConv
, 0L, 0,
869 XTYP_EXECUTE
, 10000, &tid
);
870 HeapFree(GetProcessHeap(), 0, resA
);
873 DdeFreeDataHandle(hDdeData
);
875 WARN("DdeClientTransaction failed with error %04x\n", DdeGetLastError(ddeInst
));
878 DdeDisconnect(hConv
);
881 DdeUninitialize(ddeInst
);
886 /*************************************************************************
887 * execute_from_key [Internal]
889 static UINT_PTR
execute_from_key(LPCWSTR key
, LPCWSTR lpFile
, WCHAR
*env
, LPCWSTR szCommandline
,
890 LPCWSTR executable_name
,
891 SHELL_ExecuteW32 execfunc
,
892 LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
)
894 static const WCHAR wCommand
[] = {'c','o','m','m','a','n','d',0};
895 static const WCHAR wDdeexec
[] = {'d','d','e','e','x','e','c',0};
896 WCHAR cmd
[256], param
[1024], ddeexec
[256];
897 DWORD cmdlen
= sizeof(cmd
), ddeexeclen
= sizeof(ddeexec
);
898 UINT_PTR retval
= SE_ERR_NOASSOC
;
902 TRACE("%s %s %s %s %s\n", debugstr_w(key
), debugstr_w(lpFile
), debugstr_w(env
),
903 debugstr_w(szCommandline
), debugstr_w(executable_name
));
908 /* Get the application from the registry */
909 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, cmd
, (LONG
*)&cmdlen
) == ERROR_SUCCESS
)
911 TRACE("got cmd: %s\n", debugstr_w(cmd
));
913 /* Is there a replace() function anywhere? */
914 cmdlen
/= sizeof(WCHAR
);
915 if (cmdlen
>= sizeof(cmd
)/sizeof(WCHAR
))
916 cmdlen
= sizeof(cmd
)/sizeof(WCHAR
)-1;
918 SHELL_ArgifyW(param
, sizeof(param
)/sizeof(WCHAR
), cmd
, lpFile
, (LPITEMIDLIST
)psei
->lpIDList
, szCommandline
, &resultLen
);
919 if (resultLen
> sizeof(param
)/sizeof(WCHAR
))
920 ERR("Argify buffer not large enough, truncating\n");
923 /* Get the parameters needed by the application
924 from the associated ddeexec key */
925 tmp
= const_cast<LPWSTR
>(strstrW(key
, wCommand
));
927 wcscpy(tmp
, wDdeexec
);
929 if (RegQueryValueW(HKEY_CLASSES_ROOT
, key
, ddeexec
, (LONG
*)&ddeexeclen
) == ERROR_SUCCESS
)
931 TRACE("Got ddeexec %s => %s\n", debugstr_w(key
), debugstr_w(ddeexec
));
932 if (!param
[0]) strcpyW(param
, executable_name
);
933 retval
= dde_connect(key
, param
, ddeexec
, lpFile
, env
, szCommandline
, (LPITEMIDLIST
)psei
->lpIDList
, execfunc
, psei
, psei_out
);
937 TRACE("executing: %s\n", debugstr_w(param
));
938 retval
= execfunc(param
, env
, FALSE
, psei
, psei_out
);
941 WARN("Nothing appropriate found for %s\n", debugstr_w(key
));
946 /*************************************************************************
947 * FindExecutableA [SHELL32.@]
949 HINSTANCE WINAPI
FindExecutableA(LPCSTR lpFile
, LPCSTR lpDirectory
, LPSTR lpResult
)
952 WCHAR
*wFile
= NULL
, *wDirectory
= NULL
;
953 WCHAR wResult
[MAX_PATH
];
955 if (lpFile
) __SHCloneStrAtoW(&wFile
, lpFile
);
956 if (lpDirectory
) __SHCloneStrAtoW(&wDirectory
, lpDirectory
);
958 retval
= FindExecutableW(wFile
, wDirectory
, wResult
);
959 WideCharToMultiByte(CP_ACP
, 0, wResult
, -1, lpResult
, MAX_PATH
, NULL
, NULL
);
961 SHFree( wDirectory
);
963 TRACE("returning %s\n", lpResult
);
967 /*************************************************************************
968 * FindExecutableW [SHELL32.@]
970 * This function returns the executable associated with the specified file
971 * for the default verb.
974 * lpFile [I] The file to find the association for. This must refer to
975 * an existing file otherwise FindExecutable fails and returns
977 * lpResult [O] Points to a buffer into which the executable path is
978 * copied. This parameter must not be NULL otherwise
979 * FindExecutable() segfaults. The buffer must be of size at
980 * least MAX_PATH characters.
983 * A value greater than 32 on success, less than or equal to 32 otherwise.
984 * See the SE_ERR_* constants.
987 * On Windows XP and 2003, FindExecutable() seems to first convert the
988 * filename into 8.3 format, thus taking into account only the first three
989 * characters of the extension, and expects to find an association for those.
990 * However other Windows versions behave sanely.
992 HINSTANCE WINAPI
FindExecutableW(LPCWSTR lpFile
, LPCWSTR lpDirectory
, LPWSTR lpResult
)
994 UINT_PTR retval
= SE_ERR_NOASSOC
;
997 TRACE("File %s, Dir %s\n", debugstr_w(lpFile
), debugstr_w(lpDirectory
));
999 lpResult
[0] = '\0'; /* Start off with an empty return string */
1001 return (HINSTANCE
)SE_ERR_FNF
;
1005 GetCurrentDirectoryW(sizeof(old_dir
)/sizeof(WCHAR
), old_dir
);
1006 SetCurrentDirectoryW(lpDirectory
);
1009 retval
= SHELL_FindExecutable(lpDirectory
, lpFile
, wszOpen
, lpResult
, MAX_PATH
, NULL
, NULL
, NULL
, NULL
);
1011 TRACE("returning %s\n", debugstr_w(lpResult
));
1013 SetCurrentDirectoryW(old_dir
);
1014 return (HINSTANCE
)retval
;
1017 /* FIXME: is this already implemented somewhere else? */
1018 static HKEY
ShellExecute_GetClassKey( const SHELLEXECUTEINFOW
*sei
)
1020 LPCWSTR ext
= NULL
, lpClass
= NULL
;
1022 DWORD type
= 0, sz
= 0;
1026 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1027 return sei
->hkeyClass
;
1029 if (sei
->fMask
& SEE_MASK_CLASSNAME
)
1030 lpClass
= sei
->lpClass
;
1033 ext
= PathFindExtensionW( sei
->lpFile
);
1034 TRACE("ext = %s\n", debugstr_w( ext
) );
1038 r
= RegOpenKeyW( HKEY_CLASSES_ROOT
, ext
, &hkey
);
1039 if (r
!= ERROR_SUCCESS
)
1042 r
= RegQueryValueExW( hkey
, NULL
, 0, &type
, NULL
, &sz
);
1043 if ( r
== ERROR_SUCCESS
&& type
== REG_SZ
)
1045 sz
+= sizeof (WCHAR
);
1046 cls
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0, sz
);
1048 RegQueryValueExW( hkey
, NULL
, 0, &type
, (LPBYTE
) cls
, &sz
);
1051 RegCloseKey( hkey
);
1055 TRACE("class = %s\n", debugstr_w(lpClass
) );
1059 RegOpenKeyW( HKEY_CLASSES_ROOT
, lpClass
, &hkey
);
1061 HeapFree( GetProcessHeap(), 0, cls
);
1066 static IDataObject
*shellex_get_dataobj( LPSHELLEXECUTEINFOW sei
)
1068 LPCITEMIDLIST pidllast
= NULL
;
1069 IDataObject
*dataobj
= NULL
;
1070 IShellFolder
*shf
= NULL
;
1071 LPITEMIDLIST pidl
= NULL
;
1074 if (sei
->fMask
& SEE_MASK_CLASSALL
)
1075 pidl
= (LPITEMIDLIST
)sei
->lpIDList
;
1078 WCHAR fullpath
[MAX_PATH
];
1082 ret
= GetFullPathNameW( sei
->lpFile
, MAX_PATH
, fullpath
, NULL
);
1086 pidl
= ILCreateFromPathW( fullpath
);
1089 r
= SHBindToParent( pidl
, IID_IShellFolder
, (LPVOID
*)&shf
, &pidllast
);
1093 shf
->GetUIObjectOf(NULL
, 1, &pidllast
,
1094 IID_IDataObject
, NULL
, (LPVOID
*) &dataobj
);
1097 if ( pidl
!= sei
->lpIDList
)
1104 static HRESULT
shellex_run_context_menu_default( IShellExtInit
*obj
,
1105 LPSHELLEXECUTEINFOW sei
)
1107 IContextMenu
*cm
= NULL
;
1108 CMINVOKECOMMANDINFOEX ici
;
1115 TRACE("%p %p\n", obj
, sei
);
1117 r
= obj
->QueryInterface(IID_IContextMenu
, (LPVOID
*) &cm
);
1121 hmenu
= CreateMenu();
1125 /* the number of the last menu added is returned in r */
1126 r
= cm
->QueryContextMenu(hmenu
, 0, 0x20, 0x7fff, CMF_DEFAULTONLY
);
1130 n
= GetMenuItemCount( hmenu
);
1131 for ( i
= 0; i
< n
; i
++ )
1133 memset( &info
, 0, sizeof info
);
1134 info
.cbSize
= sizeof info
;
1135 info
.fMask
= MIIM_FTYPE
| MIIM_STRING
| MIIM_STATE
| MIIM_DATA
| MIIM_ID
;
1136 info
.dwTypeData
= string
;
1137 info
.cch
= sizeof string
;
1139 GetMenuItemInfoW( hmenu
, i
, TRUE
, &info
);
1141 TRACE("menu %d %s %08x %08lx %08x %08x\n", i
, debugstr_w(string
),
1142 info
.fState
, info
.dwItemData
, info
.fType
, info
.wID
);
1143 if ( ( !sei
->lpVerb
&& (info
.fState
& MFS_DEFAULT
) ) ||
1144 ( sei
->lpVerb
&& !lstrcmpiW( sei
->lpVerb
, string
) ) )
1155 memset( &ici
, 0, sizeof ici
);
1156 ici
.cbSize
= sizeof ici
;
1157 ici
.fMask
= CMIC_MASK_UNICODE
| (sei
->fMask
& (SEE_MASK_NOASYNC
|SEE_MASK_ASYNCOK
|SEE_MASK_FLAG_NO_UI
));
1158 ici
.nShow
= sei
->nShow
;
1159 ici
.lpVerb
= MAKEINTRESOURCEA( def
);
1160 ici
.hwnd
= sei
->hwnd
;
1161 ici
.lpParametersW
= sei
->lpParameters
;
1163 r
= cm
->InvokeCommand((LPCMINVOKECOMMANDINFO
) &ici
);
1165 TRACE("invoke command returned %08x\n", r
);
1169 DestroyMenu( hmenu
);
1175 static HRESULT
shellex_load_object_and_run( HKEY hkey
, LPCGUID guid
, LPSHELLEXECUTEINFOW sei
)
1177 IDataObject
*dataobj
= NULL
;
1178 IObjectWithSite
*ows
= NULL
;
1179 IShellExtInit
*obj
= NULL
;
1182 TRACE("%p %s %p\n", hkey
, debugstr_guid( guid
), sei
);
1184 r
= CoInitialize( NULL
);
1188 r
= CoCreateInstance(*guid
, NULL
, CLSCTX_INPROC_SERVER
,
1189 IID_IShellExtInit
, (LPVOID
*)&obj
);
1192 ERR("failed %08x\n", r
);
1196 dataobj
= shellex_get_dataobj( sei
);
1199 ERR("failed to get data object\n");
1203 r
= obj
->Initialize(NULL
, dataobj
, hkey
);
1207 r
= obj
->QueryInterface(IID_IObjectWithSite
, (LPVOID
*) &ows
);
1211 ows
->SetSite(NULL
);
1213 r
= shellex_run_context_menu_default( obj
, sei
);
1227 /*************************************************************************
1228 * ShellExecute_FromContextMenu [Internal]
1230 static LONG
ShellExecute_FromContextMenu( LPSHELLEXECUTEINFOW sei
)
1232 static const WCHAR szcm
[] = { 's','h','e','l','l','e','x','\\',
1233 'C','o','n','t','e','x','t','M','e','n','u','H','a','n','d','l','e','r','s',0 };
1234 HKEY hkey
, hkeycm
= 0;
1241 TRACE("%s\n", debugstr_w(sei
->lpFile
) );
1243 hkey
= ShellExecute_GetClassKey( sei
);
1245 return ERROR_FUNCTION_FAILED
;
1247 r
= RegOpenKeyW( hkey
, szcm
, &hkeycm
);
1248 if ( r
== ERROR_SUCCESS
)
1253 r
= RegEnumKeyW( hkeycm
, i
++, szguid
, sizeof(szguid
)/sizeof(szguid
[0]) );
1254 if ( r
!= ERROR_SUCCESS
)
1257 hr
= CLSIDFromString( szguid
, &guid
);
1260 /* stop at the first one that succeeds in running */
1261 hr
= shellex_load_object_and_run( hkey
, &guid
, sei
);
1262 if ( SUCCEEDED( hr
) )
1266 RegCloseKey( hkeycm
);
1269 if ( hkey
!= sei
->hkeyClass
)
1270 RegCloseKey( hkey
);
1274 static UINT_PTR
SHELL_execute_class( LPCWSTR wszApplicationName
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1276 static const WCHAR wSpace
[] = {' ',0};
1277 WCHAR execCmd
[1024], wcmd
[1024];
1278 /* launch a document by fileclass like 'WordPad.Document.1' */
1279 /* the Commandline contains 'c:\Path\wordpad.exe "%1"' */
1280 /* FIXME: wcmd should not be of a fixed size. Fixed to 1024, MAX_PATH is way too short! */
1281 ULONG cmask
=(psei
->fMask
& SEE_MASK_CLASSALL
);
1285 HCR_GetExecuteCommandW((cmask
== SEE_MASK_CLASSKEY
) ? psei
->hkeyClass
: NULL
,
1286 (cmask
== SEE_MASK_CLASSNAME
) ? psei
->lpClass
: NULL
,
1288 execCmd
, sizeof(execCmd
));
1290 /* FIXME: get the extension of lpFile, check if it fits to the lpClass */
1291 TRACE("SEE_MASK_CLASSNAME->%s, doc->%s\n", debugstr_w(execCmd
), debugstr_w(wszApplicationName
));
1294 done
= SHELL_ArgifyW(wcmd
, sizeof(wcmd
)/sizeof(WCHAR
), execCmd
, wszApplicationName
, (LPITEMIDLIST
)psei
->lpIDList
, NULL
, &resultLen
);
1295 if (!done
&& wszApplicationName
[0])
1297 strcatW(wcmd
, wSpace
);
1298 strcatW(wcmd
, wszApplicationName
);
1300 if (resultLen
> sizeof(wcmd
)/sizeof(WCHAR
))
1301 ERR("Argify buffer not large enough... truncating\n");
1302 return execfunc(wcmd
, NULL
, FALSE
, psei
, psei_out
);
1305 static BOOL
SHELL_translate_idlist( LPSHELLEXECUTEINFOW sei
, LPWSTR wszParameters
, DWORD parametersLen
, LPWSTR wszApplicationName
, DWORD dwApplicationNameLen
)
1307 static const WCHAR wExplorer
[] = {'e','x','p','l','o','r','e','r','.','e','x','e',0};
1308 WCHAR buffer
[MAX_PATH
];
1309 BOOL appKnownSingular
= FALSE
;
1311 /* last chance to translate IDList: now also allow CLSID paths */
1312 if (SUCCEEDED(SHELL_GetPathFromIDListForExecuteW((LPCITEMIDLIST
)sei
->lpIDList
, buffer
, sizeof(buffer
)))) {
1313 if (buffer
[0]==':' && buffer
[1]==':') {
1314 /* open shell folder for the specified class GUID */
1315 if (strlenW(buffer
) + 1 > parametersLen
)
1316 ERR("parameters len exceeds buffer size (%i > %i), truncating\n",
1317 lstrlenW(buffer
) + 1, parametersLen
);
1318 lstrcpynW(wszParameters
, buffer
, parametersLen
);
1319 if (strlenW(wExplorer
) > dwApplicationNameLen
)
1320 ERR("application len exceeds buffer size (%i > %i), truncating\n",
1321 lstrlenW(wExplorer
) + 1, dwApplicationNameLen
);
1322 lstrcpynW(wszApplicationName
, wExplorer
, dwApplicationNameLen
);
1323 appKnownSingular
= TRUE
;
1325 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1327 WCHAR target
[MAX_PATH
];
1330 /* Check if we're executing a directory and if so use the
1331 handler for the Folder class */
1332 strcpyW(target
, buffer
);
1333 attribs
= GetFileAttributesW(buffer
);
1334 if (attribs
!= INVALID_FILE_ATTRIBUTES
&&
1335 (attribs
& FILE_ATTRIBUTE_DIRECTORY
) &&
1336 HCR_GetExecuteCommandW(0, wszFolder
,
1338 buffer
, sizeof(buffer
))) {
1339 SHELL_ArgifyW(wszApplicationName
, dwApplicationNameLen
,
1340 buffer
, target
, (LPITEMIDLIST
)sei
->lpIDList
, NULL
, &resultLen
);
1341 if (resultLen
> dwApplicationNameLen
)
1342 ERR("Argify buffer not large enough... truncating\n");
1343 appKnownSingular
= FALSE
;
1345 sei
->fMask
&= ~SEE_MASK_INVOKEIDLIST
;
1348 return appKnownSingular
;
1351 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
)
1353 static const WCHAR wQuote
[] = {'"',0};
1354 static const WCHAR wSpace
[] = {' ',0};
1357 WCHAR
*wszQuotedCmd
;
1359 /* Length of quotes plus length of command plus NULL terminator */
1360 len
= 2 + lstrlenW(wcmd
) + 1;
1361 if (wszParameters
[0])
1363 /* Length of space plus length of parameters */
1364 len
+= 1 + lstrlenW(wszParameters
);
1366 wszQuotedCmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1367 /* Must quote to handle case where cmd contains spaces,
1368 * else security hole if malicious user creates executable file "C:\\Program"
1370 strcpyW(wszQuotedCmd
, wQuote
);
1371 strcatW(wszQuotedCmd
, wcmd
);
1372 strcatW(wszQuotedCmd
, wQuote
);
1373 if (wszParameters
[0]) {
1374 strcatW(wszQuotedCmd
, wSpace
);
1375 strcatW(wszQuotedCmd
, wszParameters
);
1377 TRACE("%s/%s => %s/%s\n", debugstr_w(wszApplicationName
), debugstr_w(psei
->lpVerb
), debugstr_w(wszQuotedCmd
), debugstr_w(lpstrProtocol
));
1379 retval
= execute_from_key(lpstrProtocol
, wszApplicationName
, env
, psei
->lpParameters
, wcmd
, execfunc
, psei
, psei_out
);
1381 retval
= execfunc(wszQuotedCmd
, env
, FALSE
, psei
, psei_out
);
1382 HeapFree(GetProcessHeap(), 0, wszQuotedCmd
);
1386 static UINT_PTR
SHELL_execute_url( LPCWSTR lpFile
, LPCWSTR wFile
, LPCWSTR wcmd
, LPSHELLEXECUTEINFOW psei
, LPSHELLEXECUTEINFOW psei_out
, SHELL_ExecuteW32 execfunc
)
1388 static const WCHAR wShell
[] = {'\\','s','h','e','l','l','\\',0};
1389 static const WCHAR wCommand
[] = {'\\','c','o','m','m','a','n','d',0};
1391 WCHAR
*lpstrProtocol
;
1396 lpstrRes
= strchrW(lpFile
, ':');
1398 iSize
= lpstrRes
- lpFile
;
1400 iSize
= strlenW(lpFile
);
1402 TRACE("Got URL: %s\n", debugstr_w(lpFile
));
1403 /* Looking for ...protocol\shell\lpOperation\command */
1404 len
= iSize
+ lstrlenW(wShell
) + lstrlenW(wCommand
) + 1;
1406 len
+= lstrlenW(psei
->lpVerb
);
1408 len
+= lstrlenW(wszOpen
);
1409 lpstrProtocol
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1410 memcpy(lpstrProtocol
, lpFile
, iSize
*sizeof(WCHAR
));
1411 lpstrProtocol
[iSize
] = '\0';
1412 strcatW(lpstrProtocol
, wShell
);
1413 strcatW(lpstrProtocol
, psei
->lpVerb
? psei
->lpVerb
: wszOpen
);
1414 strcatW(lpstrProtocol
, wCommand
);
1416 /* Remove File Protocol from lpFile */
1417 /* In the case file://path/file */
1418 if (!strncmpiW(lpFile
, wFile
, iSize
))
1421 while (*lpFile
== ':') lpFile
++;
1423 retval
= execute_from_key(lpstrProtocol
, lpFile
, NULL
, psei
->lpParameters
,
1424 wcmd
, execfunc
, psei
, psei_out
);
1425 HeapFree(GetProcessHeap(), 0, lpstrProtocol
);
1429 void do_error_dialog( UINT_PTR retval
, HWND hwnd
, WCHAR
* filename
)
1432 DWORD_PTR msgArguments
[3] = { (DWORD_PTR
)filename
, 0, 0 };
1435 error_code
= GetLastError();
1437 if (retval
== SE_ERR_NOASSOC
)
1438 LoadStringW(shell32_hInstance
, IDS_SHLEXEC_NOASSOC
, msg
, sizeof(msg
)/sizeof(WCHAR
));
1440 FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ARGUMENT_ARRAY
,
1445 sizeof(msg
)/sizeof(WCHAR
),
1446 (va_list*)msgArguments
);
1448 MessageBoxW(hwnd
, msg
, NULL
, MB_ICONERROR
);
1451 /*************************************************************************
1452 * SHELL_execute [Internal]
1454 BOOL
SHELL_execute( LPSHELLEXECUTEINFOW sei
, SHELL_ExecuteW32 execfunc
)
1456 static const WCHAR wSpace
[] = {' ',0};
1457 static const WCHAR wWww
[] = {'w','w','w',0};
1458 static const WCHAR wFile
[] = {'f','i','l','e',0};
1459 static const WCHAR wHttp
[] = {'h','t','t','p',':','/','/',0};
1460 static const DWORD unsupportedFlags
=
1461 SEE_MASK_INVOKEIDLIST
| SEE_MASK_ICON
| SEE_MASK_HOTKEY
|
1462 SEE_MASK_CONNECTNETDRV
| SEE_MASK_FLAG_DDEWAIT
|
1463 SEE_MASK_UNICODE
| SEE_MASK_ASYNCOK
| SEE_MASK_HMONITOR
;
1465 WCHAR parametersBuffer
[1024], dirBuffer
[MAX_PATH
], wcmdBuffer
[1024];
1466 WCHAR
*wszApplicationName
, *wszParameters
, *wszDir
, *wcmd
;
1467 DWORD dwApplicationNameLen
= MAX_PATH
+2;
1468 DWORD parametersLen
= sizeof(parametersBuffer
) / sizeof(WCHAR
);
1469 DWORD dirLen
= sizeof(dirBuffer
) / sizeof(WCHAR
);
1470 DWORD wcmdLen
= sizeof(wcmdBuffer
) / sizeof(WCHAR
);
1472 SHELLEXECUTEINFOW sei_tmp
; /* modifiable copy of SHELLEXECUTEINFO struct */
1473 WCHAR wfileName
[MAX_PATH
];
1475 WCHAR lpstrProtocol
[256];
1477 UINT_PTR retval
= SE_ERR_NOASSOC
;
1478 BOOL appKnownSingular
= FALSE
;
1480 /* make a local copy of the LPSHELLEXECUTEINFO structure and work with this from now on */
1483 TRACE("mask=0x%08x hwnd=%p verb=%s file=%s parm=%s dir=%s show=0x%08x class=%s\n",
1484 sei_tmp
.fMask
, sei_tmp
.hwnd
, debugstr_w(sei_tmp
.lpVerb
),
1485 debugstr_w(sei_tmp
.lpFile
), debugstr_w(sei_tmp
.lpParameters
),
1486 debugstr_w(sei_tmp
.lpDirectory
), sei_tmp
.nShow
,
1487 ((sei_tmp
.fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
) ?
1488 debugstr_w(sei_tmp
.lpClass
) : "not used");
1490 sei
->hProcess
= NULL
;
1492 /* make copies of all path/command strings */
1493 if (!sei_tmp
.lpFile
)
1495 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
*sizeof(WCHAR
));
1496 *wszApplicationName
= '\0';
1498 else if (*sei_tmp
.lpFile
== '\"')
1500 DWORD l
= strlenW(sei_tmp
.lpFile
+1);
1501 if(l
>= dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1502 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
*sizeof(WCHAR
));
1503 memcpy(wszApplicationName
, sei_tmp
.lpFile
+1, (l
+1)*sizeof(WCHAR
));
1504 if (wszApplicationName
[l
-1] == '\"')
1505 wszApplicationName
[l
-1] = '\0';
1506 appKnownSingular
= TRUE
;
1507 TRACE("wszApplicationName=%s\n",debugstr_w(wszApplicationName
));
1509 DWORD l
= strlenW(sei_tmp
.lpFile
)+1;
1510 if(l
> dwApplicationNameLen
) dwApplicationNameLen
= l
+1;
1511 wszApplicationName
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, dwApplicationNameLen
*sizeof(WCHAR
));
1512 memcpy(wszApplicationName
, sei_tmp
.lpFile
, l
*sizeof(WCHAR
));
1515 wszParameters
= parametersBuffer
;
1516 if (sei_tmp
.lpParameters
)
1518 len
= lstrlenW(sei_tmp
.lpParameters
) + 1;
1519 if (len
> parametersLen
)
1521 wszParameters
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1522 parametersLen
= len
;
1524 strcpyW(wszParameters
, sei_tmp
.lpParameters
);
1527 *wszParameters
= '\0';
1530 if (sei_tmp
.lpDirectory
)
1532 len
= lstrlenW(sei_tmp
.lpDirectory
) + 1;
1535 wszDir
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1538 strcpyW(wszDir
, sei_tmp
.lpDirectory
);
1543 /* adjust string pointers to point to the new buffers */
1544 sei_tmp
.lpFile
= wszApplicationName
;
1545 sei_tmp
.lpParameters
= wszParameters
;
1546 sei_tmp
.lpDirectory
= wszDir
;
1548 if (sei_tmp
.fMask
& unsupportedFlags
)
1550 FIXME("flags ignored: 0x%08x\n", sei_tmp
.fMask
& unsupportedFlags
);
1553 /* process the IDList */
1554 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1556 IShellExecuteHookW
* pSEH
;
1558 HRESULT hr
= SHBindToParent((LPCITEMIDLIST
)sei_tmp
.lpIDList
, IID_IShellExecuteHookW
, (LPVOID
*)&pSEH
, NULL
);
1562 hr
= pSEH
->Execute(&sei_tmp
);
1567 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1568 if (wszParameters
!= parametersBuffer
)
1569 HeapFree(GetProcessHeap(), 0, wszParameters
);
1570 if (wszDir
!= dirBuffer
)
1571 HeapFree(GetProcessHeap(), 0, wszDir
);
1576 SHGetPathFromIDListW((LPCITEMIDLIST
)sei_tmp
.lpIDList
, wszApplicationName
);
1577 appKnownSingular
= TRUE
;
1578 TRACE("-- idlist=%p (%s)\n", sei_tmp
.lpIDList
, debugstr_w(wszApplicationName
));
1581 if ( ERROR_SUCCESS
== ShellExecute_FromContextMenu( &sei_tmp
) )
1583 sei
->hInstApp
= (HINSTANCE
) 33;
1584 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1585 if (wszParameters
!= parametersBuffer
)
1586 HeapFree(GetProcessHeap(), 0, wszParameters
);
1587 if (wszDir
!= dirBuffer
)
1588 HeapFree(GetProcessHeap(), 0, wszDir
);
1592 if (sei_tmp
.fMask
& SEE_MASK_CLASSALL
)
1594 retval
= SHELL_execute_class( wszApplicationName
, &sei_tmp
, sei
,
1596 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1603 Info
.pcszFile
= wszApplicationName
;
1604 Info
.pcszClass
= NULL
;
1605 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1607 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1608 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1610 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1611 if (wszParameters
!= parametersBuffer
)
1612 HeapFree(GetProcessHeap(), 0, wszParameters
);
1613 if (wszDir
!= dirBuffer
)
1614 HeapFree(GetProcessHeap(), 0, wszDir
);
1618 /* Has the IDList not yet been translated? */
1619 if (sei_tmp
.fMask
& SEE_MASK_IDLIST
)
1621 appKnownSingular
= SHELL_translate_idlist( &sei_tmp
, wszParameters
,
1624 dwApplicationNameLen
);
1627 /* expand environment strings */
1628 len
= ExpandEnvironmentStringsW(sei_tmp
.lpFile
, NULL
, 0);
1632 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(),0,(len
+1)*sizeof(WCHAR
));
1634 ExpandEnvironmentStringsW(sei_tmp
.lpFile
, buf
, len
+1);
1635 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1636 dwApplicationNameLen
= len
+1;
1637 wszApplicationName
= buf
;
1638 /* appKnownSingular unmodified */
1640 sei_tmp
.lpFile
= wszApplicationName
;
1643 if (*sei_tmp
.lpParameters
)
1645 len
= ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, NULL
, 0);
1650 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
1651 ExpandEnvironmentStringsW(sei_tmp
.lpParameters
, buf
, len
);
1652 if (wszParameters
!= parametersBuffer
)
1653 HeapFree(GetProcessHeap(), 0, wszParameters
);
1654 wszParameters
= buf
;
1655 parametersLen
= len
;
1656 sei_tmp
.lpParameters
= wszParameters
;
1660 if (*sei_tmp
.lpDirectory
)
1662 len
= ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, NULL
, 0);
1667 buf
= (LPWSTR
)HeapAlloc(GetProcessHeap(),0,len
*sizeof(WCHAR
));
1668 ExpandEnvironmentStringsW(sei_tmp
.lpDirectory
, buf
, len
);
1669 if (wszDir
!= dirBuffer
)
1670 HeapFree(GetProcessHeap(), 0, wszDir
);
1672 sei_tmp
.lpDirectory
= wszDir
;
1676 /* Else, try to execute the filename */
1677 TRACE("execute:%s,%s,%s\n", debugstr_w(wszApplicationName
), debugstr_w(wszParameters
), debugstr_w(wszDir
));
1679 /* separate out command line arguments from executable file name */
1680 if (!*sei_tmp
.lpParameters
&& !appKnownSingular
) {
1681 /* If the executable path is quoted, handle the rest of the command line as parameters. */
1682 if (sei_tmp
.lpFile
[0] == '"') {
1683 LPWSTR src
= wszApplicationName
/*sei_tmp.lpFile*/ + 1;
1684 LPWSTR dst
= wfileName
;
1687 /* copy the unquoted executable path to 'wfileName' */
1688 while(*src
&& *src
!='"')
1696 while(isspace(*src
))
1701 /* copy the parameter string to 'wszParameters' */
1702 strcpyW(wszParameters
, src
);
1704 /* terminate previous command string after the quote character */
1709 /* If the executable name is not quoted, we have to use this search loop here,
1710 that in CreateProcess() is not sufficient because it does not handle shell links. */
1711 WCHAR buffer
[MAX_PATH
], xlpFile
[MAX_PATH
];
1714 LPWSTR beg
= wszApplicationName
/*sei_tmp.lpFile*/;
1715 for(s
=beg
; (space
= const_cast<LPWSTR
>(strchrW(s
, ' '))); s
=space
+1) {
1716 int idx
= space
-sei_tmp
.lpFile
;
1717 memcpy(buffer
, sei_tmp
.lpFile
, idx
* sizeof(WCHAR
));
1720 /*FIXME This finds directory paths if the targeted file name contains spaces. */
1721 if (SearchPathW(*sei_tmp
.lpDirectory
? sei_tmp
.lpDirectory
: NULL
, buffer
, wszExe
, sizeof(xlpFile
)/sizeof(xlpFile
[0]), xlpFile
, NULL
))
1723 /* separate out command from parameter string */
1724 LPCWSTR p
= space
+ 1;
1729 strcpyW(wszParameters
, p
);
1736 lstrcpynW(wfileName
, sei_tmp
.lpFile
,sizeof(wfileName
)/sizeof(WCHAR
));
1739 lstrcpynW(wfileName
, sei_tmp
.lpFile
,sizeof(wfileName
)/sizeof(WCHAR
));
1744 len
= lstrlenW(wszApplicationName
) + 1;
1745 if (sei_tmp
.lpParameters
[0])
1746 len
+= 1 + lstrlenW(wszParameters
);
1749 wcmd
= (LPWSTR
)HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
1752 strcpyW(wcmd
, wszApplicationName
);
1753 if (sei_tmp
.lpParameters
[0]) {
1754 strcatW(wcmd
, wSpace
);
1755 strcatW(wcmd
, wszParameters
);
1758 retval
= execfunc(wcmd
, NULL
, FALSE
, &sei_tmp
, sei
);
1760 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1761 if (wszParameters
!= parametersBuffer
)
1762 HeapFree(GetProcessHeap(), 0, wszParameters
);
1763 if (wszDir
!= dirBuffer
)
1764 HeapFree(GetProcessHeap(), 0, wszDir
);
1765 if (wcmd
!= wcmdBuffer
)
1766 HeapFree(GetProcessHeap(), 0, wcmd
);
1770 /* Else, try to find the executable */
1772 retval
= SHELL_FindExecutable(sei_tmp
.lpDirectory
, lpFile
, sei_tmp
.lpVerb
, wcmd
, wcmdLen
, lpstrProtocol
, &env
, (LPITEMIDLIST
)sei_tmp
.lpIDList
, sei_tmp
.lpParameters
);
1773 if (retval
> 32) /* Found */
1775 retval
= SHELL_quote_and_execute( wcmd
, wszParameters
, lpstrProtocol
,
1776 wszApplicationName
, env
, &sei_tmp
,
1778 HeapFree( GetProcessHeap(), 0, env
);
1780 else if (PathIsDirectoryW(lpFile
))
1782 static const WCHAR wExplorer
[] = {'e','x','p','l','o','r','e','r',0};
1783 static const WCHAR wQuote
[] = {'"',0};
1784 WCHAR wExec
[MAX_PATH
];
1785 WCHAR
* lpQuotedFile
= (LPWSTR
)HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR
) * (strlenW(lpFile
) + 3) );
1789 retval
= SHELL_FindExecutable( sei_tmp
.lpDirectory
, wExplorer
,
1790 wszOpen
, wExec
, MAX_PATH
,
1791 NULL
, &env
, NULL
, NULL
);
1794 strcpyW(lpQuotedFile
, wQuote
);
1795 strcatW(lpQuotedFile
, lpFile
);
1796 strcatW(lpQuotedFile
, wQuote
);
1797 retval
= SHELL_quote_and_execute( wExec
, lpQuotedFile
,
1799 wszApplicationName
, env
,
1800 &sei_tmp
, sei
, execfunc
);
1801 HeapFree( GetProcessHeap(), 0, env
);
1803 HeapFree( GetProcessHeap(), 0, lpQuotedFile
);
1806 retval
= 0; /* Out of memory */
1808 else if (PathIsURLW(lpFile
)) /* File not found, check for URL */
1810 retval
= SHELL_execute_url( lpFile
, wFile
, wcmd
, &sei_tmp
, sei
, execfunc
);
1812 /* Check if file specified is in the form www.??????.*** */
1813 else if (!strncmpiW(lpFile
, wWww
, 3))
1815 /* if so, append lpFile http:// and call ShellExecute */
1816 WCHAR lpstrTmpFile
[256];
1817 strcpyW(lpstrTmpFile
, wHttp
);
1818 strcatW(lpstrTmpFile
, lpFile
);
1819 retval
= (UINT_PTR
)ShellExecuteW(sei_tmp
.hwnd
, sei_tmp
.lpVerb
, lpstrTmpFile
, NULL
, NULL
, 0);
1822 TRACE("retval %lu\n", retval
);
1824 if (retval
<= 32 && !(sei_tmp
.fMask
& SEE_MASK_FLAG_NO_UI
))
1831 Info
.pcszFile
= wszApplicationName
;
1832 Info
.pcszClass
= NULL
;
1833 Info
.oaifInFlags
= OAIF_ALLOW_REGISTRATION
| OAIF_EXEC
;
1835 //if (SHOpenWithDialog(sei_tmp.hwnd, &Info) != S_OK)
1836 do_error_dialog(retval
, sei_tmp
.hwnd
, wszApplicationName
);
1839 HeapFree(GetProcessHeap(), 0, wszApplicationName
);
1840 if (wszParameters
!= parametersBuffer
)
1841 HeapFree(GetProcessHeap(), 0, wszParameters
);
1842 if (wszDir
!= dirBuffer
)
1843 HeapFree(GetProcessHeap(), 0, wszDir
);
1844 if (wcmd
!= wcmdBuffer
)
1845 HeapFree(GetProcessHeap(), 0, wcmd
);
1847 sei
->hInstApp
= (HINSTANCE
)(retval
> 32 ? 33 : retval
);
1852 /*************************************************************************
1853 * ShellExecuteA [SHELL32.290]
1855 HINSTANCE WINAPI
ShellExecuteA(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
1856 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
)
1858 SHELLEXECUTEINFOA sei
;
1860 TRACE("%p,%s,%s,%s,%s,%d\n",
1861 hWnd
, debugstr_a(lpOperation
), debugstr_a(lpFile
),
1862 debugstr_a(lpParameters
), debugstr_a(lpDirectory
), iShowCmd
);
1864 sei
.cbSize
= sizeof(sei
);
1865 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1867 sei
.lpVerb
= lpOperation
;
1868 sei
.lpFile
= lpFile
;
1869 sei
.lpParameters
= lpParameters
;
1870 sei
.lpDirectory
= lpDirectory
;
1871 sei
.nShow
= iShowCmd
;
1878 ShellExecuteExA (&sei
);
1879 return sei
.hInstApp
;
1882 /*************************************************************************
1883 * ShellExecuteExA [SHELL32.292]
1886 BOOL WINAPI
ShellExecuteExA (LPSHELLEXECUTEINFOA sei
)
1888 SHELLEXECUTEINFOW seiW
;
1890 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
, *wClass
= NULL
;
1894 memcpy(&seiW
, sei
, sizeof(SHELLEXECUTEINFOW
));
1897 seiW
.lpVerb
= __SHCloneStrAtoW(&wVerb
, sei
->lpVerb
);
1900 seiW
.lpFile
= __SHCloneStrAtoW(&wFile
, sei
->lpFile
);
1902 if (sei
->lpParameters
)
1903 seiW
.lpParameters
= __SHCloneStrAtoW(&wParameters
, sei
->lpParameters
);
1905 if (sei
->lpDirectory
)
1906 seiW
.lpDirectory
= __SHCloneStrAtoW(&wDirectory
, sei
->lpDirectory
);
1908 if ((sei
->fMask
& SEE_MASK_CLASSALL
) == SEE_MASK_CLASSNAME
&& sei
->lpClass
)
1909 seiW
.lpClass
= __SHCloneStrAtoW(&wClass
, sei
->lpClass
);
1911 seiW
.lpClass
= NULL
;
1913 ret
= SHELL_execute( &seiW
, SHELL_ExecuteW
);
1915 sei
->hInstApp
= seiW
.hInstApp
;
1917 if (sei
->fMask
& SEE_MASK_NOCLOSEPROCESS
)
1918 sei
->hProcess
= seiW
.hProcess
;
1922 SHFree(wParameters
);
1929 /*************************************************************************
1930 * ShellExecuteExW [SHELL32.293]
1933 BOOL WINAPI
ShellExecuteExW (LPSHELLEXECUTEINFOW sei
)
1935 return SHELL_execute( sei
, SHELL_ExecuteW
);
1938 /*************************************************************************
1939 * ShellExecuteW [SHELL32.294]
1941 * WINSHELLAPI HINSTANCE APIENTRY ShellExecuteW(HWND hwnd, LPCWSTR lpOperation,
1942 * LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd);
1944 HINSTANCE WINAPI
ShellExecuteW(HWND hwnd
, LPCWSTR lpOperation
, LPCWSTR lpFile
,
1945 LPCWSTR lpParameters
, LPCWSTR lpDirectory
, INT nShowCmd
)
1947 SHELLEXECUTEINFOW sei
;
1950 sei
.cbSize
= sizeof(sei
);
1951 sei
.fMask
= SEE_MASK_FLAG_NO_UI
;
1953 sei
.lpVerb
= lpOperation
;
1954 sei
.lpFile
= lpFile
;
1955 sei
.lpParameters
= lpParameters
;
1956 sei
.lpDirectory
= lpDirectory
;
1957 sei
.nShow
= nShowCmd
;
1964 SHELL_execute( &sei
, SHELL_ExecuteW
);
1965 return sei
.hInstApp
;
1968 /*************************************************************************
1969 * WOWShellExecute [SHELL32.@]
1971 * FIXME: the callback function most likely doesn't work the same way on Windows.
1973 EXTERN_C HINSTANCE WINAPI
WOWShellExecute(HWND hWnd
, LPCSTR lpOperation
,LPCSTR lpFile
,
1974 LPCSTR lpParameters
,LPCSTR lpDirectory
, INT iShowCmd
, void *callback
)
1976 SHELLEXECUTEINFOW seiW
;
1977 WCHAR
*wVerb
= NULL
, *wFile
= NULL
, *wParameters
= NULL
, *wDirectory
= NULL
;
1978 HANDLE hProcess
= 0;
1980 seiW
.lpVerb
= lpOperation
? __SHCloneStrAtoW(&wVerb
, lpOperation
) : NULL
;
1981 seiW
.lpFile
= lpFile
? __SHCloneStrAtoW(&wFile
, lpFile
) : NULL
;
1982 seiW
.lpParameters
= lpParameters
? __SHCloneStrAtoW(&wParameters
, lpParameters
) : NULL
;
1983 seiW
.lpDirectory
= lpDirectory
? __SHCloneStrAtoW(&wDirectory
, lpDirectory
) : NULL
;
1985 seiW
.cbSize
= sizeof(seiW
);
1988 seiW
.nShow
= iShowCmd
;
1993 seiW
.hProcess
= hProcess
;
1995 SHELL_execute( &seiW
, (SHELL_ExecuteW32
)callback
);
1999 SHFree(wParameters
);
2001 return seiW
.hInstApp
;
2004 /*************************************************************************
2005 * OpenAs_RunDLLA [SHELL32.@]
2007 EXTERN_C
void WINAPI
OpenAs_RunDLLA(HWND hwnd
, HINSTANCE hinst
, LPCSTR cmdline
, int cmdshow
)
2009 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_a(cmdline
), cmdshow
);
2012 /*************************************************************************
2013 * OpenAs_RunDLLW [SHELL32.@]
2015 EXTERN_C
void WINAPI
OpenAs_RunDLLW(HWND hwnd
, HINSTANCE hinst
, LPCWSTR cmdline
, int cmdshow
)
2017 FIXME("%p, %p, %s, %d\n", hwnd
, hinst
, debugstr_w(cmdline
), cmdshow
);